UNPKG

@wordpress/block-library

Version:
169 lines (168 loc) • 7.92 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_i18n = require("@wordpress/i18n"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_components = require("@wordpress/components"); let _wordpress_data = require("@wordpress/data"); let _wordpress_element = require("@wordpress/element"); let ___tabs_tab_toolbar_controls_jsx = require("../tabs/tab-toolbar-controls.cjs"); ___tabs_tab_toolbar_controls_jsx = __toESM(___tabs_tab_toolbar_controls_jsx); let __tab_movers_jsx = require("./tab-movers.cjs"); __tab_movers_jsx = __toESM(__tab_movers_jsx); let ___tabs_use_tab_actions_js = require("../tabs/use-tab-actions.cjs"); ___tabs_use_tab_actions_js = __toESM(___tabs_use_tab_actions_js); let ___utils_hooks_js = require("../utils/hooks.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/tab-list/edit.jsx const EMPTY_ARRAY = []; function Edit({ attributes, clientId, setAttributes, __unstableLayoutClassNames: layoutClassNames }) { const { ariaLabel } = attributes; const backgroundProps = (0, _wordpress_block_editor.useBackgroundProps)(attributes); const colorProps = (0, _wordpress_block_editor.__experimentalUseColorProps)(attributes); const borderProps = (0, _wordpress_block_editor.__experimentalUseBorderProps)(attributes); const spacingProps = (0, _wordpress_block_editor.__experimentalGetSpacingClassesAndStyles)(attributes); const shadowProps = (0, _wordpress_block_editor.__experimentalGetShadowClassesAndStyles)(attributes); const dropdownMenuProps = (0, ___utils_hooks_js.useToolsPanelDropdownMenuProps)(); const { tabsClientId, tabPanels, editorActiveTabIndex, activeTabIndex } = (0, _wordpress_data.useSelect)((select) => { const { getBlockRootClientId, getBlockAttributes, getBlocks } = select(_wordpress_block_editor.store); const rootClientId = getBlockRootClientId(clientId); const tabsAttributes = getBlockAttributes(rootClientId); return { tabsClientId: rootClientId, tabPanels: (getBlocks(rootClientId)?.find((block) => block.name === "core/tab-panels"))?.innerBlocks ?? EMPTY_ARRAY, editorActiveTabIndex: tabsAttributes?.editorActiveTabIndex, activeTabIndex: tabsAttributes?.activeTabIndex ?? 0 }; }, [clientId]); const registry = (0, _wordpress_data.useRegistry)(); const { isBlockSelected, hasSelectedInnerBlock } = (0, _wordpress_data.useSelect)(_wordpress_block_editor.store); const { updateBlockAttributes, selectBlock, __unstableMarkNextChangeAsNotPersistent } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); const { insertTab, removeTab } = (0, ___tabs_use_tab_actions_js.default)(tabsClientId); const effectiveActiveIndex = editorActiveTabIndex ?? activeTabIndex; const tabsList = (0, _wordpress_element.useMemo)(() => tabPanels.map((tab) => ({ label: tab.attributes.label || "", clientId: tab.clientId })), [tabPanels]); function selectTabPanel(tabIndex) { if (tabsClientId && tabIndex !== effectiveActiveIndex) registry.batch(() => { selectBlock(clientId); __unstableMarkNextChangeAsNotPersistent(); updateBlockAttributes(tabsClientId, { editorActiveTabIndex: tabIndex }); }); } function handleLabelChange(tabIndex, newLabel) { const tab = tabsList[tabIndex]; if (tab?.clientId) updateBlockAttributes(tab.clientId, { label: newLabel }); } const menuRef = (0, _wordpress_element.useRef)(); const prevTabCountRef = (0, _wordpress_element.useRef)(tabsList.length); (0, _wordpress_element.useEffect)(() => { const prevCount = prevTabCountRef.current; prevTabCountRef.current = tabsList.length; if (!menuRef.current || tabsList.length === prevCount) return; if (!isBlockSelected(tabsClientId) && !hasSelectedInnerBlock(tabsClientId, true)) return; const focusButtonAt = (index) => { window.requestAnimationFrame(() => { const button = menuRef.current?.querySelectorAll("button")?.[index]; (button?.querySelector("[contenteditable]") ?? button)?.focus(); }); }; focusButtonAt(effectiveActiveIndex); }, [ effectiveActiveIndex, hasSelectedInnerBlock, isBlockSelected, tabsClientId, tabsList.length ]); const blockProps = (0, _wordpress_block_editor.useBlockProps)({ role: "tablist", ref: menuRef, className: layoutClassNames }); const buttonClassName = (0, clsx.default)(backgroundProps.className, colorProps.className, borderProps.className); const buttonStyle = { ...backgroundProps.style, ...colorProps.style, ...borderProps.style, ...spacingProps.style, ...shadowProps.style }; return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { group: "settings", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanel, { label: (0, _wordpress_i18n.__)("Settings"), resetAll: () => setAttributes({ ariaLabel: void 0 }), dropdownMenuProps, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, { label: (0, _wordpress_i18n.__)("Label"), isShownByDefault: true, hasValue: () => !!ariaLabel, onDeselect: () => setAttributes({ ariaLabel: void 0 }), children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextControl, { label: (0, _wordpress_i18n.__)("Label"), help: (0, _wordpress_i18n.__)("Briefly describe this tab section for screen reader users. Examples: Event information, Product details, and Account settings."), value: ariaLabel || "", onChange: (value) => setAttributes({ ariaLabel: value || void 0 }) }) }) }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__tab_movers_jsx.default, { tabsClientId }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___tabs_tab_toolbar_controls_jsx.default, { tabsClientId }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...blockProps, children: tabsList.map((tab, index) => { return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", { type: "button", role: "tab", "aria-selected": index === effectiveActiveIndex, className: buttonClassName || void 0, style: buttonStyle, tabIndex: -1, onFocus: () => { selectTabPanel(index); }, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.RichText, { tagName: "span", withoutInteractiveFormatting: true, placeholder: (0, _wordpress_i18n.__)("Tab title"), value: tab.label, onChange: (newLabel) => handleLabelChange(index, newLabel), __unstableOnSplitAtEnd: () => insertTab(index + 1), onRemove: () => removeTab(index) }) }, tab.clientId || index); }) }) ] }); } exports.default = Edit; //# sourceMappingURL=edit.cjs.map