Preferences

A user's choices from fixed lists — list or grid, a density, a panel open — kept, and written as data-* attributes for your styles to follow.

UtilitiesExperimental

Usage

Usage

  • report.pdf
  • notes.md
  • budget.xlsx
  • photo.png
import { Component, ViewEncapsulation } from '@angular/core';import {  ScPreferencesApply,  ScSegmentedControl,  ScSegmentedControlIndicator,  ScSegmentedControlItem,  definePreference,  injectPreference,} from '@semantic-components/ui';

Anatomy

A preference is declared once, in a file of yours; any component reads and sets it; an element shows it as an attribute.

import { ScPreferencesApply, definePreference, injectPreference } from '@semantic-components/ui';
// view.tsexport const view = definePreference({ attribute: 'data-view', values: ['list', 'grid'], defaultValue: 'list', storageKey: 'app-view' });
// a componentreadonly view = injectPreference(view); // view.value(), view.set("grid")
<div [scPreferences]="[view]">  <ul class="grid in-data-[view=grid]:grid-cols-2">…</ul></div>

Kept, and shown

With a storageKey, a choice is saved as a cookie — the server reads it, so a page rendered per request is drawn as the user left it, from the first paint — copied to localStorage; another tab's choice comes through. Without one, it lasts as long as the app. A saved value not in values is ignored: the default. A prerendered page has no request: it shows the default until the app starts. The theme (light, dark, system) is the theme manager's.

Examples

Read the value

  • report.pdf
  • notes.md
  • budget.xlsx
  • photo.png

list

import { Component, ViewEncapsulation } from '@angular/core';import {  ScButton,  ScPreferencesApply,  definePreference,  injectPreference,} from '@semantic-components/ui';
const view = definePreference({  attribute: 'data-view',

The whole page

Monthly report

Spacing from the page's density.
import { Component, ViewEncapsulation } from '@angular/core';import {  ScCard,  ScCardBody,  ScCardHeader,  ScCardTitle,  ScPreferencesApply,  ScSegmentedControl,  ScSegmentedControlIndicator,  ScSegmentedControlItem,

Several preferences

  • report.pdf
  • notes.md
  • budget.xlsx
  • photo.png
import { Component, ViewEncapsulation } from '@angular/core';import {  ScButton,  ScPreferencesApply,  definePreference,  injectPreference,} from '@semantic-components/ui';
const view = definePreference({  attribute: 'data-view',

API reference

PartSelectorDescription
definePreferencefunction Declares a preference: its attribute, its values, its default, where it is kept.
injectPreferencefunction The preference's instance, the same everywhere: value() (a signal), set(value).
ScPreferencesApply[scPreferences] Writes the preferences as data-* attributes on its element — or <html> for a global one.
InputTypeDefaultDescription
attribute`data-${string}`— The attribute it is written in.
valuesreadonly V[]— The values it can take.
defaultValueVundefined Its value while none is saved. Left out: undefined until the user chooses (no attribute), and set(undefined) forgets a choice.
storageKeystring— Kept under this name; without it, for the app’s life.
globalbooleanfalse On <html>, for the whole page.