@chakra-ui/react
Version:
Responsive and accessible React UI components built with React and Emotion
245 lines (242 loc) • 9.1 kB
JavaScript
"use strict";
"use client";
import { jsx, jsxs } from 'react/jsx-runtime';
import { dataAttr } from '@ark-ui/react';
import { Combobox, useComboboxContext } from '@ark-ui/react/combobox';
import { Dialog } from '@ark-ui/react/dialog';
import { forwardRef, useMemo, useId } from 'react';
import { createSlotRecipeContext } from '../../styled-system/create-slot-recipe-context.js';
import { SearchIcon } from '../icons.js';
import { CommandContextProvider, useCommandProviderContext, useCommandContext, CommandProviderContextProvider } from './command-context.js';
import { getCommandMatchRanges } from './command-score.js';
import { splitCommandProps, useCommand } from './use-command.js';
import { DialogPositioner, DialogContent, DialogTitle, DialogDescription, DialogBackdrop, DialogTrigger, DialogRoot, DialogContext } from '../dialog/dialog.js';
const {
withProvider,
withContext,
useStyles: useCommandStyles,
PropsProvider
} = createSlotRecipeContext({ key: "command" });
function CommandProvider(props) {
const labelId = useId();
const context = useMemo(
() => ({ value: props.value, labelId }),
[props.value, labelId]
);
return /* @__PURE__ */ jsx(CommandProviderContextProvider, { value: context, children: props.children });
}
const CommandRootProviderBase = forwardRef(function CommandRootProviderBase2(props, ref) {
const { value, onClose: _onClose, ...restProps } = props;
return /* @__PURE__ */ jsx(CommandContextProvider, { value, children: /* @__PURE__ */ jsx(
Combobox.RootProvider,
{
ref,
value: value.combobox,
...restProps,
"data-loading": dataAttr(value.loading),
"data-empty": dataAttr(value.combobox.collection.size === 0)
}
) });
});
const CommandRootProvider = withProvider(CommandRootProviderBase, "root", { forwardAsChild: true });
const CommandRootBase = forwardRef(
function CommandRootBase2(props, ref) {
const [commandProps, localProps] = splitCommandProps(props);
const provider = useCommandProviderContext();
const command = useCommand({
...commandProps,
command: commandProps.command ?? provider?.value,
ids: { label: provider?.labelId, ...commandProps.ids }
});
return /* @__PURE__ */ jsx(
CommandRootProviderBase,
{
ref,
value: command,
...localProps
}
);
}
);
const CommandRoot = withProvider(
CommandRootBase,
"root",
{ forwardAsChild: true }
);
const CommandPropsProvider = PropsProvider;
const CommandDialogTrigger = DialogTrigger;
const CommandDialog = (props) => {
const { children, onEscapeKeyDown, onExitComplete, ...rootProps } = props;
const provider = useCommandProviderContext();
const source = provider?.value;
return /* @__PURE__ */ jsx(
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__ */ jsx(DialogContext, { children: (dialog) => /* @__PURE__ */ jsx(PropsProvider, { value: { onClose: () => dialog.setOpen(false) }, children }) })
}
);
};
const CommandDialogContent = forwardRef(function CommandDialogContent2(props, ref) {
const {
title = "Command palette",
description = "Search for a command to run",
children,
...rest
} = props;
return /* @__PURE__ */ jsx(DialogPositioner, { children: /* @__PURE__ */ jsxs(DialogContent, { ref, overflow: "hidden", ...rest, children: [
/* @__PURE__ */ jsx(DialogTitle, { srOnly: true, children: title }),
/* @__PURE__ */ jsx(DialogDescription, { srOnly: true, children: description }),
children
] }) });
});
const CommandDialogBackdrop = DialogBackdrop;
const CommandLabel = withContext(
Combobox.Label,
"label",
{ forwardAsChild: true }
);
const CommandControl = withContext(
Combobox.Control,
"control",
{ forwardAsChild: true }
);
const CommandIndicatorBase = forwardRef(function CommandIndicatorBase2(props, ref) {
const { loading } = useCommandContext();
const { children = /* @__PURE__ */ jsx(SearchIcon, {}), ...restProps } = props;
return /* @__PURE__ */ jsx("div", { ref, ...restProps, "data-loading": dataAttr(loading), children });
});
const CommandIndicator = withContext(CommandIndicatorBase, "indicator");
const isComposing = (event) => event.isComposing || event.keyCode === 229;
const CommandInputBase = forwardRef(
function CommandInputBase2(props, ref) {
const { command, combobox, selectItem } = useCommandContext();
const provider = useCommandProviderContext();
return /* @__PURE__ */ jsx(
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.highlightedValue;
if (highlighted && highlighted !== combobox.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.ClearTrigger, "clearTrigger", { forwardAsChild: true });
const CommandList = withContext(
Combobox.Content,
"list",
{ forwardAsChild: true }
);
const CommandItemGroup = withContext(Combobox.ItemGroup, "itemGroup", { forwardAsChild: true });
const CommandItemGroupLabel = withContext(Combobox.ItemGroupLabel, "itemGroupLabel", { forwardAsChild: true });
const CommandItem = withContext(
Combobox.Item,
"item",
{ forwardAsChild: true, defaultProps: { persistFocus: true } }
);
const CommandItemTextBase = forwardRef(
function CommandItemTextBase2(props, ref) {
const { highlight, children, ...restProps } = props;
const combobox = useComboboxContext();
const { command } = useCommandContext();
const query = command?.filterQuery ?? combobox.inputValue;
const text = highlight && typeof children === "string" ? children : null;
const content = useMemo(() => {
if (text === null) return children;
const ranges = 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__ */ 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__ */ jsx(
Combobox.ItemText,
{
ref,
...restProps,
children: content
}
);
}
);
const CommandItemText = withContext(CommandItemTextBase, "itemText", {
forwardAsChild: true,
forwardProps: ["highlight"]
});
const CommandItemCommand = withContext("kbd", "itemCommand");
const CommandLoadingBase = forwardRef(function CommandLoadingBase2(props, ref) {
const { loading } = useCommandContext();
if (!loading) return null;
return /* @__PURE__ */ jsx("div", { ref, ...props });
});
const CommandLoading = withContext(
CommandLoadingBase,
"loading"
);
const CommandEmptyBase = forwardRef(
function CommandEmptyBase2(props, ref) {
const { loading } = useCommandContext();
if (loading) return null;
return /* @__PURE__ */ jsx(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.Context;
const CommandDialogContext = Dialog.Context;
const CommandItemContext = Combobox.ItemContext;
export { CommandClearTrigger, CommandContext, CommandControl, CommandDialog, CommandDialogBackdrop, CommandDialogContent, CommandDialogContext, CommandDialogTrigger, CommandEmpty, CommandFooter, CommandIndicator, CommandInput, CommandItem, CommandItemCommand, CommandItemContext, CommandItemGroup, CommandItemGroupLabel, CommandItemText, CommandLabel, CommandList, CommandLoading, CommandPropsProvider, CommandProvider, CommandRoot, CommandRootProvider, CommandSeparator, useCommandStyles };