Skip to content
Flow-UILive
On this page

Theming server driven SwiftUI

Hex colors with dark_hex out of the box. Design tokens through ThemeProvider. Light and dark without a second payload.

Ayush MishraPublished 6 min read

Theming server driven SwiftUI is a resolution problem, not a second page. The backend sends color and font as data. The client turns that data into a Color and a Font through one protocol. Light and dark can live on the same atom. You do not fetch a night envelope when the user flips appearance.

Flow-UI never hardcodes a product palette. Atoms carry ColorData and FontData. ThemeProvider is the host seam. The default implementation understands hex pairs. Apps with a design system implement the protocol once, map tokens, and every widget below the root picks it up through the environment.

This post is the production walkthrough. The contract lives in theming. Native dark mode and Dynamic Type are why SDUI is not a WebView; that argument is in what native means.

What the backend actually sends#

ColorData is one shape used on widgets, layout chrome, buttons, tags, and nav bars. Three levels of sophistication decode into the same type.

ShapeJSONWhen to use it
Bare hex"#4F8CFF"One-off colour, no dark variant
Hex pair{ "hex": "#4F8CFF", "dark_hex": "#FF6B6B" }Appearance-aware colour without a design system
Token{ "token": "surface.primary", "alpha": 0.8 }Your palette, resolved by the host theme

A bare string is shorthand for { "hex": "..." }. Eight-digit hex carries alpha first: #CCFFFFFF is white at 80 percent. A separate alpha field is an opacity multiplier applied after resolution, in the 0 to 1 range.

FontData mirrors the idea: { "size": 16, "weight": "semibold" } or { "token": "title.large" }. Weights map to the system scale: regular, medium, semibold, bold, heavy, light.

Resolution order#

The intended priority is documented on ThemeProvider: token first when the theme understands it, then hex with dark_hex, then nothing. DefaultTheme has no token table, so hex is the only source there. If you send a token and forget to install a theme that maps it, the colour is nil and the widget falls back to ThemeDefaults.

That order is why you can migrate a product from hex pairs to tokens without changing widget views. Swap the theme. Keep the envelope keys.

Dark mode without a second payload#

dark_hex is the dark appearance override for hex. Color.flowDynamic(hex:darkHex:) in ColorResolution.swift builds a dynamic colour that follows the system. On iOS it uses UIColor with a trait collection. On macOS it uses a named NSColor. If dark_hex is missing, the light value is used in both appearances. A dark-only colour is unusual but valid: the helper will still produce a colour from dark_hex alone.

Send dark_hex on surfaces the user will stare at: page backgrounds, widget layout fills, separators that must not vanish. Decorative one-offs can stay as a single hex if contrast already holds.

You do not encode "is dark" in the page id. Appearance is a client trait. The payload describes both variants; the platform picks.

BrandTheme.swiftswift
import SwiftUI
import FlowCore
import FlowRender
 
struct BrandTheme: ThemeProvider {
    let defaults = ThemeDefaults()
 
    func color(_ data: ColorData?) -> Color? {
        if let token = data?.token {
            return BrandPalette.color(named: token)
        }
        return DefaultTheme().color(data)
    }
 
    func font(_ data: FontData?) -> Font? {
        if let token = data?.token {
            return BrandPalette.font(named: token)
        }
        return DefaultTheme().font(data)
    }
}

Install it once at the SwiftUI root. Every built-in atom and every custom WidgetView that reads @Environment(\.flowTheme) resolves through that instance.

swift
FlowPageView(store: store)
    .flowTheme(BrandTheme())

UIKit hosts pass the same theme into FlowHostingController(store:dispatcher:theme:). There is no second API. The controller wraps FlowPageView and applies .flowTheme when the argument is non-nil.

ThemeDefaults are the floor#

ThemeDefaults is not optional flavour. It is the fallback text colour, secondary text, accent, surface, page background, separator, corner radius, spacing, and body font used when the response leaves something unspecified. Tune it so an unstyled payload still looks like your app rather than like a system demo.

scalesFontsWithDynamicType lives on the same struct and defaults to true. Backend point sizes go through FlowFontScaling in DynamicTypeScaling.swift so they follow the user's text size setting. Turn the flag off only for tightly art-directed chrome, a fixed banner lockup for instance. Accessibility in server driven SwiftUI is a separate article; the hook for type is here because theming is where sizes become Font values.

Do not put brand hex values in widget bodies. If a custom widget calls Color(red:green:blue:) you have stepped outside the contract. The next appearance bug will be yours, and the backend cannot fix it with JSON.

Layout chrome is theme data too#

Widget layout can send background as ColorData, plus gradients, borders, padding, and margin. The renderer applies chrome in one fixed order around the widget body. Your WidgetView should not pad or fill its own container. If you do both, every page looks over-padded and nobody knows which layer to edit.

A merchandising row that needs a surface in light and a raised fill in dark is a layout.background hex pair, not a new widget type. Rearranging known widgets and restyling known chrome is a data deploy. Inventing a new visual that has no widget yet is still a binary. That split is the product claim, not a theming trick.

What theming is not#

It is not a network feature. Flow-UI performs no HTTP. PageLoader is still how bytes arrive. The theme does not fetch palettes.

It is not a router. Deeplinks stay host handlers.

It is not a second design system inside the package. DefaultTheme understands hex and system fonts. Tokens are your table.

It is not a promise that contrast is correct. dark_hex lets you send a pair. You still have to pick values that pass your own contrast bar.

A practical rollout#

Start with hex pairs on the pages that already exist. Confirm light and dark in the playground by pasting the same document and toggling the phone chrome, then confirm on device with the system appearance switch. When the design team has named tokens, implement ThemeProvider and change payloads additively: send token and keep hex as a comment in your CMS until old binaries are gone. Old binaries that only know hex still paint. New binaries prefer the token.

Do not version the whole envelope for a palette rename. Additive fields are the compatibility rule in resilience. A new optional token is safe. Repurposing hex to mean something else is not.

Custom widgets reuse atoms. FlowText, FlowButton, FlowTag, and FlowImage already resolve through the environment. If your widget draws a SwiftUI Text with a raw font, you have opted out of both tokens and Dynamic Type scaling. That is a product decision, not a framework bug.

The Flow-UI mark: two nodes joined through a filled centre

Theming server driven SwiftUI: hex plus dark_hex out of the box, tokens through ThemeProvider, one payload for both appearances. The binary already knows how to paint. The server describes which colour to use.

More on the blog.

Read the source

Flow-UI is MIT licensed. The schema, renderer and starter widgets live on GitHub.

GitHub

Get started with Flow-UI

Install the Swift package, register a widget, and render a page from JSON.

Get started