11import { clsx } from 'clsx' ;
22import type { CSSMotionProps } from '@rc-component/motion' ;
33import CSSMotion from '@rc-component/motion' ;
4- import KeyCode from '@rc-component/util/lib/KeyCode' ;
54import pickAttrs from '@rc-component/util/lib/pickAttrs' ;
65import * as React from 'react' ;
76import type { DrawerContextProps } from './context' ;
@@ -15,14 +14,7 @@ import useDrag from './hooks/useDrag';
1514import { parseWidthHeight } from './util' ;
1615import type { DrawerClassNames , DrawerStyles } from './inter' ;
1716import { useEvent } from '@rc-component/util' ;
18-
19- const sentinelStyle : React . CSSProperties = {
20- width : 0 ,
21- height : 0 ,
22- overflow : 'hidden' ,
23- outline : 'none' ,
24- position : 'absolute' ,
25- } ;
17+ import useFocusable from './hooks/useFocusable' ;
2618
2719export type Placement = 'left' | 'right' | 'top' | 'bottom' ;
2820
@@ -37,9 +29,12 @@ export interface DrawerPopupProps
3729 inline ?: boolean ;
3830 push ?: boolean | PushConfig ;
3931 forceRender ?: boolean ;
40- autoFocus ?: boolean ;
4132 keyboard ?: boolean ;
4233
34+ // Focus
35+ autoFocus ?: boolean ;
36+ focusTrap ?: boolean ;
37+
4338 // Root
4439 rootClassName ?: string ;
4540 rootStyle ?: React . CSSProperties ;
@@ -104,7 +99,10 @@ const DrawerPopup: React.ForwardRefRenderFunction<
10499 inline,
105100 push,
106101 forceRender,
102+
103+ // Focus
107104 autoFocus,
105+ focusTrap,
108106
109107 // classNames
110108 classNames : drawerClassNames ,
@@ -149,39 +147,11 @@ const DrawerPopup: React.ForwardRefRenderFunction<
149147
150148 // ================================ Refs ================================
151149 const panelRef = React . useRef < HTMLDivElement > ( null ) ;
152- const sentinelStartRef = React . useRef < HTMLDivElement > ( null ) ;
153- const sentinelEndRef = React . useRef < HTMLDivElement > ( null ) ;
154150
155151 React . useImperativeHandle ( ref , ( ) => panelRef . current ) ;
156152
157- const onPanelKeyDown : React . KeyboardEventHandler < HTMLDivElement > = event => {
158- const { keyCode, shiftKey } = event ;
159-
160- switch ( keyCode ) {
161- // Tab active
162- case KeyCode . TAB : {
163- if ( keyCode === KeyCode . TAB ) {
164- if ( ! shiftKey && document . activeElement === sentinelEndRef . current ) {
165- sentinelStartRef . current ?. focus ( { preventScroll : true } ) ;
166- } else if (
167- shiftKey &&
168- document . activeElement === sentinelStartRef . current
169- ) {
170- sentinelEndRef . current ?. focus ( { preventScroll : true } ) ;
171- }
172- }
173- break ;
174- }
175- }
176- } ;
177-
178- // ========================== Control ===========================
179- // Auto Focus
180- React . useEffect ( ( ) => {
181- if ( open && autoFocus ) {
182- panelRef . current ?. focus ( { preventScroll : true } ) ;
183- }
184- } , [ open ] ) ;
153+ // ========================= Focusable ==========================
154+ useFocusable ( ( ) => panelRef . current , open , autoFocus , focusTrap , mask ) ;
185155
186156 // ============================ Push ============================
187157 const [ pushed , setPushed ] = React . useState ( false ) ;
@@ -345,9 +315,7 @@ const DrawerPopup: React.ForwardRefRenderFunction<
345315 { ...motionProps }
346316 visible = { open }
347317 forceRender = { forceRender }
348- onVisibleChanged = { nextVisible => {
349- afterOpenChange ?.( nextVisible ) ;
350- } }
318+ onVisibleChanged = { afterOpenChange }
351319 removeOnLeave = { false }
352320 leavedClassName = { `${ prefixCls } -content-wrapper-hidden` }
353321 >
@@ -404,24 +372,9 @@ const DrawerPopup: React.ForwardRefRenderFunction<
404372 style = { containerStyle }
405373 tabIndex = { - 1 }
406374 ref = { panelRef }
407- onKeyDown = { onPanelKeyDown }
408375 >
409376 { maskNode }
410- < div
411- tabIndex = { 0 }
412- ref = { sentinelStartRef }
413- style = { sentinelStyle }
414- aria-hidden = "true"
415- data-sentinel = "start"
416- />
417377 { panelNode }
418- < div
419- tabIndex = { 0 }
420- ref = { sentinelEndRef }
421- style = { sentinelStyle }
422- aria-hidden = "true"
423- data-sentinel = "end"
424- />
425378 </ div >
426379 </ DrawerContext . Provider >
427380 ) ;
0 commit comments