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
170 lines (149 loc) · 5.08 KB
/
index.js
File metadata and controls
170 lines (149 loc) · 5.08 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
161
162
163
164
165
166
167
168
169
170
import React from 'react';
import SplitPane from 'react-split-pane';
import { isSmallMobileScreen } from 'embed/util/mobile';
import { GlobalActions, NavigationActions } from '../Actions';
import { Container, PaneContainer, RESIZER_WIDTH } from './elements';
export default function SplitView({
showEditor,
showPreview,
isSmallScreen,
sidebarOpen,
children,
showNavigationActions,
refresh,
openInNewWindow,
sandbox,
toggleLike,
initialEditorSize = 50, // in percent
hideDevTools,
setEditorSize,
setDragging: setDraggingProp,
...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) - RESIZER_WIDTH;
// #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);
React.useEffect(() => {
setEditorSize(size);
}, [size, setEditorSize]);
// #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 = isSmallScreen ? maxSize / 2 : 50;
const rightSnapThreshold = isSmallScreen ? 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 (isSmallScreen) 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(Math.min(newSize, maxSize));
}
};
const onDragStarted = () => {
setDragging(true);
setDraggingProp(true);
};
const onDragFinished = newSize => {
setDragging(false);
setDraggingProp(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);
setEditorSize(size);
};
const openEditor = () => {
setSize(maxSize);
};
const openPreview = () => {
setSize(0);
};
// TODO: #4. Handle edge case of keeping panes snapped
// on window.resize and sidebar toggle
/* 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;
/**
* For small touch screens we show different buttons. We change for example that the
* editor can be opened with a button
*/
const smallTouchScreen = isSmallMobileScreen();
return (
<Container
isDragging={isDragging}
size={size}
maxSize={maxSize}
fullSize={size === maxSize}
>
<GlobalActions
sandbox={sandbox}
toggleLike={toggleLike}
previewVisible={size < maxSize}
isDragging={isDragging}
offsetBottom={!hideDevTools && size < maxSize}
openEditor={openEditor}
openPreview={openPreview}
smallTouchScreen={smallTouchScreen}
/>
<SplitPane
split="vertical"
onDragStarted={onDragStarted}
onDragFinished={onDragFinished}
minSize="0%"
maxSize={maxSize}
onMouseEnter={onDragStarted}
size={size}
{...props}
>
<PaneContainer>{children[0]}</PaneContainer>
<PaneContainer>
{showNavigationActions && !outOfSpaceForNavigation ? (
<NavigationActions
refresh={refresh}
openInNewWindow={openInNewWindow}
isDragging={isDragging}
offsetBottom={!hideDevTools && size < maxSize}
/>
) : null}
{children[1]}
</PaneContainer>
</SplitPane>
</Container>
);
}