{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "preferences",
  "title": "Preferences",
  "description": "Kino's settings, light or dark, density, feel, shape and reduced motion, as ui-sdk preferences: saved in the browser and written as data attributes before the first paint, with shadcn's dark class beside the colour scheme.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/refs.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/preferences.tsx",
      "content": "'use client'\n\nimport { commitStyles } from '@/registry/lib/refs'\nimport {\n  type ColorSchemeState,\n  createColorScheme,\n  createLocalStorage,\n  createPreference,\n  type PreferenceLike,\n} from '@ui-sdk/core'\nimport { Preferences as PreferencesPrimitive } from '@ui-sdk/react/preferences'\nimport { type ComponentProps, useLayoutEffect } from 'react'\n\n/*\n * Kino's settings as ui-sdk preferences, saved in the browser and written as\n * the data attributes Kino's tokens answer (`data-density`, `data-feel`,\n * `data-shape`, `data-motion`), before the first paint. A preference of your\n * own is ui-sdk's `createPreference`, read with its `usePreference`.\n */\n\n/**\n * Light, dark or the device's own, saved under `theme` with next-themes'\n * values, so a project moving from it keeps every visitor's choice.\n */\nconst colorSchemePreference = createColorScheme({\n  global: true,\n  storage: createLocalStorage('theme'),\n})\n\n/** How much room controls, rows and surfaces take (the `theme` item's density tokens). */\nconst densityPreference = createPreference({\n  values: ['snug', 'balanced', 'airy'],\n  defaultValue: 'balanced',\n  attribute: 'data-density',\n  global: true,\n  storage: createLocalStorage('density'),\n})\n\n/** The timing everything moves with (the `theme` item's feels). */\nconst feelPreference = createPreference({\n  values: ['calm', 'crisp', 'lively'],\n  defaultValue: 'crisp',\n  attribute: 'data-feel',\n  global: true,\n  storage: createLocalStorage('feel'),\n})\n\n/** How round corners are (the `theme` item's shape tokens). */\nconst shapePreference = createPreference({\n  values: ['standard', 'round'],\n  defaultValue: 'standard',\n  attribute: 'data-shape',\n  global: true,\n  storage: createLocalStorage('shape'),\n})\n\n/**\n * Whether to reduce motion as the device asks, or always. The item's CSS\n * makes Tailwind's `motion-reduce:` and `motion-safe:` answer\n * `data-motion='reduce'` too, on the page or on any part of it.\n * `useMotionTransition` reads it from <html> only, as it does the feel.\n */\nconst motionPreference = createPreference({\n  values: ['system', 'reduce'],\n  defaultValue: 'system',\n  attribute: 'data-motion',\n  global: true,\n  storage: createLocalStorage('motion'),\n})\n\nconst KINO_PREFERENCES = [\n  colorSchemePreference,\n  densityPreference,\n  feelPreference,\n  shapePreference,\n  motionPreference,\n]\n\n/**\n * Puts the colour scheme where shadcn's theme reads it: the `dark` or `light`\n * class, and `color-scheme` for the browser's own scrollbars and fields.\n * ui-sdk writes only `data-color-scheme`, the choice, so a `system` choice\n * asks the device. Inlined in the script, so it references nothing outside\n * itself.\n */\nfunction writeScheme(html: HTMLElement, choice: string | null) {\n  const scheme =\n    choice === 'dark' ||\n    (choice !== 'light' && window.matchMedia('(prefers-color-scheme: dark)').matches)\n      ? 'dark'\n      : 'light'\n  if (html.classList.contains(scheme)) return\n  html.classList.remove('light', 'dark')\n  html.classList.add(scheme)\n  html.style.colorScheme = scheme\n}\n\n// After ui-sdk's script, which has put the saved choice on <html>.\nconst SCHEME_SCRIPT = `(${writeScheme.toString()})(document.documentElement,document.documentElement.getAttribute(${JSON.stringify(colorSchemePreference.attribute)}))`\n\n/** A change of choice, or of the device's scheme while the choice is `system`. */\nfunction showScheme({ value, resolved }: ColorSchemeState) {\n  const html = document.documentElement\n  if (html.classList.contains(resolved === 'dark' ? 'dark' : 'light')) return\n  // Every colour would fade at its own pace: switch them all at once.\n  // Only colours: a press or a highlight moving now keeps moving.\n  const pause = document.createElement('style')\n  pause.textContent =\n    '*,*::before,*::after{transition-property:transform,translate,scale,rotate,clip-path,opacity!important}'\n  document.head.append(pause)\n  writeScheme(html, value)\n  commitStyles(html)\n  requestAnimationFrame(() => pause.remove())\n}\n\n/** The class and `color-scheme` from the first paint on: the script, then each change. */\nfunction SchemeClass({ nonce }: { nonce?: string }) {\n  useLayoutEffect(() => {\n    writeScheme(document.documentElement, colorSchemePreference.get().value)\n    return colorSchemePreference.subscribe(showScheme)\n  }, [])\n  return (\n    <script\n      // It runs once, from the server's HTML, after ui-sdk's.\n      type={typeof window === 'undefined' ? undefined : 'text/plain'}\n      nonce={nonce}\n      suppressHydrationWarning\n      // oxlint-disable-next-line react/no-danger\n      dangerouslySetInnerHTML={{ __html: SCHEME_SCRIPT }}\n    />\n  )\n}\n\n/**\n * Applies preferences and keeps them applied, Kino's five by default:\n * page-wide ones on <html>, the others on a `display: contents` wrapper\n * around `children`. Put it at the top of <body>, before what it styles, so\n * its script runs before the first paint.\n */\nfunction Preferences({\n  preferences = KINO_PREFERENCES,\n  nonce,\n  children,\n  ...props\n}: Omit<ComponentProps<typeof PreferencesPrimitive>, 'preferences'> & {\n  /** The preferences to apply, from Kino or ui-sdk's `createPreference`. */\n  preferences?: readonly PreferenceLike[]\n}) {\n  return (\n    <PreferencesPrimitive preferences={preferences} nonce={nonce} {...props}>\n      {preferences.includes(colorSchemePreference) ? <SchemeClass nonce={nonce} /> : null}\n      {children}\n    </PreferencesPrimitive>\n  )\n}\n\nexport {\n  colorSchemePreference,\n  densityPreference,\n  feelPreference,\n  motionPreference,\n  Preferences,\n  shapePreference,\n}\n\nexport { SCHEME_SCRIPT as schemeScript }\n",
      "type": "registry:component",
      "target": "@components/kino/preferences.tsx"
    }
  ],
  "css": {
    "@custom-variant motion-reduce": {
      "@media (prefers-reduced-motion: reduce)": {
        "@slot": {}
      },
      "&:where([data-motion='reduce'], [data-motion='reduce'] *)": {
        "@slot": {}
      }
    },
    "@custom-variant motion-safe": {
      "@media (prefers-reduced-motion: no-preference)": {
        "&:where(:not([data-motion='reduce'], [data-motion='reduce'] *))": {
          "@slot": {}
        }
      }
    }
  },
  "type": "registry:component"
}