Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tabs: remove custom logic #66097

Merged
merged 16 commits into from
Oct 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use activeId/defaultActiveId/setActiveId on Tabs
  • Loading branch information
ciampo committed Oct 28, 2024
commit 149121fb40d22225c93c16bee2648bd4ce7b14d3
47 changes: 38 additions & 9 deletions packages/components/src/tabs/index.tsx
Original file line number Diff line number Diff line change
@@ -19,6 +19,22 @@ import { Tab } from './tab';
import { TabList } from './tablist';
import { TabPanel } from './tabpanel';

function externalToInternalTabId(
externalId: string | undefined | null,
instanceId: string
) {
return externalId && `${ instanceId }-${ externalId }`;
}

function internalToExternalTabId(
internalId: string | undefined | null,
instanceId: string
) {
return typeof internalId === 'string'
? internalId.replace( `${ instanceId }-`, '' )
: internalId;
}

/**
* Display one panel of content at a time with a tabbed interface, based on the
* WAI-ARIA Tabs Pattern⁠.
@@ -34,21 +50,34 @@ export const Tabs = Object.assign(
onSelect,
children,
selectedTabId,
activeTabId,
defaultActiveTabId,
onActiveTabIdChange,
}: TabsProps ) {
const instanceId = useInstanceId( Tabs, 'tabs' );
const store = Ariakit.useTabStore( {
selectOnMove,
orientation,
defaultSelectedId:
defaultTabId && `${ instanceId }-${ defaultTabId }`,
setSelectedId: ( selectedId ) => {
const strippedDownId =
typeof selectedId === 'string'
? selectedId.replace( `${ instanceId }-`, '' )
: selectedId;
onSelect?.( strippedDownId );
defaultSelectedId: externalToInternalTabId(
defaultTabId,
instanceId
),
setSelectedId: ( newSelectedId ) => {
onSelect?.(
internalToExternalTabId( newSelectedId, instanceId )
);
},
selectedId: externalToInternalTabId( selectedTabId, instanceId ),
defaultActiveId: externalToInternalTabId(
defaultActiveTabId,
instanceId
),
setActiveId: ( newActiveId ) => {
onActiveTabIdChange?.(
internalToExternalTabId( newActiveId, instanceId )
);
},
selectedId: selectedTabId && `${ instanceId }-${ selectedTabId }`,
activeId: externalToInternalTabId( activeTabId, instanceId ),
rtl: isRTL(),
} );