Skip to content

Commit f123196

Browse files
committed
chore: focus refactor
1 parent 2f25bf6 commit f123196

2 files changed

Lines changed: 8 additions & 38 deletions

File tree

src/Dialog/Content/Panel.tsx

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,6 @@ import type { IDialogPropTypes } from '../../IDialogPropTypes';
77
import MemoChildren from './MemoChildren';
88
import pickAttrs from '@rc-component/util/lib/pickAttrs';
99

10-
const sentinelStyle: React.CSSProperties = {
11-
width: 0,
12-
height: 0,
13-
overflow: 'hidden',
14-
outline: 'none',
15-
};
16-
17-
const entityStyle: React.CSSProperties = {
18-
outline: 'none',
19-
};
20-
2110
export interface PanelProps extends Omit<IDialogPropTypes, 'getOpenCount'> {
2211
prefixCls: string;
2312
ariaId?: string;
@@ -28,7 +17,6 @@ export interface PanelProps extends Omit<IDialogPropTypes, 'getOpenCount'> {
2817

2918
export type PanelRef = {
3019
focus: () => void;
31-
changeActive: (next: boolean) => void;
3220
};
3321

3422
const Panel = React.forwardRef<PanelRef, PanelProps>((props, ref) => {
@@ -59,23 +47,14 @@ const Panel = React.forwardRef<PanelRef, PanelProps>((props, ref) => {
5947

6048
// ================================= Refs =================================
6149
const { panel: panelRef } = React.useContext(RefContext);
50+
const internalRef = useRef<HTMLDivElement>(null);
51+
const mergedRef = useComposeRef(holderRef, panelRef, internalRef);
6252

63-
const mergedRef = useComposeRef(holderRef, panelRef);
64-
65-
const sentinelStartRef = useRef<HTMLDivElement>(null);
66-
const sentinelEndRef = useRef<HTMLDivElement>(null);
53+
useLockFocus(visible, () => internalRef.current);
6754

6855
React.useImperativeHandle(ref, () => ({
6956
focus: () => {
70-
sentinelStartRef.current?.focus({ preventScroll: true });
71-
},
72-
changeActive: (next) => {
73-
const { activeElement } = document;
74-
if (next && activeElement === sentinelEndRef.current) {
75-
sentinelStartRef.current.focus({ preventScroll: true });
76-
} else if (!next && activeElement === sentinelStartRef.current) {
77-
sentinelEndRef.current.focus({ preventScroll: true });
78-
}
57+
internalRef.current?.focus({ preventScroll: true });
7958
},
8059
}));
8160

@@ -168,13 +147,11 @@ const Panel = React.forwardRef<PanelRef, PanelProps>((props, ref) => {
168147
className={classNames(prefixCls, className)}
169148
onMouseDown={onMouseDown}
170149
onMouseUp={onMouseUp}
150+
tabIndex={-1}
171151
>
172-
<div ref={sentinelStartRef} tabIndex={0} style={entityStyle}>
173-
<MemoChildren shouldUpdate={visible || forceRender}>
174-
{modalRender ? modalRender(content) : content}
175-
</MemoChildren>
176-
</div>
177-
<div tabIndex={0} ref={sentinelEndRef} style={sentinelStyle} />
152+
<MemoChildren shouldUpdate={visible || forceRender}>
153+
{modalRender ? modalRender(content) : content}
154+
</MemoChildren>
178155
</div>
179156
);
180157
});

src/Dialog/index.tsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
4747

4848
if (process.env.NODE_ENV !== 'production') {
4949
['wrapStyle', 'bodyStyle', 'maskStyle'].forEach((prop) => {
50-
// (prop in props) && console.error(`Warning: ${prop} is deprecated, please use styles instead.`)
5150
warning(!(prop in props), `${prop} is deprecated, please use styles instead.`);
5251
});
5352
if ('wrapClassName' in props) {
@@ -148,11 +147,6 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
148147
onInternalClose(e);
149148
return;
150149
}
151-
152-
// keep focus inside dialog
153-
if (visible && e.keyCode === KeyCode.TAB) {
154-
contentRef.current.changeActive(!e.shiftKey);
155-
}
156150
}
157151

158152
// ========================= Effect =========================
@@ -200,7 +194,6 @@ const Dialog: React.FC<IDialogPropTypes> = (props) => {
200194
className={modalClassNames?.mask}
201195
/>
202196
<div
203-
tabIndex={-1}
204197
onKeyDown={onWrapperKeyDown}
205198
className={classNames(`${prefixCls}-wrap`, wrapClassName, modalClassNames?.wrapper)}
206199
ref={wrapperRef}

0 commit comments

Comments
 (0)