|
1 | 1 |
|
2 |
| -import { Box } from "@hope-ui/solid"; |
3 |
| -import { ParentComponent, onMount } from "solid-js"; |
4 |
| - |
| 2 | +import { Box, IconButton, Tooltip } from "@hope-ui/solid"; |
| 3 | +import { Accessor } from "solid-js"; |
| 4 | +import { createMemo, ParentComponent, Component, JSX, createSignal, children } from "solid-js"; |
| 5 | +import { ResolvedJSXElement } from "solid-js/types/reactive/signal"; |
| 6 | + |
| 7 | + |
| 8 | +const SidebarButton: Component<{ |
| 9 | + label: string; |
| 10 | + onClick: () => void; |
| 11 | + icon: JSX.Element; |
| 12 | + top: string; |
| 13 | + right: string; |
| 14 | +}> = (props) => { |
| 15 | + return ( |
| 16 | + <Tooltip label={props.label}> |
| 17 | + <IconButton |
| 18 | + variant="ghost" |
| 19 | + size="sm" |
| 20 | + position="relative" |
| 21 | + top={props.top} |
| 22 | + right={props.right} |
| 23 | + onClick={props.onClick} |
| 24 | + aria-label={props.label} |
| 25 | + backgroundColor="white" |
| 26 | + icon={props.icon} |
| 27 | + /> |
| 28 | + </Tooltip> |
| 29 | + ); |
| 30 | +}; |
5 | 31 |
|
| 32 | +let x = 0; |
| 33 | +let initialLeftWidth = 0; |
6 | 34 | const SplitView: ParentComponent<{
|
7 | 35 | }> = (props) => {
|
8 |
| - onMount(() => { |
9 |
| - document.addEventListener('DOMContentLoaded', function () { |
10 |
| - // Query the element |
11 |
| - const resizer = document.getElementById('dragMe'); |
12 |
| - const leftSide = resizer.previousElementSibling as HTMLElement; |
13 |
| - const rightSide = resizer.nextElementSibling as HTMLElement; |
14 |
| - |
15 |
| - // The current position of mouse |
16 |
| - let x = 0; |
17 |
| - let y = 0; |
18 |
| - let leftWidth = 0; |
19 |
| - |
20 |
| - // Handle the mousedown event |
21 |
| - // that's triggered when user drags the resizer |
22 |
| - const mouseDownHandler = function (e) { |
23 |
| - // Get the current mouse position |
24 |
| - x = e.clientX; |
25 |
| - y = e.clientY; |
26 |
| - leftWidth = leftSide.getBoundingClientRect().width; |
27 |
| - |
28 |
| - // Attach the listeners to `document` |
29 |
| - document.addEventListener('mousemove', mouseMoveHandler); |
30 |
| - document.addEventListener('mouseup', mouseUpHandler); |
31 |
| - }; |
32 | 36 |
|
33 |
| - const mouseMoveHandler = function (e) { |
34 |
| - // How far the mouse has been moved |
35 |
| - const dx = e.clientX - x; |
36 |
| - const dy = e.clientY - y; |
| 37 | + // Somehow using `HtmlElement` here causes a type error |
| 38 | + let leftSide: any; |
| 39 | + let rightSide: any; |
| 40 | + let resizer: any; |
37 | 41 |
|
38 |
| - const newLeftWidth = ((leftWidth + dx) * 100) / (resizer.parentNode as HTMLElement).getBoundingClientRect().width; |
39 |
| - leftSide.style.width = `${newLeftWidth}%`; |
40 | 42 |
|
41 |
| - resizer.style.cursor = 'col-resize'; |
42 |
| - document.body.style.cursor = 'col-resize'; |
| 43 | + const resolvedChildren = children(() => props.children) as Accessor<ResolvedJSXElement[]>; |
43 | 44 |
|
44 |
| - leftSide.style.userSelect = 'none'; |
45 |
| - leftSide.style.pointerEvents = 'none'; |
46 |
| - |
47 |
| - rightSide.style.userSelect = 'none'; |
48 |
| - rightSide.style.pointerEvents = 'none'; |
49 |
| - }; |
| 45 | + let [isResizing, setIsResizing] = createSignal(false); |
| 46 | + let [leftWidth, setLeftWidth] = createSignal(60); |
| 47 | + let leftStyleGet = createMemo(() => { |
| 48 | + return { |
| 49 | + width: leftWidth() + "%", |
| 50 | + "user-select": isResizing() ? "none" : "user-select", |
| 51 | + "pointer-event": isResizing() ? "none" : "pointer-events" |
| 52 | + } |
| 53 | + }) |
| 54 | + let rightStyleGet = createMemo(() => { |
| 55 | + return { |
| 56 | + "user-select": isResizing() ? "none" : "user-select", |
| 57 | + "pointer-event": isResizing() ? "none" : "pointer-events" |
| 58 | + } |
| 59 | + }) |
| 60 | + let resizerStyleGet = createMemo(() => { |
| 61 | + return isResizing() ? {cursor: 'col-resize'} : undefined |
| 62 | + }) |
50 | 63 |
|
51 |
| - const mouseUpHandler = function () { |
52 |
| - resizer.style.removeProperty('cursor'); |
53 |
| - document.body.style.removeProperty('cursor'); |
54 | 64 |
|
55 |
| - leftSide.style.removeProperty('user-select'); |
56 |
| - leftSide.style.removeProperty('pointer-events'); |
| 65 | + function mouseMoveHandler(e: MouseEvent) { |
| 66 | + const dx = e.clientX - x; |
| 67 | + setLeftWidth(((initialLeftWidth + dx) * 100) / (resizer.parentNode as HTMLElement).getBoundingClientRect().width) |
| 68 | + document.body.style.cursor = 'col-resize'; |
| 69 | + }; |
57 | 70 |
|
58 |
| - rightSide.style.removeProperty('user-select'); |
59 |
| - rightSide.style.removeProperty('pointer-events'); |
| 71 | + function mouseUpHandler () { |
| 72 | + setIsResizing(false); |
| 73 | + document.body.style.removeProperty('cursor'); |
| 74 | + document.removeEventListener('mousemove', mouseMoveHandler); |
| 75 | + document.removeEventListener('mouseup', mouseUpHandler); |
| 76 | + }; |
60 | 77 |
|
61 |
| - // Remove the handlers of `mousemove` and `mouseup` |
62 |
| - document.removeEventListener('mousemove', mouseMoveHandler); |
63 |
| - document.removeEventListener('mouseup', mouseUpHandler); |
64 |
| - }; |
| 78 | + function mouseDownHandler(e: MouseEvent) { |
| 79 | + x = e.clientX; |
| 80 | + initialLeftWidth = leftSide.getBoundingClientRect().width; |
| 81 | + setIsResizing(true); |
| 82 | + document.addEventListener('mousemove', mouseMoveHandler); |
| 83 | + document.addEventListener('mouseup', mouseUpHandler); |
| 84 | + }; |
65 | 85 |
|
66 |
| - // Attach the handler |
67 |
| - resizer.addEventListener('mousedown', mouseDownHandler); |
68 |
| - }); |
69 |
| - }) |
70 | 86 | return (
|
71 |
| - <Box h="$full" class="splitview-container"> |
72 |
| - <div class="splitview-container__left">{props.children[0]}</div> |
73 |
| - <div class="splitview-resizer" id="dragMe"></div> |
74 |
| - <div class="splitview-container__right">{props.children[1]}</div> |
75 |
| - </Box> |
| 87 | + <Box h="$full" class="splitview-container"> |
| 88 | + <div class="splitview-container-left" style={leftStyleGet()} ref={leftSide}>{resolvedChildren()[0]}</div> |
| 89 | + <div class="splitview-resizer" style={resizerStyleGet()} ref={resizer} onMouseDown={mouseDownHandler}></div> |
| 90 | + <div class="splitview-container-right" style={rightStyleGet()} ref={rightSide}>{resolvedChildren()[1]}</div> |
| 91 | + </Box> |
76 | 92 | );
|
77 | 93 | };
|
78 | 94 |
|
|
0 commit comments