Apply user preferences to data attributes so your styles can follow them.

uiPreferences puts your preferences on data-* attributes and keeps them up to date. Use it around a part of your app, or apply a preference to the whole page.

Console
import { Component } from "@angular/core"
import { useLog } from "./log"
import { Button } from "@ui-sdk/angular/button"
import { Preferences, usePreference } from "@ui-sdk/angular/preferences"
import { createLocalStorage, createPreference } from "@ui-sdk/core"

@Component({
  imports: [Button, Preferences],
  template: `
    <div uiPreferences [preferences]="[view]">
      <div data-slot="stack">
        <div data-slot="controls" role="group" aria-label="View">
          <button
            uiButton
            [attr.aria-pressed]="state().value === 'list'"
            (click)="choose('list', $event)"
          >
            List
          </button>
          <button
            uiButton
            [attr.aria-pressed]="state().value === 'grid'"
            (click)="choose('grid', $event)"
          >
            Grid
          </button>
        </div>
        <ul data-slot="files" aria-label="Files">
          @for (file of files; track file.name) {
            <li>
              <span>{{ file.name }}</span>
              <span data-slot="hint">{{ file.detail }}</span>
            </li>
          }
        </ul>
      </div>
    </div>
  `,
})
class Example {
  readonly view = createPreference({
    values: ["list", "grid"],
    defaultValue: "list",
    attribute: "data-view",
    storage: createLocalStorage("ui-sdk-preferences-view"),
  })

  readonly files = [
    { name: "Q3 roadmap.pdf", detail: "2.4 MB" },
    { name: "Launch checklist.md", detail: "12 KB" },
    { name: "Offsite photos", detail: "24 items" },
    { name: "Brand assets.zip", detail: "18 MB" },
  ]

  readonly log = useLog()

  readonly state = usePreference(this.view)

  choose(next: "list" | "grid", event: MouseEvent) {
    this.view.set(next, { reason: "press", event })
    this.log(`Saved the ${next} view`)
  }
}

export { Example }
Choose Grid, then reload the page. The files come back in the view you chose, and the console logs each change.

Installation

npm install @ui-sdk/angular @ui-sdk/core

Usage

Define the preference you want to apply. Here, view chooses how a file list is laid out:

preferences.ts
import { createPreference } from "@ui-sdk/core"

const view = createPreference({
  values: ["list", "grid"],
  defaultValue: "list",
  attribute: "data-view",
})

export { view }

Pass it to uiPreferences, then style the content by its attribute:

import { Component } from "@angular/core"
import { view } from "./preferences"
import { Preferences } from "@ui-sdk/angular/preferences"

@Component({
  selector: "app-example",
  imports: [Preferences],
  template: `
    <div uiPreferences [preferences]="[view]">
      <ul data-slot="files">
        <li>Q3 roadmap.pdf</li>
        <li>Launch checklist.md</li>
      </ul>
    </div>
  `,
})
class Example {
  readonly view = view
}

export { Example }
[data-view="grid"] [data-slot="files"] {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
}

uiPreferences starts with data-view="list". Calling view.set("grid") updates it, and the list becomes a grid. uiPreferences renders with display: contents, so it adds no box of its own.

Save the Value

Add storage to save the choice between visits:

preferences.ts
import { createLocalStorage, createPreference } from "@ui-sdk/core"

const view = createPreference({
  values: ["list", "grid"],
  defaultValue: "list",
  attribute: "data-view",
  storage: createLocalStorage("ui-sdk-preferences-view"),
})

export { view }

The preference now saves changes in local storage and restores the saved value when the page reloads. You can also use session storage or cookies with createPreference.

Read the Value

Use usePreference when the value affects what your component renders:

import { Component } from "@angular/core"
import { view } from "./preferences"
import { usePreference } from "@ui-sdk/angular/preferences"

@Component({
  selector: "app-example",
  template: `
    <button
      [attr.aria-pressed]="state().value === 'grid'"
      (click)="view.set(state().value === 'grid' ? 'list' : 'grid')"
    >
      Grid
    </button>
  `,
})
class Example {
  readonly view = view

  readonly state = usePreference(view)
}

export { Example }

Pass the same preference instance wherever you need its value. usePreference also works outside a uiPreferences element.

Apply to the Whole Page

Set global: true when defining the preference:

const view = createPreference({
  values: ["list", "grid"],
  defaultValue: "list",
  attribute: "data-view",
  global: true,
  storage: createLocalStorage("ui-sdk-preferences-view"),
})

Keep passing it to uiPreferences. Its attribute now goes on <html>, so styles throughout the page can use it. A global preference needs only one uiPreferences on the page to apply it.

Multiple Preferences

Pass more than one preference in the list. For example, you can apply view alongside a color scheme:

<div uiPreferences [preferences]="[view, colorScheme]">
  <router-outlet />
</div>

Each preference uses its own attribute and global option. Wrappers that share a preference instance follow the same value. For independent choices, use separate preference instances and different storage keys.

Server Rendering

On the server, uiPreferences includes a script that applies saved values before the page paints. CSS can use those attributes immediately. usePreference starts with the server's value and updates after hydration.

Follow the server rendering setup to place uiPreferences in your app and keep cookie-backed preferences separate for each request.

If your Content Security Policy requires a nonce, pass the nonce from your response:

<div uiPreferences [preferences]="[view]" [nonce]="nonce">
  <router-outlet />
</div>

API Reference

Preferences

PropTypeDescription
preferencesreadonly PreferenceLike[]The preferences to apply, from createPreference or createColorScheme. See Usage.
nonce?stringThe nonce your Content Security Policy gives inline scripts. See Server Rendering.

usePreference

usePreference(preference) returns a signal of the preference's state. For a preference created with createPreference, read state().value. For a color scheme, the state also includes system and resolved. Call it in a field initializer, and read the signal where you render it so the template follows changes.