11import { 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' ;
34import track from '@codesandbox/common/lib/utils/analytics' ;
45import Template from '@codesandbox/common/lib/components/Template' ;
56import { ImportTab } from './ImportTab' ;
@@ -26,7 +27,7 @@ interface INewSandboxModalProps {
2627
2728interface ITab {
2829 name : string ;
29- tabIndex : number ;
30+ tabIndex : string ;
3031}
3132
3233export 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 ) ;
0 commit comments