UNPKG

@wordpress/components

Version:
176 lines (175 loc) • 6.59 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 _ariakit_react = require("@ariakit/react"); _ariakit_react = __toESM(_ariakit_react); let clsx = require("clsx"); clsx = __toESM(clsx); let _wordpress_element = require("@wordpress/element"); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_i18n = require("@wordpress/i18n"); let ___button_index_tsx = require("../button/index.cjs"); ___button_index_tsx = __toESM(___button_index_tsx); let react_jsx_runtime = require("react/jsx-runtime"); // packages/components/src/tab-panel/index.tsx const extractTabName = (id) => { if (typeof id === "undefined" || id === null) return; return id.match(/^tab-panel-[0-9]*-(.*)/)?.[1]; }; /** * TabPanel is an ARIA-compliant tabpanel. * * TabPanels organize content across different screens, data sets, and interactions. * It has two sections: a list of tabs, and the view to show when tabs are chosen. * * ```jsx * import { TabPanel } from '@wordpress/components'; * * const onSelect = ( tabName ) => { * console.log( 'Selecting tab', tabName ); * }; * * const MyTabPanel = () => ( * <TabPanel * className="my-tab-panel" * activeClass="active-tab" * onSelect={ onSelect } * tabs={ [ * { * name: 'tab1', * title: 'Tab 1', * className: 'tab-one', * }, * { * name: 'tab2', * title: 'Tab 2', * className: 'tab-two', * }, * ] } * > * { ( tab ) => <p>{ tab.title }</p> } * </TabPanel> * ); * ``` */ const UnforwardedTabPanel = ({ className, children, tabs, selectOnMove = true, initialTabName, orientation = "horizontal", activeClass = "is-active", onSelect }, ref) => { const instanceId = (0, _wordpress_compose.useInstanceId)(TabPanel, "tab-panel"); const prependInstanceId = (0, _wordpress_element.useCallback)((tabName) => { if (typeof tabName === "undefined") return; return `${instanceId}-${tabName}`; }, [instanceId]); const tabStore = _ariakit_react.useTabStore({ setSelectedId: (newTabValue) => { if (typeof newTabValue === "undefined" || newTabValue === null) return; const newTab = tabs.find((t) => prependInstanceId(t.name) === newTabValue); if (newTab?.disabled || newTab === selectedTab) return; const simplifiedTabName = extractTabName(newTabValue); if (typeof simplifiedTabName === "undefined") return; onSelect?.(simplifiedTabName); }, orientation, selectOnMove, defaultSelectedId: prependInstanceId(initialTabName), rtl: (0, _wordpress_i18n.isRTL)() }); const selectedTabName = extractTabName(_ariakit_react.useStoreState(tabStore, "selectedId")); const setTabStoreSelectedId = (0, _wordpress_element.useCallback)((tabName) => { tabStore.setState("selectedId", prependInstanceId(tabName)); }, [prependInstanceId, tabStore]); const selectedTab = tabs.find(({ name }) => name === selectedTabName); const previousSelectedTabName = (0, _wordpress_compose.usePrevious)(selectedTabName); (0, _wordpress_element.useEffect)(() => { if (previousSelectedTabName !== selectedTabName && selectedTabName === initialTabName && !!selectedTabName) onSelect?.(selectedTabName); }, [ selectedTabName, initialTabName, onSelect, previousSelectedTabName ]); (0, _wordpress_element.useLayoutEffect)(() => { if (selectedTab) return; const initialTab = tabs.find((tab) => tab.name === initialTabName); if (initialTabName && !initialTab) return; if (initialTab && !initialTab.disabled) setTabStoreSelectedId(initialTab.name); else { const firstEnabledTab = tabs.find((tab) => !tab.disabled); if (firstEnabledTab) setTabStoreSelectedId(firstEnabledTab.name); } }, [ tabs, selectedTab, initialTabName, instanceId, setTabStoreSelectedId ]); (0, _wordpress_element.useEffect)(() => { if (!selectedTab?.disabled) return; const firstEnabledTab = tabs.find((tab) => !tab.disabled); if (firstEnabledTab) setTabStoreSelectedId(firstEnabledTab.name); }, [ tabs, selectedTab?.disabled, setTabStoreSelectedId, instanceId ]); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { className, ref, children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_ariakit_react.TabList, { store: tabStore, className: "components-tab-panel__tabs", children: tabs.map((tab) => { return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_ariakit_react.Tab, { id: prependInstanceId(tab.name), className: (0, clsx.default)("components-tab-panel__tabs-item", tab.className, { [activeClass]: tab.name === selectedTabName }), disabled: tab.disabled, "aria-controls": `${prependInstanceId(tab.name)}-view`, render: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(___button_index_tsx.default, { __next40pxDefaultSize: true, icon: tab.icon, label: tab.icon && tab.title, showTooltip: !!tab.icon }), children: !tab.icon && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { className: "components-tab-panel__tabs-item-label", children: tab.title }) }, tab.name); }) }), selectedTab && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_ariakit_react.TabPanel, { id: `${prependInstanceId(selectedTab.name)}-view`, store: tabStore, tabId: prependInstanceId(selectedTab.name), className: "components-tab-panel__tab-content", children: children(selectedTab) })] }); }; const TabPanel = (0, _wordpress_element.forwardRef)(UnforwardedTabPanel); TabPanel.displayName = "TabPanel"; exports.TabPanel = TabPanel; exports.default = TabPanel; //# sourceMappingURL=index.cjs.map