# Example: Define a Size enum
class Size(VEnum):
SMALL = "small"
MEDIUM = "medium"
LARGE = "large"
# Concatenate enums with strings
Size.SMALL + "primary" # Returns: "small primary"
"btn " + Size.LARGE # Returns: "btn large"π§ Foundations
Utilities to help with integration of BeerCSS with FastHTML
π¦ VEnum
Base class for creating enums that work seamlessly with FastHTML components.
| Feature | Description |
|---|---|
__str__ |
Returns the enumβs value as a string |
__add__ / __radd__ |
Concatenates with other values via stringify |
π‘ Use case: Define CSS class enums that can be combined with
+operator.
VEnum
def VEnum(
args:VAR_POSITIONAL, kwds:VAR_KEYWORD
):
Enum with string conversion and concatenation support
π stringify
Converts various input types into space-separated strings for FT component class attributes.
| Input | Output |
|---|---|
None / empty list |
"" |
| Single value | str(value) |
| List/tuple | Space-joined string |
stringify
def stringify(
o
):
Converts input types into strings that can be passed to FT components
stringify(["primary", "large", "rounded"]) # "primary large rounded"
stringify(Size.SMALL) # "small"
stringify((Size.MEDIUM, "filled")) # "medium filled"π§Ή Token Utilities
Functions for normalizing and deduplicating CSS class tokens.
| Function | Purpose |
|---|---|
normalize_tokens |
Converts mixed inputs (strings, enums, lists) into a flat list of tokens |
dedupe_preserve_order |
Removes duplicate tokens while keeping original order |
π‘ Use case: Clean up class attributes before passing to components β handles
"btn primary",[Size.LARGE, "btn"], or mixed inputs.
normalize_tokens
def normalize_tokens(
cls
):
Normalize class input to list of string tokens
dedupe_preserve_order
def dedupe_preserve_order(
tokens
):
Remove duplicates while preserving order
# Pipeline: normalize β dedupe β stringify
tokens = normalize_tokens(["btn primary", Size.LARGE, "primary"]) # ['btn', 'primary', 'large', 'primary']
unique = dedupe_preserve_order(tokens) # ['btn', 'primary', 'large']
stringify(unique) # "btn primary large"