-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
index.js
119 lines (111 loc) · 2.56 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// @flow
import * as React from 'react';
import type { Sandbox } from '@codesandbox/common/lib/types';
import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name';
import { embedUrl } from '@codesandbox/common/lib/utils/url-generator';
import track from '@codesandbox/common/lib/utils/analytics';
import {
Button,
Container,
Title,
RightAligned,
CenterAligned,
LeftAligned,
MenuIcon,
HeartButton,
HeartIcon,
LinkIcon,
EditorViewIcon,
SplitViewIcon,
PreviewViewIcon,
} from './elements';
type Props = {
sandbox: Sandbox,
showEditor: boolean,
showPreview: boolean,
setEditorView: () => void,
setPreviewView: () => void,
setMixedView: () => void,
toggleSidebar: () => void,
};
const copyToClipboard = str => {
track('Embed - Copy URL');
const el = document.createElement('textarea');
el.value = str;
el.setAttribute('readonly', '');
el.style.position = 'absolute';
el.style.left = '-9999px';
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
};
function Header({
sandbox,
showEditor,
showPreview,
setEditorView,
setPreviewView,
setMixedView,
toggleSidebar,
toggleLike,
liked,
}: Props) {
const sandboxTitle = getSandboxName(sandbox);
return (
<Container>
<LeftAligned>
<MenuIcon onClick={toggleSidebar} />
<Title title={sandboxTitle}>{sandboxTitle}</Title>
</LeftAligned>
<CenterAligned>
<ModeIcons
showEditor={showEditor}
showPreview={showPreview}
setEditorView={setEditorView}
setPreviewView={setPreviewView}
setMixedView={setMixedView}
/>
</CenterAligned>
<RightAligned>
<Button
onClick={() =>
copyToClipboard(`https://codesandbox.io${embedUrl(sandbox)}`)
}
>
<LinkIcon />
</Button>
{toggleLike && (
<HeartButton onClick={toggleLike} liked={liked}>
<HeartIcon liked={liked} />
</HeartButton>
)}
</RightAligned>
</Container>
);
}
function ModeIcons({
showEditor,
showPreview,
setEditorView,
setMixedView,
setPreviewView,
}) {
return (
<>
<EditorViewIcon
active={showEditor && !showPreview}
onClick={setEditorView}
/>
<SplitViewIcon
active={showEditor && showPreview}
onClick={setMixedView}
/>
<PreviewViewIcon
active={showPreview && !showEditor}
onClick={setPreviewView}
/>
</>
);
}
export default Header;