Skip to content

Commit 75a6f04

Browse files
yeion7SaraVieira
authored andcommitted
use reakit for new sandbox modal (#2618)
1 parent fd94309 commit 75a6f04

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

packages/app/src/app/pages/Dashboard/Content/CreateNewSandbox/NewSandboxModal/NewSandboxModal.tsx

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { inject, hooksObserver } from 'app/componentConnectors';
2-
import React, { useState } from 'react';
2+
import React from 'react';
3+
import { useTabState, Tab as ReakitTab, TabList, TabPanel } from 'reakit/Tab';
34
import track from '@codesandbox/common/lib/utils/analytics';
45
import Template from '@codesandbox/common/lib/components/Template';
56
import { ImportTab } from './ImportTab';
@@ -26,7 +27,7 @@ interface INewSandboxModalProps {
2627

2728
interface ITab {
2829
name: string;
29-
tabIndex: number;
30+
tabIndex: string;
3031
}
3132

3233
export const NewSandboxModal = inject('store', 'signals')(
@@ -37,7 +38,7 @@ export const NewSandboxModal = inject('store', 'signals')(
3738
createSandbox,
3839
store: { user },
3940
}: INewSandboxModalProps) => {
40-
const [selectedTab, setSelectedTab] = useState(0);
41+
const tabState = useTabState({ selectedId: '0' });
4142

4243
const tabs: ITab[] = [
4344
'Overview',
@@ -48,15 +49,17 @@ export const NewSandboxModal = inject('store', 'signals')(
4849
]
4950
.map((buttonName, index) => ({
5051
name: buttonName,
51-
tabIndex: index,
52+
tabIndex: String(index),
5253
}))
5354
.filter(({ name }) => Boolean(name));
5455

55-
const selectTab = (tab: ITab) => {
56-
setSelectedTab(tab.tabIndex);
56+
React.useEffect(() => {
57+
const selectedTab = tabs[tabState.currentId];
5758

58-
track('New Sandbox Modal - Open Tab', { tabName: tab.name });
59-
};
59+
if (selectedTab) {
60+
track('New Sandbox Modal - Open Tab', { tabName: selectedTab.name });
61+
}
62+
}, [tabs, tabState.currentId]);
6063

6164
const selectTemplate = template => {
6265
track('New Sandbox Modal - Select Template', { template });
@@ -65,22 +68,28 @@ export const NewSandboxModal = inject('store', 'signals')(
6568

6669
return (
6770
<Container closing={closing} forking={forking}>
68-
<TabContainer forking={forking} closing={closing}>
71+
<TabList
72+
as={TabContainer}
73+
{...tabState}
74+
aria-label="new sandbox"
75+
closing={closing}
76+
forking={forking}
77+
>
6978
{tabs.map(tab => (
70-
<Button
79+
<ReakitTab
80+
as={Button}
81+
{...tabState}
7182
key={tab.name}
72-
onClick={() => {
73-
selectTab(tab);
74-
}}
75-
selected={selectedTab === tab.tabIndex}
83+
stopId={tab.tabIndex}
84+
selected={tabState.currentId === tab.tabIndex}
7685
>
7786
{tab.name}
78-
</Button>
87+
</ReakitTab>
7988
))}
80-
</TabContainer>
89+
</TabList>
8190

8291
<InnerContainer forking={forking} closing={closing}>
83-
<Tab visible={selectedTab === 0}>
92+
<TabPanel {...tabState} as={Tab} stopId="0">
8493
{user && <MyTemplates selectTemplate={selectTemplate} />}
8594
<Title>Popular Templates</Title>
8695
<Templates>
@@ -95,13 +104,13 @@ export const NewSandboxModal = inject('store', 'signals')(
95104
))
96105
)}
97106
</Templates>
98-
</Tab>
107+
</TabPanel>
99108
{user && (
100-
<Tab visible={selectedTab === 1}>
109+
<TabPanel {...tabState} as={Tab} stopId="1">
101110
<MyTemplatesTab selectTemplate={selectTemplate} />
102-
</Tab>
111+
</TabPanel>
103112
)}
104-
<Tab visible={selectedTab === 2}>
113+
<TabPanel {...tabState} as={Tab} stopId="2">
105114
<Title>Client Templates</Title>
106115
<Templates>
107116
{client.map(template => (
@@ -113,8 +122,6 @@ export const NewSandboxModal = inject('store', 'signals')(
113122
/>
114123
))}
115124
</Templates>
116-
{/* TODO: Find a fix for css props
117-
// @ts-ignore */}
118125
<Title
119126
css={`
120127
margin-top: 1rem;
@@ -133,8 +140,8 @@ export const NewSandboxModal = inject('store', 'signals')(
133140
/>
134141
))}
135142
</Templates>
136-
</Tab>
137-
<Tab visible={selectedTab === 3}>
143+
</TabPanel>
144+
<TabPanel {...tabState} as={Tab} stopId="3">
138145
<Title>Container Templates</Title>
139146
<Templates>
140147
{container.map(template => (
@@ -146,10 +153,10 @@ export const NewSandboxModal = inject('store', 'signals')(
146153
/>
147154
))}
148155
</Templates>
149-
</Tab>
150-
<Tab visible={selectedTab === 4}>
156+
</TabPanel>
157+
<TabPanel {...tabState} as={Tab} stopId="4">
151158
<ImportTab username={user ? user.username : undefined} />
152-
</Tab>
159+
</TabPanel>
153160
</InnerContainer>
154161
</Container>
155162
);

packages/app/src/app/pages/Dashboard/Content/CreateNewSandbox/NewSandboxModal/elements.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,9 @@ export const ImportChoices = styled.div`
9999
${delayEffect(0.1)};
100100
`;
101101

102-
export const Tab = styled.section<{ visible: boolean }>`
103-
${({ visible }) => css`
104-
display: ${visible ? 'block' : 'none'};
105-
transition: 0.15s ease opacity;
106-
`}
102+
export const Tab = styled.section`
103+
display: block;
104+
transition: 0.15s ease opacity;
107105
`;
108106

109107
export const ImportChoice = styled.a.attrs({
@@ -157,7 +155,6 @@ export const Button = styled.button<{
157155
`};
158156
159157
&:focus {
160-
outline: none;
161158
color: white;
162159
}
163160
&:hover {

0 commit comments

Comments
 (0)