/
Offcanvas.tsx
379 lines (332 loc) · 9.62 KB
/
Offcanvas.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import classNames from 'classnames';
import useBreakpoint from '@restart/hooks/useBreakpoint';
import useEventCallback from '@restart/hooks/useEventCallback';
import PropTypes from 'prop-types';
import * as React from 'react';
import {
useCallback,
useContext,
useEffect,
useMemo,
useRef,
useState,
} from 'react';
import BaseModal, {
ModalProps as BaseModalProps,
ModalHandle,
} from '@restart/ui/Modal';
import Fade from './Fade';
import OffcanvasBody from './OffcanvasBody';
import OffcanvasToggling from './OffcanvasToggling';
import ModalContext from './ModalContext';
import NavbarContext from './NavbarContext';
import OffcanvasHeader from './OffcanvasHeader';
import OffcanvasTitle from './OffcanvasTitle';
import { BsPrefixRefForwardingComponent } from './helpers';
import { useBootstrapPrefix } from './ThemeProvider';
import BootstrapModalManager, {
getSharedManager,
} from './BootstrapModalManager';
export type OffcanvasPlacement = 'start' | 'end' | 'top' | 'bottom';
export interface OffcanvasProps
extends Omit<
BaseModalProps,
| 'role'
| 'renderBackdrop'
| 'renderDialog'
| 'transition'
| 'backdrop'
| 'backdropTransition'
> {
bsPrefix?: string;
backdropClassName?: string;
scroll?: boolean;
placement?: OffcanvasPlacement;
responsive?: 'sm' | 'md' | 'lg' | 'xl' | 'xxl' | string;
renderStaticNode?: boolean;
}
const propTypes = {
/**
* @default 'offcanvas'
*/
bsPrefix: PropTypes.string,
/**
* Include a backdrop component. Specify 'static' for a backdrop that doesn't
* trigger an "onHide" when clicked.
*/
backdrop: PropTypes.oneOf(['static', true, false]),
/**
* Add an optional extra class name to .offcanvas-backdrop.
*/
backdropClassName: PropTypes.string,
/**
* Closes the offcanvas when escape key is pressed.
*/
keyboard: PropTypes.bool,
/**
* Allow body scrolling while offcanvas is open.
*/
scroll: PropTypes.bool,
/**
* Which side of the viewport the offcanvas will appear from.
*/
placement: PropTypes.oneOf<OffcanvasPlacement>([
'start',
'end',
'top',
'bottom',
]),
/**
* Hide content outside the viewport from a specified breakpoint and down.
* @type {("sm"|"md"|"lg"|"xl"|"xxl")}
*/
responsive: PropTypes.string,
/**
* When `true` The offcanvas will automatically shift focus to itself when it
* opens, and replace it to the last focused element when it closes.
* Generally this should never be set to false as it makes the offcanvas less
* accessible to assistive technologies, like screen-readers.
*/
autoFocus: PropTypes.bool,
/**
* When `true` The offcanvas will prevent focus from leaving the offcanvas while
* open. Consider leaving the default value here, as it is necessary to make
* the offcanvas work well with assistive technologies, such as screen readers.
*/
enforceFocus: PropTypes.bool,
/**
* When `true` The offcanvas will restore focus to previously focused element once
* offcanvas is hidden
*/
restoreFocus: PropTypes.bool,
/**
* Options passed to focus function when `restoreFocus` is set to `true`
*
* @link https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus#Parameters
*/
restoreFocusOptions: PropTypes.shape({
preventScroll: PropTypes.bool,
}),
/**
* When `true` The offcanvas will show itself.
*/
show: PropTypes.bool,
/**
* A callback fired when the offcanvas is opening.
*/
onShow: PropTypes.func,
/**
* A callback fired when the header closeButton or backdrop is
* clicked. Required if either are specified.
*/
onHide: PropTypes.func,
/**
* A callback fired when the escape key, if specified in `keyboard`, is pressed.
*/
onEscapeKeyDown: PropTypes.func,
/**
* Callback fired before the offcanvas transitions in
*/
onEnter: PropTypes.func,
/**
* Callback fired as the offcanvas begins to transition in
*/
onEntering: PropTypes.func,
/**
* Callback fired after the offcanvas finishes transitioning in
*/
onEntered: PropTypes.func,
/**
* Callback fired right before the offcanvas transitions out
*/
onExit: PropTypes.func,
/**
* Callback fired as the offcanvas begins to transition out
*/
onExiting: PropTypes.func,
/**
* Callback fired after the offcanvas finishes transitioning out
*/
onExited: PropTypes.func,
/**
* @private
*/
container: PropTypes.any,
/**
* For internal use to render static node from NavbarOffcanvas.
*
* @private
*/
renderStaticNode: PropTypes.bool,
'aria-labelledby': PropTypes.string,
};
const defaultProps: Partial<OffcanvasProps> = {
show: false,
backdrop: true,
keyboard: true,
scroll: false,
autoFocus: true,
enforceFocus: true,
restoreFocus: true,
placement: 'start',
renderStaticNode: false,
};
function DialogTransition(props) {
return <OffcanvasToggling {...props} />;
}
function BackdropTransition(props) {
return <Fade {...props} />;
}
const Offcanvas: BsPrefixRefForwardingComponent<'div', OffcanvasProps> =
React.forwardRef<ModalHandle, OffcanvasProps>(
(
{
bsPrefix,
className,
children,
'aria-labelledby': ariaLabelledby,
placement,
responsive,
/* BaseModal props */
show,
backdrop,
keyboard,
scroll,
onEscapeKeyDown,
onShow,
onHide,
container,
autoFocus,
enforceFocus,
restoreFocus,
restoreFocusOptions,
onEntered,
onExit,
onExiting,
onEnter,
onEntering,
onExited,
backdropClassName,
manager: propsManager,
renderStaticNode,
...props
},
ref,
) => {
const modalManager = useRef<BootstrapModalManager>();
bsPrefix = useBootstrapPrefix(bsPrefix, 'offcanvas');
const { onToggle } = useContext(NavbarContext) || {};
const [showOffcanvas, setShowOffcanvas] = useState(false);
const hideResponsiveOffcanvas = useBreakpoint(
(responsive as any) || 'xs',
'up',
);
useEffect(() => {
// Handles the case where screen is resized while the responsive
// offcanvas is shown. If `responsive` not provided, just use `show`.
setShowOffcanvas(responsive ? show && !hideResponsiveOffcanvas : show);
}, [show, responsive, hideResponsiveOffcanvas]);
const handleHide = useEventCallback(() => {
onToggle?.();
onHide?.();
});
const modalContext = useMemo(
() => ({
onHide: handleHide,
}),
[handleHide],
);
function getModalManager() {
if (propsManager) return propsManager;
if (scroll) {
// Have to use a different modal manager since the shared
// one handles overflow.
if (!modalManager.current)
modalManager.current = new BootstrapModalManager({
handleContainerOverflow: false,
});
return modalManager.current;
}
return getSharedManager();
}
const handleEnter = (node, ...args) => {
if (node) node.style.visibility = 'visible';
onEnter?.(node, ...args);
};
const handleExited = (node, ...args) => {
if (node) node.style.visibility = '';
onExited?.(...args);
};
const renderBackdrop = useCallback(
(backdropProps) => (
<div
{...backdropProps}
className={classNames(`${bsPrefix}-backdrop`, backdropClassName)}
/>
),
[backdropClassName, bsPrefix],
);
const renderDialog = (dialogProps) => (
<div
{...dialogProps}
{...props}
className={classNames(
className,
responsive ? `${bsPrefix}-${responsive}` : bsPrefix,
`${bsPrefix}-${placement}`,
)}
aria-labelledby={ariaLabelledby}
>
{children}
</div>
);
return (
<>
{/*
Only render static elements when offcanvas isn't shown so we
don't duplicate elements.
TODO: Should follow bootstrap behavior and don't unmount children
when show={false} in BaseModal. Will do this next major version.
*/}
{!showOffcanvas &&
(responsive || renderStaticNode) &&
renderDialog({})}
<ModalContext.Provider value={modalContext}>
<BaseModal
show={showOffcanvas}
ref={ref}
backdrop={backdrop}
container={container}
keyboard={keyboard}
autoFocus={autoFocus}
enforceFocus={enforceFocus && !scroll}
restoreFocus={restoreFocus}
restoreFocusOptions={restoreFocusOptions}
onEscapeKeyDown={onEscapeKeyDown}
onShow={onShow}
onHide={handleHide}
onEnter={handleEnter}
onEntering={onEntering}
onEntered={onEntered}
onExit={onExit}
onExiting={onExiting}
onExited={handleExited}
manager={getModalManager()}
transition={DialogTransition}
backdropTransition={BackdropTransition}
renderBackdrop={renderBackdrop}
renderDialog={renderDialog}
/>
</ModalContext.Provider>
</>
);
},
);
Offcanvas.displayName = 'Offcanvas';
Offcanvas.propTypes = propTypes;
Offcanvas.defaultProps = defaultProps;
export default Object.assign(Offcanvas, {
Body: OffcanvasBody,
Header: OffcanvasHeader,
Title: OffcanvasTitle,
});