-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathDrawer.tsx
More file actions
82 lines (70 loc) · 2.57 KB
/
Drawer.tsx
File metadata and controls
82 lines (70 loc) · 2.57 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
import { createContext, createSignal, Show, useContext } from 'solid-js'
import type { Accessor, JSXElement, ParentComponent, Setter, VoidComponent } from 'solid-js'
import IconButton from '~/components/material/IconButton'
import { useDimensions } from '~/utils/window'
interface DrawerContext {
modal: Accessor<boolean>
open: Accessor<boolean>
setOpen: Setter<boolean>
}
const DrawerContext = createContext<DrawerContext>()
export function useDrawerContext() {
const context = useContext(DrawerContext)
if (!context) throw new Error("can't find DrawerContext")
return context
}
export const DrawerToggleButton: VoidComponent = () => {
const { modal, setOpen } = useDrawerContext()
return (
<Show when={modal()}>
<IconButton name="menu" onClick={() => setOpen((prev) => !prev)} />
</Show>
)
}
const PEEK = 56
interface DrawerProps {
drawer: JSXElement
}
const Drawer: ParentComponent<DrawerProps> = (props) => {
const dimensions = useDimensions()
const drawerWidth = () => Math.min(dimensions().width - PEEK, 320)
const modal = () => dimensions().width < 1280
const contentWidth = () => `calc(100% - ${modal() ? 0 : drawerWidth()}px)`
const [open, setOpen] = createSignal(false)
const drawerVisible = () => !modal() || open()
return (
<DrawerContext.Provider value={{ modal, open, setOpen }}>
<nav
class="hide-scrollbar fixed inset-y-0 left-0 h-full touch-pan-y overflow-y-auto overscroll-y-contain transition-drawer ease-in-out duration-300"
style={{
left: drawerVisible() ? 0 : `${-PEEK}px`,
// Opacity should be 0 when drawer is closed, otherwise it can be visible when overscrolling
opacity: drawerVisible() ? 1 : 0,
width: `${drawerWidth()}px`,
}}
>
<div class="flex size-full flex-col rounded-r-lg bg-surface-container-low text-on-surface-variant sm:rounded-r-none">
{props.drawer}
</div>
</nav>
<main
class="absolute inset-y-0 overflow-y-auto bg-background transition-drawer ease-in-out duration-300"
style={{
left: drawerVisible() ? `${drawerWidth()}px` : 0,
width: contentWidth(),
}}
>
{props.children}
<div
class="absolute inset-0 z-[9999] bg-background transition-drawer ease-in-out duration-300"
style={{
'pointer-events': modal() && open() ? 'auto' : 'none',
opacity: modal() && open() ? 0.5 : 0,
}}
onClick={() => setOpen(false)}
/>
</main>
</DrawerContext.Provider>
)
}
export default Drawer