flavors
a flavor is a named set of CSS variables with a light or dark classification. register definitions first, then select a registered name. the library doesn’t ship the documentation site’s palette collection as a consumer default.
register and select a palette
put shared definitions in an app-owned module. this example keeps the complete palette data separate from browser initialization so you can reuse it in a picker later.
// flavors.ts
import { registerFlavors } from '@ciabi/loafkit-ui';
export const readingFlavors = [
{
name: 'paper', description: 'A light reading room', isDark: false,
cssVariables: {
'--loaf-surface': '#FFFDF7', '--loaf-surface-secondary': '#EEE9DD',
'--loaf-text': '#292720', '--loaf-accent': '#566B3A',
'--loaf-text-on-accent': '#FFFFFF'
}
},
{
name: 'ink', description: 'A dark reading room', isDark: true,
cssVariables: {
'--loaf-surface': '#191D20', '--loaf-surface-secondary': '#272F34',
'--loaf-text': '#F1EEE5', '--loaf-accent': '#B5CA8A',
'--loaf-text-on-accent': '#1D2810'
}
}
];
registerFlavors(readingFlavors);then initialize the app in its root component. the initializer can read browser storage, so do that after mounting:
<script>
import { onMount } from 'svelte';
import { FlavorPicker, initializeFlavors } from '@ciabi/loafkit-ui';
import './flavors';
onMount(() => initializeFlavors({ defaultFlavor: 'paper', storageKey: 'reading-flavor' }));
</script>
<FlavorPicker />the picker uses the registered definitions. choosing one updates the public flavor store. for your own control, call flavor.set('ink') from a browser action after registration.
try a local palette
global selection changes the document root. a preview can use the same definitions without registration or persistence: own the selection and apply its variables to one container.
reading room
try a palette without changing the docs
Weekend reading
one article to come back to.
<script>
import { FlavorPicker, Card } from '@ciabi/loafkit-ui';
let selected = $state('paper');
let articleSaved = $state(false);
const previewFlavors = [
{ name: 'paper', description: 'a light reading room', isDark: false, cssVariables: { '--loaf-surface': '#FFFDF7', '--loaf-text': '#292720', '--loaf-accent': '#566B3A', '--loaf-text-on-accent': '#FFFFFF' } },
{ name: 'ink', description: 'a dark reading room', isDark: true, cssVariables: { '--loaf-surface': '#191D20', '--loaf-text': '#F1EEE5', '--loaf-accent': '#B5CA8A', '--loaf-text-on-accent': '#1D2810' } }
];
let active = $derived(previewFlavors.find(item => item.name === selected) ?? previewFlavors[0]);
</script>
<FlavorPicker pickerOnly flavors={previewFlavors} {selected} onSelect={item => selected = item.name} title="reading room" description="try a palette without changing the docs" />
<div style={`${Object.entries(active.cssVariables).map(([key,value]) => `${key}: ${value}`).join(';')}; --loaf-card-background: var(--loaf-surface); color: var(--loaf-text)`}>
<Card title="Weekend reading"><p>one article to come back to.</p><button type="button" class="loaf-primary" aria-pressed={articleSaved} onclick={() => articleSaved = !articleSaved}>{articleSaved ? 'saved for later' : 'save for later'}</button></Card>
</div>the local --loaf-card-background hook makes Card use this container’s surface. derived custom properties inherited from the root don’t automatically resolve again against a nested palette. this example applies only its own color pairs; a full app palette should also check secondary surfaces, navigation and status colors in the token reference.
selection and persistence
initializeFlavors({ defaultFlavor, storageKey }) rejects an unknown default. a recognized saved name wins over that default. missing, unknown or inaccessible storage leaves the configured default available. persistence is opt-in: omit storageKey when selection should live only in memory. initialization configures shared module state, so call it once for the app rather than from each picker.
the flavor store starts as an empty string and flavorType starts as light. setting a registered flavor updates its type and applies its variables to the document root in the browser. it sets data-flavor and data-flavor-type. changing to another flavor removes variables tracked from the previous application when the new definition omits them.
applyFlavor(name) only applies the definition to the DOM and returns success. it doesn’t itself update the selection store or persistence. prefer flavor.set(name) for global selection. unregistering a flavor doesn’t automatically choose another one; select a remaining name explicitly.
isolated previews
pass flavors, selected and onSelect to FlavorPicker when editing a local draft. that mode doesn’t register definitions or change the app’s selected flavor for you. apply preview variables to the preview container and keep its foreground/background pairs together. don’t call the root flavor helper for a preview that should stay isolated.
registries and stores are module-level state. don’t set request-specific user selections during server rendering. use static shared definitions and initialize browser selection in the app. CSS tokens provide defaults before initialization.
definition contract
names must contain non-whitespace text, and the variable map must contain at least one entry. a repeated name replaces that definition. registration copies the variable map; use registration again when replacing a definition rather than mutating a returned copy.
registerFlavor registers one definition; registerFlavors loops over several. getFlavor(name) returns a copy or undefined, listFlavors() returns copies, and unregisterFlavor(name) returns whether one was removed. bulk registration isn’t an atomic transaction: an invalid later definition doesn’t roll back earlier ones.
FlavorDefinition
the definition accepted by the flavor registry.
name-
unique registration and selection name.
description-
supporting text displayed by the picker.
iconSrc-
app-owned picker image.
isDark-
whether this palette uses dark surfaces.
cssVariables-
semantic CSS variable names and values; at least one entry.