Log inGet started
Package

ui_theme.module

Newest version

This package holds more than this page lists. See all of its files in its world.

README.md

ui_theme

The $token cascade of a UI theme. A theme is a Luau table { name, tokens, styles }: each token's value is a literal or a $name reference to another token, and each style maps a selector to properties whose values are literals or $name references to a token. Resolving a theme replaces every reference with the literal it names, following chains of references, so the engine's theme registry receives flat values only.

The ui namespace reaches it through two calls:

  • ui.resolveTheme(theme) resolves a theme and registers nothing. It returns (resolved, nil) with { name, tokens, styles } flat, or (nil, reason).
  • ui.loadTheme(name, theme?) resolves a theme and registers it under name, ready for ui.setTheme(name). With no theme it loads the built-in theme @builtin::themes.<name> (dark, light, debug). It returns (true, nil) once registered, or (false, reason).
ui.loadTheme("mine", {
    tokens = {
        accent = "#ff8a3d",
        ["accent-hover"] = "$accent",
    },
    styles = {
        button = { background = "$accent-hover" },
    },
})
ui.setTheme("mine")

local dark = ui.resolveTheme(require("@builtin::themes.dark"))
print(dark and dark.tokens["accent"])

The cascade

  • A token value that is not a string, or a string that does not start with $, is a literal and stays as it is.
  • $name resolves to the literal tokens[name] resolves to, so a = "$b", b = "$c", c = "#fff" gives a = "#fff".
  • A style property's $name resolves against the resolved tokens.
  • A reference to a token the theme does not hold fails with unknown token: $name, naming the token or the style selector and property that holds it.
  • A chain of references that comes back to a token already on it fails with cycle detected: a → b → a, naming every token of the cycle.

Functions

The module's own functions, for code that resolves a theme without the ui namespace:

  • resolve(theme): the theme with its tokens and styles resolved, as (resolved, nil) or (nil, reason). theme.name must be a non-empty string.
  • resolveTokens(theme): the theme's tokens with every reference resolved, as (tokens, nil) or (nil, reason). A theme with no tokens gives {}.
  • resolveStyles(theme, tokens): the theme's styles with every property reference resolved against tokens (the result of resolveTokens), as (styles, nil) or (nil, reason). A theme with no styles gives {}.