πŸ”§ 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.


source

VEnum


def VEnum(
    args:VAR_POSITIONAL, kwds:VAR_KEYWORD
):

Enum with string conversion and concatenation support

# 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"

πŸ”„ 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

source

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.


source

normalize_tokens


def normalize_tokens(
    cls
):

Normalize class input to list of string tokens


source

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"