Builtin Library
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 undername, ready forui.setTheme(name). With nothemeit 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. $nameresolves to the literaltokens[name]resolves to, soa = "$b",b = "$c",c = "#fff"givesa = "#fff".- A style property's
$nameresolves 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.namemust be a non-empty string.resolveTokens(theme): the theme's tokens with every reference resolved, as(tokens, nil)or(nil, reason). A theme with notokensgives{}.resolveStyles(theme, tokens): the theme's styles with every property reference resolved againsttokens(the result ofresolveTokens), as(styles, nil)or(nil, reason). A theme with nostylesgives{}.