ThemeEx (theme_ex v0.3.0)

View Source

An Elixir package that implements data structures for the Theme UI theme specification and provides utilities for parsing, validation, and CSS variable generation.

Usage

# Create a theme
theme = %ThemeEx.Theme{
  colors: %ThemeEx.Colors{
    text: "#000000",
    background: "#ffffff",
    primary: "#0066cc"
  },
  fonts: %ThemeEx.Fonts{
    body: "system-ui, sans-serif",
    heading: "Georgia, serif"
  },
  fontSizes: [12, 14, 16, 20, 24, 32, 48, 64]
}

# Generate CSS variables
css = ThemeEx.to_css_variables(theme)

# Generate JSON Schema
schema = ThemeEx.json_schema()

# Validate a theme
{:ok, validated_theme} = ThemeEx.validate(theme)

Summary

Functions

Parse a theme from JSON string.

Convert a map to a theme struct.

Generate JSON Schema for theme validation.

Convert a theme to CSS variables.

Validate a theme struct against the schema.

Functions

from_json(json_string)

@spec from_json(String.t()) :: {:ok, ThemeEx.Theme.t()} | {:error, term()}

Parse a theme from JSON string.

Examples

iex> json = ~s({"colors": {"primary": "#0066cc"}})
iex> {:ok, theme} = ThemeEx.from_json(json)
iex> theme.colors.primary
"#0066cc"

from_map(data)

@spec from_map(map()) :: {:ok, ThemeEx.Theme.t()} | {:error, term()}

Convert a map to a theme struct.

Examples

iex> map = %{"colors" => %{"primary" => "#0066cc"}}
iex> {:ok, theme} = ThemeEx.from_map(map)
iex> theme.colors.primary
"#0066cc"

json_schema()

@spec json_schema() :: map()

Generate JSON Schema for theme validation.

Examples

iex> schema = ThemeEx.json_schema()
iex> is_map(schema)
true

to_css_variables(theme)

@spec to_css_variables(ThemeEx.Theme.t()) :: String.t()

Convert a theme to CSS variables.

Examples

iex> theme = %ThemeEx.Theme{colors: %ThemeEx.Colors{primary: "#0066cc"}}
iex> css = ThemeEx.to_css_variables(theme)
iex> String.contains?(css, "--theme-colors-primary: #0066cc;")
true

validate(theme)

@spec validate(ThemeEx.Theme.t()) :: {:ok, ThemeEx.Theme.t()} | {:error, list()}

Validate a theme struct against the schema.

Examples

iex> theme = %ThemeEx.Theme{colors: %ThemeEx.Colors{primary: "#0066cc"}}
iex> {:ok, validated} = ThemeEx.validate(theme)
iex> validated.colors.primary
"#0066cc"