Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Styling & Theming

Styling in nano-ui is scoped rather than selector-based. A Theme holds a Style record for each surface kind, and styled or themed adjusts that theme for a subtree. There is no per-widget CSS and no global mutable stylesheet.

To experiment with modifiers live, open the Interactive Styling Lab. For subtle surface interactions, check Styling Gotchas.

The five theme surfaces

Theme fieldWidgets that read it
themeButtonButtons, toggles, dropdown triggers
themeInputText fields, search boxes, numeric inputs, sliders
themePanelPanels, cards, containers, sidebars, toolbars
themePopupDropdown menus, context menus, popups
themeFloatingWindowModals, floating windows, inspectors

Each Style sets styleBg, styleFg, styleBorder, styleBorderWidth, styleCornerRadius, styleHoverBg, styleActiveBg, and styleBorderSides. Beyond those five surfaces, a Theme defines shared semantic colors: themeAccent, themeOnAccent, themeMuted, themeSelection, themeFocusRing, themeWindow, themeSeparator, and the success, warning, and danger tones. Construct colors with colorRGB, colorRGBA, colorHex "#38bdf8", or colorHexA "#38bdf8cc".

The backdrop behind your view is pageBackground, which defaults to themeWindow unless the theme sets themePage (via pageColor). Calling styled panelPage puts a borderless view directly on the panel surface. The window color itself still peeks through gaps between panels:

-- An app with no panel of its own: the panel surface is the page.
panelPageOptions :: SdlOptions
panelPageOptions = defaultSdlOptions {sdlAppTheme = Just (panelPage defaultTheme)}

-- Or scope it to a page with no panels.
body :: NanoUI ()
body = styled panelPage (columnWith (padAll 12) (label "Body"))

A full-window scroller (scrollWith grow) paints using this page color instead of drawing a bordered well.

defaultTheme (dark) and defaultLightTheme live in NanoUI; extra presets (Tomorrow variants) and Base16 generators (themeFromBase16, themeFromBase16Dark, themeFromBase16Light) live in NanoUI.Themes. Set a theme at startup with sdlAppTheme = Just appTheme, change it dynamically per frame with setTheme, or match the OS appearance automatically with followSystemTheme or lightDark.

Scoped styling

themedViews = do
  -- Replace the base theme for a subtree.
  themed defaultLightTheme mainContent

  -- Modify the active theme for a subtree.
  whenM (styled (primary . rounded 999) (button "Continue")) continue

Style and theme modifiers compose with (.), and nested styled blocks build on top of outer overrides. Theme changes affect painting only and never alter layout sizes.

  • Style modifiers (Style -> Style): background, foreground, borderColor, borderWidth, borderSides, cornerRadius, hoverBackground, pressBackground, and fillColor.
  • Theme modifiers (Theme -> Theme): buttonStyle, inputStyle, panelStyle, everyStyle, accentColor, textColor, rounded, subtle, tinted, and readableOn.
  • Semantic shortcuts: primary, destructive, success, warning, tone Danger, calloutTone Warning, and toneColor Warning.

For example, here is a quiet, borderless button helper:

quiet :: Theme -> Theme
quiet t = (subtle . buttonStyle (foreground (themeMuted t))) t

quietSkip = whenM (styled quiet (button "Skip")) skip

When designing a custom visual identity, start with a palette record, construct one complete Theme from it, and build small scoped helpers on top. Keeping three clear surface levels (recessed wells, panel surfaces, and elevated controls) while reserving accent color for meaning keeps interfaces clean and readable.

Interaction states

Every interactive widget renders in one of four visual states:

StateVisual treatment
DefaultstyleBg, styleFg, styleBorder
HoverstyleHoverBg
PressedstyleActiveBg while the primary mouse button is held on the widget
DisabledDimmed colors with all pointer and keyboard input ignored

Wrap any subtree in disabledWhen cond to dim it and disable interaction without changing layout or losing state:

savingButtons = do
  isSaving <- useVar False
  saving <- readVar isSaving
  disabledWhen saving $ row $ do
    whenM (button "Cancel") cancel
    whenM (button "Confirm") confirm

Shortcuts and mouse events inside a disabled subtree are blocked automatically.

Compare all four states across dark and light themes:

Typography and fonts

Font modifiers compose right alongside layout modifiers on text and labeled widgets (labelWith, buttonWith, selectWith, textAreaWith, and ticLayout inside textInputConfigured):

  • Size and face: fontSize px, fontSizeScale factor, fontRegular, fontProportional, fontMono, fontTabular (tabular lining digits so numeric columns align without jumping)
  • Weight, slant, decoration: fontBold (or fontWeight), fontItalic, fontUnderline, fontStrike
  • Color and tone: fontTone Warning, fontColor color
  • Single-line truncation: singleLine keeps a label on one line even inside a column and truncates overflow with an ellipsis; ellipsisEnd (default), ellipsisMiddle (for file paths), or ellipsisStart chooses where the ellipsis sits (textEllipsis).
warningLabel = labelWith (fontMono . fontSize 15 . fontTone Warning) "warning: low disk"

The SDL backend bundles Inter as its default font and can also search installed system fonts by family name:

fontOptions :: SdlOptions
fontOptions =
  defaultSdlOptions
    { sdlAppFont = FontSearch ["Segoe UI", "SF Pro Text", "Helvetica Neue"]
    , sdlAppMonoFont = FontSearch ["Cascadia Mono", "JetBrains Mono", "Consolas"]
    , sdlAppFontSize = 14
    }

Call setSdlUiFont to swap fonts mid-session, and set sdlAppUiScale = 0 on Windows to follow the OS display scale. Whenever you need exact string widths or line metrics, use lineWidthUi, fmLineHeight, and fmSnapScale instead of adding per-glyph advances by hand.

Preview the font modifiers side by side: