Skip to content

Commit 02d4072

Browse files
committed
fix(ui): change focus after visible change
1 parent 88ed1a9 commit 02d4072

File tree

4 files changed

+41
-53
lines changed

4 files changed

+41
-53
lines changed

packages/ui/src/components/drawer/Drawer.tsx

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,6 @@ export const DDrawer: {
169169
// eslint-disable-next-line react-hooks/exhaustive-deps
170170
}, [visible]);
171171

172-
useEffect(() => {
173-
if (visible) {
174-
dataRef.current.prevActiveEl = document.activeElement as HTMLElement | null;
175-
176-
if (drawerRef.current) {
177-
drawerRef.current.focus({ preventScroll: true });
178-
}
179-
} else if (dataRef.current.prevActiveEl) {
180-
dataRef.current.prevActiveEl.focus({ preventScroll: true });
181-
}
182-
}, [visible]);
183-
184172
const transitionStyles: Partial<Record<DTransitionState, React.CSSProperties>> = (() => {
185173
const transform =
186174
dPlacement === 'top'
@@ -237,9 +225,18 @@ export const DDrawer: {
237225
dDestroyWhenLeaved={dDestroyAfterClose}
238226
afterEnter={() => {
239227
afterVisibleChange?.(true);
228+
229+
dataRef.current.prevActiveEl = document.activeElement as HTMLElement | null;
230+
if (drawerRef.current) {
231+
drawerRef.current.focus({ preventScroll: true });
232+
}
240233
}}
241234
afterLeave={() => {
242235
afterVisibleChange?.(false);
236+
237+
if (dataRef.current.prevActiveEl) {
238+
dataRef.current.prevActiveEl.focus({ preventScroll: true });
239+
}
243240
}}
244241
>
245242
{(state) => (

packages/ui/src/components/image/ImagePreview.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { isUndefined } from 'lodash';
2-
import React, { useEffect, useRef, useState } from 'react';
2+
import React, { useRef, useState } from 'react';
33
import ReactDOM from 'react-dom';
44

55
import { useEvent, useImmer, useIsomorphicLayoutEffect, useLockScroll, useRefExtra } from '@react-devui/hooks';
@@ -142,18 +142,6 @@ export function DImagePreview(props: DImagePreviewProps): JSX.Element | null {
142142

143143
useLockScroll(visible);
144144

145-
useEffect(() => {
146-
if (visible) {
147-
dataRef.current.prevActiveEl = document.activeElement as HTMLElement | null;
148-
149-
if (previewRef.current) {
150-
previewRef.current.focus({ preventScroll: true });
151-
}
152-
} else if (dataRef.current.prevActiveEl) {
153-
dataRef.current.prevActiveEl.focus({ preventScroll: true });
154-
}
155-
}, [visible]);
156-
157145
const listenDragEvent = visible && isDragging;
158146

159147
useEvent<TouchEvent>(
@@ -237,9 +225,18 @@ export function DImagePreview(props: DImagePreviewProps): JSX.Element | null {
237225
dDestroyWhenLeaved
238226
afterEnter={() => {
239227
afterVisibleChange?.(true);
228+
229+
dataRef.current.prevActiveEl = document.activeElement as HTMLElement | null;
230+
if (previewRef.current) {
231+
previewRef.current.focus({ preventScroll: true });
232+
}
240233
}}
241234
afterLeave={() => {
242235
afterVisibleChange?.(false);
236+
237+
if (dataRef.current.prevActiveEl) {
238+
dataRef.current.prevActiveEl.focus({ preventScroll: true });
239+
}
243240
}}
244241
>
245242
{(state) => {

packages/ui/src/components/modal/Modal.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { DModalFooterPrivateProps } from './ModalFooter';
22
import type { DModalHeaderPrivateProps } from './ModalHeader';
33

44
import { isNumber, isString, isUndefined } from 'lodash';
5-
import React, { useEffect, useRef } from 'react';
5+
import React, { useRef } from 'react';
66
import ReactDOM from 'react-dom';
77

88
import { useId, useLockScroll, useRefExtra } from '@react-devui/hooks';
@@ -108,18 +108,6 @@ export const DModal: {
108108

109109
useLockScroll(visible);
110110

111-
useEffect(() => {
112-
if (visible) {
113-
dataRef.current.prevActiveEl = document.activeElement as HTMLElement | null;
114-
115-
if (modalRef.current) {
116-
modalRef.current.focus({ preventScroll: true });
117-
}
118-
} else if (dataRef.current.prevActiveEl) {
119-
dataRef.current.prevActiveEl.focus({ preventScroll: true });
120-
}
121-
}, [visible]);
122-
123111
const headerNode = (() => {
124112
if (dHeader) {
125113
const node = isString(dHeader) ? <DModalHeader>{dHeader}</DModalHeader> : dHeader;
@@ -151,9 +139,18 @@ export const DModal: {
151139
}}
152140
afterEnter={() => {
153141
afterVisibleChange?.(true);
142+
143+
dataRef.current.prevActiveEl = document.activeElement as HTMLElement | null;
144+
if (modalRef.current) {
145+
modalRef.current.focus({ preventScroll: true });
146+
}
154147
}}
155148
afterLeave={() => {
156149
afterVisibleChange?.(false);
150+
151+
if (dataRef.current.prevActiveEl) {
152+
dataRef.current.prevActiveEl.focus({ preventScroll: true });
153+
}
157154
}}
158155
>
159156
{(state) => {

packages/ui/src/components/popover/Popover.tsx

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { DRefExtra } from '@react-devui/hooks/useRefExtra';
44
import type { DPopupPlacement } from '@react-devui/utils/position';
55

66
import { isString, isUndefined } from 'lodash';
7-
import React, { useEffect, useImperativeHandle, useRef, useState } from 'react';
7+
import React, { useImperativeHandle, useRef, useState } from 'react';
88
import ReactDOM from 'react-dom';
99

1010
import { useEvent, useEventCallback, useId, useLockScroll, useRefExtra } from '@react-devui/hooks';
@@ -246,20 +246,6 @@ function Popover(props: DPopoverProps, ref: React.ForwardedRef<DPopoverRef>): JS
246246
!dEscClosable || !visible
247247
);
248248

249-
useEffect(() => {
250-
if (dModal) {
251-
if (visible) {
252-
dataRef.current.prevActiveEl = document.activeElement as HTMLElement | null;
253-
254-
if (popoverRef.current) {
255-
popoverRef.current.focus({ preventScroll: true });
256-
}
257-
} else if (dataRef.current.prevActiveEl) {
258-
dataRef.current.prevActiveEl.focus({ preventScroll: true });
259-
}
260-
}
261-
}, [dModal, visible]);
262-
263249
const headerNode = (() => {
264250
if (dHeader) {
265251
const node = isString(dHeader) ? <DPopoverHeader>{dHeader}</DPopoverHeader> : dHeader;
@@ -311,9 +297,20 @@ function Popover(props: DPopoverProps, ref: React.ForwardedRef<DPopoverRef>): JS
311297
onEnter={updatePosition}
312298
afterEnter={() => {
313299
afterVisibleChange?.(true);
300+
301+
if (dModal) {
302+
dataRef.current.prevActiveEl = document.activeElement as HTMLElement | null;
303+
if (popoverRef.current) {
304+
popoverRef.current.focus({ preventScroll: true });
305+
}
306+
}
314307
}}
315308
afterLeave={() => {
316309
afterVisibleChange?.(false);
310+
311+
if (dModal && dataRef.current.prevActiveEl) {
312+
dataRef.current.prevActiveEl.focus({ preventScroll: true });
313+
}
317314
}}
318315
>
319316
{(state) => {

0 commit comments

Comments
 (0)