@funstack/ui 0.1.0-alpha.16 → 0.1.0-alpha.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. package/dist/accordion/accordion-root.mjs +7 -5
  2. package/dist/accordion/accordion-root.mjs.map +1 -1
  3. package/dist/button/button.d.mts +27 -12
  4. package/dist/button/button.d.mts.map +1 -1
  5. package/dist/button/button.mjs +16 -9
  6. package/dist/button/button.mjs.map +1 -1
  7. package/dist/checkbox-group/checkbox-group-root.mjs +12 -5
  8. package/dist/checkbox-group/checkbox-group-root.mjs.map +1 -1
  9. package/dist/combobox/combobox-popup.d.mts +4 -1
  10. package/dist/combobox/combobox-popup.d.mts.map +1 -1
  11. package/dist/combobox/combobox-popup.mjs.map +1 -1
  12. package/dist/combobox/combobox-root.d.mts +15 -5
  13. package/dist/combobox/combobox-root.d.mts.map +1 -1
  14. package/dist/combobox/combobox-root.mjs +9 -11
  15. package/dist/combobox/combobox-root.mjs.map +1 -1
  16. package/dist/combobox/combobox-trigger.mjs +7 -2
  17. package/dist/combobox/combobox-trigger.mjs.map +1 -1
  18. package/dist/dialog/dialog-close.d.mts +13 -3
  19. package/dist/dialog/dialog-close.d.mts.map +1 -1
  20. package/dist/dialog/dialog-close.mjs +3 -2
  21. package/dist/dialog/dialog-close.mjs.map +1 -1
  22. package/dist/dialog/dialog-context.mjs.map +1 -1
  23. package/dist/dialog/dialog-form.d.mts +2 -1
  24. package/dist/dialog/dialog-form.d.mts.map +1 -1
  25. package/dist/dialog/dialog-form.mjs.map +1 -1
  26. package/dist/dialog/dialog-popup.d.mts +9 -4
  27. package/dist/dialog/dialog-popup.d.mts.map +1 -1
  28. package/dist/dialog/dialog-popup.mjs +21 -21
  29. package/dist/dialog/dialog-popup.mjs.map +1 -1
  30. package/dist/dialog/dialog-root.d.mts +38 -24
  31. package/dist/dialog/dialog-root.d.mts.map +1 -1
  32. package/dist/dialog/dialog-root.mjs +10 -8
  33. package/dist/dialog/dialog-root.mjs.map +1 -1
  34. package/dist/dialog/dialog-trigger.d.mts +3 -3
  35. package/dist/dialog/dialog-trigger.mjs.map +1 -1
  36. package/dist/dialog/index.d.mts +5 -5
  37. package/dist/dialog/index.mjs +5 -5
  38. package/dist/dialog/index.mjs.map +1 -1
  39. package/dist/docs/components/accordion.md +1 -1
  40. package/dist/docs/components/button.md +71 -24
  41. package/dist/docs/components/checkbox-group.md +12 -3
  42. package/dist/docs/components/combobox.md +85 -22
  43. package/dist/docs/components/context-menu.md +8 -8
  44. package/dist/docs/components/dialog.md +42 -36
  45. package/dist/docs/components/menu.md +106 -25
  46. package/dist/docs/components/popover.md +101 -36
  47. package/dist/docs/components/progress.md +1 -1
  48. package/dist/docs/components/slider.md +1 -1
  49. package/dist/docs/components/toast.md +37 -14
  50. package/dist/docs/components/toolbar.md +2 -1
  51. package/dist/docs/components/tooltip.md +1 -1
  52. package/dist/docs/concepts/action-props.md +55 -25
  53. package/dist/docs/concepts/browser-owned-state.md +27 -12
  54. package/dist/docs/concepts/pending-and-optimistic.md +15 -2
  55. package/dist/docs/concepts/render.md +23 -0
  56. package/dist/docs/concepts/server-components.md +15 -1
  57. package/dist/docs/concepts/suspense.md +8 -3
  58. package/dist/docs/concepts/transitions.md +3 -9
  59. package/dist/docs/getting-started.md +9 -22
  60. package/dist/docs/index.md +5 -4
  61. package/dist/docs/roadmap.md +54 -0
  62. package/dist/docs/why.md +7 -3
  63. package/dist/index.d.mts +2 -2
  64. package/dist/internal/call-urgent-handler.mjs +37 -0
  65. package/dist/internal/call-urgent-handler.mjs.map +1 -0
  66. package/dist/internal/css-transition-cut.mjs +66 -0
  67. package/dist/internal/css-transition-cut.mjs.map +1 -0
  68. package/dist/internal/overlay-popup.mjs.map +1 -1
  69. package/dist/internal/report-dropped.mjs +45 -0
  70. package/dist/internal/report-dropped.mjs.map +1 -0
  71. package/dist/internal/types.d.mts +33 -7
  72. package/dist/internal/types.d.mts.map +1 -1
  73. package/dist/internal/use-action-trigger.mjs +32 -17
  74. package/dist/internal/use-action-trigger.mjs.map +1 -1
  75. package/dist/internal/use-controllable-action.mjs +56 -17
  76. package/dist/internal/use-controllable-action.mjs.map +1 -1
  77. package/dist/internal/use-exit-animation.mjs +1 -1
  78. package/dist/internal/use-exit-animation.mjs.map +1 -1
  79. package/dist/internal/use-native-group-reconciliation.mjs +8 -6
  80. package/dist/internal/use-native-group-reconciliation.mjs.map +1 -1
  81. package/dist/internal/use-overlay-close.mjs +10 -4
  82. package/dist/internal/use-overlay-close.mjs.map +1 -1
  83. package/dist/internal/use-overlay-root.mjs +40 -7
  84. package/dist/internal/use-overlay-root.mjs.map +1 -1
  85. package/dist/internal/use-popover-popup.mjs +25 -7
  86. package/dist/internal/use-popover-popup.mjs.map +1 -1
  87. package/dist/internal/use-popover-trigger.d.mts +1 -1
  88. package/dist/internal/use-popover-trigger.d.mts.map +1 -1
  89. package/dist/internal/use-popover-trigger.mjs +11 -3
  90. package/dist/internal/use-popover-trigger.mjs.map +1 -1
  91. package/dist/menu/menu-checkbox-item.d.mts +1 -1
  92. package/dist/menu/menu-checkbox-item.mjs.map +1 -1
  93. package/dist/menu/menu-item.d.mts +15 -4
  94. package/dist/menu/menu-item.d.mts.map +1 -1
  95. package/dist/menu/menu-item.mjs +10 -5
  96. package/dist/menu/menu-item.mjs.map +1 -1
  97. package/dist/menu/menu-outside.mjs +2 -2
  98. package/dist/menu/menu-outside.mjs.map +1 -1
  99. package/dist/menu/menu-popup.d.mts +3 -0
  100. package/dist/menu/menu-popup.d.mts.map +1 -1
  101. package/dist/menu/menu-popup.mjs.map +1 -1
  102. package/dist/menu/menu-root.d.mts +27 -12
  103. package/dist/menu/menu-root.d.mts.map +1 -1
  104. package/dist/menu/menu-root.mjs +5 -4
  105. package/dist/menu/menu-root.mjs.map +1 -1
  106. package/dist/number-field/number-field-root.d.mts.map +1 -1
  107. package/dist/number-field/number-field-root.mjs +2 -8
  108. package/dist/number-field/number-field-root.mjs.map +1 -1
  109. package/dist/popover/index.d.mts +3 -3
  110. package/dist/popover/index.mjs +3 -3
  111. package/dist/popover/index.mjs.map +1 -1
  112. package/dist/popover/popover-close.d.mts +14 -3
  113. package/dist/popover/popover-close.d.mts.map +1 -1
  114. package/dist/popover/popover-close.mjs +4 -2
  115. package/dist/popover/popover-close.mjs.map +1 -1
  116. package/dist/popover/popover-popup.d.mts +6 -3
  117. package/dist/popover/popover-popup.d.mts.map +1 -1
  118. package/dist/popover/popover-popup.mjs +3 -2
  119. package/dist/popover/popover-popup.mjs.map +1 -1
  120. package/dist/popover/popover-root.d.mts +31 -18
  121. package/dist/popover/popover-root.d.mts.map +1 -1
  122. package/dist/popover/popover-root.mjs +9 -7
  123. package/dist/popover/popover-root.mjs.map +1 -1
  124. package/dist/slider/slider-root.d.mts.map +1 -1
  125. package/dist/slider/slider-root.mjs +2 -8
  126. package/dist/slider/slider-root.mjs.map +1 -1
  127. package/dist/toast/toast-action.d.mts +14 -3
  128. package/dist/toast/toast-action.d.mts.map +1 -1
  129. package/dist/toast/toast-action.mjs +6 -4
  130. package/dist/toast/toast-action.mjs.map +1 -1
  131. package/dist/toast/toast-context.mjs.map +1 -1
  132. package/dist/toast/toast-queue.mjs.map +1 -1
  133. package/dist/toast/toast-viewport.d.mts.map +1 -1
  134. package/dist/toast/toast-viewport.mjs +15 -15
  135. package/dist/toast/toast-viewport.mjs.map +1 -1
  136. package/dist/toast/toast.d.mts +10 -7
  137. package/dist/toast/toast.d.mts.map +1 -1
  138. package/dist/toast/toast.mjs.map +1 -1
  139. package/dist/toggle-group/toggle-group-root.mjs +1 -1
  140. package/dist/toggle-group/toggle-group-root.mjs.map +1 -1
  141. package/package.json +1 -1
  142. package/skills/funstack-ui-knowledge/SKILL.md +6 -6
  143. package/dist/internal/use-superseded-rejection.mjs +0 -47
  144. package/dist/internal/use-superseded-rejection.mjs.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"toggle-group-root.mjs","names":[],"sources":["../../src/toggle-group/toggle-group-root.tsx"],"sourcesContent":["\"use client\";\nimport { useCallback, useMemo, useRef, useState } from \"react\";\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport { createActionDetails } from \"../internal/action-details.js\";\nimport { dataAttr } from \"../internal/data-attr.js\";\nimport { compareDocumentOrder } from \"../internal/dom-order.js\";\nimport { useToolbarContext } from \"../internal/toolbar-context.js\";\nimport { transitionTypes } from \"../internal/transition-types.js\";\nimport type { ActionDetails, ChangeAction, RenderProp, StateValue } from \"../internal/types.js\";\nimport { useControllableAction } from \"../internal/use-controllable-action.js\";\nimport { useDevWarning } from \"../internal/use-dev-warning.js\";\nimport { useItemOrder } from \"../internal/use-item-order.js\";\nimport type { ItemLayout } from \"../internal/use-item-order.js\";\nimport { useLatest } from \"../internal/use-latest.js\";\nimport { useNativeFocusgroup } from \"../internal/use-native-focusgroup.js\";\nimport { useRender } from \"../internal/use-render.js\";\nimport { useRovingFocus } from \"../internal/use-roving-focus.js\";\nimport { useValueSet } from \"../internal/use-value-set.js\";\nimport { applyPatch, EMPTY_SET, arraysEqual, sortByOrder } from \"../internal/value-set.js\";\nimport type { SetPatch, ValueSet } from \"../internal/value-set.js\";\nimport { ToggleGroupContext } from \"./toggle-group-context.js\";\nimport type { ToggleGroupContextValue, ToggleGroupOrientation } from \"./toggle-group-context.js\";\n\nexport type { ToggleGroupOrientation } from \"./toggle-group-context.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/** Details passed as the second argument of `valueChangeAction`, in both modes. */\nexport interface ToggleGroupValueChangeDetails extends ActionDetails {\n /** Values this dispatch pressed (document order of their Items). Exclusive mode: `[next]` or `[]`. */\n readonly added: readonly string[];\n /** Values this dispatch released. Exclusive mode: `[previous]` or `[]`. */\n readonly removed: readonly string[];\n}\n\n/**\n * State of `ToggleGroup.Root`, passed to the function forms of `className`, `style`, `children` and\n * `render`.\n */\nexport interface ToggleGroupRootState {\n /**\n * Optimistic pressed values in document order of the Items (exclusive mode: `[value]` or `[]`,\n * so one state shape serves both modes). The server render and the first client render use the\n * value's own order. Content rendered from it must not suspend.\n */\n readonly value: readonly string[];\n /** An action dispatched through this group is in flight. */\n readonly pending: boolean;\n /** The `disabled` prop. */\n readonly disabled: boolean;\n /** The effective orientation: the Toolbar's when inside one. */\n readonly orientation: ToggleGroupOrientation;\n}\n\ninterface ToggleGroupRootBaseProps extends Omit<\n ComponentPropsWithRef<\"div\">,\n | \"role\"\n | \"onKeyDown\"\n | \"onFocus\"\n | \"onBlur\"\n | \"onPointerDown\"\n | \"defaultValue\"\n | \"className\"\n | \"style\"\n | \"children\"\n> {\n /**\n * Axis of the arrow keys and `data-orientation`. Ignored inside a Toolbar (dev warning).\n *\n * @defaultValue `\"horizontal\"`\n */\n orientation?: ToggleGroupOrientation;\n /**\n * Whether arrows wrap from the last Item to the first. Ignored inside a Toolbar (dev warning).\n *\n * @defaultValue `true`\n */\n loop?: boolean;\n /**\n * Disables every Item (native `disabled`).\n *\n * @defaultValue `false`\n */\n disabled?: boolean;\n /**\n * Render `focusgroup=\"toolbar …\"` and, where the browser implements it, let the browser move\n * focus between the Items. Chromium 150+ only; elsewhere the JS path stays active. Ignored\n * inside a Toolbar (a nested focusgroup would split the Toolbar's sequence; dev warning).\n *\n * @defaultValue `false`\n */\n experimental_focusgroup?: boolean;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, ToggleGroupRootState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, ToggleGroupRootState>;\n /** Content, or a function of the part's state. */\n children?: StateValue<ReactNode, ToggleGroupRootState>;\n /** Replace the default `<div>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"div\">, ToggleGroupRootState>;\n}\n\n/**\n * Props of `ToggleGroup.Root` in multiple mode (the default): any number of Items pressed, `value`\n * is a `string[]`.\n *\n * @see https://ui.funstack.work/components/toggle-group\n */\nexport interface ToggleGroupMultipleProps extends ToggleGroupRootBaseProps {\n /**\n * `true` switches to exclusive mode (at most one Item pressed; see `ToggleGroupExclusiveProps`).\n *\n * @defaultValue `false`\n */\n exclusive?: false;\n /**\n * Controlled pressed values. `[]` presses nothing; `undefined` means uncontrolled. Order is not\n * significant; duplicates ignored.\n */\n value?: string[];\n /**\n * Initial pressed values in uncontrolled mode.\n *\n * @defaultValue `[]`\n */\n defaultValue?: string[];\n /**\n * Runs inside the Root's transition whenever the set of pressed Items changes. Sync or async (a\n * Server Function may be passed directly); rejections reach the nearest Error Boundary and roll\n * the change back. `next` is the whole set as a fresh array in document order;\n * `details.added` / `details.removed` say what changed. A `setState` after your own `await` is\n * not a Transition update; wrap it in `startTransition`. Prefer a functional update keyed by\n * `added` / `removed` when controlled.\n */\n valueChangeAction?: ChangeAction<string[], ToggleGroupValueChangeDetails>;\n /**\n * Exclusive mode only: a type error here (TypeScript's union excess-property check would let it\n * through otherwise).\n */\n deselectable?: never;\n}\n\n/**\n * Props of `ToggleGroup.Root` in exclusive mode (`exclusive`): at most one Item pressed, `value` is\n * `string | null`.\n *\n * @see https://ui.funstack.work/components/toggle-group\n */\nexport interface ToggleGroupExclusiveProps extends ToggleGroupRootBaseProps {\n /** At most one Item is pressed; `value` is `string | null`. */\n exclusive: true;\n /** Controlled pressed value. `null` presses nothing; `undefined` means uncontrolled. */\n value?: string | null;\n /**\n * Initial pressed value in uncontrolled mode.\n *\n * @defaultValue `null`\n */\n defaultValue?: string | null;\n /**\n * Whether pressing the pressed Item releases it (`next === null`). With `false` that press is a\n * no-op and dispatches nothing; `value` stays `string | null` because the group may start with\n * nothing pressed, but the action is then never called with `null`.\n *\n * @defaultValue `true`\n */\n deselectable?: boolean;\n /** See {@link ToggleGroupMultipleProps.valueChangeAction}; `next` is the pressed value or `null`. */\n valueChangeAction?: ChangeAction<string | null, ToggleGroupValueChangeDetails>;\n}\n\n/**\n * Props of `ToggleGroup.Root`, discriminated on `exclusive`.\n *\n * @see https://ui.funstack.work/components/toggle-group\n */\nexport type ToggleGroupRootProps = ToggleGroupMultipleProps | ToggleGroupExclusiveProps;\n\ntype GroupValue = string[] | string | null;\n\n/** Both variants as one shape, for the implementation. */\ninterface InternalProps extends ToggleGroupRootBaseProps {\n exclusive?: boolean;\n value?: GroupValue;\n defaultValue?: GroupValue;\n deselectable?: boolean;\n valueChangeAction?: ChangeAction<GroupValue, ToggleGroupValueChangeDetails>;\n}\n\n/** Standalone entry (design 6.2): the first pressed item, else the first item (radio-like). */\nfunction pressedFirst(items: readonly HTMLElement[]): HTMLElement | undefined {\n return items.find((el) => el.getAttribute(\"aria-pressed\") === \"true\") ?? items[0];\n}\n\n/** The Items' document order and their disabled values, as of the Root's last commit. */\ninterface ToggleGroupLayout extends ItemLayout {\n readonly disabled: readonly string[];\n}\n\nconst NO_LAYOUT: ToggleGroupLayout = { order: [], disabled: [] };\n\n/**\n * `<div role=\"group\">` that owns the pressed set, the transition and, when standalone, the roving\n * focus over its Items (design/components/toggle-group.md 16.2). Inside a `Toolbar.Root` it runs\n * no engine of its own: its Items are members of the Toolbar's sequence.\n *\n * Owned keys (conventions 1.2): `role`, `data-orientation`, `data-pending`, `data-disabled`;\n * standalone also `onKeyDown`, `onFocus`, `onBlur`, `onPointerDown` and, with the flag,\n * `focusgroup`.\n *\n * @see https://ui.funstack.work/components/toggle-group\n */\nexport function ToggleGroupRoot(props: ToggleGroupRootProps) {\n const {\n exclusive = false,\n value,\n defaultValue,\n valueChangeAction,\n deselectable = true,\n orientation: orientationProp,\n loop: loopProp,\n disabled = false,\n experimental_focusgroup: focusgroupProp,\n className,\n style,\n children,\n render,\n ...rest\n } = props as InternalProps;\n\n const toolbar = useToolbarContext();\n const inToolbar = toolbar !== null;\n useDevWarning(\n inToolbar && orientationProp !== undefined,\n isDev &&\n \"ToggleGroup.Root: `orientation` is ignored inside a Toolbar.Root; the group follows the \" +\n \"Toolbar's orientation.\",\n );\n useDevWarning(\n inToolbar && loopProp !== undefined,\n isDev &&\n \"ToggleGroup.Root: `loop` is ignored inside a Toolbar.Root; the Toolbar's `loop` applies.\",\n );\n useDevWarning(\n inToolbar && focusgroupProp === true,\n isDev &&\n \"ToggleGroup.Root: `experimental_focusgroup` is ignored inside a Toolbar.Root. A nested \" +\n \"focusgroup would split the Toolbar's single Tab stop; set the flag on the Toolbar.Root.\",\n );\n const orientation: ToggleGroupOrientation =\n toolbar?.orientation ?? orientationProp ?? \"horizontal\";\n const loop = loopProp ?? true;\n const flag = !inToolbar && focusgroupProp === true;\n\n // The mode is fixed at mount (design 3.1).\n const [mountExclusive] = useState(exclusive);\n useDevWarning(\n mountExclusive !== exclusive,\n isDev &&\n \"ToggleGroup.Root: `exclusive` changed after mount. Switching between exclusive and \" +\n \"multiple mode is not supported; the group keeps the mode it mounted with.\",\n );\n\n const controlledSet = useValueSet(value);\n const defaultSet = useValueSet(defaultValue);\n\n // Item registration (design 16.2): element → value, written from the Items' callback refs.\n const registry = useRef(new Map<HTMLElement, string>());\n const registerItem = useCallback((element: HTMLElement, itemValue: string) => {\n registry.current.set(element, itemValue);\n return () => {\n registry.current.delete(element);\n };\n }, []);\n const sortedItems = (): [HTMLElement, string][] =>\n [...registry.current].sort(([a], [b]) => compareDocumentOrder(a, b));\n /** Sorts by the Items' current document order. Reads the DOM: handlers and effects only. */\n const sortByDocument = (values: Iterable<string>): string[] =>\n sortByOrder(\n values,\n sortedItems().map(([, v]) => v),\n );\n\n const { optimistic, committed, pending, dispatch } = useControllableAction<\n ValueSet,\n ToggleGroupValueChangeDetails\n >({\n controlled: controlledSet,\n defaultValue: defaultSet,\n initialValue: EMPTY_SET,\n action:\n valueChangeAction &&\n ((next, details) =>\n valueChangeAction(mountExclusive ? ([...next][0] ?? null) : sortByDocument(next), details)),\n transitionTypes: (_next, _prev, details) => [\n ...transitionTypes(\"toggle-group\"),\n ...(details.added.length > 0 ? [\"fsui-toggle-group-press\"] : []),\n ...(details.removed.length > 0 ? [\"fsui-toggle-group-release\"] : []),\n ],\n reducer: (prev, _next, details) => applyPatch(prev, details),\n warn: { component: \"ToggleGroup.Root\", prop: \"value\" },\n });\n\n // Read by the stable `toggle`: a handler only runs against the committed UI.\n const latest = useLatest({ optimistic, exclusive: mountExclusive, deselectable });\n const toggle = useCallback(\n (itemValue: string, event: Event) => {\n const { optimistic, exclusive, deselectable } = latest.current;\n let patch: SetPatch;\n if (optimistic.has(itemValue)) {\n // Exclusive and not deselectable: pressing the pressed Item is a no-op (design 5.2).\n if (exclusive && !deselectable) return;\n patch = { added: [], removed: [itemValue] };\n } else {\n patch = { added: [itemValue], removed: exclusive ? [...optimistic] : [] };\n }\n dispatch(applyPatch(optimistic, patch), createActionDetails(event, patch));\n },\n [dispatch, latest],\n );\n\n // `state.value` is in document order; `focusgroupstart` also renders from which values have a\n // disabled Item.\n const { layout, renderedValue } = useItemOrder<ToggleGroupLayout>({\n optimistic,\n initial: NO_LAYOUT,\n read: () => {\n const items = sortedItems();\n return {\n order: items.map(([, v]) => v),\n disabled: items.filter(([el]) => el.matches(\":disabled\")).map(([, v]) => v),\n };\n },\n changed: (prev, next) =>\n flag && (!arraysEqual(next.order, prev.order) || !arraysEqual(next.disabled, prev.disabled)),\n duplicateMessage:\n isDev &&\n ((duplicate) =>\n `ToggleGroup.Item: two Items share the value \"${duplicate}\" in one ToggleGroup.Root. ` +\n \"Item values must be unique; both follow the same pressed state.\"),\n });\n\n const native = useNativeFocusgroup(flag);\n\n const focusgroupStartValue =\n flag && !disabled\n ? (renderedValue.find(\n (v) =>\n (layout.order.length === 0 || layout.order.includes(v)) && !layout.disabled.includes(v),\n ) ?? null)\n : null;\n\n const rootRef = useRef<HTMLElement | null>(null);\n const roving = useRovingFocus(rootRef, {\n enabled: !inToolbar,\n orientation,\n loop,\n native,\n entry: pressedFirst,\n memory: false,\n });\n\n const context = useMemo<ToggleGroupContextValue>(\n () => ({\n optimistic,\n committed,\n pending,\n disabled,\n orientation,\n focusgroupStartValue,\n toggle,\n registerItem,\n }),\n [\n optimistic,\n committed,\n pending,\n disabled,\n orientation,\n focusgroupStartValue,\n toggle,\n registerItem,\n ],\n );\n\n const state: ToggleGroupRootState = { value: renderedValue, pending, disabled, orientation };\n\n const focusgroup = flag\n ? `toolbar ${orientation === \"vertical\" ? \"block\" : \"inline\"} ${loop ? \"wrap\" : \"nowrap\"} nomemory`\n : undefined;\n\n const element = useRender<ComponentPropsWithRef<\"div\">, ToggleGroupRootState>({\n defaultElement: \"div\",\n render,\n state,\n className,\n style,\n children,\n props: rest,\n libraryProps: {\n role: \"group\",\n \"data-orientation\": orientation,\n \"data-pending\": dataAttr(pending),\n \"data-disabled\": dataAttr(disabled),\n // Inside a Toolbar the group has no engine: the keys are not owned there.\n ...(!inToolbar && roving),\n ...(focusgroup !== undefined && { focusgroup }),\n },\n ref: rootRef,\n });\n\n return <ToggleGroupContext value={context}>{element}</ToggleGroupContext>;\n}\n\nToggleGroupRoot.displayName = \"ToggleGroupRoot\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAyBA,MAAM,QAAQ,QAAQ,IAAI,aAAa;;AAqKvC,SAAS,aAAa,OAAwD;CAC5E,OAAO,MAAM,MAAM,OAAO,GAAG,aAAa,cAAc,MAAM,MAAM,KAAK,MAAM;AACjF;AAOA,MAAM,YAA+B;CAAE,OAAO,CAAC;CAAG,UAAU,CAAC;AAAE;;;;;;;;;;;;AAa/D,SAAgB,gBAAgB,OAA6B;CAC3D,MAAM,EACJ,YAAY,OACZ,OACA,cACA,mBACA,eAAe,MACf,aAAa,iBACb,MAAM,UACN,WAAW,OACX,yBAAyB,gBACzB,WACA,OACA,UACA,QACA,GAAG,SACD;CAEJ,MAAM,UAAU,kBAAkB;CAClC,MAAM,YAAY,YAAY;CAC9B,cACE,aAAa,oBAAoB,KAAA,GACjC,SACE,gHAEJ;CACA,cACE,aAAa,aAAa,KAAA,GAC1B,SACE,0FACJ;CACA,cACE,aAAa,mBAAmB,MAChC,SACE,gLAEJ;CACA,MAAM,cACJ,SAAS,eAAe,mBAAmB;CAC7C,MAAM,OAAO,YA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AE3F,MAAM,aAAa,OACf,WAAW,gBAAgB,aAAa,UAAU,SAAS,GAAG,OAAO,SAAS,SAAS,aACvF,KAAA;CAEJ,MAAM,UAAU,UAA8D;EAC5E,gBAAgB;EAChB;EACA;EACA;EACA;EACA;EACA,OAAO;EACP,cAAc;GACZ,MAAM;GACN,oBAAoB;GACpB,gBAAgB,SAAS,OAAO;GAChC,iBAAiB,SAAS,QAAQ;GAElC,GAAI,CAAC,aAAa;GAClB,GAAI,eAAe,KAAA,KAAa,EAAE,WAAW;EAC/C;EACA,KAAK;CACP,CAAC;CAED,OAAO,oBAAC,oBAAD;EAAoB,OAAO;EAAU,UAAA;CAA4B,CAAA;AAC1E;AAEA,gBAAgB,cAAc"}
1
+ {"version":3,"file":"toggle-group-root.mjs","names":[],"sources":["../../src/toggle-group/toggle-group-root.tsx"],"sourcesContent":["\"use client\";\nimport { useCallback, useMemo, useRef, useState } from \"react\";\nimport type { ComponentPropsWithRef, CSSProperties, ReactNode } from \"react\";\nimport { createActionDetails } from \"../internal/action-details.js\";\nimport { dataAttr } from \"../internal/data-attr.js\";\nimport { compareDocumentOrder } from \"../internal/dom-order.js\";\nimport { useToolbarContext } from \"../internal/toolbar-context.js\";\nimport { transitionTypes } from \"../internal/transition-types.js\";\nimport type { ActionDetails, ChangeAction, RenderProp, StateValue } from \"../internal/types.js\";\nimport { useControllableAction } from \"../internal/use-controllable-action.js\";\nimport { useDevWarning } from \"../internal/use-dev-warning.js\";\nimport { useItemOrder } from \"../internal/use-item-order.js\";\nimport type { ItemLayout } from \"../internal/use-item-order.js\";\nimport { useLatest } from \"../internal/use-latest.js\";\nimport { useNativeFocusgroup } from \"../internal/use-native-focusgroup.js\";\nimport { useRender } from \"../internal/use-render.js\";\nimport { useRovingFocus } from \"../internal/use-roving-focus.js\";\nimport { useValueSet } from \"../internal/use-value-set.js\";\nimport { applyPatch, EMPTY_SET, arraysEqual, sortByOrder } from \"../internal/value-set.js\";\nimport type { SetPatch, ValueSet } from \"../internal/value-set.js\";\nimport { ToggleGroupContext } from \"./toggle-group-context.js\";\nimport type { ToggleGroupContextValue, ToggleGroupOrientation } from \"./toggle-group-context.js\";\n\nexport type { ToggleGroupOrientation } from \"./toggle-group-context.js\";\n\nconst isDev = process.env.NODE_ENV !== \"production\";\n\n/** Details passed as the second argument of `valueChangeAction`, in both modes. */\nexport interface ToggleGroupValueChangeDetails extends ActionDetails {\n /** Values this dispatch pressed (document order of their Items). Exclusive mode: `[next]` or `[]`. */\n readonly added: readonly string[];\n /** Values this dispatch released. Exclusive mode: `[previous]` or `[]`. */\n readonly removed: readonly string[];\n}\n\n/**\n * State of `ToggleGroup.Root`, passed to the function forms of `className`, `style`, `children` and\n * `render`.\n */\nexport interface ToggleGroupRootState {\n /**\n * Optimistic pressed values in document order of the Items (exclusive mode: `[value]` or `[]`,\n * so one state shape serves both modes). The server render and the first client render use the\n * value's own order. Content rendered from it must not suspend.\n */\n readonly value: readonly string[];\n /** An action dispatched through this group is in flight. */\n readonly pending: boolean;\n /** The `disabled` prop. */\n readonly disabled: boolean;\n /** The effective orientation: the Toolbar's when inside one. */\n readonly orientation: ToggleGroupOrientation;\n}\n\ninterface ToggleGroupRootBaseProps extends Omit<\n ComponentPropsWithRef<\"div\">,\n | \"role\"\n | \"onKeyDown\"\n | \"onFocus\"\n | \"onBlur\"\n | \"onPointerDown\"\n | \"defaultValue\"\n | \"className\"\n | \"style\"\n | \"children\"\n> {\n /**\n * Axis of the arrow keys and `data-orientation`. Ignored inside a Toolbar (dev warning).\n *\n * @defaultValue `\"horizontal\"`\n */\n orientation?: ToggleGroupOrientation;\n /**\n * Whether arrows wrap from the last Item to the first. Ignored inside a Toolbar (dev warning).\n *\n * @defaultValue `true`\n */\n loop?: boolean;\n /**\n * Disables every Item (native `disabled`).\n *\n * @defaultValue `false`\n */\n disabled?: boolean;\n /**\n * Render `focusgroup=\"toolbar …\"` and, where the browser implements it, let the browser move\n * focus between the Items. Chromium 150+ only; elsewhere the JS path stays active. Ignored\n * inside a Toolbar (a nested focusgroup would split the Toolbar's sequence; dev warning).\n *\n * @defaultValue `false`\n */\n experimental_focusgroup?: boolean;\n /** Class name, or a function of the part's state that returns one. */\n className?: StateValue<string | undefined, ToggleGroupRootState>;\n /** Inline style, or a function of the part's state that returns one. */\n style?: StateValue<CSSProperties | undefined, ToggleGroupRootState>;\n /** Content, or a function of the part's state. */\n children?: StateValue<ReactNode, ToggleGroupRootState>;\n /** Replace the default `<div>` (element form) or take over rendering (function form). */\n render?: RenderProp<ComponentPropsWithRef<\"div\">, ToggleGroupRootState>;\n}\n\n/**\n * Props of `ToggleGroup.Root` in multiple mode (the default): any number of Items pressed, `value`\n * is a `string[]`.\n *\n * @see https://ui.funstack.work/components/toggle-group\n */\nexport interface ToggleGroupMultipleProps extends ToggleGroupRootBaseProps {\n /**\n * `true` switches to exclusive mode (at most one Item pressed; see `ToggleGroupExclusiveProps`).\n *\n * @defaultValue `false`\n */\n exclusive?: false;\n /**\n * Controlled pressed values. `[]` presses nothing; `undefined` means uncontrolled. Order is not\n * significant; duplicates ignored.\n */\n value?: string[];\n /**\n * Initial pressed values in uncontrolled mode.\n *\n * @defaultValue `[]`\n */\n defaultValue?: string[];\n /**\n * Runs inside the Root's transition whenever the set of pressed Items changes. Sync or async (a\n * Server Function may be passed directly); rejections reach the nearest Error Boundary and roll\n * the change back. `next` is the whole set as a fresh array in document order;\n * `details.added` / `details.removed` say what changed. A `setState` after your own `await` is\n * not a Transition update; wrap it in `startTransition`. Prefer a functional update keyed by\n * `added` / `removed` when controlled.\n */\n valueChangeAction?: ChangeAction<string[], ToggleGroupValueChangeDetails>;\n /**\n * Exclusive mode only: a type error here (TypeScript's union excess-property check would let it\n * through otherwise).\n */\n deselectable?: never;\n}\n\n/**\n * Props of `ToggleGroup.Root` in exclusive mode (`exclusive`): at most one Item pressed, `value` is\n * `string | null`.\n *\n * @see https://ui.funstack.work/components/toggle-group\n */\nexport interface ToggleGroupExclusiveProps extends ToggleGroupRootBaseProps {\n /** At most one Item is pressed; `value` is `string | null`. */\n exclusive: true;\n /** Controlled pressed value. `null` presses nothing; `undefined` means uncontrolled. */\n value?: string | null;\n /**\n * Initial pressed value in uncontrolled mode.\n *\n * @defaultValue `null`\n */\n defaultValue?: string | null;\n /**\n * Whether pressing the pressed Item releases it (`next === null`). With `false` that press is a\n * no-op and dispatches nothing; `value` stays `string | null` because the group may start with\n * nothing pressed, but the action is then never called with `null`.\n *\n * @defaultValue `true`\n */\n deselectable?: boolean;\n /** See {@link ToggleGroupMultipleProps.valueChangeAction}; `next` is the pressed value or `null`. */\n valueChangeAction?: ChangeAction<string | null, ToggleGroupValueChangeDetails>;\n}\n\n/**\n * Props of `ToggleGroup.Root`, discriminated on `exclusive`.\n *\n * @see https://ui.funstack.work/components/toggle-group\n */\nexport type ToggleGroupRootProps = ToggleGroupMultipleProps | ToggleGroupExclusiveProps;\n\ntype GroupValue = string[] | string | null;\n\n/** Both variants as one shape, for the implementation. */\ninterface InternalProps extends ToggleGroupRootBaseProps {\n exclusive?: boolean;\n value?: GroupValue;\n defaultValue?: GroupValue;\n deselectable?: boolean;\n valueChangeAction?: ChangeAction<GroupValue, ToggleGroupValueChangeDetails>;\n}\n\n/** Standalone entry (design 6.2): the first pressed item, else the first item (radio-like). */\nfunction pressedFirst(items: readonly HTMLElement[]): HTMLElement | undefined {\n return items.find((el) => el.getAttribute(\"aria-pressed\") === \"true\") ?? items[0];\n}\n\n/** The Items' document order and their disabled values, as of the Root's last commit. */\ninterface ToggleGroupLayout extends ItemLayout {\n readonly disabled: readonly string[];\n}\n\nconst NO_LAYOUT: ToggleGroupLayout = { order: [], disabled: [] };\n\n/**\n * `<div role=\"group\">` that owns the pressed set, the transition and, when standalone, the roving\n * focus over its Items (design/components/toggle-group.md 16.2). Inside a `Toolbar.Root` it runs\n * no engine of its own: its Items are members of the Toolbar's sequence.\n *\n * Owned keys (conventions 1.2): `role`, `data-orientation`, `data-pending`, `data-disabled`;\n * standalone also `onKeyDown`, `onFocus`, `onBlur`, `onPointerDown` and, with the flag,\n * `focusgroup`.\n *\n * @see https://ui.funstack.work/components/toggle-group\n */\nexport function ToggleGroupRoot(props: ToggleGroupRootProps) {\n const {\n exclusive = false,\n value,\n defaultValue,\n valueChangeAction,\n deselectable = true,\n orientation: orientationProp,\n loop: loopProp,\n disabled = false,\n experimental_focusgroup: focusgroupProp,\n className,\n style,\n children,\n render,\n ...rest\n } = props as InternalProps;\n\n const toolbar = useToolbarContext();\n const inToolbar = toolbar !== null;\n useDevWarning(\n inToolbar && orientationProp !== undefined,\n isDev &&\n \"ToggleGroup.Root: `orientation` is ignored inside a Toolbar.Root; the group follows the \" +\n \"Toolbar's orientation.\",\n );\n useDevWarning(\n inToolbar && loopProp !== undefined,\n isDev &&\n \"ToggleGroup.Root: `loop` is ignored inside a Toolbar.Root; the Toolbar's `loop` applies.\",\n );\n useDevWarning(\n inToolbar && focusgroupProp === true,\n isDev &&\n \"ToggleGroup.Root: `experimental_focusgroup` is ignored inside a Toolbar.Root. A nested \" +\n \"focusgroup would split the Toolbar's single Tab stop; set the flag on the Toolbar.Root.\",\n );\n const orientation: ToggleGroupOrientation =\n toolbar?.orientation ?? orientationProp ?? \"horizontal\";\n const loop = loopProp ?? true;\n const flag = !inToolbar && focusgroupProp === true;\n\n // The mode is fixed at mount (design 3.1).\n const [mountExclusive] = useState(exclusive);\n useDevWarning(\n mountExclusive !== exclusive,\n isDev &&\n \"ToggleGroup.Root: `exclusive` changed after mount. Switching between exclusive and \" +\n \"multiple mode is not supported; the group keeps the mode it mounted with.\",\n );\n\n const controlledSet = useValueSet(value);\n const defaultSet = useValueSet(defaultValue);\n\n // Item registration (design 16.2): element → value, written from the Items' callback refs.\n const registry = useRef(new Map<HTMLElement, string>());\n const registerItem = useCallback((element: HTMLElement, itemValue: string) => {\n registry.current.set(element, itemValue);\n return () => {\n registry.current.delete(element);\n };\n }, []);\n const sortedItems = (): [HTMLElement, string][] =>\n [...registry.current].sort(([a], [b]) => compareDocumentOrder(a, b));\n /** Sorts by the Items' current document order. Reads the DOM: handlers and effects only. */\n const sortByDocument = (values: Iterable<string>): string[] =>\n sortByOrder(\n values,\n sortedItems().map(([, v]) => v),\n );\n\n const { optimistic, committed, pending, dispatch } = useControllableAction<\n ValueSet,\n ToggleGroupValueChangeDetails\n >({\n controlled: controlledSet,\n defaultValue: defaultSet,\n initialValue: EMPTY_SET,\n action:\n valueChangeAction &&\n ((next, details) =>\n valueChangeAction(mountExclusive ? ([...next][0] ?? null) : sortByDocument(next), details)),\n transitionTypes: (_next, _prev, details) => [\n ...transitionTypes(\"toggle-group\"),\n ...(details.added.length > 0 ? [\"fsui-toggle-group-press\"] : []),\n ...(details.removed.length > 0 ? [\"fsui-toggle-group-release\"] : []),\n ],\n // Multiple: a patch, re-based on the committed set; it is also how the uncontrolled commit is\n // folded over the resolved dispatches (#150). Exclusive is single-valued: no reducer, so the\n // optimistic value is `next` and the latest dispatch commits it, like Tabs.\n reducer: mountExclusive ? undefined : (prev, _next, details) => applyPatch(prev, details),\n warn: { component: \"ToggleGroup.Root\", prop: \"value\" },\n });\n\n // Read by the stable `toggle`: a handler only runs against the committed UI.\n const latest = useLatest({ optimistic, exclusive: mountExclusive, deselectable });\n const toggle = useCallback(\n (itemValue: string, event: Event) => {\n const { optimistic, exclusive, deselectable } = latest.current;\n let patch: SetPatch;\n if (optimistic.has(itemValue)) {\n // Exclusive and not deselectable: pressing the pressed Item is a no-op (design 5.2).\n if (exclusive && !deselectable) return;\n patch = { added: [], removed: [itemValue] };\n } else {\n patch = { added: [itemValue], removed: exclusive ? [...optimistic] : [] };\n }\n dispatch(applyPatch(optimistic, patch), createActionDetails(event, patch));\n },\n [dispatch, latest],\n );\n\n // `state.value` is in document order; `focusgroupstart` also renders from which values have a\n // disabled Item.\n const { layout, renderedValue } = useItemOrder<ToggleGroupLayout>({\n optimistic,\n initial: NO_LAYOUT,\n read: () => {\n const items = sortedItems();\n return {\n order: items.map(([, v]) => v),\n disabled: items.filter(([el]) => el.matches(\":disabled\")).map(([, v]) => v),\n };\n },\n changed: (prev, next) =>\n flag && (!arraysEqual(next.order, prev.order) || !arraysEqual(next.disabled, prev.disabled)),\n duplicateMessage:\n isDev &&\n ((duplicate) =>\n `ToggleGroup.Item: two Items share the value \"${duplicate}\" in one ToggleGroup.Root. ` +\n \"Item values must be unique; both follow the same pressed state.\"),\n });\n\n const native = useNativeFocusgroup(flag);\n\n const focusgroupStartValue =\n flag && !disabled\n ? (renderedValue.find(\n (v) =>\n (layout.order.length === 0 || layout.order.includes(v)) && !layout.disabled.includes(v),\n ) ?? null)\n : null;\n\n const rootRef = useRef<HTMLElement | null>(null);\n const roving = useRovingFocus(rootRef, {\n enabled: !inToolbar,\n orientation,\n loop,\n native,\n entry: pressedFirst,\n memory: false,\n });\n\n const context = useMemo<ToggleGroupContextValue>(\n () => ({\n optimistic,\n committed,\n pending,\n disabled,\n orientation,\n focusgroupStartValue,\n toggle,\n registerItem,\n }),\n [\n optimistic,\n committed,\n pending,\n disabled,\n orientation,\n focusgroupStartValue,\n toggle,\n registerItem,\n ],\n );\n\n const state: ToggleGroupRootState = { value: renderedValue, pending, disabled, orientation };\n\n const focusgroup = flag\n ? `toolbar ${orientation === \"vertical\" ? \"block\" : \"inline\"} ${loop ? \"wrap\" : \"nowrap\"} nomemory`\n : undefined;\n\n const element = useRender<ComponentPropsWithRef<\"div\">, ToggleGroupRootState>({\n defaultElement: \"div\",\n render,\n state,\n className,\n style,\n children,\n props: rest,\n libraryProps: {\n role: \"group\",\n \"data-orientation\": orientation,\n \"data-pending\": dataAttr(pending),\n \"data-disabled\": dataAttr(disabled),\n // Inside a Toolbar the group has no engine: the keys are not owned there.\n ...(!inToolbar && roving),\n ...(focusgroup !== undefined && { focusgroup }),\n },\n ref: rootRef,\n });\n\n return <ToggleGroupContext value={context}>{element}</ToggleGroupContext>;\n}\n\nToggleGroupRoot.displayName = \"ToggleGroupRoot\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAyBA,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EACF;EACA,UAAU,MAAM,SACd,SAAS,CAAC,YAAY,KAAK,OAAO,KAAK,KAAK,KAAK,CAAC,YAAY,KAAK,UAAU,KAAK,QAAQ;EAC5F,kBACE,WACE,cACA,gDAAgD,UAAU;CAEhE,CAAC;CAED,MAAM,SAAS,oBAAoB,IAAI;CAEvC,MAAM,uBACJ,QAAQ,CAAC,WACJ,cAAc,MACZ,OACE,OAAO,MAAM,WAAW,KAAK,OAAO,MAAM,SAAS,CAAC,MAAM,CAAC,OAAO,SAAS,SAAS,CAAC,CAC1F,KAAK,OACL;CAEN,MAAM,UAAU,OAA2B,IAAI;CAC/C,MAAM,SAAS,eAAe,SAAS;EACrC,SAAS,CAAC;EACV;EACA;EACA;EACA,OAAO;EACP,QAAQ;CACV,CAAC;CAED,MAAM,UAAU,eACP;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,IACA;EACE;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF,CACF;CAEA,MAAM,QAA8B;EAAE,OAAO;EAAe;EAAS;EAAU;CAAY;CAE3F,MAAM,aAAa,OACf,WAAW,gBAAgB,aAAa,UAAU,SAAS,GAAG,OAAO,SAAS,SAAS,aACvF,KAAA;CAEJ,MAAM,UAAU,UAA8D;EAC5E,gBAAgB;EAChB;EACA;EACA;EACA;EACA;EACA,OAAO;EACP,cAAc;GACZ,MAAM;GACN,oBAAoB;GACpB,gBAAgB,SAAS,OAAO;GAChC,iBAAiB,SAAS,QAAQ;GAElC,GAAI,CAAC,aAAa;GAClB,GAAI,eAAe,KAAA,KAAa,EAAE,WAAW;EAC/C;EACA,KAAK;CACP,CAAC;CAED,OAAO,oBAAC,oBAAD;EAAoB,OAAO;EAAU,UAAA;CAA4B,CAAA;AAC1E;AAEA,gBAAgB,cAAc"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funstack/ui",
3
- "version": "0.1.0-alpha.16",
3
+ "version": "0.1.0-alpha.18",
4
4
  "description": "Headless UI library for React — the first Async React-native UI library",
5
5
  "keywords": [
6
6
  "components",
@@ -14,15 +14,15 @@ Requirements: `react` and `react-dom` `>=19.3.0` (peer dependencies). ESM only.
14
14
  - **Action props, not event handlers.** Interactive props are named `action` or `fooAction` (`pressedChangeAction`, `valueChangeAction`, `openChangeAction`, `checkedChangeAction`). The component calls them inside its own React transition and awaits a returned promise. Change actions (`xChangeAction`) take `(next, details)`; task actions (`action` on `Button`, `Menu.Item`, `Dialog.Close`, `Popover.Close`, `Toast.Action`) take only `(details)`, so write `action={() => save()}`, not `action={(next) => ...}`. The return type is `unknown`. `details.event` is the native DOM event; `details.reason` (where present) is `"activate" | "platform" | "dismiss" | "timeout"`.
15
15
  - **State triple.** A controllable state `x` comes as `x` (controlled), `defaultX` (uncontrolled initial value) and `xChangeAction`. `undefined` means uncontrolled; "nothing selected" is `null`. Do not switch between controlled and uncontrolled after mount.
16
16
  - **Optimistic and pending are built in.** While an action is pending, triggers (the parts the user acts on) already show the new value, and the state owner (usually the `Root`) and the trigger carry `data-pending`. If the action rejects, the value rolls back and the error goes to the nearest Error Boundary. Uncontrolled state is written only after success.
17
- - **Latest intent wins, for uncontrolled state.** A second click while pending is a new intent: the optimistic display follows it, and uncontrolled state commits only the most recent dispatch. In controlled mode, your own `setState` after an `await` lands in completion order, so an older response can overwrite a newer one; guard it yourself or queue the calls with React's `useActionState`. Exception: a pure trigger (a part that runs a task, such as `Button`, `Dialog.Close`, `Menu.Item`) ignores clicks while its own action is pending and is `aria-disabled` meanwhile. Parts are never natively disabled while pending.
17
+ - **Latest intent wins, for uncontrolled state.** A second click while pending is a new intent: the optimistic display follows it, and uncontrolled state commits only the most recent dispatch (set-valued state, `CheckboxGroup` and non-`exclusive` `ToggleGroup` / `Accordion`, commits every change whose action resolved, in order, and leaves out one whose action rejected; a later action's `next` still includes an earlier pending change, so persist `details.added` / `details.removed`, not the whole `next`; exception: a `CheckboxGroup` form reset, `reason: "platform"` with `details.event?.type === "reset"`, is absolute, so persist its `next` whole, its `added` / `removed` can be empty; to keep saves from overlapping at all, queue them with React's `useActionState`). The rejection of an older action that a newer one superseded does not reach the Error Boundary (as with React's own `useTransition`); the library passes it to the global `reportError`, so it fires the window `error` event and fails a test run that does not stub `reportError`. The same goes for a rejection that arrives after its component unmounted (a toast or menu closed while its action ran) or while `<Activity>` hides it. In controlled mode, your own `setState` after an `await` lands in completion order, so an older response can overwrite a newer one; guard it yourself or queue the calls with React's `useActionState`. A pure trigger (a part that runs a task: `Button`, `Dialog.Close`, `Popover.Close`, `Menu.Item`, `Toast.Action`) has no value to protect; what a click while its own action is pending does is its `whilePending` prop (`"run" | "ignore"`, type `WhilePending`). `"run"` runs the action again (runs overlap; `data-pending` until the last settles, no `aria-disabled`; an older run's rejection goes to `reportError`); `"ignore"` drops the click, cancels its default action and renders `aria-disabled="true"` while pending. Defaults: `Button` `"run"` (put `whilePending="ignore"` on a Save, Pay or Delete, and on a `formAction` submitter that must not submit twice: React drops an older overlapping submission's rejection); Close parts `"ignore"`; `Menu.Item` and `Toast.Action` `"ignore"`, but `"run"` with `closeOnActivate={false}` / `closeOnSettle={false}`. Parts are never natively disabled while pending.
18
18
  - **Content regions show the committed value.** Panels and popup content follow the committed state, so content that suspends keeps the old content on screen until it is ready (with value or open props, an Accordion item, Dialog or Popover stays closed until then; without them their content is rendered ahead and must not suspend). The library never adds `<Suspense>` and has no `fallback` props; you place boundaries.
19
- - **`transition` prop = `<ViewTransition>`.** Parts whose content is driven by library state accept `transition`; passing it wraps the part in `<ViewTransition default={transition}>`. Without it, no boundary is rendered. Each dispatch is tagged with `fsui-<component>` transition types (for example `fsui-tabs-next`), usable in a type map or `:active-view-transition-type()`.
20
- - **Browser-owned state.** Where the platform owns the state (popover top layer, `<details open>`, `<dialog>` dismissal, native inputs), the library observes it instead of copying it. Escape and click-outside are browser dismissals: `openChangeAction(false)` is a notification and cannot be vetoed.
19
+ - **`transition` prop = `<ViewTransition>`.** Parts whose content is driven by library state accept `transition`; passing it wraps the part in `<ViewTransition default={transition}>`. Without it, no boundary is rendered. Each dispatch is tagged with `fsui-<component>` transition types (for example `fsui-tabs-next`), usable in a type map or `:active-view-transition-type()`. On a popup (`Dialog`, `Popover`, `Menu`, `Combobox`), write a CSS exit for dismissals next to `transition`: the library turns the popup's CSS transitions off for the moment of its own show or hide while a view transition runs, so an `html:active-view-transition .popup { transition: none }` rule (older docs needed one) is not needed: remove it (keeping it is harmless, but it also cuts a dismissal's fade during an unrelated view transition). Only CSS transitions are cut: write the popup's exit as a `transition`, not a `@keyframes` animation.
20
+ - **Browser-owned state.** Where the platform owns the state (popover top layer, `<details open>`, `<dialog>` dismissal, native inputs), the library observes it instead of copying it. Escape and click-outside are browser dismissals, **facts**: they are not passed to `openChangeAction` (requests only) but to `onOpenChange(false, details)`, called synchronously, not awaited, never pending, and they cannot be vetoed. A controlled overlay therefore takes both: `open={open} openChangeAction={setOpen} onOpenChange={setOpen}` (set state directly in `onOpenChange`, never in `startTransition` or after an `await`; a parent that keeps `open={true}` re-opens it).
21
21
  - **Headless.** No provider, no theme, no stylesheet. Style with your CSS on `data-*` attributes (present as `data-x=""` when true, absent when false; enum states carry a value, `data-orientation="vertical"`). Hover, focus and active are not attributes: use `:hover`, `:focus-visible`, `:active`.
22
22
 
23
23
  ## Things agents get wrong
24
24
 
25
- 1. **Do not use `onClick`, `onChange`, `onValueChange`, `onOpenChange`, `onPressedChange`.** They do not exist as library props. Handlers a part installs itself (for example `onClick` on `Toggle`) are removed from its props type; unused DOM handlers pass through to the element. The one `on*` prop the library defines is `Combobox.Root`'s `onInputValueChange` (controlled input text must update synchronously).
25
+ 1. **Do not use `onClick`, `onChange`, `onValueChange`, `onPressedChange`.** They do not exist as library props. Handlers a part installs itself (for example `onClick` on `Toggle`) are removed from its props type; unused DOM handlers pass through to the element. The library's own `on*` props report **facts** (changes that had already happened when the library heard of them, such as a popup dismissed with Escape): `onOpenChange` on `Popover.Root`, `Menu.Root`, `Combobox.Root` and `Dialog.Root` (dismissals only, always `false`), `Combobox.Root`'s `onInputValueChange` (input text), and a toast's `onClose` (`toast({ onClose })`, formerly `closeAction`). Each is called synchronously, outside any transition, not awaited; it may return a promise; a rejection, or a throw, goes to `reportError`.
26
26
  2. **Do not wrap the action in `startTransition` yourself**, and do not add `useOptimistic` or a pending flag around a component: it already does this. But a `setState` that runs **after your own `await`** inside an action is outside the transition (a React rule); wrap that one in `startTransition`.
27
27
  3. **Return a promise only if the UI must wait for it.** A pending async action holds every other transition in the app (including router navigations). For background work, do not return the promise: `valueChangeAction={(v) => { void save(v); }}`.
28
28
  4. **No `asChild`, no `as`.** Use `render`: `render={<MyButton />}` (the element must accept `ref` and spread its props) or `render={(props, state) => <a {...props} />}`.
@@ -38,14 +38,14 @@ Requirements: `react` and `react-dom` `>=19.3.0` (peer dependencies). ESM only.
38
38
 
39
39
  Each line: import path, parts, main state props.
40
40
 
41
- - `Button` (`@funstack/ui/button`): `action` (pure trigger, ignores clicks while pending), `formAction`.
41
+ - `Button` (`@funstack/ui/button`): `action` (pure trigger; a click while pending runs again unless `whilePending="ignore"`), `formAction`, `whilePending`.
42
42
  - `Toggle` (`@funstack/ui/toggle`): `pressed` / `defaultPressed` / `pressedChangeAction`.
43
43
  - `ToggleGroup` (`@funstack/ui/toggle-group`): `Root`, `Item`. `value` is `string[]`, or `string | null` with `exclusive`.
44
44
  - `Toolbar` (`@funstack/ui/toolbar`): `Root`, `Separator`. Any focusable descendant is an item; no item part.
45
45
  - `Tabs` (`@funstack/ui/tabs`): `Root`, `List`, `Tab`, `Panel`, `Indicator`. `value` / `valueChangeAction`; `Panel` takes `transition`, `keepMounted`.
46
46
  - `Accordion` (`@funstack/ui/accordion`): `Root`, `Item` (`<details>`), `Trigger` (`<summary>`, first child of the Item), `Panel`. Without value props it needs no JavaScript; `exclusive` for one open item.
47
47
  - `Tooltip` (`@funstack/ui/tooltip`): `Root`, `Trigger`, `Popup`, `Arrow`. Open state is the browser's (no open props); `delay`, `closeDelay`.
48
- - `Popover` (`@funstack/ui/popover`): `Root`, `Trigger`, `Popup`, `Close`, `Title`, `Description`. `open` / `openChangeAction`; without open props it needs no JavaScript.
48
+ - `Popover` (`@funstack/ui/popover`): `Root`, `Trigger`, `Popup`, `Close`, `Title`, `Description`. `open` / `openChangeAction` (requests) / `onOpenChange` (dismissals); without open props it needs no JavaScript.
49
49
  - `Dialog` (`@funstack/ui/dialog`): `Root`, `Trigger`, `Popup` (`<dialog>`), `Close`, `Form`, `Title`, `Description`. Same modes as Popover; `Dialog.Form` closes after its form action settles.
50
50
  - `Menu` (`@funstack/ui/menu`): `Root`, `Trigger`, `Popup`, `Item` (`action`), `CheckboxItem`, `RadioGroup`, `RadioItem`, `Group`, `GroupLabel`, `Separator`, `SubmenuTrigger`, `ContextTrigger` (context menu). A `Root` inside a `Popup` is a submenu.
51
51
  - `Toast` (`@funstack/ui/toast`): `toast()` (also `toast.promise`, `toast.update`, `toast.dismiss`), `createToastManager`, parts `Viewport`, `Root`, `Title`, `Description`, `Action`, `Close`.
@@ -1,47 +0,0 @@
1
- "use client";
2
- import { useCallback, useEffect, useRef, useState } from "react";
3
- //#region src/internal/use-superseded-rejection.ts
4
- /** Reports an error that cannot reach a boundary (conventions 2.4: never swallow). */
5
- function report(error) {
6
- if (typeof reportError === "function") reportError(error);
7
- else console.error(error);
8
- }
9
- /**
10
- * Surfaces the rejections of superseded transitions (conventions 2.4) for `useControllableAction`
11
- * and `useActionTrigger`. React keeps only the newest thenable per `useTransition`, so a rejection
12
- * from a run that a later run superseded would be dropped entirely (no boundary, no
13
- * `reportError`). The caller rethrows the latest run's rejection itself (React's own route to the
14
- * boundary) and hands any other to the returned `capture`: the first is thrown from render, so it
15
- * reaches the same nearest Error Boundary; later ones of the burst are reported. Nothing is thrown
16
- * once unmounted (React drops its own thenables the same way).
17
- *
18
- * Call it **before `useTransition`**: React retries an errored render with every pending lane,
19
- * and in that retry `useTransition` unwraps the still-pending action thenable and suspends, which
20
- * would hold the boundary commit until the newer action settles.
21
- */
22
- function useSupersededRejection() {
23
- const [superseded, setSuperseded] = useState(null);
24
- const captured = useRef(false);
25
- const mounted = useRef(false);
26
- useEffect(() => {
27
- mounted.current = true;
28
- return () => {
29
- mounted.current = false;
30
- };
31
- }, []);
32
- const capture = useCallback((error) => {
33
- if (!mounted.current) return;
34
- if (captured.current) {
35
- report(error);
36
- return;
37
- }
38
- captured.current = true;
39
- setSuperseded({ error });
40
- }, []);
41
- if (superseded !== null) throw superseded.error;
42
- return capture;
43
- }
44
- //#endregion
45
- export { useSupersededRejection };
46
-
47
- //# sourceMappingURL=use-superseded-rejection.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-superseded-rejection.mjs","names":[],"sources":["../../src/internal/use-superseded-rejection.ts"],"sourcesContent":["\"use client\";\nimport { useCallback, useEffect, useRef, useState } from \"react\";\n\n/** Reports an error that cannot reach a boundary (conventions 2.4: never swallow). */\nfunction report(error: unknown): void {\n if (typeof reportError === \"function\") reportError(error);\n else console.error(error);\n}\n\n/**\n * Surfaces the rejections of superseded transitions (conventions 2.4) for `useControllableAction`\n * and `useActionTrigger`. React keeps only the newest thenable per `useTransition`, so a rejection\n * from a run that a later run superseded would be dropped entirely (no boundary, no\n * `reportError`). The caller rethrows the latest run's rejection itself (React's own route to the\n * boundary) and hands any other to the returned `capture`: the first is thrown from render, so it\n * reaches the same nearest Error Boundary; later ones of the burst are reported. Nothing is thrown\n * once unmounted (React drops its own thenables the same way).\n *\n * Call it **before `useTransition`**: React retries an errored render with every pending lane,\n * and in that retry `useTransition` unwraps the still-pending action thenable and suspends, which\n * would hold the boundary commit until the newer action settles.\n */\nexport function useSupersededRejection(): (error: unknown) => void {\n const [superseded, setSuperseded] = useState<{ readonly error: unknown } | null>(null);\n const captured = useRef(false);\n const mounted = useRef(false);\n\n useEffect(() => {\n mounted.current = true;\n return () => {\n mounted.current = false;\n };\n }, []);\n\n const capture = useCallback((error: unknown): void => {\n if (!mounted.current) return;\n if (captured.current) {\n // One boundary reset per burst is enough; later errors are still reported.\n report(error);\n return;\n }\n captured.current = true;\n // Called after an await, outside the transition: an urgent update, so the owner re-renders\n // promptly and throws.\n setSuperseded({ error });\n }, []);\n\n if (superseded !== null) throw superseded.error;\n return capture;\n}\n"],"mappings":";;;;AAIA,SAAS,OAAO,OAAsB;CACpC,IAAI,OAAO,gBAAgB,YAAY,YAAY,KAAK;MACnD,QAAQ,MAAM,KAAK;AAC1B;;;;;;;;;;;;;;AAeA,SAAgB,yBAAmD;CACjE,MAAM,CAAC,YAAY,iBAAiB,SAA6C,IAAI;CACrF,MAAM,WAAW,OAAO,KAAK;CAC7B,MAAM,UAAU,OAAO,KAAK;CAE5B,gBAAgB;EACd,QAAQ,UAAU;EAClB,aAAa;GACX,QAAQ,UAAU;EACpB;CACF,GAAG,CAAC,CAAC;CAEL,MAAM,UAAU,aAAa,UAAyB;EACpD,IAAI,CAAC,QAAQ,SAAS;EACtB,IAAI,SAAS,SAAS;GAEpB,OAAO,KAAK;GACZ;EACF;EACA,SAAS,UAAU;EAGnB,cAAc,EAAE,MAAM,CAAC;CACzB,GAAG,CAAC,CAAC;CAEL,IAAI,eAAe,MAAM,MAAM,WAAW;CAC1C,OAAO;AACT"}