UNPKG

@saas-ui/command-bar

Version:

Chakra UI CommandBar Component

392 lines (385 loc) 10.6 kB
'use client' "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { CommandBar: () => CommandBar, CommandBarContent: () => CommandBarContent, CommandBarDialog: () => CommandBarDialog, CommandBarEmpty: () => CommandBarEmpty, CommandBarGroup: () => CommandBarGroup, CommandBarInput: () => CommandBarInput, CommandBarItem: () => CommandBarItem, CommandBarList: () => CommandBarList, CommandBarLoading: () => CommandBarLoading, CommandBarSeparator: () => CommandBarSeparator, useCommandBar: () => useCommandBar, useCommandBarContext: () => useCommandBarContext, useCommandBarStyles: () => useCommandBarStyles, useCommandState: () => import_cmdk2.useCommandState }); module.exports = __toCommonJS(src_exports); // src/command-bar.tsx var import_react3 = require("@chakra-ui/react"); // src/command-bar-context.ts var import_react = require("@chakra-ui/react"); var import_utils = require("@chakra-ui/utils"); var import_react2 = require("react"); var [CommandBarStylesProvider, useCommandBarStyles] = (0, import_utils.createContext)({ name: "CommandBarStylesProvider" }); var [CommandBarProvider, useCommandBarContext] = (0, import_utils.createContext)({ name: "CommandBarProvider" }); var useCommandBar = (props) => { const { shouldFilter = true, onFilter, value, onChange, onSelect, isOpen: isOpenProp, onClose: onCloseProp, closeOnSelect, size } = props; const { isOpen, onClose } = (0, import_react.useDisclosure)({ isOpen: isOpenProp, onClose: onCloseProp }); const getCommandProps = (0, import_react2.useCallback)( (props2) => { return { shouldFilter, filter: onFilter, value, onValueChange: onChange, ...props2 }; }, [shouldFilter, onFilter, value, onChange] ); const getItemProps = (0, import_react2.useCallback)( (props2) => { const { isDisabled, onSelect: onSelectProp, ...rest } = props2; return { ...rest, disabled: isDisabled, onSelect: (0, import_utils.callAllHandlers)(onSelectProp, onSelect, () => { if (closeOnSelect) { onClose(); } }) }; }, [onSelect, closeOnSelect, onClose] ); const getDialogProps = (0, import_react2.useCallback)( (props2) => { return { isOpen, onClose: (0, import_utils.callAllHandlers)(props2 == null ? void 0 : props2.onClose, onClose), size, ...props2 }; }, [isOpen, onClose] ); return { getCommandProps, getItemProps, getDialogProps }; }; // src/command-bar.tsx var import_utils2 = require("@chakra-ui/utils"); var import_cmdk = require("cmdk"); // src/command-bar-theme.ts var import_styled_system = require("@chakra-ui/styled-system"); var parts = [ "container", "dialog", "input", "list", "loading", "empty", "group", "item", "separator" ]; var { definePartsStyle, defineMultiStyleConfig } = (0, import_styled_system.createMultiStyleConfigHelpers)(parts); var baseStyle = definePartsStyle({ container: { display: "flex", flexDirection: "column", minHeight: 1, borderRadius: "md", bg: "white", color: "inherit", py: 2, zIndex: "modal", flex: 1, _dark: { bg: "gray.700" } }, input: { px: 4, py: 2, mb: 2, outline: "none", bg: "transparent", w: "full" }, list: { borderTopWidth: "1px", overflow: "auto", overscrollBehavior: "contain", flex: 1 }, group: { "& > [cmdk-group-heading]": { px: 4, py: 2, fontSize: "sm", fontWeight: "semibold", color: "muted" } }, item: { display: "flex", alignItems: "center", cursor: "pointer", fontSize: "md", h: 12, px: 4, gap: 4, userSelect: "none", transition: "common", transitionDuration: "normal", "&[data-disabled=true]": { opacity: 0.6 }, "&[data-selected=true]": { bg: "gray.100", _dark: { bg: "whiteAlpha.100" } }, _active: { bg: "gray.50", _dark: { bg: "whiteAlpha.50" }, _disabled: { bg: "transparent", cursor: "inherit" } } }, empty: { display: "flex", alignItems: "center", justifyContent: "center", textAlign: "center", color: "muted", fontSize: "md", h: 16 }, loading: { display: "flex", alignItems: "center", color: "muted", fontSize: "md", px: 4, py: 2, h: 12 }, dialog: { borderWidth: "1px" } }); function getSize(value) { if (value === "full") { return definePartsStyle({ dialog: { maxW: "100vw", minH: "$100vh", my: "0", borderRadius: "0" } }); } return definePartsStyle({ dialog: { maxW: value } }); } var sizes = { xs: getSize("xs"), sm: getSize("sm"), md: getSize("md"), lg: getSize("lg"), xl: getSize("xl"), "2xl": getSize("2xl"), "3xl": getSize("3xl"), "4xl": getSize("4xl"), "5xl": getSize("5xl"), "6xl": getSize("6xl"), full: getSize("full") }; var commandBarTheme = defineMultiStyleConfig({ defaultProps: { size: "md" }, baseStyle, sizes }); // src/command-bar.tsx var import_jsx_runtime = require("react/jsx-runtime"); function cmdkFactory(Component, key) { const StyledComponent = (0, import_react3.chakra)(Component); return (0, import_react3.forwardRef)((props, ref) => { const styles = useCommandBarStyles(); const componentStyles = { ...styles[key] }; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( StyledComponent, { ref, ...props, __css: componentStyles, className: (0, import_utils2.cx)(`sui-command-bar__${key}`, props.className) } ); }); } var StyledCommand = (0, import_react3.chakra)(import_cmdk.CommandRoot); var StyledInput = (0, import_react3.chakra)(import_cmdk.CommandInput); var StyledItem = (0, import_react3.chakra)(import_cmdk.CommandItem); var CommandBarList = cmdkFactory(import_cmdk.CommandList, "list"); var CommandBarLoading = cmdkFactory(import_cmdk.CommandLoading, "loading"); var CommandBarEmpty = cmdkFactory(import_cmdk.CommandEmpty, "empty"); var CommandBarSeparator = cmdkFactory(import_cmdk.CommandSeparator, "separator"); var CommandBarGroup = cmdkFactory(import_cmdk.CommandGroup, "group"); var CommandBar = (props) => { var _a; const theme = (0, import_react3.useTheme)(); const styleConfig = (_a = theme.components["SuiCommandBar"]) != null ? _a : commandBarTheme; const styles = (0, import_react3.useMultiStyleConfig)("SuiCommandBar", { styleConfig, ...props }); const context = useCommandBar(props); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CommandBarProvider, { value: context, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CommandBarStylesProvider, { value: styles, children: props.children }) }); }; CommandBar.displayName = "CommandBar"; var CommandBarContent = (0, import_react3.forwardRef)( (props, ref) => { const styles = useCommandBarStyles(); const containerStyles = { ...styles.container }; const { getCommandProps } = useCommandBarContext(); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( StyledCommand, { ref, ...props, ...getCommandProps(props), __css: containerStyles, className: (0, import_utils2.cx)("sui-command-bar", props.className) } ); } ); CommandBarContent.displayName = "CommandBarContent"; var CommandBarInput = (0, import_react3.forwardRef)( (props, ref) => { const { onChange, value, ...rest } = props; const styles = useCommandBarStyles(); const inputStyles = { ...styles.input }; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( StyledInput, { ref, value, onValueChange: onChange, ...rest, __css: inputStyles, className: (0, import_utils2.cx)("sui-command-bar__input", props.className) } ); } ); CommandBarInput.displayName = "CommandBarInput"; var CommandBarItem = (0, import_react3.forwardRef)( (props, ref) => { const styles = useCommandBarStyles(); const itemStyles = { ...styles.item }; const { getItemProps } = useCommandBarContext(); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( StyledItem, { ref, ...getItemProps(props), __css: itemStyles, className: (0, import_utils2.cx)("sui-command-bar__item", props.className) } ); } ); CommandBarItem.displayName = "CommandBarItem"; var CommandBarDialog = (0, import_react3.forwardRef)( (props, ref) => { const { children, contentProps, overlay, ...rest } = props; const { getDialogProps } = useCommandBarContext(); return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_react3.Modal, { scrollBehavior: "inside", ...getDialogProps(rest), children: [ overlay && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CommandBarOverlay, {}), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react3.ModalContent, { ref, bg: "transparent", ...contentProps, children }) ] }); } ); CommandBarDialog.displayName = "CommandBarDialog"; var CommandBarOverlay = import_react3.ModalOverlay; // src/index.ts var import_cmdk2 = require("cmdk"); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CommandBar, CommandBarContent, CommandBarDialog, CommandBarEmpty, CommandBarGroup, CommandBarInput, CommandBarItem, CommandBarList, CommandBarLoading, CommandBarSeparator, useCommandBar, useCommandBarContext, useCommandBarStyles, useCommandState }); //# sourceMappingURL=index.js.map