@wordpress/components
Version:
UI components for WordPress.
165 lines (163 loc) • 6.36 kB
JavaScript
;
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/components/src/tab-panel/index.tsx
var tab_panel_exports = {};
__export(tab_panel_exports, {
TabPanel: () => TabPanel2,
default: () => tab_panel_default
});
module.exports = __toCommonJS(tab_panel_exports);
var Ariakit = __toESM(require("@ariakit/react"));
var import_clsx = __toESM(require("clsx"));
var import_element = require("@wordpress/element");
var import_compose = require("@wordpress/compose");
var import_i18n = require("@wordpress/i18n");
var import_button = __toESM(require("../button"));
var import_jsx_runtime = require("react/jsx-runtime");
var extractTabName = (id) => {
if (typeof id === "undefined" || id === null) {
return;
}
return id.match(/^tab-panel-[0-9]*-(.*)/)?.[1];
};
var UnforwardedTabPanel = ({
className,
children,
tabs,
selectOnMove = true,
initialTabName,
orientation = "horizontal",
activeClass = "is-active",
onSelect
}, ref) => {
const instanceId = (0, import_compose.useInstanceId)(TabPanel2, "tab-panel");
const prependInstanceId = (0, import_element.useCallback)((tabName) => {
if (typeof tabName === "undefined") {
return;
}
return `${instanceId}-${tabName}`;
}, [instanceId]);
const tabStore = Ariakit.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, import_i18n.isRTL)()
});
const selectedTabName = extractTabName(Ariakit.useStoreState(tabStore, "selectedId"));
const setTabStoreSelectedId = (0, import_element.useCallback)((tabName) => {
tabStore.setState("selectedId", prependInstanceId(tabName));
}, [prependInstanceId, tabStore]);
const selectedTab = tabs.find(({
name
}) => name === selectedTabName);
const previousSelectedTabName = (0, import_compose.usePrevious)(selectedTabName);
(0, import_element.useEffect)(() => {
if (previousSelectedTabName !== selectedTabName && selectedTabName === initialTabName && !!selectedTabName) {
onSelect?.(selectedTabName);
}
}, [selectedTabName, initialTabName, onSelect, previousSelectedTabName]);
(0, import_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, import_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, import_jsx_runtime.jsxs)("div", {
className,
ref,
children: [/* @__PURE__ */ (0, import_jsx_runtime.jsx)(Ariakit.TabList, {
store: tabStore,
className: "components-tab-panel__tabs",
children: tabs.map((tab) => {
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Ariakit.Tab, {
id: prependInstanceId(tab.name),
className: (0, import_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, import_jsx_runtime.jsx)(import_button.default, {
__next40pxDefaultSize: true,
icon: tab.icon,
label: tab.icon && tab.title,
showTooltip: !!tab.icon
}),
children: !tab.icon && tab.title
}, tab.name);
})
}), selectedTab && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Ariakit.TabPanel, {
id: `${prependInstanceId(selectedTab.name)}-view`,
store: tabStore,
tabId: prependInstanceId(selectedTab.name),
className: "components-tab-panel__tab-content",
children: children(selectedTab)
})]
});
};
var TabPanel2 = (0, import_element.forwardRef)(UnforwardedTabPanel);
var tab_panel_default = TabPanel2;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
TabPanel
});
//# sourceMappingURL=index.js.map