@wordpress/components
Version:
UI components for WordPress.
74 lines (73 loc) • 2.96 kB
JavaScript
import * as Ariakit from "@ariakit/react";
import { useInstanceId } from "@wordpress/compose";
import { useMemo } from "@wordpress/element";
import { isRTL } from "@wordpress/i18n";
import { TabsContext } from "./context.mjs";
import { Tab } from "./tab.mjs";
import { TabList } from "./tablist.mjs";
import { TabPanel } from "./tabpanel.mjs";
import { jsx } from "react/jsx-runtime";
// packages/components/src/tabs/index.tsx
function externalToInternalTabId(externalId, instanceId) {
return externalId && `${instanceId}-${externalId}`;
}
function internalToExternalTabId(internalId, instanceId) {
return typeof internalId === "string" ? internalId.replace(`${instanceId}-`, "") : internalId;
}
/**
* Tabs is a collection of React components that combine to render
* an [ARIA-compliant tabs pattern](https://www.w3.org/WAI/ARIA/apg/patterns/tabs/).
*
* Tabs organizes content across different screens, data sets, and interactions.
* It has two sections: a list of tabs, and the view to show when a tab is chosen.
*
* `Tabs` itself is a wrapper component and context provider.
* It is responsible for managing the state of the tabs, and rendering one instance of the `Tabs.TabList` component and one or more instances of the `Tab.TabPanel` component.
*/
const Tabs = Object.assign(function TabsRoot({ selectOnMove = true, defaultTabId, orientation = "horizontal", onSelect, children, selectedTabId, activeTabId, defaultActiveTabId, onActiveTabIdChange }) {
const instanceId = useInstanceId(TabsRoot, "tabs");
const store = Ariakit.useTabStore({
selectOnMove,
orientation,
defaultSelectedId: externalToInternalTabId(defaultTabId, instanceId),
setSelectedId: (newSelectedId) => {
onSelect?.(internalToExternalTabId(newSelectedId, instanceId));
},
selectedId: externalToInternalTabId(selectedTabId, instanceId),
defaultActiveId: externalToInternalTabId(defaultActiveTabId, instanceId),
setActiveId: (newActiveId) => {
onActiveTabIdChange?.(internalToExternalTabId(newActiveId, instanceId));
},
activeId: externalToInternalTabId(activeTabId, instanceId),
rtl: isRTL()
});
const contextValue = useMemo(() => ({
store,
instanceId
}), [store, instanceId]);
return /* @__PURE__ */ jsx(TabsContext.Provider, {
value: contextValue,
children
});
}, {
/**
* Renders a single tab.
*
* The currently active tab receives default styling that can be
* overridden with CSS targeting `[aria-selected="true"]`.
*/
Tab: Object.assign(Tab, { displayName: "Tabs.Tab" }),
/**
* A wrapper component for the `Tab` components.
*
* It is responsible for rendering the list of tabs.
*/
TabList: Object.assign(TabList, { displayName: "Tabs.TabList" }),
/**
* Renders the content to display for a single tab once that tab is selected.
*/
TabPanel: Object.assign(TabPanel, { displayName: "Tabs.TabPanel" }),
Context: Object.assign(TabsContext, { displayName: "Tabs.Context" })
});
export { Tabs };
//# sourceMappingURL=index.mjs.map