{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "native-select-control",
  "title": "Native select control",
  "description": "The browser's own select as a ui-sdk primitive: an agent reads its value and options and picks one as a person does, and each change says who made it. The Native select's.",
  "dependencies": [
    "@ui-sdk/core"
  ],
  "files": [
    {
      "path": "registry/lib/native-select-control.ts",
      "content": "import { type ChangeDetails, createChange, definePrimitive } from '@ui-sdk/core'\n\n/*\n * The browser's own `<select>` as a ui-sdk primitive: the Native select's.\n * The select keeps its value and its options, as a native one does; this\n * reads them, so an agent's `snapshot()` lists the select with its name,\n * value and options, and `setValue` picks an option as a person does, with\n * `change` and `input` events the page hears. Each change reaches\n * `onValueChange` with who made it in `details.source`.\n */\n\n/** Why the value changed: a pick in the select, a form's reset, or code and agents (`imperative`). */\ntype NativeSelectReason = 'input-change' | 'reset' | 'imperative'\n\ntype NativeSelectOptions = {\n  /** Called after each change of the picked option, with who made it in `details.source`; `details.cancel()` puts the last one back. */\n  onValueChange?: (value: string, details: ChangeDetails<NativeSelectReason>) => void\n}\n\nconst nativeSelectControl = definePrimitive({\n  name: 'native-select-control',\n  options: { onValueChange: 'callback' },\n  // The primitive reads its options' type from this parameter; the select holds the value.\n  state: (options: NativeSelectOptions) => {\n    // oxlint-disable-next-line typescript/no-meaningless-void-operator -- marks a parameter kept for its type\n    void options\n    return { value: '' }\n  },\n  api: (context) => {\n    const { store } = context\n    const change = createChange(context)\n    const select = () =>\n      (context.element('root') as HTMLSelectElement | undefined) ?? null\n    // The events `setValue` sends are its own: the select's handler leaves them.\n    let writing = false\n    /** Tells the owner of a change the select has already made; a cancel puts the last value back. */\n    const changed = (\n      el: HTMLSelectElement,\n      details: { reason: NativeSelectReason; event?: Event },\n    ) => {\n      const last = store.get().value\n      if (el.value === last) return\n      if (!change('value', el.value, details)) el.value = last\n    }\n    return {\n      /** The picked option's value, from the select itself: after a render that set it. */\n      read: () => {\n        const el = select()\n        if (el && el.value !== store.get().value) store.set({ value: el.value })\n      },\n      changed,\n      writing: () => writing,\n      /** Picks the option with this value, as a person does: false when there is none, or it's off. */\n      setValue: (\n        value: string,\n        details: { reason?: NativeSelectReason; event?: Event } = {},\n      ) => {\n        const el = select()\n        if (!el || el.disabled) return false\n        const option = [...el.options].find((each) => each.value === value)\n        if (!option || option.disabled) return false\n        if (value === el.value) return true\n        if (\n          !change('value', value, {\n            reason: details.reason ?? 'imperative',\n            event: details.event,\n          })\n        )\n          return false\n        el.value = value\n        writing = true\n        el.dispatchEvent(new Event('input', { bubbles: true }))\n        el.dispatchEvent(new Event('change', { bubbles: true }))\n        writing = false\n        return true\n      },\n      /** The options an agent can pick. */\n      options: () =>\n        [...(select()?.options ?? [])]\n          .filter((option) => !option.disabled)\n          .map((option) => ({ value: option.value, label: option.label })),\n    }\n  },\n  slots: (slot) => ({\n    root: slot({\n      on: {\n        change: (event, { api, el }) => {\n          if (el && !api.writing())\n            api.changed(el as HTMLSelectElement, { reason: 'input-change', event })\n        },\n      },\n      bind: (el, { api }) => {\n        const select = el as HTMLSelectElement\n        api.read()\n        // A form's reset puts the select back once the event is done.\n        const onReset = (event: Event) => {\n          if (event.target !== select.form) return\n          queueMicrotask(() => api.changed(select, { reason: 'reset' }))\n        }\n        select.ownerDocument.addEventListener('reset', onReset, true)\n        return () => select.ownerDocument.removeEventListener('reset', onReset, true)\n      },\n    }),\n  }),\n  agent: { actions: ['setValue'], closed: true, items: (api) => api.options() },\n})\n\nexport { nativeSelectControl }\nexport type { NativeSelectOptions, NativeSelectReason }\n",
      "type": "registry:lib",
      "target": "@lib/native-select-control.ts"
    }
  ],
  "type": "registry:lib"
}