/
DrawerBase.tsx
144 lines (127 loc) · 3.87 KB
/
DrawerBase.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
/** @jsx jsx */
import { Fragment, KeyboardEvent, MutableRefObject, ReactNode, useCallback, useRef } from 'react';
import FocusLock from 'react-focus-lock';
import { RemoveScroll } from 'react-remove-scroll';
import { makeId, useId, useTheme, Portal, jsx } from '@keystone-ui/core';
import { Blanket } from './Blanket';
import { useDrawerManager } from './drawer-context';
import { TransitionState } from './types';
import { DrawerControllerContextProvider } from './DrawerController';
const widths = {
narrow: 448,
wide: 720,
};
const easing = 'cubic-bezier(0.2, 0, 0, 1)';
export type DrawerBaseProps = {
children: ReactNode;
initialFocusRef?: MutableRefObject<any>;
onClose: () => void;
transitionState: TransitionState;
onSubmit?: () => void;
width?: keyof typeof widths;
};
const blanketTransition = {
entering: { opacity: 0 },
entered: { opacity: 1 },
exiting: { opacity: 0 },
exited: { opacity: 0 },
};
export const DrawerBase = ({
children,
initialFocusRef,
onClose,
onSubmit,
width = 'narrow',
transitionState,
...props
}: DrawerBaseProps) => {
const theme = useTheme();
const containerRef = useRef(null);
const id = useId();
const uniqueKey = makeId('drawer', id);
// sync drawer state
let drawerDepth = useDrawerManager(uniqueKey);
const onKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape' && !event.defaultPrevented) {
event.preventDefault();
onClose();
}
};
const activateFocusLock = useCallback(() => {
if (initialFocusRef && initialFocusRef.current) {
initialFocusRef.current.focus();
}
}, [initialFocusRef]);
const dialogTransition = getDialogTransition(drawerDepth);
let Tag: 'div' | 'form' = 'div';
if (onSubmit) {
Tag = 'form';
let oldOnSubmit = onSubmit;
// @ts-ignore
onSubmit = (event: any) => {
if (!event.defaultPrevented) {
event.preventDefault();
oldOnSubmit();
}
};
}
return (
<Portal>
<Fragment>
<Blanket
onClick={onClose}
style={{
transition: `opacity 150ms linear`,
...blanketTransition[transitionState],
zIndex: theme.elevation.e400,
}}
/>
<FocusLock autoFocus returnFocus onActivation={activateFocusLock}>
<RemoveScroll enabled>
<Tag
onSubmit={onSubmit}
aria-modal="true"
role="dialog"
ref={containerRef}
tabIndex={-1}
onKeyDown={onKeyDown}
style={dialogTransition[transitionState]}
css={{
backgroundColor: theme.colors.background,
borderRadius: theme.radii.large,
bottom: theme.spacing.small,
boxShadow: theme.shadow.s400,
position: 'fixed',
right: theme.spacing.small,
top: theme.spacing.small,
transition: `transform 150ms ${easing}`,
width: widths[width],
zIndex: theme.elevation.e400,
// flex layout must be applied here so content will grow/shrink properly
display: 'flex',
flexDirection: 'column',
}}
{...props}
>
<DrawerControllerContextProvider value={null}>
{children}
</DrawerControllerContextProvider>
</Tag>
</RemoveScroll>
</FocusLock>
</Fragment>
</Portal>
);
};
// Utils
// ------------------------------
function getDialogTransition(depth: number) {
let scaleInc = 0.05;
let transformValue = `scale(${1 - scaleInc * depth}) translateX(-${depth * 40}px)`;
return {
entering: { transform: 'translateX(100%)' },
entered: { transform: transformValue },
exiting: { transform: 'translateX(100%)' },
exited: { transform: 'translateX(100%)' },
};
}