Introducing a new way to review your PRs.

Read the announcement

arhamkhnz / next-shadcn-admin-dashboard

Modern Admin Dashboard Template built with Shadcn UI and Next.js 16

View on GitHub

22 documents · Updated Nov 23, 2026

docs / preferences-and-theme-runtime

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:

GroupPreferenceAvailable valuesDefault
ThemeTheme modeLight, dark, systemLight
ThemeTheme presetDefault, Brutalist, Soft Pop, TangerineDefault
TypographyFontThe registered font familiesGeist
Page frameContent layoutCentered, full widthCentered
Page frameNavbar styleSticky, scrollSticky
Page frameSidebar variantSidebar, inset, floatingSidebar
Page frameSidebar collapse modeIcon, offcanvasIcon

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:

  1. A control sends a validated value to the store.
  2. The runtime applies the value to the document. Theme mode also returns the resolved light or dark mode.
  3. The store updates its in-memory values.
  4. 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 methodPurposeFirst-render use
No saveTemporary choiceNo
Browser cookieClient-written value that the browser sends on later requestsYes
Server cookieValue written through a server actionYes
Local storageClient-only value that does not need server renderingNo

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.

PresetGeneral visual role
DefaultBase neutral style from the global stylesheet
BrutalistSquare shapes with strong black borders and offset shadows
Soft PopRounded shapes with bright blue, green, and yellow accents
TangerineWarm 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

TriggerConsequenceGuard
A saved value is not in the registryThe value is rejected and the related choice falls back to its defaultParse every external value through the registry definition
A layout-critical choice uses client-only persistenceThe server shell can render one frame and the browser can switch to another after loadKeep shell-shaping preferences on cookie persistence
Boot attributes and provider state are not reconciledControls can show stale values or the shell can change after hydrationRead the boot-applied document attributes before marking the store synced
System mode is selected without an active media listenerThe page does not follow a later operating-system theme changeSubscribe only while system mode is active and remove the old listener when it changes
A preset CSS file lacks valid metadata or a primary colorThe generated option can have a fallback label or an empty preview colorKeep label, value, and light/dark --primary data in each preset
A preset is generated but its CSS is not included globallyThe control can offer a style that produces no visual changeKeep the preset import and generated option in sync

Key files

FileWhy to read it
src/…/preferences-config.tsDefines the preference contract, defaults, persistence, and document attributes
src/…/preferences-store.tsShows how changes and resets update the document, store, and saved values
src/…/preferences-provider.tsxConnects the boot-applied document state to client controls and system theme changes
src/…/preferences-storage.tsMaps preference persistence modes to browser and server save paths
src/…/server-actions.tsProvides validated cookie reads and server cookie writes
src/…/theme-boot.tsxApplies saved preferences before hydration to protect the first rendered frame
src/…/theme-utils.tsResolves and applies light, dark, and system theme behavior
src/…/generate-theme-presets.tsBuilds the preset metadata from CSS styles and global defaults
src/…/theme.tsHolds theme modes and the generated preset options used by controls
src/…/layout.tsDefines the supported dashboard frame choices
src/…/layout.tsxApplies server-visible layout choices to the dashboard frame
src/…/layout-controls.tsxExposes theme, font, and layout choices to users
src/…/globals.cssMaps document attributes and semantic variables to the visible style system
src/…/brutalist.cssDefines the Brutalist light and dark visual preset
src/…/soft-pop.cssDefines the Soft Pop light and dark visual preset
src/…/tangerine.cssDefines the Tangerine light and dark visual preset