forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
160 lines (141 loc) · 5.03 KB
/
index.js
File metadata and controls
160 lines (141 loc) · 5.03 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import React from 'react';
import SplitPane from 'react-split-pane';
import { GlobalActions, NavigationActions } from '../Actions';
import { Container, PaneContainer, PointerOverlay } from './elements';
export default function SplitView({
showEditor,
showPreview,
isMobile,
sidebarOpen,
children,
showNavigationActions,
refresh,
openInNewWindow,
sandbox,
toggleLike,
initialEditorSize = 50, // in percent
hideDevTools,
...props
}) {
/* Things this component should do
1. set inital size based on props
2. let user move it around
3. snap to edges
4. stay snapped on window.resize and sidebar toggle (not implemented)
5. introduce the resizer element with animation
*/
const windowWidth = document.body.clientWidth;
// TODO: pick this from the sidebar or ref instead of hardcoding
const sidebarWidth = 250;
const maxSize = sidebarOpen ? windowWidth - sidebarWidth : windowWidth;
// #1. set initial size based on props
let initialSize = null;
if (showEditor && showPreview)
initialSize = (initialEditorSize / 100) * maxSize;
else if (showEditor && !showPreview) initialSize = maxSize;
else if (showPreview && !showEditor) initialSize = 0;
const [size, setSize] = React.useState(initialSize);
// #2. We track dragging so that we can add an overlay on top
// of the iframe which lets us keep focus on the resize toggle
const [isDragging, setDragging] = React.useState(false);
// #3. snap to edges, much logic, such wow.
const handleAutomaticSnapping = newSize => {
/* snap threshold on desktop is 50px on the left
and 175px on the right (to keep the open sandbox button on one side)
On mobile, it's 50% of the screen
*/
const leftSnapThreshold = isMobile ? maxSize / 2 : 50;
const rightSnapThreshold = isMobile ? maxSize / 2 : maxSize - 175;
if (newSize === size) {
/* if the size is unchanged, we assume it's a click.
we don't rely on onResizerClick from react-split-pane
because it requires the resizer to have some size which
in our case isn't true because we artificially overlap
the resizer on top of the preview/editor
on mobile, we want to flip the current state.
on desktop, we want to set it half if it's snapped to an edge.
*/
if (isMobile) setSize(size === 0 ? maxSize : 0);
else if (size === 0 || size === maxSize) setSize(maxSize / 2);
} else {
// this means the user was able to drag
// eslint-disable-next-line no-lonely-if
if (newSize < leftSnapThreshold) setSize(0);
else if (newSize > rightSnapThreshold) setSize(maxSize);
else setSize(newSize);
}
};
const onDragStarted = () => setDragging(true);
const onDragFinished = newSize => {
setDragging(false);
// react-split-pane doesn't set newSize until
// the end of the first drag, that breaks the click case
// so we set it to the size that already is set
handleAutomaticSnapping(newSize || size);
};
// TODO: #4. Handle edge case of keeping panes snapped
// on window.resize and sidebar toggle
// #5. Intoduce resizer on first mouse over
// there is no mousover on mobile, so we introduce on load
const [hasAttention, setAttention] = React.useState(!!isMobile);
const [hasBeenIntroduced, setHasBeenIntroduction] = React.useState(false);
const ANIMATION_DURATION = 3000;
const onMouseOver = () => {
// this should run only once
if (hasAttention) return;
setAttention(true);
window.setTimeout(() => {
setHasBeenIntroduction(true);
}, ANIMATION_DURATION);
};
/* We need at least 270px of space in the preview to
fit global actions and navigation actions
if there isn't enough space, navigation gets
depririotized
*/
const outOfSpaceForNavigation = maxSize - size < 270;
return (
<Container
isDragging={isDragging}
size={size}
maxSize={maxSize}
fullSize={size === maxSize}
hasAttention={hasAttention}
onMouseOver={onMouseOver}
onFocus={onMouseOver}
hasBeenIntroduced={hasBeenIntroduced}
>
<GlobalActions
sandbox={sandbox}
toggleLike={toggleLike}
previewVisible={size < maxSize}
isDragging={isDragging}
offsetBottom={!hideDevTools && size < maxSize}
/>
<SplitPane
split="vertical"
onDragStarted={onDragStarted}
onDragFinished={onDragFinished}
minSize="0%"
maxSize="100%"
onMouseEnter={onDragStarted}
size={size}
{...props}
>
<PaneContainer>{children[0]}</PaneContainer>
<PaneContainer>
{showNavigationActions && !outOfSpaceForNavigation ? (
<NavigationActions
refresh={refresh}
openInNewWindow={openInNewWindow}
isDragging={isDragging}
offsetBottom={!hideDevTools && size < maxSize}
/>
) : null}
{isDragging ? <PointerOverlay /> : null}
{children[1]}
</PaneContainer>
</SplitPane>
</Container>
);
}