Skip to content

Commit f48397f

Browse files
fix(@clayui/modal): simplifies subportal grouping implementation and removes the extra container
1 parent 733a89f commit f48397f

File tree

2 files changed

+30
-42
lines changed

2 files changed

+30
-42
lines changed

packages/clay-modal/src/Hook.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,13 @@ const FOCUSABLE_ELEMENTS = [
2727
* modal by pressing the ESC key and control the focus within the Modal.
2828
*/
2929
const useUserInteractions = (
30-
elementRef: React.MutableRefObject<any>,
30+
modalElementRef: React.MutableRefObject<any>,
31+
modalBodyElementRef: React.MutableRefObject<any>,
3132
onClick: () => void
3233
) => {
3334
const getFocusableNodes = () => {
34-
if (elementRef.current) {
35-
const nodes = elementRef.current.querySelectorAll(
35+
if (modalBodyElementRef.current) {
36+
const nodes = modalBodyElementRef.current.querySelectorAll(
3637
FOCUSABLE_ELEMENTS
3738
);
3839

@@ -45,11 +46,11 @@ const useUserInteractions = (
4546
const handleKeydown = (event: KeyboardEvent) => {
4647
if (event.keyCode === KEY_CODE_TAB) {
4748
if (
48-
elementRef.current &&
49+
modalElementRef.current &&
4950
event.target !== null &&
50-
!elementRef.current.contains(event.target)
51+
!modalElementRef.current.contains(event.target)
5152
) {
52-
elementRef.current.focus();
53+
modalBodyElementRef.current.focus();
5354
} else {
5455
const focusableNodes = getFocusableNodes();
5556
const focusedItemIndex = focusableNodes.indexOf(
@@ -83,12 +84,7 @@ const useUserInteractions = (
8384
return;
8485
}
8586

86-
if (
87-
elementRef.current &&
88-
event.target !== null &&
89-
!elementRef.current.contains(event.target as HTMLDivElement) &&
90-
document.contains(event.target as HTMLElement)
91-
) {
87+
if (event.target === modalElementRef.current) {
9288
onClick();
9389
}
9490
};

packages/clay-modal/src/Modal.tsx

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import {ClayPortal} from '@clayui/shared';
77
import classNames from 'classnames';
8-
import React from 'react';
8+
import React, {useEffect, useRef, useState} from 'react';
99
import warning from 'warning';
1010

1111
import Body from './Body';
@@ -64,24 +64,22 @@ const ClayModal: React.FunctionComponent<IProps> = ({
6464
status,
6565
...otherProps
6666
}: IProps) => {
67-
const modalBodyElementRef = React.useRef<HTMLDivElement | null>(null);
67+
const modalElementRef = useRef<HTMLDivElement | null>(null);
68+
const modalBodyElementRef = useRef<HTMLDivElement | null>(null);
6869

6970
warning(observer !== undefined, warningMessage);
7071

71-
useUserInteractions(modalBodyElementRef, () =>
72+
useUserInteractions(modalElementRef, modalBodyElementRef, () =>
7273
observer.dispatch(ObserverType.Close)
7374
);
7475

75-
React.useEffect(() => observer.dispatch(ObserverType.Open), []);
76-
77-
// Defines a default Modal size when size is not set.
78-
const maxWidth = size ? {} : {maxWidth: '600px'};
76+
useEffect(() => observer.dispatch(ObserverType.Open), []);
7977

8078
const [show, content] =
8179
observer && observer.mutation ? observer.mutation : [false, false];
8280

8381
return (
84-
<ClayPortal subPortalRef={modalBodyElementRef}>
82+
<ClayPortal subPortalRef={modalElementRef}>
8583
<div
8684
className={classNames('modal-backdrop fade', {
8785
show,
@@ -92,34 +90,28 @@ const ClayModal: React.FunctionComponent<IProps> = ({
9290
className={classNames('fade modal d-block', className, {
9391
show,
9492
})}
93+
ref={modalElementRef}
9594
>
9695
<div
97-
className={classNames({
96+
className={classNames('modal-dialog', {
9897
[`modal-${size}`]: size,
98+
[`modal-${status}`]: status,
99+
'modal-dialog-centered': center,
99100
})}
100101
ref={modalBodyElementRef}
101-
style={{margin: 'auto', ...maxWidth}}
102+
tabIndex={-1}
102103
>
103-
<div
104-
className={classNames('modal-dialog', {
105-
[`modal-${size}`]: size,
106-
[`modal-${status}`]: status,
107-
'modal-dialog-centered': center,
108-
})}
109-
tabIndex={-1}
110-
>
111-
<div className="modal-content">
112-
<Context.Provider
113-
value={{
114-
onClose: () =>
115-
observer.dispatch(ObserverType.Close),
116-
spritemap,
117-
status,
118-
}}
119-
>
120-
{content && children}
121-
</Context.Provider>
122-
</div>
104+
<div className="modal-content">
105+
<Context.Provider
106+
value={{
107+
onClose: () =>
108+
observer.dispatch(ObserverType.Close),
109+
spritemap,
110+
status,
111+
}}
112+
>
113+
{content && children}
114+
</Context.Provider>
123115
</div>
124116
</div>
125117
</div>

0 commit comments

Comments
 (0)