-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(getting-started): fix cjs/esm or js/typescript
- Loading branch information
1 parent
f0a1c74
commit 6a8f178
Showing
2 changed files
with
184 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Tab, Tabs, Box } from '@mui/material' | ||
import { TabPanel, TabContext } from '@mui/lab' | ||
import { useCallback } from 'react' | ||
import useSWR from 'swr' | ||
|
||
enum ProjectType { | ||
ESM = 'esm', | ||
CJS = 'cjs', | ||
ESM_TS = 'esm-typescript', | ||
CJS_TS = 'cjs-typescript', | ||
} | ||
const storageKey = 'ProjectType' | ||
|
||
export default function ProjectTypeTabs({ | ||
children, | ||
}: { | ||
children: JSX.Element[] | ||
}): JSX.Element { | ||
// Use SWR to sync every ProjectType choice. | ||
const { data, mutate } = useSWR(storageKey, (key) => | ||
localStorage?.getItem(key), | ||
) | ||
const value = data || ProjectType.ESM | ||
|
||
const onChange = useCallback( | ||
(value: ProjectType) => { | ||
localStorage.setItem(storageKey, value) | ||
mutate(value, false) | ||
}, | ||
[mutate], | ||
) | ||
|
||
return ( | ||
<TabContext value={value}> | ||
<Box sx={{ borderBottom: 1, borderColor: 'divider' }}> | ||
<Tabs value={value} onChange={(_, _value) => onChange(_value)}> | ||
<Tab label="ESM" value={ProjectType.ESM} /> | ||
<Tab label="CJS" value={ProjectType.CJS} /> | ||
<Tab label="ESM + Typescript" value={ProjectType.ESM_TS} /> | ||
<Tab label="CJS + Typescript" value={ProjectType.CJS_TS} /> | ||
</Tabs> | ||
</Box> | ||
<TabPanel value={ProjectType.ESM}>{children[0]}</TabPanel> | ||
<TabPanel value={ProjectType.CJS}>{children[1]}</TabPanel> | ||
<TabPanel value={ProjectType.ESM_TS}>{children[2]}</TabPanel> | ||
<TabPanel value={ProjectType.CJS_TS}>{children[3]}</TabPanel> | ||
</TabContext> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters