UNPKG

@wordpress/block-library

Version:
173 lines (171 loc) 6.86 kB
"use strict"; 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); // packages/block-library/src/tab-list/edit.js var edit_exports = {}; __export(edit_exports, { default: () => edit_default }); module.exports = __toCommonJS(edit_exports); var import_clsx = __toESM(require("clsx")); var import_i18n = require("@wordpress/i18n"); var import_block_editor = require("@wordpress/block-editor"); var import_data = require("@wordpress/data"); var import_element = require("@wordpress/element"); var import_add_tab_toolbar_control = __toESM(require("../tab-panel/add-tab-toolbar-control.cjs")); var import_remove_tab_toolbar_control = __toESM(require("../tab-panel/remove-tab-toolbar-control.cjs")); var import_jsx_runtime = require("react/jsx-runtime"); var EMPTY_ARRAY = []; function Edit({ attributes, clientId, context, __unstableLayoutClassNames: layoutClassNames }) { const tabsList = context["core/tabs-list"] || EMPTY_ARRAY; const colorProps = (0, import_block_editor.__experimentalUseColorProps)(attributes); const borderProps = (0, import_block_editor.__experimentalUseBorderProps)(attributes); const spacingProps = (0, import_block_editor.__experimentalGetSpacingClassesAndStyles)(attributes); const { tabsClientId, editorActiveTabIndex, activeTabIndex } = (0, import_data.useSelect)( (select) => { const { getBlockRootClientId, getBlockAttributes } = select(import_block_editor.store); const _tabsClientId = getBlockRootClientId(clientId); const tabsAttributes = _tabsClientId ? getBlockAttributes(_tabsClientId) : {}; return { tabsClientId: _tabsClientId, editorActiveTabIndex: tabsAttributes?.editorActiveTabIndex, activeTabIndex: tabsAttributes?.activeTabIndex ?? 0 }; }, [clientId] ); const effectiveActiveIndex = editorActiveTabIndex ?? activeTabIndex; const { __unstableMarkNextChangeAsNotPersistent, updateBlockAttributes } = (0, import_data.useDispatch)(import_block_editor.store); const handleTabClick = (0, import_element.useCallback)( (tabIndex) => { if (tabsClientId && tabIndex !== effectiveActiveIndex) { __unstableMarkNextChangeAsNotPersistent(); updateBlockAttributes(tabsClientId, { editorActiveTabIndex: tabIndex }); } }, [ tabsClientId, effectiveActiveIndex, updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent ] ); const handleLabelChange = (0, import_element.useCallback)( (tabIndex, newLabel) => { const tab = tabsList[tabIndex]; if (tab?.clientId) { updateBlockAttributes(tab.clientId, { label: newLabel }); } }, [tabsList, updateBlockAttributes] ); const menuRef = (0, import_element.useRef)(); const prevTabCountRef = (0, import_element.useRef)(tabsList.length); (0, import_element.useEffect)(() => { const prevCount = prevTabCountRef.current; prevTabCountRef.current = tabsList.length; if (!menuRef.current || tabsList.length === prevCount) { return; } const focusButtonAt = (index) => { window.requestAnimationFrame(() => { const buttons = menuRef.current?.querySelectorAll("button"); const target = buttons?.[index]; if (!target) { return; } const richText = target.querySelector("[contenteditable]"); if (richText) { richText.focus(); } else { target.focus(); } }); }; if (tabsList.length > prevCount) { focusButtonAt(tabsList.length - 1); } else { focusButtonAt(effectiveActiveIndex); } }, [tabsList.length, effectiveActiveIndex]); const blockProps = (0, import_block_editor.useBlockProps)({ role: "tablist", ref: menuRef, // Applied manually since this block has no inner blocks for the layout // support to add its container classes to. className: layoutClassNames }); const buttonClassName = (0, import_clsx.default)(colorProps.className, borderProps.className); const buttonStyle = { ...colorProps.style, ...borderProps.style, ...spacingProps.style }; return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_add_tab_toolbar_control.default, { tabsClientId }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_remove_tab_toolbar_control.default, { tabsClientId }), /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { ...blockProps, children: tabsList.map((tab, index) => { const isActive = index === effectiveActiveIndex; return /* @__PURE__ */ (0, import_jsx_runtime.jsx)( "button", { type: "button", role: "tab", "aria-selected": isActive, className: buttonClassName || void 0, style: buttonStyle, tabIndex: -1, onClick: (event) => { event.preventDefault(); handleTabClick(index); }, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)( import_block_editor.RichText, { tagName: "span", withoutInteractiveFormatting: true, placeholder: (0, import_i18n.__)("Tab title"), value: tab.label, onChange: (newLabel) => handleLabelChange(index, newLabel) } ) }, tab.clientId || index ); }) }) ] }); } var edit_default = Edit; //# sourceMappingURL=edit.cjs.map