Skip to content

Commit 815156e

Browse files
committed
Fix the hash focus
1 parent de1ab25 commit 815156e

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

packages/components/src/components/tabs/index.client.tsx

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
useEffect,
99
useId,
1010
useLayoutEffect,
11-
useMemo,
1211
useRef,
1312
useState,
1413
} from 'react';
@@ -205,20 +204,14 @@ function useActiveTabFromURL(
205204
tabsInSearchParams.includes(getTabKey(items, index)),
206205
);
207206

208-
console.log(
209-
'tabIndexFromSearchParams',
210-
tabIndexFromSearchParams,
211-
tabsInSearchParams,
212-
searchParamKey,
213-
);
214-
215207
useIsomorphicLayoutEffect(() => {
216208
const tabPanel = hash
217209
? tabPanelsRef.current?.querySelector(`[role=tabpanel]:has([id="${hash}"])`)
218210
: null;
219211

220212
if (tabPanel) {
221-
for (const [index, el] of Object.entries(tabPanel)) {
213+
let index = 0;
214+
for (const el of tabPanelsRef.current!.children) {
222215
if (el === tabPanel) {
223216
setSelectedIndex(Number(index));
224217
// Note for posterity:
@@ -231,9 +224,9 @@ function useActiveTabFromURL(
231224
// Execute on next tick after `selectedIndex` update
232225
requestAnimationFrame(() => (location.hash = `#${hash}`));
233226
}
227+
index++;
234228
}
235229
} else if (tabIndexFromSearchParams !== -1) {
236-
console.log('setSelectedTab from search params', tabIndexFromSearchParams);
237230
// if we don't have content to scroll to, we look at the search params
238231
setSelectedIndex(tabIndexFromSearchParams);
239232
}

packages/components/src/components/tabs/tabs.stories.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,11 @@ export const ManyTabs: Story = {
416416
export const CustomStyling: Story = {
417417
args: {
418418
items: ['Design', 'Develop', 'Deploy'],
419-
className: 'border-b-4 border-purple-500',
419+
className: 'border-b-4 gap-0.5 border-green-800',
420420
tabClassName: args =>
421421
args.selected
422-
? 'bg-purple-500 text-white'
423-
: 'bg-gray-100 text-gray-700 dark:bg-gray-800 dark:text-gray-300',
422+
? 'bg-green-800 text-white rounded-t-none'
423+
: 'bg-gray-100 text-gray-700 rounded-t-none',
424424
children: (
425425
<>
426426
<Tabs.Tab>Design your application</Tabs.Tab>
@@ -454,3 +454,17 @@ export const TabsSyncedWithStorageEvents: Story = {
454454
);
455455
},
456456
};
457+
458+
export const ContentInHiddenPanelOpensByHash: Story = {
459+
args: {
460+
items: ['Docker', 'Binary'],
461+
children: (
462+
<>
463+
<Tabs.Tab />
464+
<Tabs.Tab>
465+
<div id="binary">Binary</div>
466+
</Tabs.Tab>
467+
</>
468+
),
469+
},
470+
};

0 commit comments

Comments
 (0)