@cognitap/radiant 0.0.12 → 0.0.14
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.
- package/CHANGELOG.md +9 -1
- package/dist/library/AlertDialog.d.ts +25 -0
- package/dist/library/AlertDialog.d.ts.map +1 -0
- package/dist/library/AlertDialog.js +56 -0
- package/dist/library/AlertDialog.js.map +1 -0
- package/dist/library/Combobox.d.ts +88 -0
- package/dist/library/Combobox.d.ts.map +1 -0
- package/dist/library/Combobox.js +365 -0
- package/dist/library/Combobox.js.map +1 -0
- package/dist/library/Dialog.d.ts +7 -1
- package/dist/library/Dialog.d.ts.map +1 -1
- package/dist/library/Dialog.js +33 -16
- package/dist/library/Dialog.js.map +1 -1
- package/dist/library/Select.js +1 -1
- package/dist/library/Select.js.map +1 -1
- package/dist/library/cn.d.ts.map +1 -1
- package/dist/library/cn.js +18 -1
- package/dist/library/cn.js.map +1 -1
- package/dist/library/comboboxStyles.d.ts +6 -0
- package/dist/library/comboboxStyles.d.ts.map +1 -0
- package/dist/library/comboboxStyles.js +38 -0
- package/dist/library/comboboxStyles.js.map +1 -0
- package/dist/library/index.d.ts +4 -0
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +2 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/motion/popup.js +5 -5
- package/dist/library/motion/popup.js.map +1 -1
- package/dist/library/react/useComboboxPopup.d.ts +4 -0
- package/dist/library/react/useComboboxPopup.d.ts.map +1 -0
- package/dist/library/react/useComboboxPopup.js +118 -0
- package/dist/library/react/useComboboxPopup.js.map +1 -0
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/components.d.ts +25 -0
- package/dist/library/themes/components.d.ts.map +1 -1
- package/dist/library/themes/components.js +25 -0
- package/dist/library/themes/components.js.map +1 -1
- package/docs/architecture.md +30 -21
- package/package.json +1 -1
- package/src/AlertDialog.tsx +110 -0
- package/src/Combobox.tsx +752 -0
- package/src/Dialog.tsx +52 -22
- package/src/Select.tsx +1 -1
- package/src/cn.ts +18 -1
- package/src/comboboxStyles.ts +41 -0
- package/src/index.ts +43 -0
- package/src/motion/popup.ts +5 -5
- package/src/react/useComboboxPopup.ts +130 -0
- package/src/themes/components.ts +25 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,14 @@
|
|
|
4
4
|
|
|
5
5
|
## Unreleased
|
|
6
6
|
|
|
7
|
+
## 0.0.14
|
|
8
|
+
|
|
9
|
+
- Add AlertDialog.
|
|
10
|
+
|
|
11
|
+
## 0.0.13
|
|
12
|
+
|
|
13
|
+
- Add Combobox.
|
|
14
|
+
|
|
7
15
|
## 0.0.12
|
|
8
16
|
|
|
9
17
|
- Add Tabs.
|
|
@@ -28,7 +36,7 @@
|
|
|
28
36
|
|
|
29
37
|
- Breaking: the example themes moved out of the core entry. Before, `import { jelly, themes } from '@cognitap/radiant'`. After, `import { jelly, themes } from '@cognitap/radiant/examples'`. `defaultTheme` stays in core, and `themeDefinitions` exposes each example's definition.
|
|
30
38
|
|
|
31
|
-
- Reconcile
|
|
39
|
+
- Reconcile pre-hydration disclosure changes: local Collapsible state adopts once; controlled disclosures and Accordion items restore their owner's value without a request.
|
|
32
40
|
|
|
33
41
|
- Align Card root minimum-width styling with its compound parts in narrow layouts.
|
|
34
42
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type DialogProps, type DialogState, type DialogContentProps, type DialogTitleProps, type DialogDescriptionProps, type DialogTriggerProps, type DialogRenderProps } from './Dialog.js';
|
|
2
|
+
/** Always modal. Backdrop clicks never dismiss an urgent decision. */
|
|
3
|
+
export type AlertDialogProps = DialogProps & {
|
|
4
|
+
modal?: never;
|
|
5
|
+
closeOnBackdrop?: never;
|
|
6
|
+
};
|
|
7
|
+
export type AlertDialogState = DialogState;
|
|
8
|
+
export type AlertDialogRenderProps = DialogRenderProps;
|
|
9
|
+
export type AlertDialogTriggerProps = DialogTriggerProps;
|
|
10
|
+
export type AlertDialogContentProps = Omit<DialogContentProps, 'role' | 'aria-modal'>;
|
|
11
|
+
export type AlertDialogTitleProps = DialogTitleProps;
|
|
12
|
+
export type AlertDialogDescriptionProps = DialogDescriptionProps;
|
|
13
|
+
export type AlertDialogActionProps = DialogTriggerProps;
|
|
14
|
+
export type AlertDialogCancelProps = DialogTriggerProps;
|
|
15
|
+
export declare function useAlertDialog(): AlertDialogState | null;
|
|
16
|
+
export declare function AlertDialog(props: AlertDialogProps): import("react").JSX.Element;
|
|
17
|
+
export declare function AlertDialogTrigger(props: AlertDialogTriggerProps): import("react").JSX.Element;
|
|
18
|
+
export declare function AlertDialogContent(props: AlertDialogContentProps): import("react").JSX.Element;
|
|
19
|
+
export declare function AlertDialogTitle(props: AlertDialogTitleProps): import("react").JSX.Element;
|
|
20
|
+
export declare function AlertDialogDescription(props: AlertDialogDescriptionProps): import("react").JSX.Element;
|
|
21
|
+
/** Runs the caller's action, then requests close unless the action prevents default. */
|
|
22
|
+
export declare function AlertDialogAction(props: AlertDialogActionProps): import("react").JSX.Element;
|
|
23
|
+
/** Focuses the safe choice on every native open, including when it follows the action. */
|
|
24
|
+
export declare function AlertDialogCancel({ autoFocus, ref, ...props }: AlertDialogCancelProps): import("react").JSX.Element;
|
|
25
|
+
//# sourceMappingURL=AlertDialog.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AlertDialog.d.ts","sourceRoot":"","sources":["../../src/AlertDialog.tsx"],"names":[],"mappings":"AAGA,OAAO,EASL,KAAK,WAAW,EAChB,KAAK,WAAW,EAChB,KAAK,kBAAkB,EACvB,KAAK,gBAAgB,EACrB,KAAK,sBAAsB,EAC3B,KAAK,kBAAkB,EACvB,KAAK,iBAAiB,EACvB,MAAM,aAAa,CAAA;AAIpB,sEAAsE;AACtE,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG;IAAE,KAAK,CAAC,EAAE,KAAK,CAAC;IAAC,eAAe,CAAC,EAAE,KAAK,CAAA;CAAE,CAAA;AACvF,MAAM,MAAM,gBAAgB,GAAG,WAAW,CAAA;AAC1C,MAAM,MAAM,sBAAsB,GAAG,iBAAiB,CAAA;AACtD,MAAM,MAAM,uBAAuB,GAAG,kBAAkB,CAAA;AACxD,MAAM,MAAM,uBAAuB,GAAG,IAAI,CAAC,kBAAkB,EAAE,MAAM,GAAG,YAAY,CAAC,CAAA;AACrF,MAAM,MAAM,qBAAqB,GAAG,gBAAgB,CAAA;AACpD,MAAM,MAAM,2BAA2B,GAAG,sBAAsB,CAAA;AAChE,MAAM,MAAM,sBAAsB,GAAG,kBAAkB,CAAA;AACvD,MAAM,MAAM,sBAAsB,GAAG,kBAAkB,CAAA;AAEvD,wBAAgB,cAAc,IAAI,gBAAgB,GAAG,IAAI,CAIxD;AAQD,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,+BASlD;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,uBAAuB,+BAGhE;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,uBAAuB,+BAGhE;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,+BAG5D;AAED,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,2BAA2B,+BAGxE;AAED,wFAAwF;AACxF,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,sBAAsB,+BAQ9D;AAED,0FAA0F;AAC1F,wBAAgB,iBAAiB,CAAC,EAAE,SAAgB,EAAE,GAAG,EAAE,GAAG,KAAK,EAAE,EAAE,sBAAsB,+BAoB5F"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { useCallback } from 'react';
|
|
4
|
+
import { DialogRoot, DialogTrigger, DialogContent, DialogTitle, DialogDescription, DialogClose, useDialog, useDialogFamily } from './Dialog.js';
|
|
5
|
+
import { joinIds } from './internal/ids.js';
|
|
6
|
+
import { useComposedRefs } from './react/useComposedRefs.js';
|
|
7
|
+
export function useAlertDialog() {
|
|
8
|
+
const state = useDialog();
|
|
9
|
+
const family = useDialogFamily();
|
|
10
|
+
return family === 'alert-dialog' ? state : null;
|
|
11
|
+
}
|
|
12
|
+
function useAlertDialogContext() {
|
|
13
|
+
const state = useAlertDialog();
|
|
14
|
+
if (!state)
|
|
15
|
+
throw new Error('AlertDialog parts must render inside AlertDialog.');
|
|
16
|
+
return state;
|
|
17
|
+
}
|
|
18
|
+
export function AlertDialog(props) {
|
|
19
|
+
return (_jsx(DialogRoot, { ...props, family: "alert-dialog", modal: true, closeOnBackdrop: false }));
|
|
20
|
+
}
|
|
21
|
+
export function AlertDialogTrigger(props) {
|
|
22
|
+
useAlertDialogContext();
|
|
23
|
+
return _jsx(DialogTrigger, { ...props });
|
|
24
|
+
}
|
|
25
|
+
export function AlertDialogContent(props) {
|
|
26
|
+
useAlertDialogContext();
|
|
27
|
+
return _jsx(DialogContent, { ...props });
|
|
28
|
+
}
|
|
29
|
+
export function AlertDialogTitle(props) {
|
|
30
|
+
useAlertDialogContext();
|
|
31
|
+
return _jsx(DialogTitle, { ...props });
|
|
32
|
+
}
|
|
33
|
+
export function AlertDialogDescription(props) {
|
|
34
|
+
useAlertDialogContext();
|
|
35
|
+
return _jsx(DialogDescription, { ...props });
|
|
36
|
+
}
|
|
37
|
+
/** Runs the caller's action, then requests close unless the action prevents default. */
|
|
38
|
+
export function AlertDialogAction(props) {
|
|
39
|
+
useAlertDialogContext();
|
|
40
|
+
return (_jsx(DialogClose, { ...props, "data-slot": joinIds('alert-dialog-action', props['data-slot']) }));
|
|
41
|
+
}
|
|
42
|
+
/** Focuses the safe choice on every native open, including when it follows the action. */
|
|
43
|
+
export function AlertDialogCancel({ autoFocus = true, ref, ...props }) {
|
|
44
|
+
useAlertDialogContext();
|
|
45
|
+
const autofocusRef = useCallback((node) => {
|
|
46
|
+
// React's autoFocus focuses on mount; the native dialog also needs the attribute on reopen.
|
|
47
|
+
if (node)
|
|
48
|
+
node.autofocus = autoFocus;
|
|
49
|
+
}, [autoFocus]);
|
|
50
|
+
const hostRef = useComposedRefs(autofocusRef, ref);
|
|
51
|
+
return (_jsx(DialogClose, { ...props, ref: hostRef,
|
|
52
|
+
// Cancel is the safe initial target inside a native modal decision.
|
|
53
|
+
// oxlint-disable-next-line jsx-a11y/no-autofocus
|
|
54
|
+
autoFocus: autoFocus, "data-slot": joinIds('alert-dialog-cancel', props['data-slot']) }));
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=AlertDialog.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AlertDialog.js","sourceRoot":"","sources":["../../src/AlertDialog.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAA;;AAEZ,OAAO,EAAE,WAAW,EAAE,MAAM,OAAO,CAAA;AACnC,OAAO,EACL,UAAU,EACV,aAAa,EACb,aAAa,EACb,WAAW,EACX,iBAAiB,EACjB,WAAW,EACX,SAAS,EACT,eAAe,EAQhB,MAAM,aAAa,CAAA;AACpB,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AAa5D,MAAM,UAAU,cAAc;IAC5B,MAAM,KAAK,GAAG,SAAS,EAAE,CAAA;IACzB,MAAM,MAAM,GAAG,eAAe,EAAE,CAAA;IAChC,OAAO,MAAM,KAAK,cAAc,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAA;AACjD,CAAC;AAED,SAAS,qBAAqB;IAC5B,MAAM,KAAK,GAAG,cAAc,EAAE,CAAA;IAC9B,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAA;IAChF,OAAO,KAAK,CAAA;AACd,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,KAAuB;IACjD,OAAO,CACL,KAAC,UAAU,OACL,KAAK,EACT,MAAM,EAAC,cAAc,EACrB,KAAK,QACL,eAAe,EAAE,KAAK,GACtB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AE,SAAS,eACT,OAAO,CAAC,qBAAqB,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC,GAC7D,CACH,CAAA;AACH,CAAC","sourcesContent":["'use client'\n\nimport { useCallback } from 'react'\nimport {\n DialogRoot,\n DialogTrigger,\n DialogContent,\n DialogTitle,\n DialogDescription,\n DialogClose,\n useDialog,\n useDialogFamily,\n type DialogProps,\n type DialogState,\n type DialogContentProps,\n type DialogTitleProps,\n type DialogDescriptionProps,\n type DialogTriggerProps,\n type DialogRenderProps\n} from './Dialog.js'\nimport { joinIds } from './internal/ids.js'\nimport { useComposedRefs } from './react/useComposedRefs.js'\n\n/** Always modal. Backdrop clicks never dismiss an urgent decision. */\nexport type AlertDialogProps = DialogProps & { modal?: never; closeOnBackdrop?: never }\nexport type AlertDialogState = DialogState\nexport type AlertDialogRenderProps = DialogRenderProps\nexport type AlertDialogTriggerProps = DialogTriggerProps\nexport type AlertDialogContentProps = Omit<DialogContentProps, 'role' | 'aria-modal'>\nexport type AlertDialogTitleProps = DialogTitleProps\nexport type AlertDialogDescriptionProps = DialogDescriptionProps\nexport type AlertDialogActionProps = DialogTriggerProps\nexport type AlertDialogCancelProps = DialogTriggerProps\n\nexport function useAlertDialog(): AlertDialogState | null {\n const state = useDialog()\n const family = useDialogFamily()\n return family === 'alert-dialog' ? state : null\n}\n\nfunction useAlertDialogContext() {\n const state = useAlertDialog()\n if (!state) throw new Error('AlertDialog parts must render inside AlertDialog.')\n return state\n}\n\nexport function AlertDialog(props: AlertDialogProps) {\n return (\n <DialogRoot\n {...props}\n family=\"alert-dialog\"\n modal\n closeOnBackdrop={false}\n />\n )\n}\n\nexport function AlertDialogTrigger(props: AlertDialogTriggerProps) {\n useAlertDialogContext()\n return <DialogTrigger {...props} />\n}\n\nexport function AlertDialogContent(props: AlertDialogContentProps) {\n useAlertDialogContext()\n return <DialogContent {...props} />\n}\n\nexport function AlertDialogTitle(props: AlertDialogTitleProps) {\n useAlertDialogContext()\n return <DialogTitle {...props} />\n}\n\nexport function AlertDialogDescription(props: AlertDialogDescriptionProps) {\n useAlertDialogContext()\n return <DialogDescription {...props} />\n}\n\n/** Runs the caller's action, then requests close unless the action prevents default. */\nexport function AlertDialogAction(props: AlertDialogActionProps) {\n useAlertDialogContext()\n return (\n <DialogClose\n {...props}\n data-slot={joinIds('alert-dialog-action', props['data-slot'])}\n />\n )\n}\n\n/** Focuses the safe choice on every native open, including when it follows the action. */\nexport function AlertDialogCancel({ autoFocus = true, ref, ...props }: AlertDialogCancelProps) {\n useAlertDialogContext()\n const autofocusRef = useCallback(\n (node: HTMLButtonElement | null) => {\n // React's autoFocus focuses on mount; the native dialog also needs the attribute on reopen.\n if (node) node.autofocus = autoFocus\n },\n [autoFocus]\n )\n const hostRef = useComposedRefs(autofocusRef, ref)\n return (\n <DialogClose\n {...props}\n ref={hostRef}\n // Cancel is the safe initial target inside a native modal decision.\n // oxlint-disable-next-line jsx-a11y/no-autofocus\n autoFocus={autoFocus}\n data-slot={joinIds('alert-dialog-cancel', props['data-slot'])}\n />\n )\n}\n"]}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { ComponentProps, ReactNode } from 'react';
|
|
2
|
+
import { type ButtonProps } from './Button.js';
|
|
3
|
+
import type { ValueOwner } from './react/useNativeOwner.js';
|
|
4
|
+
import type { ControlSize } from './presentation.js';
|
|
5
|
+
import type { ThemeStyle } from './themes/defineTheme.js';
|
|
6
|
+
export type ComboboxOption = {
|
|
7
|
+
value: string;
|
|
8
|
+
disabled?: boolean;
|
|
9
|
+
} & ({
|
|
10
|
+
label: string;
|
|
11
|
+
textValue?: string;
|
|
12
|
+
} | {
|
|
13
|
+
label: ReactNode;
|
|
14
|
+
textValue: string;
|
|
15
|
+
});
|
|
16
|
+
export type ComboboxProps = ValueOwner<string | null> & {
|
|
17
|
+
items: readonly ComboboxOption[];
|
|
18
|
+
onValueChange?: (value: string | null) => void;
|
|
19
|
+
/** Application-owned selected option when it is absent from asynchronous results. */
|
|
20
|
+
selectedItem?: ComboboxOption;
|
|
21
|
+
/** Local text matching by default. Pass null for application-filtered results. */
|
|
22
|
+
filter?: ((item: ComboboxOption, query: string) => boolean) | null;
|
|
23
|
+
onQueryChange?: (query: string) => void;
|
|
24
|
+
loading?: boolean;
|
|
25
|
+
error?: ReactNode;
|
|
26
|
+
loadingText?: string;
|
|
27
|
+
emptyText?: string;
|
|
28
|
+
name?: string;
|
|
29
|
+
form?: string;
|
|
30
|
+
disabled?: boolean;
|
|
31
|
+
required?: boolean;
|
|
32
|
+
controlSize?: ControlSize;
|
|
33
|
+
children?: ReactNode;
|
|
34
|
+
} & ({
|
|
35
|
+
query: string;
|
|
36
|
+
defaultQuery?: never;
|
|
37
|
+
} | {
|
|
38
|
+
query?: undefined;
|
|
39
|
+
defaultQuery?: string;
|
|
40
|
+
}) & ({
|
|
41
|
+
open: boolean;
|
|
42
|
+
defaultOpen?: never;
|
|
43
|
+
} | {
|
|
44
|
+
open?: undefined;
|
|
45
|
+
defaultOpen?: boolean;
|
|
46
|
+
}) & {
|
|
47
|
+
onOpenChange?: (open: boolean) => void;
|
|
48
|
+
};
|
|
49
|
+
export type ComboboxState = {
|
|
50
|
+
value: string | null;
|
|
51
|
+
query: string;
|
|
52
|
+
open: boolean;
|
|
53
|
+
disabled: boolean;
|
|
54
|
+
setValue(value: string | null): void;
|
|
55
|
+
setQuery(query: string): void;
|
|
56
|
+
setOpen(open: boolean): void;
|
|
57
|
+
};
|
|
58
|
+
export declare function useCombobox(): ComboboxState | null;
|
|
59
|
+
/** Editable search with explicit single selection. Fetching and validation messages belong to the application. */
|
|
60
|
+
export declare function Combobox({ items, value, defaultValue, onValueChange, selectedItem, filter, query, defaultQuery, onQueryChange, open, defaultOpen, onOpenChange, loading, error, loadingText, emptyText, name, form, disabled: disabledProp, required: requiredProp, controlSize, children }: ComboboxProps): import("react").JSX.Element;
|
|
61
|
+
export type ComboboxInputProps = Omit<ComponentProps<'input'>, 'value' | 'defaultValue' | 'name' | 'form' | 'type' | 'role' | 'disabled' | 'required' | 'style' | 'aria-controls' | 'aria-expanded' | 'aria-autocomplete' | 'aria-activedescendant'> & {
|
|
62
|
+
style?: ThemeStyle;
|
|
63
|
+
};
|
|
64
|
+
export declare function ComboboxInput({ className, ref, onChange, onClick, onKeyDown, onBlur, ...props }: ComboboxInputProps): import("react").JSX.Element;
|
|
65
|
+
export type ComboboxTriggerProps = Omit<ButtonProps, 'type' | 'disabled' | 'pending' | 'variant' | 'tone' | 'controlSize' | 'iconOnly' | 'fullWidth' | 'motion' | 'tabIndex' | 'aria-controls' | 'aria-expanded'>;
|
|
66
|
+
export declare function ComboboxTrigger({ children, className, render, onClick, onPointerDown, ...props }: ComboboxTriggerProps): import("react").JSX.Element;
|
|
67
|
+
export type ComboboxClearProps = ComboboxTriggerProps;
|
|
68
|
+
export declare function ComboboxClear({ children, className, render, onClick, onPointerDown, ...props }: ComboboxClearProps): import("react").JSX.Element | null;
|
|
69
|
+
export declare function ComboboxPortal({ children }: {
|
|
70
|
+
children?: ReactNode;
|
|
71
|
+
}): import("react").ReactPortal | null;
|
|
72
|
+
export type ComboboxContentProps = Omit<ComponentProps<'div'>, 'style' | 'popover' | 'role' | 'id'> & {
|
|
73
|
+
style?: ThemeStyle;
|
|
74
|
+
sideOffset?: number;
|
|
75
|
+
collisionPadding?: number;
|
|
76
|
+
motion?: 'inherit' | 'off';
|
|
77
|
+
};
|
|
78
|
+
export declare function ComboboxContent({ children, className, ref, sideOffset, collisionPadding, motion, ...props }: ComboboxContentProps): import("react").JSX.Element;
|
|
79
|
+
export type ComboboxItemProps = Omit<ComponentProps<'div'>, 'style' | 'id' | 'role' | 'tabIndex'> & {
|
|
80
|
+
value: string;
|
|
81
|
+
style?: ThemeStyle;
|
|
82
|
+
};
|
|
83
|
+
export declare function ComboboxItem({ value, children, className, onPointerDown, onPointerMove, onClick, ...props }: ComboboxItemProps): import("react").JSX.Element | null;
|
|
84
|
+
export type ComboboxStatusProps = Omit<ComponentProps<'div'>, 'style' | 'role'> & {
|
|
85
|
+
style?: ThemeStyle;
|
|
86
|
+
};
|
|
87
|
+
export declare function ComboboxStatus({ children, className, ...props }: ComboboxStatusProps): import("react").JSX.Element;
|
|
88
|
+
//# sourceMappingURL=Combobox.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Combobox.d.ts","sourceRoot":"","sources":["../../src/Combobox.tsx"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAa,MAAM,OAAO,CAAA;AAEjE,OAAO,EAAU,KAAK,WAAW,EAAE,MAAM,aAAa,CAAA;AActD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AAC3D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AACpD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAEzD,MAAM,MAAM,cAAc,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,OAAO,CAAA;CAAE,GAAG,CACjE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GACrC;IAAE,KAAK,EAAE,SAAS,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAC1C,CAAA;AACD,MAAM,MAAM,aAAa,GAAG,UAAU,CAAC,MAAM,GAAG,IAAI,CAAC,GAAG;IACtD,KAAK,EAAE,SAAS,cAAc,EAAE,CAAA;IAChC,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,KAAK,IAAI,CAAA;IAC9C,qFAAqF;IACrF,YAAY,CAAC,EAAE,cAAc,CAAA;IAC7B,kFAAkF;IAClF,MAAM,CAAC,EAAE,CAAC,CAAC,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,GAAG,IAAI,CAAA;IAClE,aAAa,CAAC,EAAE,CAA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|
+
'use client';
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
3
|
+
/* oxlint-disable jsx-a11y/prefer-tag-over-role -- An editable combobox uses a native text input and an ARIA listbox; its submitted value has a separate native form bridge. */
|
|
4
|
+
import { createContext, useContext, useEffect, useId, useLayoutEffect, useRef, useState } from 'react';
|
|
5
|
+
import { createPortal } from 'react-dom';
|
|
6
|
+
import { Button } from './Button.js';
|
|
7
|
+
import { useField, useFieldControl } from './Field.js';
|
|
8
|
+
import { cn } from './cn.js';
|
|
9
|
+
import { controlMetrics } from './controlStyles.js';
|
|
10
|
+
import { comboboxInputClass, comboboxContentClass, comboboxItemClass, comboboxActionClass } from './comboboxStyles.js';
|
|
11
|
+
import { useControllableState } from './react/useControllableState.js';
|
|
12
|
+
import { useComposedRefs } from './react/useComposedRefs.js';
|
|
13
|
+
import { useTransitionHost } from './motion/host.js';
|
|
14
|
+
import { useComboboxPopup } from './react/useComboboxPopup.js';
|
|
15
|
+
const ComboboxContext = createContext(null);
|
|
16
|
+
export function useCombobox() {
|
|
17
|
+
return useContext(ComboboxContext);
|
|
18
|
+
}
|
|
19
|
+
function useComboboxContext() {
|
|
20
|
+
const context = useContext(ComboboxContext);
|
|
21
|
+
if (!context)
|
|
22
|
+
throw new Error('Combobox parts must render inside Combobox.');
|
|
23
|
+
return context;
|
|
24
|
+
}
|
|
25
|
+
const textOf = (item) => item?.textValue ?? (typeof item?.label === 'string' ? item.label : '');
|
|
26
|
+
const localFilter = (item, query) => textOf(item).toLocaleLowerCase().includes(query.trim().toLocaleLowerCase());
|
|
27
|
+
/** Editable search with explicit single selection. Fetching and validation messages belong to the application. */
|
|
28
|
+
export function Combobox({ items, value, defaultValue, onValueChange, selectedItem, filter = localFilter, query, defaultQuery, onQueryChange, open, defaultOpen, onOpenChange, loading = false, error, loadingText = 'Loading results…', emptyText = 'No results found.', name, form, disabled: disabledProp, required: requiredProp, controlSize = 'medium', children }) {
|
|
29
|
+
const ids = useId();
|
|
30
|
+
const field = useField();
|
|
31
|
+
const disabled = !!(disabledProp || field?.disabled);
|
|
32
|
+
const required = !!(requiredProp || field?.required);
|
|
33
|
+
if (new Set(items.map(item => item.value)).size !== items.length)
|
|
34
|
+
throw new TypeError('Combobox item values must be distinct.');
|
|
35
|
+
if ([...items, ...(selectedItem ? [selectedItem] : [])].some(item => typeof item.value !== 'string' || !textOf(item).trim()))
|
|
36
|
+
throw new TypeError('Combobox items need a string value and readable textValue.');
|
|
37
|
+
const selection = useControllableState({
|
|
38
|
+
name: 'Combobox',
|
|
39
|
+
value,
|
|
40
|
+
defaultValue,
|
|
41
|
+
initialValue: null,
|
|
42
|
+
onChange: onValueChange
|
|
43
|
+
});
|
|
44
|
+
const search = useControllableState({
|
|
45
|
+
name: 'Combobox query',
|
|
46
|
+
value: query,
|
|
47
|
+
defaultValue: defaultQuery,
|
|
48
|
+
initialValue: '',
|
|
49
|
+
onChange: onQueryChange
|
|
50
|
+
});
|
|
51
|
+
const expansion = useControllableState({
|
|
52
|
+
name: 'Combobox open',
|
|
53
|
+
value: open,
|
|
54
|
+
defaultValue: defaultOpen,
|
|
55
|
+
initialValue: false,
|
|
56
|
+
onChange: onOpenChange
|
|
57
|
+
});
|
|
58
|
+
const [input, setInput] = useState(null);
|
|
59
|
+
const content = useRef(null);
|
|
60
|
+
const bridge = useRef(null);
|
|
61
|
+
const [active, highlight] = useState(null);
|
|
62
|
+
const [searching, setSearching] = useState(!!search.value);
|
|
63
|
+
const [retained, setRetained] = useState(() => new Map());
|
|
64
|
+
const currentItem = items.find(item => item.value === selection.value) ??
|
|
65
|
+
(selectedItem?.value === selection.value ? selectedItem : undefined);
|
|
66
|
+
if (currentItem && retained.get(currentItem.value) !== currentItem)
|
|
67
|
+
setRetained(new Map(retained).set(currentItem.value, currentItem));
|
|
68
|
+
const selected = currentItem ?? (selection.value === null ? undefined : retained.get(selection.value));
|
|
69
|
+
const unavailable = () => disabled || !!input?.matches(':disabled');
|
|
70
|
+
const isOpen = expansion.value && !disabled;
|
|
71
|
+
const visible = loading || error ? [] : filter ? items.filter(item => filter(item, search.value)) : items;
|
|
72
|
+
const enabled = visible.filter(item => !item.disabled);
|
|
73
|
+
const activeValue = enabled.some(item => item.value === active) ? active : null;
|
|
74
|
+
if (active !== null && activeValue === null)
|
|
75
|
+
highlight(null);
|
|
76
|
+
const text = isOpen && (searching || search.value !== '')
|
|
77
|
+
? search.value
|
|
78
|
+
: selected
|
|
79
|
+
? textOf(selected)
|
|
80
|
+
: (selection.value ?? '');
|
|
81
|
+
const setOpen = (next) => {
|
|
82
|
+
if (next && unavailable())
|
|
83
|
+
return;
|
|
84
|
+
expansion.request(next);
|
|
85
|
+
if (!next)
|
|
86
|
+
highlight(null);
|
|
87
|
+
};
|
|
88
|
+
const dismiss = () => {
|
|
89
|
+
expansion.request(false);
|
|
90
|
+
highlight(null);
|
|
91
|
+
setSearching(false);
|
|
92
|
+
search.request('');
|
|
93
|
+
};
|
|
94
|
+
const setQuery = (next) => {
|
|
95
|
+
if (unavailable())
|
|
96
|
+
return;
|
|
97
|
+
setSearching(true);
|
|
98
|
+
highlight(null);
|
|
99
|
+
search.request(next);
|
|
100
|
+
setOpen(true);
|
|
101
|
+
};
|
|
102
|
+
const pending = useRef(null);
|
|
103
|
+
const requestValue = (next) => {
|
|
104
|
+
if (unavailable())
|
|
105
|
+
return;
|
|
106
|
+
if (next !== selection.value)
|
|
107
|
+
pending.current = { value: next };
|
|
108
|
+
selection.request(next);
|
|
109
|
+
queueMicrotask(() => {
|
|
110
|
+
pending.current = null;
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
const commit = (next) => {
|
|
114
|
+
if (unavailable() || (next !== null && !enabled.some(item => item.value === next)))
|
|
115
|
+
return;
|
|
116
|
+
const item = enabled.find(item => item.value === next);
|
|
117
|
+
if (item)
|
|
118
|
+
setRetained(previous => new Map(previous).set(item.value, item));
|
|
119
|
+
requestValue(next);
|
|
120
|
+
dismiss();
|
|
121
|
+
input?.focus({ preventScroll: true });
|
|
122
|
+
};
|
|
123
|
+
const nativeValue = selection.value ?? '';
|
|
124
|
+
const latest = useRef({ selection, search, nativeValue, expansion });
|
|
125
|
+
useLayoutEffect(() => {
|
|
126
|
+
latest.current = { selection, search, nativeValue, expansion };
|
|
127
|
+
});
|
|
128
|
+
useLayoutEffect(() => {
|
|
129
|
+
const node = bridge.current;
|
|
130
|
+
if (!node)
|
|
131
|
+
return;
|
|
132
|
+
const changedByUser = pending.current && Object.is(pending.current.value, selection.value);
|
|
133
|
+
if (changedByUser) {
|
|
134
|
+
// Bypass React's value tracker so form onChange observes this accepted selection.
|
|
135
|
+
const setter = Object.getOwnPropertyDescriptor(node.ownerDocument.defaultView.HTMLInputElement.prototype, 'value').set;
|
|
136
|
+
setter.call(node, nativeValue);
|
|
137
|
+
}
|
|
138
|
+
else
|
|
139
|
+
node.value = nativeValue;
|
|
140
|
+
if (selection.controlled)
|
|
141
|
+
node.defaultValue = nativeValue;
|
|
142
|
+
node.setCustomValidity(required && selection.value === null ? 'Please select an option.' : '');
|
|
143
|
+
if (pending.current && Object.is(pending.current.value, selection.value)) {
|
|
144
|
+
pending.current = null;
|
|
145
|
+
const EventType = node.ownerDocument.defaultView.Event;
|
|
146
|
+
node.dispatchEvent(new EventType('input', { bubbles: true }));
|
|
147
|
+
node.dispatchEvent(new EventType('change', { bubbles: true }));
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
useEffect(() => {
|
|
151
|
+
const node = bridge.current;
|
|
152
|
+
const owner = node?.form;
|
|
153
|
+
if (!node || !owner)
|
|
154
|
+
return;
|
|
155
|
+
const win = node.ownerDocument.defaultView;
|
|
156
|
+
let timer;
|
|
157
|
+
const reset = (event) => {
|
|
158
|
+
win.clearTimeout(timer);
|
|
159
|
+
timer = win.setTimeout(() => {
|
|
160
|
+
if (event.defaultPrevented || !node.isConnected)
|
|
161
|
+
return;
|
|
162
|
+
latest.current.selection.reset();
|
|
163
|
+
latest.current.search.reset();
|
|
164
|
+
setSearching(false);
|
|
165
|
+
highlight(null);
|
|
166
|
+
latest.current.expansion.request(false);
|
|
167
|
+
node.value = latest.current.nativeValue;
|
|
168
|
+
}, 0);
|
|
169
|
+
};
|
|
170
|
+
owner.addEventListener('reset', reset);
|
|
171
|
+
return () => {
|
|
172
|
+
owner.removeEventListener('reset', reset);
|
|
173
|
+
win.clearTimeout(timer);
|
|
174
|
+
};
|
|
175
|
+
}, [input, form]);
|
|
176
|
+
useLayoutEffect(() => {
|
|
177
|
+
if (!isOpen || activeValue === null)
|
|
178
|
+
return;
|
|
179
|
+
const option = content.current?.ownerDocument.getElementById(`${ids}-option-${encodeURIComponent(activeValue)}`);
|
|
180
|
+
option?.scrollIntoView({ block: 'nearest' });
|
|
181
|
+
}, [activeValue, isOpen, ids]);
|
|
182
|
+
const context = {
|
|
183
|
+
value: selection.value,
|
|
184
|
+
query: search.value,
|
|
185
|
+
open: isOpen,
|
|
186
|
+
disabled,
|
|
187
|
+
setValue: next => {
|
|
188
|
+
if (next === null || items.some(item => item.value === next && !item.disabled))
|
|
189
|
+
requestValue(next);
|
|
190
|
+
},
|
|
191
|
+
setQuery,
|
|
192
|
+
setOpen,
|
|
193
|
+
items,
|
|
194
|
+
visible,
|
|
195
|
+
active: activeValue,
|
|
196
|
+
highlight,
|
|
197
|
+
text,
|
|
198
|
+
input,
|
|
199
|
+
setInput,
|
|
200
|
+
content,
|
|
201
|
+
inputId: field?.controlProps?.id ?? `${ids}-input`,
|
|
202
|
+
listId: `${ids}-listbox`,
|
|
203
|
+
optionId: next => `${ids}-option-${encodeURIComponent(next)}`,
|
|
204
|
+
required,
|
|
205
|
+
controlSize,
|
|
206
|
+
loading,
|
|
207
|
+
error,
|
|
208
|
+
status: loading ? loadingText : error || (visible.length === 0 ? emptyText : ''),
|
|
209
|
+
dismiss,
|
|
210
|
+
commit
|
|
211
|
+
};
|
|
212
|
+
return (_jsxs(ComboboxContext.Provider, { value: context, children: [children ?? (_jsxs(_Fragment, { children: [_jsxs("div", { className: "relative w-full min-w-0", "data-slot": "combobox-control", children: [_jsx(ComboboxInput, {}), _jsx(ComboboxTrigger, { className: "absolute inset-y-0 end-0 w-12" })] }), _jsx(ComboboxPortal, { children: _jsx(ComboboxContent, {}) })] })), _jsx("input", { ref: bridge, className: "sr-only", "data-slot": "combobox-native", type: "text", name: name, form: form, disabled: disabled, "aria-hidden": "true", tabIndex: -1, autoComplete: "off", defaultValue: nativeValue, onFocus: () => input?.focus(), onInvalid: event => {
|
|
213
|
+
event.preventDefault();
|
|
214
|
+
input?.focus();
|
|
215
|
+
} })] }));
|
|
216
|
+
}
|
|
217
|
+
export function ComboboxInput({ className, ref, onChange, onClick, onKeyDown, onBlur, ...props }) {
|
|
218
|
+
const box = useComboboxContext();
|
|
219
|
+
const control = useFieldControl({
|
|
220
|
+
id: props.id ?? box.inputId,
|
|
221
|
+
disabled: box.disabled,
|
|
222
|
+
required: box.required,
|
|
223
|
+
'aria-describedby': props['aria-describedby'],
|
|
224
|
+
'aria-invalid': props['aria-invalid']
|
|
225
|
+
});
|
|
226
|
+
const inputRef = useComposedRefs(box.setInput, ref);
|
|
227
|
+
return (_jsx("input", { ...props, ...control, ref: inputRef, type: "text", role: "combobox", required: false, autoComplete: props.autoComplete ?? 'off', value: box.text, className: cn(comboboxInputClass, controlMetrics({ controlSize: box.controlSize }), className), "data-slot": "combobox-input", "data-control-size": box.controlSize, "aria-autocomplete": "list", "aria-expanded": box.open, "aria-controls": box.open ? box.listId : undefined, "aria-activedescendant": box.open && box.active !== null ? box.optionId(box.active) : undefined, onChange: event => {
|
|
228
|
+
onChange?.(event);
|
|
229
|
+
box.setQuery(event.currentTarget.value);
|
|
230
|
+
}, onClick: event => {
|
|
231
|
+
onClick?.(event);
|
|
232
|
+
if (!event.defaultPrevented)
|
|
233
|
+
box.setOpen(true);
|
|
234
|
+
}, onBlur: event => {
|
|
235
|
+
onBlur?.(event);
|
|
236
|
+
if (!box.content.current?.contains(event.relatedTarget) &&
|
|
237
|
+
!event.currentTarget.parentElement?.contains(event.relatedTarget))
|
|
238
|
+
box.dismiss();
|
|
239
|
+
}, onKeyDown: event => {
|
|
240
|
+
onKeyDown?.(event);
|
|
241
|
+
if (event.defaultPrevented ||
|
|
242
|
+
event.nativeEvent.isComposing ||
|
|
243
|
+
event.keyCode === 229 ||
|
|
244
|
+
event.ctrlKey ||
|
|
245
|
+
event.metaKey)
|
|
246
|
+
return;
|
|
247
|
+
if (event.key === 'Escape' && !box.open && box.value !== null) {
|
|
248
|
+
event.preventDefault();
|
|
249
|
+
box.commit(null);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
if (event.key === 'Escape' && box.open) {
|
|
253
|
+
event.preventDefault();
|
|
254
|
+
event.stopPropagation();
|
|
255
|
+
box.dismiss();
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (event.key === 'Tab') {
|
|
259
|
+
box.dismiss();
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (event.altKey) {
|
|
263
|
+
if (event.key === 'ArrowDown') {
|
|
264
|
+
event.preventDefault();
|
|
265
|
+
box.setOpen(true);
|
|
266
|
+
}
|
|
267
|
+
else if (event.key === 'ArrowUp') {
|
|
268
|
+
event.preventDefault();
|
|
269
|
+
box.dismiss();
|
|
270
|
+
}
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
if (event.key === 'ArrowDown' || event.key === 'ArrowUp') {
|
|
274
|
+
event.preventDefault();
|
|
275
|
+
box.setOpen(true);
|
|
276
|
+
const options = box.visible.filter(item => !item.disabled);
|
|
277
|
+
const index = options.findIndex(item => item.value === box.active);
|
|
278
|
+
const next = index < 0
|
|
279
|
+
? event.key === 'ArrowUp'
|
|
280
|
+
? options.at(-1)
|
|
281
|
+
: options[0]
|
|
282
|
+
: options[Math.max(0, Math.min(options.length - 1, index + (event.key === 'ArrowUp' ? -1 : 1)))];
|
|
283
|
+
box.highlight(next?.value ?? null);
|
|
284
|
+
}
|
|
285
|
+
else if (event.key === 'Enter' && box.open && box.active !== null) {
|
|
286
|
+
event.preventDefault();
|
|
287
|
+
box.commit(box.active);
|
|
288
|
+
}
|
|
289
|
+
} }));
|
|
290
|
+
}
|
|
291
|
+
export function ComboboxTrigger({ children, className, render, onClick, onPointerDown, ...props }) {
|
|
292
|
+
const box = useComboboxContext();
|
|
293
|
+
return (_jsx(Button, { variant: "ghost", tone: "neutral", ...props, type: "button", disabled: box.disabled, pending: false, motion: "off", controlSize: box.controlSize, render: resolved => {
|
|
294
|
+
const native = { ...resolved, className: cn(comboboxActionClass, className) };
|
|
295
|
+
return render ? render(native) : _jsx("button", { ...native });
|
|
296
|
+
}, tabIndex: -1, "aria-label": props['aria-label'] ?? 'Show options', "aria-controls": box.open ? box.listId : undefined, "aria-expanded": box.open, "data-slot": "button combobox-trigger", onPointerDown: event => {
|
|
297
|
+
onPointerDown?.(event);
|
|
298
|
+
if (!event.defaultPrevented)
|
|
299
|
+
event.preventDefault();
|
|
300
|
+
}, onClick: event => {
|
|
301
|
+
onClick?.(event);
|
|
302
|
+
if (!event.defaultPrevented) {
|
|
303
|
+
box.input?.focus({ preventScroll: true });
|
|
304
|
+
if (box.open)
|
|
305
|
+
box.dismiss();
|
|
306
|
+
else
|
|
307
|
+
box.setOpen(true);
|
|
308
|
+
}
|
|
309
|
+
}, children: children ?? (_jsx("svg", { "aria-hidden": "true", width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", children: _jsx("path", { d: "m4 6 4 4 4-4", stroke: "currentColor", strokeWidth: "1.5" }) })) }));
|
|
310
|
+
}
|
|
311
|
+
export function ComboboxClear({ children, className, render, onClick, onPointerDown, ...props }) {
|
|
312
|
+
const box = useComboboxContext();
|
|
313
|
+
if (box.value === null)
|
|
314
|
+
return null;
|
|
315
|
+
return (_jsx(Button, { variant: "ghost", tone: "neutral", ...props, type: "button", disabled: box.disabled, pending: false, motion: "off", controlSize: box.controlSize, render: resolved => {
|
|
316
|
+
const native = { ...resolved, className: cn(comboboxActionClass, className) };
|
|
317
|
+
return render ? render(native) : _jsx("button", { ...native });
|
|
318
|
+
}, tabIndex: -1, "aria-label": props['aria-label'] ?? 'Clear selection', "data-slot": "button combobox-clear", onPointerDown: event => {
|
|
319
|
+
onPointerDown?.(event);
|
|
320
|
+
if (!event.defaultPrevented)
|
|
321
|
+
event.preventDefault();
|
|
322
|
+
}, onClick: event => {
|
|
323
|
+
onClick?.(event);
|
|
324
|
+
if (!event.defaultPrevented)
|
|
325
|
+
box.commit(null);
|
|
326
|
+
}, children: children ?? (_jsx("svg", { "aria-hidden": "true", width: "16", height: "16", viewBox: "0 0 16 16", fill: "none", children: _jsx("path", { d: "m5 5 6 6m0-6-6 6", stroke: "currentColor", strokeWidth: "1.5" }) })) }));
|
|
327
|
+
}
|
|
328
|
+
export function ComboboxPortal({ children }) {
|
|
329
|
+
const box = useComboboxContext();
|
|
330
|
+
return box.input?.parentElement ? createPortal(children, box.input.parentElement) : null;
|
|
331
|
+
}
|
|
332
|
+
export function ComboboxContent({ children, className, ref, sideOffset = 4, collisionPadding = 8, motion = 'inherit', ...props }) {
|
|
333
|
+
const box = useComboboxContext();
|
|
334
|
+
const contentRef = useComposedRefs(box.content, ref);
|
|
335
|
+
const { ref: transitionRef, active: motionActive, off: motionOff } = useTransitionHost('popup', contentRef, motion === 'off');
|
|
336
|
+
useComboboxPopup(box.content, box.input, box.open, box.dismiss, sideOffset, collisionPadding);
|
|
337
|
+
return (_jsxs("div", { ...props, ref: transitionRef, popover: "manual", "data-slot": "combobox-content", "data-motion": motion, "data-state": box.open ? 'open' : 'closed', className: cn(comboboxContentClass, controlMetrics({ controlSize: box.controlSize }), motionActive &&
|
|
338
|
+
!motionOff &&
|
|
339
|
+
'opacity-[var(--_rad-popup-enter,0)] motion-reduce:opacity-100', className), children: [_jsx("div", { id: box.listId, role: "listbox", "aria-label": props['aria-label'] ?? box.input?.getAttribute('aria-label') ?? 'Suggestions', "aria-labelledby": props['aria-label'] ? undefined : box.input?.labels?.[0]?.id, "aria-busy": box.loading || undefined, "data-slot": "combobox-listbox", className: "max-h-[calc(var(--_rad-combobox-max-height,100dvh)-0.5rem)] overflow-y-auto overscroll-contain", children: children ??
|
|
340
|
+
box.visible.map(item => (_jsx(ComboboxItem, { value: item.value }, item.value))) }), _jsx(ComboboxStatus, {})] }));
|
|
341
|
+
}
|
|
342
|
+
export function ComboboxItem({ value, children, className, onPointerDown, onPointerMove, onClick, ...props }) {
|
|
343
|
+
const box = useComboboxContext();
|
|
344
|
+
const item = box.visible.find(item => item.value === value);
|
|
345
|
+
if (!item)
|
|
346
|
+
return null;
|
|
347
|
+
return (_jsx("div", { ...props, id: box.optionId(value), role: "option", tabIndex: -1, onKeyDown: props.onKeyDown, "aria-selected": box.active === value, "aria-disabled": item.disabled || undefined, "data-slot": "combobox-item", "data-value": value, "data-highlighted": box.active === value ? '' : undefined, "data-selected": box.value === value ? '' : undefined, "data-disabled": item.disabled ? '' : undefined, className: cn(comboboxItemClass, className), onPointerDown: event => {
|
|
348
|
+
onPointerDown?.(event);
|
|
349
|
+
if (!event.defaultPrevented)
|
|
350
|
+
event.preventDefault();
|
|
351
|
+
}, onPointerMove: event => {
|
|
352
|
+
onPointerMove?.(event);
|
|
353
|
+
if (!event.defaultPrevented && event.pointerType === 'mouse' && !item.disabled)
|
|
354
|
+
box.highlight(value);
|
|
355
|
+
}, onClick: event => {
|
|
356
|
+
onClick?.(event);
|
|
357
|
+
if (!event.defaultPrevented && !item.disabled)
|
|
358
|
+
box.commit(value);
|
|
359
|
+
}, children: children ?? (_jsxs(_Fragment, { children: [_jsx("span", { className: "min-w-0 flex-1", children: item.label }), _jsx("span", { "aria-hidden": "true", className: cn(box.value !== value && 'invisible'), children: "\u2713" })] })) }));
|
|
360
|
+
}
|
|
361
|
+
export function ComboboxStatus({ children, className, ...props }) {
|
|
362
|
+
const box = useComboboxContext();
|
|
363
|
+
return (_jsx("div", { ...props, role: "status", "aria-live": "polite", "aria-atomic": "true", "data-slot": "combobox-status", className: cn('px-3 py-2 empty:hidden', className), children: box.open ? (children ?? box.status) : null }));
|
|
364
|
+
}
|
|
365
|
+
//# sourceMappingURL=Combobox.js.map
|