@wordpress/components
Version:
UI components for WordPress.
98 lines (97 loc) • 4.19 kB
JavaScript
;
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_element = require("@wordpress/element");
let ___toolbar_button_index_tsx = require("../toolbar-button/index.cjs");
___toolbar_button_index_tsx = __toESM(___toolbar_button_index_tsx);
let __toolbar_group_container_tsx = require("./toolbar-group-container.cjs");
__toolbar_group_container_tsx = __toESM(__toolbar_group_container_tsx);
let __toolbar_group_collapsed_tsx = require("./toolbar-group-collapsed.cjs");
__toolbar_group_collapsed_tsx = __toESM(__toolbar_group_collapsed_tsx);
let ___toolbar_context_index_ts = require("../toolbar-context/index.cjs");
___toolbar_context_index_ts = __toESM(___toolbar_context_index_ts);
let react_jsx_runtime = require("react/jsx-runtime");
// packages/components/src/toolbar/toolbar-group/index.tsx
function isNestedArray(arr) {
return Array.isArray(arr) && Array.isArray(arr[0]);
}
/**
* Renders a collapsible group of controls
*
* The `controls` prop accepts an array of sets. A set is an array of controls.
* Controls have the following shape:
*
* ```
* {
* icon: string,
* title: string,
* subscript: string,
* onClick: Function,
* isActive: boolean,
* isDisabled: boolean
* }
* ```
*
* For convenience it is also possible to pass only an array of controls. It is
* then assumed this is the only set.
*
* Either `controls` or `children` is required, otherwise this components
* renders nothing.
*
* @param props Component props.
* @param [props.controls] The controls to render in this toolbar.
* @param [props.children] Any other things to render inside the toolbar besides the controls.
* @param [props.className] Class to set on the container div.
* @param [props.isCollapsed] Turns ToolbarGroup into a dropdown menu.
* @param [props.title] ARIA label for dropdown menu if is collapsed.
*/
function ToolbarGroup({ controls = [], children, className, isCollapsed, title, ...props }) {
const accessibleToolbarState = (0, _wordpress_element.useContext)(___toolbar_context_index_ts.default);
if ((!controls || !controls.length) && !children) return null;
const finalClassName = (0, clsx.default)(accessibleToolbarState ? "components-toolbar-group" : "components-toolbar", className);
let controlSets;
if (isNestedArray(controls)) controlSets = controls;
else controlSets = [controls];
if (isCollapsed) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__toolbar_group_collapsed_tsx.default, {
label: title,
controls: controlSets,
className: finalClassName,
children,
...props
});
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(__toolbar_group_container_tsx.default, {
className: finalClassName,
...props,
children: [controlSets?.flatMap((controlSet, indexOfSet) => controlSet.map((control, indexOfControl) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___toolbar_button_index_tsx.default, {
containerClassName: indexOfSet > 0 && indexOfControl === 0 ? "has-left-divider" : void 0,
...control
}, [indexOfSet, indexOfControl].join()))), children]
});
}
exports.default = ToolbarGroup;
//# sourceMappingURL=index.cjs.map