-
Notifications
You must be signed in to change notification settings - Fork 1
/
modal.tsx
173 lines (160 loc) · 4.26 KB
/
modal.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
import {
forwardRef,
useEffect,
useRef,
type FC,
type ForwardedRef,
type PropsWithChildren,
type ReactNode,
} from 'react';
import { createPortal } from 'react-dom';
import { Box, type BoxProps } from './box.js';
import { ButtonIcon } from './button.js';
import { DesignSystem } from './design-system.js';
import { useDesignSystem } from './hooks/use-design-system.js';
import { useStringLikeDetector } from './hooks/use-string-like.js';
import { CloseIcon } from './icons.js';
import { Block, Flex, Inline } from './layout.js';
import {
commonDimensionsClass,
dialogClass,
modalClassName,
modalInnerClassName,
} from './modal.css.js';
import type { Falsy, Merge } from './types.js';
import { Heading } from './typography.js';
type InnerProps<T extends string | 'dismiss' = 'dismiss'> = PropsWithChildren<{
close: (returnValue: T | 'dismiss') => void;
heading?: ReactNode | Falsy;
dismissable?: true | Falsy;
lightDismiss?: true | Falsy;
}>;
export type DialogProps<
T extends string,
C extends 'dialog' | 'div' = 'dialog',
> = Merge<
BoxProps<C>,
InnerProps<T | 'dismiss'> & {
show: () => void;
}
>;
const ModalInner: FC<InnerProps> = ({
children,
dismissable = true,
lightDismiss,
close,
heading,
...props
}) => {
const isStringLike = useStringLikeDetector();
return (
<Block
component="section"
padding="8"
className={modalInnerClassName}
{...props}
>
<Inline flexWrap="nowrap">
{isStringLike(heading) ? <Heading>{heading}</Heading> : heading}
{dismissable && (
<Inline component="form" method="dialog" justifySelf="end">
<ButtonIcon
onClick={() => close('dismiss')}
type="submit"
variant="invisible"
value="close"
label="close"
autoFocus={false}
icon={<CloseIcon />}
/>
</Inline>
)}
</Inline>
<Block flexGrow>{children}</Block>
</Block>
);
};
export const Modal = forwardRef(
<T extends string>(
{
children,
className,
close,
show,
heading,
...props
}: DialogProps<T, 'div'>,
forwardedRef: ForwardedRef<HTMLDivElement | null>,
) => {
const ds = useDesignSystem();
const { dismissable = true, lightDismiss = dismissable } = props;
useEffect(() => {
if (dismissable) {
const escapeHandler = (e: KeyboardEvent) => {
if (e.key === 'Escape') {
close('dismiss');
}
};
window.addEventListener('keyup', escapeHandler);
return (): void => {
window.removeEventListener('keyup', escapeHandler);
};
}
return () => {};
}, [close, dismissable]);
const backdropRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const el = backdropRef.current;
if (lightDismiss && el) {
const handler = (e: MouseEvent) => {
if (e.target === e.currentTarget) {
close('dismiss');
}
};
el.addEventListener('click', handler);
return (): void => {
el.removeEventListener('click', handler);
};
}
return () => {};
}, [close, lightDismiss]);
return createPortal(
<DesignSystem {...ds} integrationMode>
<Flex
justifyContent="center"
className={modalClassName}
ref={backdropRef}
>
<Block
ref={forwardedRef}
className={[className, commonDimensionsClass]}
>
<ModalInner {...{ children, close, heading }} {...props} />
</Block>
</Flex>
</DesignSystem>,
document.body,
);
},
);
export const Dialog = forwardRef(
<T extends string>(
{ children, className, close, show, heading, ...props }: DialogProps<T>,
ref: ForwardedRef<HTMLDialogElement | null>,
) => {
const ds = useDesignSystem();
return createPortal(
<DesignSystem {...ds} integrationMode>
<Box
{...props}
component="dialog"
className={[className, commonDimensionsClass, dialogClass]}
ref={ref}
>
<ModalInner {...{ children, close, heading }} />
</Box>
</DesignSystem>,
document.body,
);
},
);