Skip to content

Commit 7f52300

Browse files
authored
chore(theme/Tabs): keepDOM in Tabs and stablize the Tabs classname (#2679)
1 parent ae4884f commit 7f52300

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

e2e/fixtures/package-manager-tabs/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ test.describe('tabs-component test', async () => {
2020
test('Index page', async ({ page }) => {
2121
await page.goto(`http://localhost:${appPort}`);
2222

23-
await page.waitForSelector('.rp-tabs__tab');
24-
const tabs = page.locator('.rp-tabs__tab');
23+
await page.waitForSelector('.rp-tabs__label__item');
24+
const tabs = page.locator('.rp-tabs__label__item');
2525
const tabsText = (await tabs.allInnerTexts()).map(text => text.trim());
2626

2727
expect(tabsText).toEqual([

packages/theme-default/src/components/Search/SearchPanel.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@
8787
position: sticky;
8888
background-color: var(--rp-c-bg-soft);
8989
padding-bottom: 6px;
90+
91+
&.rp-tabs {
92+
// overrides .rp-tabs
93+
border-radius: 0;
94+
border: none;
95+
margin: 0;
96+
}
9097
}
9198

9299
&__group {

packages/theme-default/src/components/Tabs/index.scss

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
}
1515
}
1616

17-
&__list {
17+
&__label {
1818
padding-left: 8px;
1919
padding-top: 4px;
2020
display: flex;
@@ -31,7 +31,7 @@
3131
}
3232
}
3333

34-
&__tab {
34+
&__label__item {
3535
position: relative;
3636
color: var(--rp-c-text-2);
3737
border-bottom: 2px solid transparent;
@@ -70,4 +70,15 @@
7070
}
7171
}
7272
}
73+
74+
&__content {
75+
&__item {
76+
&--active {
77+
display: block;
78+
}
79+
&--hidden {
80+
display: none;
81+
}
82+
}
83+
}
7384
}

packages/theme-default/src/components/Tabs/index.tsx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ interface TabsProps {
3030
groupId?: string;
3131
tabContainerClassName?: string;
3232
tabPosition?: 'left' | 'center';
33+
/**
34+
* It is very useful during the transition animation and the first screen rendering of Static Site Generation (SSG).
35+
* @default true
36+
*/
37+
keepDOM?: boolean;
3338
}
3439

3540
function isTabItem(item: unknown): item is TabItem {
@@ -58,6 +63,7 @@ export const Tabs = forwardRef(
5863
groupId,
5964
tabPosition = 'left',
6065
tabContainerClassName,
66+
keepDOM = false,
6167
} = props;
6268
// remove "\n" character when write JSX element in multiple lines, use Children.toArray for Tabs with no Tab element
6369
const children = Children.toArray(rawChildren).filter(
@@ -139,7 +145,7 @@ export const Tabs = forwardRef(
139145
<div className={clsx('rp-tabs', tabContainerClassName)} ref={ref}>
140146
{tabValues.length ? (
141147
<div
142-
className="rp-tabs__list rp-tabs__list--no-scrollbar"
148+
className="rp-tabs__label rp-tabs__label--no-scrollbar"
143149
style={{
144150
justifyContent:
145151
tabPosition === 'center' ? 'center' : 'flex-start',
@@ -150,10 +156,10 @@ export const Tabs = forwardRef(
150156
<div
151157
key={index}
152158
className={clsx(
153-
'rp-tabs__tab',
159+
'rp-tabs__label__item',
154160
currentIndex === index
155-
? 'rp-tabs__tab--selected'
156-
: 'rp-tabs__tab--not-selected',
161+
? 'rp-tabs__label__item--selected'
162+
: 'rp-tabs__label__item--not-selected',
157163
)}
158164
onClick={() => {
159165
onChange?.(index);
@@ -171,7 +177,30 @@ export const Tabs = forwardRef(
171177
})}
172178
</div>
173179
) : null}
174-
<div>{Children.toArray(children)[currentIndex]}</div>
180+
<div className="rp-tabs__content">
181+
{Children.map(children, (child, index) => {
182+
const isActive = index === currentIndex;
183+
if (!keepDOM && !isActive) {
184+
return null;
185+
}
186+
187+
return (
188+
<div
189+
className={clsx(
190+
'rp-tabs__content__item',
191+
isActive
192+
? 'rp-tabs__content__item--active'
193+
: 'rp-tabs__content__item--hidden',
194+
)}
195+
aria-hidden={!isActive}
196+
data-index={index}
197+
key={index}
198+
>
199+
{child}
200+
</div>
201+
);
202+
})}
203+
</div>
175204
</div>
176205
);
177206
},

0 commit comments

Comments
 (0)