UNPKG

@wordpress/components

Version:
176 lines (175 loc) • 6.45 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js 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 __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let clsx = require("clsx"); clsx = __toESM(clsx); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_element = require("@wordpress/element"); let __circular_option_picker_context_tsx = require("./circular-option-picker-context.cjs"); let ___composite_index_tsx = require("../composite/index.cjs"); let __circular_option_picker_option_tsx = require("./circular-option-picker-option.cjs"); let __circular_option_picker_option_group_tsx = require("./circular-option-picker-option-group.cjs"); let __circular_option_picker_actions_tsx = require("./circular-option-picker-actions.cjs"); let __utils_tsx = require("./utils.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/components/src/circular-option-picker/circular-option-picker.tsx /** *`CircularOptionPicker` is a component that displays a set of options as circular buttons. * * ```jsx * import { CircularOptionPicker } from '../circular-option-picker'; * import { useState } from '@wordpress/element'; * * const Example = () => { * const [ currentColor, setCurrentColor ] = useState(); * const colors = [ * { color: '#f00', name: 'Red' }, * { color: '#0f0', name: 'Green' }, * { color: '#00f', name: 'Blue' }, * ]; * const colorOptions = ( * <> * { colors.map( ( { color, name }, index ) => { * return ( * <CircularOptionPicker.Option * key={ `${ color }-${ index }` } * tooltipText={ name } * style={ { backgroundColor: color, color } } * isSelected={ index === currentColor } * onClick={ () => setCurrentColor( index ) } * /> * ); * } ) } * </> * ); * return ( * <CircularOptionPicker * options={ colorOptions } * actions={ * <CircularOptionPicker.ButtonAction * onClick={ () => setCurrentColor( undefined ) } * > * { 'Clear' } * </CircularOptionPicker.ButtonAction> * } * /> * ); * }; * ``` */ function ListboxCircularOptionPicker(props) { const { actions, options, baseId, className, loop = true, children, ...additionalProps } = props; const [activeId, setActiveId] = (0, _wordpress_element.useState)(void 0); const contextValue = (0, _wordpress_element.useMemo)(() => ({ baseId, activeId, setActiveId, presentation: "listbox" }), [ baseId, activeId, setActiveId ]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className, children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(__circular_option_picker_context_tsx.CircularOptionPickerContext.Provider, { value: contextValue, children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___composite_index_tsx.Composite, { ...additionalProps, id: baseId, focusLoop: loop, rtl: (0, _wordpress_i18n.isRTL)(), role: "listbox", activeId, setActiveId, children: options }), children, actions ] }) }); } function ButtonsCircularOptionPicker(props) { const { actions, options, children, baseId, presentation, ...additionalProps } = props; const contextValue = (0, _wordpress_element.useMemo)(() => ({ baseId, presentation }), [baseId, presentation]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...additionalProps, role: "group", id: baseId, children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(__circular_option_picker_context_tsx.CircularOptionPickerContext.Provider, { value: contextValue, children: [ options, children, actions ] }) }); } function CircularOptionPicker(props) { const { asButtons, presentation, loop, actions: actionsProp, options: optionsProp, children, className, ...additionalProps } = props; (0, __utils_tsx.warnIfCircularOptionPickerAsButtonsIsSet)("CircularOptionPicker", asButtons); const resolvedPresentation = (0, __utils_tsx.resolveCircularOptionPickerPresentation)(presentation, asButtons); const baseId = (0, _wordpress_compose.useInstanceId)(CircularOptionPicker, "components-circular-option-picker", additionalProps.id); const actions = actionsProp ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: "components-circular-option-picker__custom-clear-wrapper", children: actionsProp }) : void 0; const options = /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { className: "components-circular-option-picker__swatches", children: optionsProp }); const sharedProps = { baseId, className: (0, clsx.default)("components-circular-option-picker", className), actions, options, children }; if (resolvedPresentation === "listbox") return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ListboxCircularOptionPicker, { ...additionalProps, ...sharedProps, loop }); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ButtonsCircularOptionPicker, { ...additionalProps, ...sharedProps, presentation: resolvedPresentation }); } CircularOptionPicker.Option = __circular_option_picker_option_tsx.Option; CircularOptionPicker.OptionGroup = __circular_option_picker_option_group_tsx.OptionGroup; CircularOptionPicker.ButtonAction = __circular_option_picker_actions_tsx.ButtonAction; CircularOptionPicker.DropdownLinkAction = __circular_option_picker_actions_tsx.DropdownLinkAction; CircularOptionPicker.displayName = "CircularOptionPicker"; exports.default = CircularOptionPicker; //# sourceMappingURL=circular-option-picker.cjs.map