Preferences and theme runtime
The preferences and theme runtime is part of the personalization system and gives users one way to control the app’s appearance and dashboard frame. It validates each choice, applies it to the document, keeps the client state current, and saves values so the next page load can start with the same theme and layout. Its layout choices shape the application shell.
Preference model
Preference registry
The preference registry is the shared definition of every supported display choice. It gives each preference a fixed set of values, a default, a save method, and a data-* attribute that connects the choice to the document and its styles. The registry is the source used by the client store, the server cookie reader, and the early boot step.
The current preference groups are:
| Group | Preference | Available values | Default |
|---|---|---|---|
| Theme | Theme mode | Light, dark, system | Light |
| Theme | Theme preset | Default, Brutalist, Soft Pop, Tangerine | Default |
| Typography | Font | The registered font families | Geist |
| Page frame | Content layout | Centered, full width | Centered |
| Page frame | Navbar style | Sticky, scroll | Sticky |
| Page frame | Sidebar variant | Sidebar, inset, floating | Sidebar |
| Page frame | Sidebar collapse mode | Icon, offcanvas | Icon |
Values that are not in the registry are replaced with the preference’s default. This keeps cookies, document attributes, and interactive controls on the same contract.
References: src/…/preferences-config.ts• L24-37 src/…/preferences-config.ts• L55-120 src/…/preferences-config.ts• L122-134 src/…/layout.ts• L1-32 src/…/theme.ts• L1-10
Preference store
The preference store is the client-side source of truth after the document starts. It holds all preference values, the resolved light or dark mode, and a sync flag that tells shell components when browser values have replaced the initial server values.
A preference change follows one path:
- A control sends a validated value to the store.
- The runtime applies the value to the document. Theme mode also returns the resolved light or dark mode.
- The store updates its in-memory values.
- The persistence utility saves the value using the registry’s save method.
Reset restores every registry default. It applies each default to the document before it replaces the store state, so the visible shell and stored state change together.
References: src/…/preferences-store.ts• L13-19 src/…/preferences-store.ts• L21-44 src/…/preferences-store.ts• L46-61 src/…/preference-runtime.ts• L7-17
Preference provider
The preference provider makes the store available to client components and reconciles it with the document state. It first uses the values supplied by the root layout. After the browser starts, it reads the document attributes that the boot step applied and marks the store as synced.
The dashboard shell uses this sync boundary to avoid changing its sidebar shape before browser values are known. The provider also watches system color changes when the selected mode is system, then reapplies the resolved theme and removes its listener when the provider stops.
References: src/…/preferences-provider.tsx• L19-37 src/…/preferences-provider.tsx• L39-54 src/…/preferences-provider.tsx• L56-90 src/…/app-sidebar.tsx• L26-39
Persistence and startup
Persistence paths
Persistence is selected per preference instead of by the caller. The persistence utility supports no save, a browser cookie, a server cookie action, or local storage. The current display preferences use browser cookies, which lets the server and browser see the same values for the first dashboard render.
The server preference reader accepts only cookie-backed preferences. It validates the cookie value through the same registry rules and returns the default for preferences that do not use cookies or for values that are not allowed.
| Save method | Purpose | First-render use |
|---|---|---|
| No save | Temporary choice | No |
| Browser cookie | Client-written value that the browser sends on later requests | Yes |
| Server cookie | Value written through a server action | Yes |
| Local storage | Client-only value that does not need server rendering | No |
References: src/…/preferences-storage.ts• L14-35 src/…/server-actions.ts• L13-40 src/…/preferences-config.ts• L1-12
Theme bootstrapping
The theme boot step runs in the document head before hydration. It reads each preference from its configured browser source, checks the value, and writes the matching document attribute. It then resolves the selected theme mode and sets the dark class and browser color scheme.
This early step prevents a saved theme or layout from appearing only after the client loads. The root layout still renders with defaults so the server has a stable document shape. The boot step changes that document before the interactive provider takes over.
The startup relationship is:
flowchart LR
Save[Saved preference] --> Boot[Early theme boot]
Defaults[Registry defaults] --> Root[Root document]
Root --> Boot
Boot --> Attributes[Document attributes]
Boot --> Mode[Dark class and color scheme]
Attributes --> CSS[Global styles]
Mode --> CSS
Attributes --> Provider[Preference provider]
Provider --> Store[Synced preference store]
Store --> Controls[Dashboard controls]
References: src/…/theme-boot.tsx• L1-7 src/…/theme-boot.tsx• L10-77 src/…/layout.tsx• L21-45 src/…/preferences-provider.tsx• L48-54
Runtime appearance
Theme mode
Theme mode is the light, dark, or system choice. Light and dark are direct results. System reads the browser’s prefers-color-scheme setting and turns it into the resolved mode used by the document and by theme preset previews.
When a user changes the mode, the runtime updates the root attribute, dark class, and color-scheme value. It briefly disables transitions while the change takes effect. When system mode is active, a media-query listener repeats this process when the operating system changes its color preference.
References: src/…/theme.ts• L1-10 src/…/theme-utils.ts• L3-21 src/…/theme-utils.ts• L24-37 src/…/preferences-provider.tsx• L56-82
Theme preset generation
Theme presets are CSS variable sets selected by the data-theme-preset document attribute. Each preset supplies light and dark values for the app’s semantic colors, shape radius, shadows, and related visual roles. The default preset comes from the base variables in src/…/globals.css.
The preset generator keeps the user-facing preset list aligned with the CSS files. It scans preset CSS files, reads their label and value markers, extracts the primary color for both modes, adds the default colors from the global stylesheet, and writes the resulting metadata into the theme definition. The primary colors support the color marker shown in the preference control.
References: src/…/generate-theme-presets.ts• L1-16 src/…/generate-theme-presets.ts• L22-67 src/…/generate-theme-presets.ts• L69-123 src/…/globals.css• L5-8 src/…/globals.css• L55-128
Style application
Global styles turn document state into visible application styles. Semantic color roles and shape values are the common layer. A selected preset overrides those roles, the dark class selects its dark values, and the font attribute selects the active type face.
This design keeps controls and pages independent of the selected visual style. They use semantic roles such as background, foreground, primary, border, and sidebar rather than choosing a preset directly.
References: src/…/globals.css• L12-53 src/…/globals.css• L55-128 src/…/globals.css• L144-231 src/…/preference-runtime.ts• L7-17
Layout preferences
Layout choices
Layout preferences describe the dashboard frame rather than page data. Content layout controls the usable page width. Navbar style controls whether the dashboard header stays at the top during scroll. Sidebar variant selects the sidebar frame, while collapse mode selects whether it becomes an icon rail or leaves the page as an off-canvas panel.
The dashboard exposes these choices beside theme mode, theme preset, and font selection. All changes use the same store path, so a layout change is immediate and saved in the same way as a color change.
References: src/…/layout.ts• L1-32 src/…/layout-controls.tsx• L16-68 src/…/layout-controls.tsx• L148-229
Layout calculations
The dashboard layout reads server-visible sidebar preferences before it builds the shell. It gives the sidebar a fixed base width and gives the header a shared height used by dashboard views. The content area then applies the selected document attributes to choose its width and header behavior.
The centered option constrains page content to the dashboard’s maximum width. Full width leaves that constraint off. Sticky navbar adds the position, stacking, background, and blur rules needed to keep the header visible. Pages can opt out of the normal content padding when they need an edge-to-edge surface.
flowchart TD
Cookies[Saved sidebar choices] --> ServerShell[Server dashboard shell]
ServerShell --> Sidebar[Sidebar variant and collapse mode]
Document[Content and navbar attributes] --> Content[Content width]
Document --> Header[Header scroll behavior]
Sidebar --> Frame[Dashboard frame]
Content --> Frame
Header --> Frame
Frame --> Pages[Dashboard pages]
References: src/…/layout.tsx• L18-44 src/…/layout.tsx• L46-73 src/…/app-sidebar.tsx• L26-39 src/…/globals.css• L144-231
Available style presets
Theme presets
The theme preset list contains one base style and three alternate visual styles. Each alternate style has separate light and dark color values and can also change shape and shadow character.
| Preset | General visual role |
|---|---|
| Default | Base neutral style from the global stylesheet |
| Brutalist | Square shapes with strong black borders and offset shadows |
| Soft Pop | Rounded shapes with bright blue, green, and yellow accents |
| Tangerine | Warm orange primary color with a soft neutral frame |
The list also stores one primary color for each mode. The dashboard uses the color for the preset marker that matches the current resolved mode.
References: src/…/theme.ts• L11-50 src/…/brutalist.css• L1-6 src/…/brutalist.css• L7-46 src/…/soft-pop.css• L1-6 src/…/soft-pop.css• L7-46 src/…/tangerine.css• L1-6 src/…/tangerine.css• L7-46 src/…/layout-controls.tsx• L85-105
Font presets
Font is a document-level preference that changes the main sans role used by the application. The font registry supplies the available family labels and the font variables that the root document loads. The current registry includes sans, mono, slab, serif, and pixel styles, with Geist as the default.
The selected font is applied through the root data-font attribute. Global styles map that value to the active --font-sans role, so pages do not need font-specific logic.
References: src/…/registry.ts• L110-196 src/…/preferences-config.ts• L70-75 src/…/globals.css• L177-230
Change checklist
When these files or sections are changed, remember to consider these:
- A new preference must be added to the registry before controls, boot logic, server reads, or document styles can use it safely.
- A layout-critical preference must remain cookie-backed so the server dashboard shell and browser shell select the same frame.
- A change to a preference value or default must stay aligned with its document attribute, CSS selector, boot validation, and control value.
- A new theme preset must include both light and dark preset blocks plus the metadata and primary color data needed by generation.
- A change to preset CSS must be followed by regeneration of the theme metadata so the control list and stored values remain current.
- A change to system theme handling must preserve both the first boot result and the later media-query update.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A saved value is not in the registry | The value is rejected and the related choice falls back to its default | Parse every external value through the registry definition |
| A layout-critical choice uses client-only persistence | The server shell can render one frame and the browser can switch to another after load | Keep shell-shaping preferences on cookie persistence |
| Boot attributes and provider state are not reconciled | Controls can show stale values or the shell can change after hydration | Read the boot-applied document attributes before marking the store synced |
| System mode is selected without an active media listener | The page does not follow a later operating-system theme change | Subscribe only while system mode is active and remove the old listener when it changes |
| A preset CSS file lacks valid metadata or a primary color | The generated option can have a fallback label or an empty preview color | Keep label, value, and light/dark --primary data in each preset |
| A preset is generated but its CSS is not included globally | The control can offer a style that produces no visual change | Keep the preset import and generated option in sync |
Key files
| File | Why to read it |
|---|---|
| src/…/preferences-config.ts | Defines the preference contract, defaults, persistence, and document attributes |
| src/…/preferences-store.ts | Shows how changes and resets update the document, store, and saved values |
| src/…/preferences-provider.tsx | Connects the boot-applied document state to client controls and system theme changes |
| src/…/preferences-storage.ts | Maps preference persistence modes to browser and server save paths |
| src/…/server-actions.ts | Provides validated cookie reads and server cookie writes |
| src/…/theme-boot.tsx | Applies saved preferences before hydration to protect the first rendered frame |
| src/…/theme-utils.ts | Resolves and applies light, dark, and system theme behavior |
| src/…/generate-theme-presets.ts | Builds the preset metadata from CSS styles and global defaults |
| src/…/theme.ts | Holds theme modes and the generated preset options used by controls |
| src/…/layout.ts | Defines the supported dashboard frame choices |
| src/…/layout.tsx | Applies server-visible layout choices to the dashboard frame |
| src/…/layout-controls.tsx | Exposes theme, font, and layout choices to users |
| src/…/globals.css | Maps document attributes and semantic variables to the visible style system |
| src/…/brutalist.css | Defines the Brutalist light and dark visual preset |
| src/…/soft-pop.css | Defines the Soft Pop light and dark visual preset |
| src/…/tangerine.css | Defines the Tangerine light and dark visual preset |