UNPKG

@wordpress/block-library

Version:
102 lines (101 loc) 3.12 kB
// packages/block-library/src/tab/tabs-list.js import clsx from "clsx"; import { __, sprintf } from "@wordpress/i18n"; import { RichText } from "@wordpress/block-editor"; import { decodeEntities } from "@wordpress/html-entities"; import { RawHTML } from "@wordpress/element"; import slugFromLabel from "./slug-from-label"; import { jsx } from "react/jsx-runtime"; function StaticLabel({ label, index }) { if (label) { return /* @__PURE__ */ jsx("span", { children: /* @__PURE__ */ jsx(RawHTML, { children: decodeEntities(label) }) }); } return /* @__PURE__ */ jsx("span", { children: sprintf( /* translators: %d is the tab index + 1 */ __("Tab %d"), index + 1 ) }); } function TabsList({ siblingTabs, currentClientId, currentBlockIndex, currentLabel, tabItemColorProps, onSelectTab, onLabelChange, labelRef, focusRef, labelElementRef }) { return /* @__PURE__ */ jsx("div", { role: "tablist", className: "tabs__list", children: siblingTabs.map((tab, index) => { const isCurrentTab = tab.clientId === currentClientId; const isSiblingTabActive = index === currentBlockIndex; const tabAttributes = tab.attributes || {}; const siblingLabel = tabAttributes.label || ""; const siblingAnchor = tabAttributes.anchor || slugFromLabel(siblingLabel, index); const siblingTabPanelId = siblingAnchor; const siblingTabLabelId = `${siblingTabPanelId}--tab`; return /* @__PURE__ */ jsx( "button", { "aria-controls": siblingTabPanelId, "aria-selected": isSiblingTabActive, id: siblingTabLabelId, role: "tab", className: clsx( "tabs__tab-label", tabItemColorProps.className ), style: { ...tabItemColorProps.style }, tabIndex: isSiblingTabActive ? 0 : -1, onClick: (event) => { event.preventDefault(); onSelectTab(tab.clientId); }, onKeyDown: (event) => { if (event.key === "Enter" && !event.shiftKey) { event.preventDefault(); onSelectTab(tab.clientId); if (isCurrentTab) { const { requestAnimationFrame } = window; focusRef.current = requestAnimationFrame( () => { labelElementRef.current?.focus(); } ); } } }, children: isCurrentTab ? /* @__PURE__ */ jsx( RichText, { ref: labelRef, tagName: "span", withoutInteractiveFormatting: true, placeholder: sprintf( /* translators: %d is the tab index + 1 */ __("Tab %d\u2026"), currentBlockIndex + 1 ), value: decodeEntities(currentLabel), onChange: onLabelChange } ) : /* @__PURE__ */ jsx( StaticLabel, { label: siblingLabel, index } ) }, tab.clientId ); }) }); } export { TabsList as default }; //# sourceMappingURL=tabs-list.js.map