pysxLearn by building

Compose an interface · Lesson 13 of 28

Styles, variables and themes

Change presentation reactively without replacing the styled DOM node.

A styled definition can be shared across instances. Its dynamic values and active theme belong to a render/session. This separation keeps reusable style rules from becoming a process-global theme toggle for every learner.

Use inherited native or callable styles for common surfaces. CSS variables carry reactive values through those rules; named themes provide a coherent set of tokens. Literal property lists avoid raw CSS braces in t-strings.

Understand the moving parts

The preview below uses the course's own shared theme definitions, while its spacing is local to this demo. A theme switch updates computed appearance; it should not remount the input or discard its draft.

Empty dynamic variable values remove the local override, allowing the declared fallback to apply. css() is for validated flat snapshot CSS text; it is not an unrestricted live stylesheet or a way to execute submitted source.

Walk through the code

from pysx import Fragment, Themes, derived, pysx, signal


def selected_name(themes: Themes) -> str:
    current = themes.current()

    return current.name if current is not None else "light"


def styles(themes: Themes) -> Fragment:
    spacing = signal("16px")
    draft = signal("")
    variables = {"preview-space": spacing}
    name = derived(lambda: selected_name(themes))

    def compact(_: object) -> None:
        spacing.set("8px" if spacing() == "16px" else "16px")

    def switch(_: object) -> None:
        themes.select("dark" if name() == "light" else "light")

    return pysx(t"""
        div:
            button(id="preview-spacing" onClick={compact}): "Change spacing"
            button(id="preview-theme" onClick={switch}): "Switch preview theme"
        section(id="style-preview" styleVars={variables}
            style="padding: var(--preview-space)")
            label(for="style-draft"): "Keep this input while changing presentation"
            input(id="style-draft" bindValue={draft})
        output(id="preview-theme-name"): {name}
    """)


def app() -> Fragment:
    themes = Themes()
    themes.register("light", {"paper": "#f7f9fe", "ink": "#102449"})
    themes.register("dark", {"paper": "#09162d", "ink": "#e8efff"})
    themes.select("light")
    preview = styles(themes)

    return pysx(
        t"""
        section(style="background: var(--paper); color: var(--ink)")
            {preview}
    """,
        themes=themes,
    )

spacing starts at 16px and supplies --preview-space; Change spacing toggles it to 8px. The theme button switches the named light/dark themes. Both updates retain the input and its draft. The course uses its full shared palette; this standalone version keeps the same paper/ink tokens and native controls.

Try it live

Change spacing and switch theme. Type into the preview before another theme switch.

What to observe

Computed spacing/colors change while the preview input and its draft remain intact.

light

Take this with you

Share definitions; keep dynamic themes and values scoped to the learner.