Skip to content

Commit 05ac9ac

Browse files
authored
add typescript export code ref to #129 (#138)
* add typescript export code ref to #129 * add .tsx
1 parent 12c7755 commit 05ac9ac

File tree

4 files changed

+83
-37
lines changed

4 files changed

+83
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"react-dnd-html5-backend": "^10.0.2",
3333
"react-dom": "^16.12.0",
3434
"react-hotkeys-hook": "^2.4.0",
35-
"react-icons": "^3.9.0",
35+
"react-icons": "^4.4.0",
3636
"react-redux": "^7.1.3",
3737
"react-scripts": "3.3.0",
3838
"react-split-pane": "^0.1.89",

src/components/Header.tsx

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,91 @@ import {
2424
import { ExternalLinkIcon, SmallCloseIcon, CheckIcon } from '@chakra-ui/icons'
2525
import { DiGithubBadge } from 'react-icons/di'
2626
import { AiFillThunderbolt } from 'react-icons/ai'
27+
import { SiTypescript } from 'react-icons/si'
2728
import { buildParameters } from '~utils/codesandbox'
2829
import { generateCode } from '~utils/code'
2930
import useDispatch from '~hooks/useDispatch'
3031
import { useSelector } from 'react-redux'
3132
import { getComponents } from '~core/selectors/components'
3233
import { getShowLayout, getShowCode } from '~core/selectors/app'
3334
import HeaderMenu from '~components/headerMenu/HeaderMenu'
35+
import { FaReact } from 'react-icons/fa'
3436

3537
const CodeSandboxButton = () => {
3638
const components = useSelector(getComponents)
3739
const [isLoading, setIsLoading] = useState(false)
3840

41+
const exportToCodeSandbox = async (isTypeScript: boolean) => {
42+
setIsLoading(true)
43+
const code = await generateCode(components)
44+
setIsLoading(false)
45+
const parameters = buildParameters(code, isTypeScript)
46+
47+
window.open(
48+
`https://codesandbox.io/api/v1/sandboxes/define?parameters=${parameters}`,
49+
'_blank',
50+
)
51+
}
52+
3953
return (
40-
<Tooltip
41-
zIndex={100}
42-
hasArrow
43-
bg="yellow.100"
44-
aria-label="Builder mode help"
45-
label="Export in CodeSandbox"
46-
>
47-
<Button
48-
onClick={async () => {
49-
setIsLoading(true)
50-
const code = await generateCode(components)
51-
setIsLoading(false)
52-
const parameters = buildParameters(code)
54+
<Popover>
55+
{({ onClose }) => (
56+
<>
57+
<PopoverTrigger>
58+
<Button
59+
isLoading={isLoading}
60+
rightIcon={<ExternalLinkIcon path="" />}
61+
variant="ghost"
62+
size="xs"
63+
>
64+
Export code
65+
</Button>
66+
</PopoverTrigger>
5367

54-
window.open(
55-
`https://codesandbox.io/api/v1/sandboxes/define?parameters=${parameters}`,
56-
'_blank',
57-
)
58-
}}
59-
isLoading={isLoading}
60-
rightIcon={<ExternalLinkIcon path="" />}
61-
variant="ghost"
62-
size="xs"
63-
>
64-
Export code
65-
</Button>
66-
</Tooltip>
68+
<LightMode>
69+
<PopoverContent zIndex={100} bg="white">
70+
<PopoverArrow />
71+
<PopoverCloseButton />
72+
<PopoverHeader>Export format</PopoverHeader>
73+
<PopoverBody fontSize="sm">
74+
Export the code in CodeSandbox in which format ?
75+
</PopoverBody>
76+
<PopoverFooter display="flex" justifyContent="flex-end">
77+
<Button
78+
size="sm"
79+
mr={2}
80+
variant="ghost"
81+
colorScheme="orange"
82+
rightIcon={<FaReact />}
83+
onClick={async () => {
84+
await exportToCodeSandbox(false)
85+
if (onClose) {
86+
onClose()
87+
}
88+
}}
89+
>
90+
JSX
91+
</Button>
92+
<Button
93+
size="sm"
94+
variant="ghost"
95+
colorScheme="blue"
96+
rightIcon={<SiTypescript />}
97+
onClick={async () => {
98+
await exportToCodeSandbox(true)
99+
if (onClose) {
100+
onClose()
101+
}
102+
}}
103+
>
104+
TSX
105+
</Button>
106+
</PopoverFooter>
107+
</PopoverContent>
108+
</LightMode>
109+
</>
110+
)}
111+
</Popover>
67112
)
68113
}
69114

src/utils/codesandbox.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { getParameters } from 'codesandbox/lib/api/define'
22

3-
export const buildParameters = (code: string): string => {
3+
export const buildParameters = (
4+
code: string,
5+
isTypeScript: boolean,
6+
): string => {
47
return getParameters({
58
files: {
69
'public/index.html': {
@@ -26,7 +29,7 @@ export const buildParameters = (code: string): string => {
2629
</html>`,
2730
isBinary: false,
2831
},
29-
'index.js': {
32+
[isTypeScript ? 'index.tsx' : 'index.js']: {
3033
content: `import React from "react";
3134
import ReactDOM from "react-dom";
3235
@@ -37,7 +40,7 @@ ReactDOM.render(<App />, rootElement);
3740
`,
3841
isBinary: false,
3942
},
40-
'App.js': {
43+
[isTypeScript ? 'App.tsx' : 'App.jsx']: {
4144
content: code,
4245
isBinary: false,
4346
},
@@ -47,7 +50,7 @@ ReactDOM.render(<App />, rootElement);
4750
"version": "1.0.0",
4851
"description": "",
4952
"keywords": [],
50-
"main": "src/index.js",
53+
"main": "src/${isTypeScript ? 'index.tsx' : 'index.js'}",
5154
"dependencies": {
5255
"@chakra-ui/react": "^1.5.0",
5356
"@chakra-ui/icons": "^1.0.9",

yarn.lock

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12952,12 +12952,10 @@ react-hotkeys-hook@^2.4.0:
1295212952
dependencies:
1295312953
hotkeys-js "3.8.1"
1295412954

12955-
react-icons@^3.9.0:
12956-
version "3.9.0"
12957-
resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-3.9.0.tgz#89a00f20a0e02e6bfd899977eaf46eb4624239d5"
12958-
integrity sha512-gKbYKR+4QsD3PmIHLAM9TDDpnaTsr3XZeK1NTAb6WQQ+gxEdJ0xuCgLq0pxXdS7Utg2AIpcVhM1ut/jlDhcyNg==
12959-
dependencies:
12960-
camelcase "^5.0.0"
12955+
react-icons@^4.4.0:
12956+
version "4.4.0"
12957+
resolved "https://registry.yarnpkg.com/react-icons/-/react-icons-4.4.0.tgz#a13a8a20c254854e1ec9aecef28a95cdf24ef703"
12958+
integrity sha512-fSbvHeVYo/B5/L4VhB7sBA1i2tS8MkT0Hb9t2H1AVPkwGfVHLJCqyr2Py9dKMxsyM63Eng1GkdZfbWj+Fmv8Rg==
1296112959

1296212960
react-is@16.13.1, react-is@^16.8.1:
1296312961
version "16.13.1"

0 commit comments

Comments
 (0)