UNPKG

@wordpress/block-library

Version:
139 lines (138 loc) • 5.86 kB
import clsx from "clsx"; import { __ } from "@wordpress/i18n"; import { InspectorControls, RichText, __experimentalGetShadowClassesAndStyles, __experimentalGetSpacingClassesAndStyles, __experimentalUseBorderProps, __experimentalUseColorProps, store, useBackgroundProps, useBlockProps } from "@wordpress/block-editor"; import { TextControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components"; import { useDispatch, useRegistry, useSelect } from "@wordpress/data"; import { useEffect, useMemo, useRef } from "@wordpress/element"; import TabToolbarControls from "../tabs/tab-toolbar-controls.mjs"; import TabMovers from "./tab-movers.mjs"; import useTabActions from "../tabs/use-tab-actions.mjs"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import { Fragment, jsx, jsxs } from "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 = useBackgroundProps(attributes); const colorProps = __experimentalUseColorProps(attributes); const borderProps = __experimentalUseBorderProps(attributes); const spacingProps = __experimentalGetSpacingClassesAndStyles(attributes); const shadowProps = __experimentalGetShadowClassesAndStyles(attributes); const dropdownMenuProps = useToolsPanelDropdownMenuProps(); const { tabsClientId, tabPanels, editorActiveTabIndex, activeTabIndex } = useSelect((select) => { const { getBlockRootClientId, getBlockAttributes, getBlocks } = select(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 = useRegistry(); const { isBlockSelected, hasSelectedInnerBlock } = useSelect(store); const { updateBlockAttributes, selectBlock, __unstableMarkNextChangeAsNotPersistent } = useDispatch(store); const { insertTab, removeTab } = useTabActions(tabsClientId); const effectiveActiveIndex = editorActiveTabIndex ?? activeTabIndex; const tabsList = 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 = useRef(); const prevTabCountRef = useRef(tabsList.length); 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 = useBlockProps({ role: "tablist", ref: menuRef, className: layoutClassNames }); const buttonClassName = clsx(backgroundProps.className, colorProps.className, borderProps.className); const buttonStyle = { ...backgroundProps.style, ...colorProps.style, ...borderProps.style, ...spacingProps.style, ...shadowProps.style }; return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(InspectorControls, { group: "settings", children: /* @__PURE__ */ jsx(__experimentalToolsPanel, { label: __("Settings"), resetAll: () => setAttributes({ ariaLabel: void 0 }), dropdownMenuProps, children: /* @__PURE__ */ jsx(__experimentalToolsPanelItem, { label: __("Label"), isShownByDefault: true, hasValue: () => !!ariaLabel, onDeselect: () => setAttributes({ ariaLabel: void 0 }), children: /* @__PURE__ */ jsx(TextControl, { label: __("Label"), help: __("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__ */ jsx(TabMovers, { tabsClientId }), /* @__PURE__ */ jsx(TabToolbarControls, { tabsClientId }), /* @__PURE__ */ jsx("div", { ...blockProps, children: tabsList.map((tab, index) => { return /* @__PURE__ */ jsx("button", { type: "button", role: "tab", "aria-selected": index === effectiveActiveIndex, className: buttonClassName || void 0, style: buttonStyle, tabIndex: -1, onFocus: () => { selectTabPanel(index); }, children: /* @__PURE__ */ jsx(RichText, { tagName: "span", withoutInteractiveFormatting: true, placeholder: __("Tab title"), value: tab.label, onChange: (newLabel) => handleLabelChange(index, newLabel), __unstableOnSplitAtEnd: () => insertTab(index + 1), onRemove: () => removeTab(index) }) }, tab.clientId || index); }) }) ] }); } export { Edit as default }; //# sourceMappingURL=edit.mjs.map