@trail-ui/react
Version:
304 lines (297 loc) • 12.2 kB
JavaScript
;
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/select-combobox/index.ts
var select_combobox_exports = {};
__export(select_combobox_exports, {
SelectCombobox: () => _SelectCombobox,
SelectComboboxItem: () => SelectComboboxItem
});
module.exports = __toCommonJS(select_combobox_exports);
// src/select-combobox/select-combobox.tsx
var import_icons = require("@trail-ui/icons");
var import_shared_utils2 = require("@trail-ui/shared-utils");
var import_theme2 = require("@trail-ui/theme");
var import_react3 = require("react");
var import_react_aria_components2 = require("react-aria-components");
// src/listbox/listbox-selected-icon.tsx
var import_jsx_runtime = require("react/jsx-runtime");
function ListBoxSelectedIcon(props) {
const { isSelected, ...otherProps } = props;
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
"svg",
{
"aria-hidden": "true",
"data-selected": isSelected,
role: "presentation",
viewBox: "0 0 16 16",
...otherProps,
children: /* @__PURE__ */ (0, import_jsx_runtime.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.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_runtime2 = require("react/jsx-runtime");
var InternalListBoxContext = (0, import_react.createContext)(
{}
);
// src/_utils/utils.tsx
var import_utils = require("@react-aria/utils");
var import_react2 = __toESM(require("react"));
var import_react_aria = require("react-aria");
var import_react_dom = __toESM(require("react-dom"));
var import_jsx_runtime3 = require("react/jsx-runtime");
if (typeof HTMLTemplateElement !== "undefined") {
const getFirstChild = Object.getOwnPropertyDescriptor(Node.prototype, "firstChild").get;
Object.defineProperty(HTMLTemplateElement.prototype, "firstChild", {
configurable: true,
enumerable: true,
get: function() {
if (this.dataset.reactAriaHidden) {
return this.content.firstChild;
} else {
return getFirstChild.call(this);
}
}
});
}
var HiddenContext = (0, import_react2.createContext)(false);
var hiddenFragment = typeof DocumentFragment !== "undefined" ? new DocumentFragment() : null;
function replaceSpacesWithHyphens(str) {
return str.toLowerCase().trim().replace(/\s+/g, "-");
}
// src/select-combobox/select-combobox.tsx
var import_jsx_runtime4 = require("react/jsx-runtime");
function SelectCombobox({
label,
errorId,
errorIcon = /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
import_icons.ErrorIcon,
{
className: "h-4 w-4 text-red-800 dark:text-red-600",
role: "img",
"aria-label": "Error",
"aria-hidden": false
}
),
errorMessage,
children,
className,
classNames,
placeholder,
items,
...props
}, ref) {
const baseStyles = (0, import_shared_utils2.clsx)(classNames == null ? void 0 : classNames.base, className);
const slots = (0, import_react3.useMemo)(() => (0, import_theme2.selectComboBox)(), []);
const targetRef = (0, import_react3.useRef)(null);
const [isOpen, setIsOpen] = (0, import_react3.useState)(false);
const inputRef = (0, import_react3.useRef)(null);
const dropdownButtonRef = (0, import_react3.useRef)(null);
(0, import_react3.useEffect)(() => {
var _a;
(_a = dropdownButtonRef.current) == null ? void 0 : _a.setAttribute("aria-hidden", "true");
}, []);
(0, import_react3.useEffect)(() => {
var _a;
if (inputRef.current) {
inputRef.current.setAttribute(
"aria-activedescendant",
props.selectedKey ? `${label ? `${replaceSpacesWithHyphens(label)}-` : ""}listbox-option-${(_a = String(props.selectedKey)) == null ? void 0 : _a.toLocaleLowerCase()}` : ""
);
}
}, [isOpen]);
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_jsx_runtime4.Fragment, { children: /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
import_react_aria_components2.ComboBox,
{
...props,
ref,
className: slots.base({
className: baseStyles
}),
onOpenChange: setIsOpen,
children: [
label && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
import_react_aria_components2.Label,
{
className: slots.label({ class: classNames == null ? void 0 : classNames.label }),
id: `${label ? replaceSpacesWithHyphens(label) : "combobox"}-label`,
children: label
}
),
/* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
"div",
{
className: slots.inputWrapper({
class: classNames == null ? void 0 : classNames.inputWrapper
}),
children: [
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
import_react_aria_components2.Input,
{
ref: inputRef,
placeholder,
"aria-autocomplete": "list",
"aria-describedby": errorId,
"aria-haspopup": "listbox",
"aria-controls": label ? `${replaceSpacesWithHyphens(label)}-listbox` : "",
className: slots.input({
class: classNames == null ? void 0 : classNames.input
}),
...props.isRequired && { "aria-required": true },
"aria-activedescendant": props.currentFocusedItem ? `${label ? `${replaceSpacesWithHyphens(label)}-` : ""}listbox-option-${String(props.currentFocusedItem).toLocaleLowerCase()}` : ""
}
),
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
import_react_aria_components2.Button,
{
ref: dropdownButtonRef,
excludeFromTabOrder: true,
className: `${slots.inputTrigger({ class: classNames == null ? void 0 : classNames.inputTrigger })}`,
"aria-label": label ? `${label} Options` : "",
children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_icons.ChevronDownIcon, { height: 24, width: 24, className: "text-neutral-800" })
}
)
]
}
),
errorMessage && /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(
import_react_aria_components2.Text,
{
id: errorId,
slot: "errorMessage",
"aria-live": "polite",
elementType: "div",
className: `${slots.errorMessage({ class: classNames == null ? void 0 : classNames.errorMessage })}`,
children: [
errorIcon,
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("span", { children: errorMessage })
]
}
),
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)("div", { ref: targetRef }),
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
import_react_aria_components2.Popover,
{
className: slots.popover({ class: classNames == null ? void 0 : classNames.popover }),
UNSTABLE_portalContainer: targetRef.current || void 0,
children: /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
import_react_aria_components2.ListBox,
{
id: label ? `${replaceSpacesWithHyphens(label)}-listbox` : "",
"aria-label": label,
className: slots.listBox({ class: classNames == null ? void 0 : classNames.listBox }),
"aria-labelledby": "",
children
}
)
}
)
]
}
) });
}
function SelectComboboxItem(props) {
const context = (0, import_react3.useContext)(InternalListBoxContext);
const {
children,
description,
startContent,
endContent,
selectedIcon,
className,
classNames = context.itemClasses,
...otherProps
} = props;
const baseStyles = (0, import_shared_utils2.clsx)(classNames == null ? void 0 : classNames.base, className);
const slots = (0, import_react3.useMemo)(() => (0, import_theme2.selectComboboxItem)(), []);
return /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_react_aria_components2.ListBoxItem, { ...otherProps, 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_runtime4.jsx)(ListBoxSelectedIcon, { isSelected });
if (typeof selectedIcon === "function") {
return selectedIcon({ icon: defaultIcon, isSelected, isDisabled });
}
if (selectedIcon)
return selectedIcon;
return defaultIcon;
};
return /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)(import_jsx_runtime4.Fragment, { children: [
startContent,
description ? /* @__PURE__ */ (0, import_jsx_runtime4.jsxs)("div", { className: slots.wrapper({ class: classNames == null ? void 0 : classNames.wrapper }), children: [
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_react_aria_components2.Text, { slot: "label", className: slots.title({ class: classNames == null ? void 0 : classNames.title }), children }),
/* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
import_react_aria_components2.Text,
{
slot: "description",
className: slots.description({
class: classNames == null ? void 0 : classNames.description
}),
children: description
}
)
] }) : /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(import_react_aria_components2.Text, { slot: "label", className: slots.title({ class: classNames == null ? void 0 : classNames.title }), children }),
isSelected && selectionMode !== "none" && /* @__PURE__ */ (0, import_jsx_runtime4.jsx)(
"span",
{
"aria-hidden": "true",
className: slots.selectedIcon({
class: classNames == null ? void 0 : classNames.selectedIcon
}),
children: selectedContent()
}
),
endContent
] });
} });
}
var _SelectCombobox = (0, import_react3.forwardRef)(SelectCombobox);
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
SelectCombobox,
SelectComboboxItem
});