UNPKG

@chakra-ui/react

Version:

Responsive and accessible React UI components built with React and Emotion

273 lines (269 loc) • 10.3 kB
"use strict"; "use client"; 'use strict'; var jsxRuntime = require('react/jsx-runtime'); var react = require('@ark-ui/react'); var combobox = require('@ark-ui/react/combobox'); var dialog$1 = require('@ark-ui/react/dialog'); var React = require('react'); var createSlotRecipeContext = require('../../styled-system/create-slot-recipe-context.cjs'); var icons = require('../icons.cjs'); var commandContext = require('./command-context.cjs'); var commandScore = require('./command-score.cjs'); var useCommand = require('./use-command.cjs'); var dialog = require('../dialog/dialog.cjs'); const { withProvider, withContext, useStyles: useCommandStyles, PropsProvider } = createSlotRecipeContext.createSlotRecipeContext({ key: "command" }); function CommandProvider(props) { const labelId = React.useId(); const context = React.useMemo( () => ({ value: props.value, labelId }), [props.value, labelId] ); return /* @__PURE__ */ jsxRuntime.jsx(commandContext.CommandProviderContextProvider, { value: context, children: props.children }); } const CommandRootProviderBase = React.forwardRef(function CommandRootProviderBase2(props, ref) { const { value, onClose: _onClose, ...restProps } = props; return /* @__PURE__ */ jsxRuntime.jsx(commandContext.CommandContextProvider, { value, children: /* @__PURE__ */ jsxRuntime.jsx( combobox.Combobox.RootProvider, { ref, value: value.combobox, ...restProps, "data-loading": react.dataAttr(value.loading), "data-empty": react.dataAttr(value.combobox.collection.size === 0) } ) }); }); const CommandRootProvider = withProvider(CommandRootProviderBase, "root", { forwardAsChild: true }); const CommandRootBase = React.forwardRef( function CommandRootBase2(props, ref) { const [commandProps, localProps] = useCommand.splitCommandProps(props); const provider = commandContext.useCommandProviderContext(); const command = useCommand.useCommand({ ...commandProps, command: commandProps.command ?? provider?.value, ids: { label: provider?.labelId, ...commandProps.ids } }); return /* @__PURE__ */ jsxRuntime.jsx( CommandRootProviderBase, { ref, value: command, ...localProps } ); } ); const CommandRoot = withProvider( CommandRootBase, "root", { forwardAsChild: true } ); const CommandPropsProvider = PropsProvider; const CommandDialogTrigger = dialog.DialogTrigger; const CommandDialog = (props) => { const { children, onEscapeKeyDown, onExitComplete, ...rootProps } = props; const provider = commandContext.useCommandProviderContext(); const source = provider?.value; return /* @__PURE__ */ jsxRuntime.jsx( dialog.DialogRoot, { lazyMount: true, unmountOnExit: true, placement: "top", ...rootProps, ids: { title: provider?.labelId, ...rootProps.ids }, onEscapeKeyDown: (event) => { onEscapeKeyDown?.(event); if (!event.defaultPrevented && source?.popPage()) { event.preventDefault(); } }, onExitComplete: () => { onExitComplete?.(); source?.reset(); }, children: /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogContext, { children: (dialog) => /* @__PURE__ */ jsxRuntime.jsx(PropsProvider, { value: { onClose: () => dialog.setOpen(false) }, children }) }) } ); }; const CommandDialogContent = React.forwardRef(function CommandDialogContent2(props, ref) { const { title = "Command palette", description = "Search for a command to run", children, ...rest } = props; return /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogPositioner, { children: /* @__PURE__ */ jsxRuntime.jsxs(dialog.DialogContent, { ref, overflow: "hidden", ...rest, children: [ /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogTitle, { srOnly: true, children: title }), /* @__PURE__ */ jsxRuntime.jsx(dialog.DialogDescription, { srOnly: true, children: description }), children ] }) }); }); const CommandDialogBackdrop = dialog.DialogBackdrop; const CommandLabel = withContext( combobox.Combobox.Label, "label", { forwardAsChild: true } ); const CommandControl = withContext( combobox.Combobox.Control, "control", { forwardAsChild: true } ); const CommandIndicatorBase = React.forwardRef(function CommandIndicatorBase2(props, ref) { const { loading } = commandContext.useCommandContext(); const { children = /* @__PURE__ */ jsxRuntime.jsx(icons.SearchIcon, {}), ...restProps } = props; return /* @__PURE__ */ jsxRuntime.jsx("div", { ref, ...restProps, "data-loading": react.dataAttr(loading), children }); }); const CommandIndicator = withContext(CommandIndicatorBase, "indicator"); const isComposing = (event) => event.isComposing || event.keyCode === 229; const CommandInputBase = React.forwardRef( function CommandInputBase2(props, ref) { const { command, combobox: combobox$1, selectItem } = commandContext.useCommandContext(); const provider = commandContext.useCommandProviderContext(); return /* @__PURE__ */ jsxRuntime.jsx( combobox.Combobox.Input, { ref, "aria-labelledby": provider?.labelId, ...props, onKeyDown: (event) => { props.onKeyDown?.(event); if (isComposing(event.nativeEvent)) return; command?.onInputKeyDown(event); if (event.defaultPrevented || event.key !== "Enter") return; if (!command || command.filterQuery === command.query) return; const highlighted = combobox$1.highlightedValue; if (highlighted && highlighted !== combobox$1.collection.firstValue) { return; } const item = command.getFirstMatch(command.query); if (!item) return; event.preventDefault(); const { metaKey, ctrlKey, shiftKey, altKey } = event; selectItem(item, { modifiers: { metaKey, ctrlKey, shiftKey, altKey } }); } } ); } ); const CommandInput = withContext( CommandInputBase, "input", { forwardAsChild: true } ); const CommandClearTrigger = withContext(combobox.Combobox.ClearTrigger, "clearTrigger", { forwardAsChild: true }); const CommandList = withContext( combobox.Combobox.Content, "list", { forwardAsChild: true } ); const CommandItemGroup = withContext(combobox.Combobox.ItemGroup, "itemGroup", { forwardAsChild: true }); const CommandItemGroupLabel = withContext(combobox.Combobox.ItemGroupLabel, "itemGroupLabel", { forwardAsChild: true }); const CommandItem = withContext( combobox.Combobox.Item, "item", { forwardAsChild: true, defaultProps: { persistFocus: true } } ); const CommandItemTextBase = React.forwardRef( function CommandItemTextBase2(props, ref) { const { highlight, children, ...restProps } = props; const combobox$1 = combobox.useComboboxContext(); const { command } = commandContext.useCommandContext(); const query = command?.filterQuery ?? combobox$1.inputValue; const text = highlight && typeof children === "string" ? children : null; const content = React.useMemo(() => { if (text === null) return children; const ranges = commandScore.getCommandMatchRanges(text, query); if (!ranges.length) return text; const parts = []; let cursor = 0; for (const [start, end] of ranges) { if (start > cursor) parts.push(text.slice(cursor, start)); parts.push(/* @__PURE__ */ jsxRuntime.jsx("mark", { children: text.slice(start, end) }, start)); cursor = end; } if (cursor < text.length) parts.push(text.slice(cursor)); return parts; }, [text, children, query]); return /* @__PURE__ */ jsxRuntime.jsx( combobox.Combobox.ItemText, { ref, ...restProps, children: content } ); } ); const CommandItemText = withContext(CommandItemTextBase, "itemText", { forwardAsChild: true, forwardProps: ["highlight"] }); const CommandItemCommand = withContext("kbd", "itemCommand"); const CommandLoadingBase = React.forwardRef(function CommandLoadingBase2(props, ref) { const { loading } = commandContext.useCommandContext(); if (!loading) return null; return /* @__PURE__ */ jsxRuntime.jsx("div", { ref, ...props }); }); const CommandLoading = withContext( CommandLoadingBase, "loading" ); const CommandEmptyBase = React.forwardRef( function CommandEmptyBase2(props, ref) { const { loading } = commandContext.useCommandContext(); if (loading) return null; return /* @__PURE__ */ jsxRuntime.jsx(combobox.Combobox.Empty, { ref, ...props }); } ); const CommandEmpty = withContext( CommandEmptyBase, "empty", { forwardAsChild: true } ); const CommandSeparator = withContext("div", "separator", { defaultProps: { role: "presentation" } }); const CommandFooter = withContext( "div", "footer" ); const CommandContext = combobox.Combobox.Context; const CommandDialogContext = dialog$1.Dialog.Context; const CommandItemContext = combobox.Combobox.ItemContext; exports.CommandClearTrigger = CommandClearTrigger; exports.CommandContext = CommandContext; exports.CommandControl = CommandControl; exports.CommandDialog = CommandDialog; exports.CommandDialogBackdrop = CommandDialogBackdrop; exports.CommandDialogContent = CommandDialogContent; exports.CommandDialogContext = CommandDialogContext; exports.CommandDialogTrigger = CommandDialogTrigger; exports.CommandEmpty = CommandEmpty; exports.CommandFooter = CommandFooter; exports.CommandIndicator = CommandIndicator; exports.CommandInput = CommandInput; exports.CommandItem = CommandItem; exports.CommandItemCommand = CommandItemCommand; exports.CommandItemContext = CommandItemContext; exports.CommandItemGroup = CommandItemGroup; exports.CommandItemGroupLabel = CommandItemGroupLabel; exports.CommandItemText = CommandItemText; exports.CommandLabel = CommandLabel; exports.CommandList = CommandList; exports.CommandLoading = CommandLoading; exports.CommandPropsProvider = CommandPropsProvider; exports.CommandProvider = CommandProvider; exports.CommandRoot = CommandRoot; exports.CommandRootProvider = CommandRootProvider; exports.CommandSeparator = CommandSeparator; exports.useCommandStyles = useCommandStyles;