UNPKG

@trail-ui/react

Version:
173 lines (166 loc) 7.36 kB
"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/listbox/index.ts var listbox_exports = {}; __export(listbox_exports, { ListBox: () => ListBox, ListBoxItem: () => ListBoxItem, ListBoxSection: () => ListBoxSection }); module.exports = __toCommonJS(listbox_exports); // src/listbox/listbox.tsx var import_shared_utils = require("@trail-ui/shared-utils"); var import_theme = require("@trail-ui/theme"); var import_react = require("react"); var import_react_aria_components = require("react-aria-components"); var import_jsx_runtime = require("react/jsx-runtime"); var InternalListBoxContext = (0, import_react.createContext)( {} ); function ListBox(props) { const { children, className, classNames, itemClasses, ...otherProps } = props; const slots = (0, import_react.useMemo)(() => (0, import_theme.menu)(), []); const baseStyles = (0, import_shared_utils.clsx)(classNames == null ? void 0 : classNames.base, className); return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(InternalListBoxContext.Provider, { value: { itemClasses }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_aria_components.ListBox, { className: slots.base({ class: baseStyles }), ...otherProps, children }) }); } // src/listbox/listbox-item.tsx var import_shared_utils2 = require("@trail-ui/shared-utils"); var import_theme2 = require("@trail-ui/theme"); var import_react2 = require("react"); var import_react_aria_components2 = require("react-aria-components"); // src/listbox/listbox-selected-icon.tsx var import_jsx_runtime2 = require("react/jsx-runtime"); function ListBoxSelectedIcon(props) { const { isSelected, ...otherProps } = props; return /* @__PURE__ */ (0, import_jsx_runtime2.jsx)( "svg", { "aria-hidden": "true", "data-selected": isSelected, role: "presentation", viewBox: "0 0 16 16", ...otherProps, children: /* @__PURE__ */ (0, import_jsx_runtime2.jsx)( "polyline", { fill: "none", points: "1 9 7 14 15 4", stroke: "currentColor", strokeDasharray: 22, strokeDashoffset: isSelected ? 44 : 66, strokeWidth: 2, style: { transition: "stroke-dashoffset 200ms ease" } } ) } ); } // src/listbox/listbox-item.tsx var import_jsx_runtime3 = require("react/jsx-runtime"); function ListBoxItem(props) { const context = (0, import_react2.useContext)(InternalListBoxContext); const { children, description, startContent, endContent, selectedIcon, className, classNames = context.itemClasses, ...otherProps } = props; const slots = (0, import_react2.useMemo)(() => (0, import_theme2.menuItem)(), []); const baseStyles = (0, import_shared_utils2.clsx)(classNames == null ? void 0 : classNames.base, className); return /* @__PURE__ */ (0, import_jsx_runtime3.jsx)( import_react_aria_components2.ListBoxItem, { ...otherProps, textValue: "ListBoxItem", className: slots.base({ class: baseStyles }), children: ({ isSelected, isDisabled, selectionMode, isFocused }) => { if (isFocused && props.textValue && props.onFocusChange) { props.onFocusChange(props.textValue); } const selectedContent = () => { const defaultIcon = /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(ListBoxSelectedIcon, { isSelected }); if (typeof selectedIcon === "function") { return selectedIcon({ icon: defaultIcon, isSelected, isDisabled }); } if (selectedIcon) return selectedIcon; return defaultIcon; }; return /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)(import_jsx_runtime3.Fragment, { children: [ startContent, description ? /* @__PURE__ */ (0, import_jsx_runtime3.jsxs)("div", { className: slots.wrapper({ class: classNames == null ? void 0 : classNames.wrapper }), children: [ /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_aria_components2.Text, { slot: "label", className: slots.title({ class: classNames == null ? void 0 : classNames.title }), children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children }) }), /* @__PURE__ */ (0, import_jsx_runtime3.jsx)( import_react_aria_components2.Text, { slot: "description", className: slots.description({ class: classNames == null ? void 0 : classNames.description }), children: description } ) ] }) : /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_react_aria_components2.Text, { slot: "label", className: slots.title({ class: classNames == null ? void 0 : classNames.title }), children: /* @__PURE__ */ (0, import_jsx_runtime3.jsx)(import_jsx_runtime3.Fragment, { children }) }), isSelected && selectionMode !== "none" && /* @__PURE__ */ (0, import_jsx_runtime3.jsx)( "span", { "aria-hidden": "true", className: slots.selectedIcon({ class: classNames == null ? void 0 : classNames.selectedIcon }), children: selectedContent() } ), endContent ] }); } } ); } // src/listbox/listbox-section.tsx var import_shared_utils3 = require("@trail-ui/shared-utils"); var import_theme3 = require("@trail-ui/theme"); var import_react3 = require("react"); var import_react_aria_components3 = require("react-aria-components"); var import_jsx_runtime4 = require("react/jsx-runtime"); function ListBoxSection(props) { const { title, children, className, classNames, ...otherProps } = props; const slots = (0, import_react3.useMemo)(() => (0, import_theme3.menuSection)(), []); const baseStyles = (0, import_shared_utils3.clsx)(classNames == null ? void 0 : classNames.base, className); return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)( import_react_aria_components3.ListBoxSection, { "data-group": "true", className: slots.base({ class: baseStyles }), ...otherProps, children: [ /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_react_aria_components3.Header, { className: slots.header({ class: classNames == null ? void 0 : classNames.header }), children: title }), /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children }) ] } ); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ListBox, ListBoxItem, ListBoxSection });