Preferences

The preferences module keeps a reader’s choices together in the browser, as one object you can add to.

savePreferences() merges what you give it into one JSON object in localStorage, so settings last across sessions. It is a record: theme and language each keep a store of their own, and that is what they read back when a page opens. Use it for choices of your own, and to keep a note of those two beside them.

Cycle calls it for you whenever a reader changes the language, the theme or the accent colour.

import { savePreferences } from '@0001-labs/design-system-1';
// Save user preferences
savePreferences({
language: 'ja',
theme: 'dark'
});

Each call merges into what is stored, so keys you do not name keep their values.

import { savePreferences } from '@0001-labs/design-system-1';
savePreferences({
language: 'ja'
});

A language is a LanguageCode. The page’s language itself is set with setLanguage(); see Languages.

savePreferences({
theme: 'dark' // or 'light' or 'system'
});

The page’s theme itself is set with setThemeMode(); see Theming.

Any other key is yours. Its value must survive JSON.stringify.

savePreferences({
customSetting: 'value',
anotherSetting: 123
});

Preferences are stored in localStorage under the key ds-one:preferences:

// Storage structure
{
"language": "ja",
"theme": "light",
"customSetting": "value"
}

The module has no function to read this object: read it with localStorage and preferenceStorageKey("preferences"), which gives the key in use. preferenceStorageKey("theme") gives the theme’s.

Each choice has its own store:

ChoiceKeySet by
The merged objectds-one:preferencessavePreferences()
Themeds-one:themesetThemeMode()
Shapeds-one:shapesetShape()
LanguagelanguagesetLanguage()

A product with its own storage names sets them on window.DS_ONE_PREFERENCES before these modules load. The language’s key is set apart, through configureI18n.

OptionDefaultWhat it does
storageKeyds-one:preferencesThe key of the merged object.
themeStorageKeyds-one:themeThe key of the theme.
themeModeUnsetsystem follows the system without reading or writing a saved theme.
themeDefaultsystemThe theme of a reader who has chosen none: light, dark or system.
themePaperThe system’s paperThe canvas and the browser’s theme colour for each theme, light and dark. false leaves both to the page.
shapeStorageKeyds-one:shapeThe key of the shape.
shapeDefaultUnsetThe shape of a reader who has chosen none: square, rounded or circular.
<script>window.DS_ONE_PREFERENCES = { storageKey: 'product:preferences', themeStorageKey: 'product:theme' };</script>
type Preferences = {
language?: LanguageCode;
theme?: ThemeMode;
[key: string]: unknown;
};

The module handles storage errors gracefully:

import { savePreferences } from '@0001-labs/design-system-1';
// Won't throw if localStorage is unavailable
savePreferences({ theme: 'dark' });
// Logs a warning if storage fails

Outside a browser it does nothing. If what is stored is not valid JSON, that save is skipped with the same warning.

Preferences work beside the other Design system 1 modules. setThemeMode() and setLanguage() store their own choice, so the second line in each example below only keeps it in the merged object too.

import { setThemeMode, savePreferences } from '@0001-labs/design-system-1';
function updateTheme(mode) {
setThemeMode(mode);
savePreferences({ theme: mode });
}
import { setLanguage, savePreferences } from '@0001-labs/design-system-1';
function updateLanguage(language) {
setLanguage(language);
savePreferences({ language });
}
  1. Save what changed - Each call merges, so pass only the keys that changed
  2. Handle errors - Storage might be unavailable in private browsing, so keep a choice working for the current page without it
  3. Validate values - Ensure preference values are valid before saving

Theming · Languages · Shape · Cycle