-
Notifications
You must be signed in to change notification settings - Fork 132
/
scrollable.tsx
181 lines (176 loc) · 5.3 KB
/
scrollable.tsx
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
171
172
173
174
175
176
177
178
179
180
181
import cx from 'classnames'
import type { NextPage } from 'next'
import { useRef, useState } from 'react'
import scrollIntoView from 'smooth-scroll-into-view-if-needed'
import Button from '../../docs/fixtures/Button'
import CloseExample from '../../docs/fixtures/CloseExample'
import Code from '../../docs/fixtures/Code'
import Container from '../../docs/fixtures/Container'
import ScrollUp from '../../docs/fixtures/ScrollUp'
import SheetContent from '../../docs/fixtures/SheetContent'
import SnapMarker from '../../docs/fixtures/SnapMarker'
import { scrollable } from '../../docs/headings'
import MetaTags from '../../docs/MetaTags'
import { BottomSheet, BottomSheetRef } from '../../src'
import type { GetStaticProps } from '../_app'
export { getStaticProps } from '../_app'
const rand = (_) => _[~~(Math.random() * _.length)]
const colors = [
'bg-gray-50',
'bg-gray-100',
'bg-gray-200',
'bg-gray-300',
'bg-gray-400',
'bg-gray-500',
'bg-gray-600',
'bg-gray-700',
'bg-gray-800',
'bg-gray-900',
]
const widths = [
'w-1/2',
'w-2/5',
'w-3/5',
'w-1/3',
'w-2/3',
'w-1/4',
'w-3/4',
'w-1/5',
'w-4/5',
'w-1/6',
'w-5/6',
'w-full',
]
const rows = Array.from(Array(20), (_, x) => ({
key: x,
bg: rand(colors),
w: rand(widths),
}))
const ScrollableFixturePage: NextPage<GetStaticProps> = ({
description,
homepage,
meta,
name,
}) => {
const [expandOnContentDrag, setExpandOnContentDrag] = useState(true)
const focusRef = useRef<HTMLButtonElement>()
const sheetRef = useRef<BottomSheetRef>()
return (
<>
<MetaTags
{...meta}
name={name}
description={description}
homepage={homepage}
title={scrollable}
/>
<Container>
<SnapMarker
className="text-white text-opacity-50"
style={{ top: '10vh' }}
/>
<SnapMarker style={{ top: '10vh', ['--size' as any]: '0.5vh' }} />
<SnapMarker
className="text-white text-opacity-50"
style={{ top: '40vh' }}
/>
<SnapMarker style={{ top: '40vh', ['--size' as any]: '0.5vh' }} />
<SnapMarker
className="text-white text-opacity-50"
style={{ top: '75vh' }}
/>
<SnapMarker style={{ top: '75vh', ['--size' as any]: '0.5vh' }} />
<BottomSheet
open
skipInitialTransition
sibling={<CloseExample className="z-10" />}
ref={sheetRef}
initialFocusRef={focusRef}
defaultSnap={({ maxHeight }) => maxHeight / 2}
snapPoints={({ maxHeight }) => [
maxHeight - maxHeight / 10,
maxHeight / 4,
maxHeight * 0.6,
]}
expandOnContentDrag={expandOnContentDrag}
>
<SheetContent>
<div className="grid grid-cols-3 w-full gap-4">
<Button
className={[
' text-sm px2 py-1',
{ 'text-xl': false, 'px-7': false, 'py-3': false },
]}
onClick={() =>
sheetRef.current.snapTo(({ snapPoints }) =>
Math.max(...snapPoints)
)
}
>
Top
</Button>
<Button
ref={focusRef}
className={[
' text-sm px2 py-1',
{ 'text-xl': false, 'px-7': false, 'py-3': false },
]}
onClick={() =>
sheetRef.current.snapTo(({ maxHeight }) => maxHeight / 2)
}
>
Middle
</Button>
<Button
className={[
' text-sm px2 py-1',
{ 'text-xl': false, 'px-7': false, 'py-3': false },
]}
onClick={() =>
sheetRef.current.snapTo(({ snapPoints }) =>
Math.min(...snapPoints)
)
}
>
Bottom
</Button>
</div>
<div className="grid w-full">
<Button
className={[
' text-sm px-2 py-1',
{ 'text-xl': false, 'px-7': false, 'py-3': false },
]}
onClick={() => setExpandOnContentDrag(!expandOnContentDrag)}
>
{expandOnContentDrag ? 'Disable' : 'Enable'} expand on content drag
</Button>
</div>
<p>
The sheet will always try to set initial focus on the first
interactive element it finds.
</p>
<p>If none is found it sets keyboard focus to the container.</p>
<p>
You can override this with <Code>initialFocusRef</Code>.
</p>
{rows.map(({ key, bg, w }) => (
<div
key={`row-${key}`}
className={cx('block rounded-md h-8', bg, w)}
/>
))}
<ScrollUp
className="my-6"
onClick={async () => {
await scrollIntoView(focusRef.current, { block: 'end' })
focusRef.current.focus()
}}
/>
</SheetContent>
</BottomSheet>
</Container>
</>
)
}
export default ScrollableFixturePage