Skip to content

Commit 1f5701a

Browse files
committed
feat: improve sibling handling and joil foc option
1 parent bba1ef9 commit 1f5701a

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

src/Modal/index.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let id = 0;
99
const ESC = 27;
1010

1111
export interface IModalProps extends IOverlayProps {
12+
dontLockFocus?: boolean;
1213
onEsc?: (event: KeyboardEvent) => void;
1314
onClose?: () => void;
1415
}
@@ -49,20 +50,28 @@ export class Modal extends Component<IModalProps, IModalState> {
4950

5051
for (let i = 0; i < siblings.length; i++) {
5152
const sibling = siblings[i] as HTMLElement;
53+
const sib = sibling as any;
5254

5355
if (sibling === this.el) {
5456
continue;
5557
}
5658

57-
if ((sibling as any).__modal_lock !== this) {
59+
if (!sib.__libreact_lock) {
5860
continue;
5961
}
6062

61-
delete (sibling as any).__modal_lock;
62-
(sibling as any).inert = false;
63-
sibling.style.removeProperty('pointer-events');
64-
sibling.style.removeProperty('user-select');
63+
if (sib.__libreact_lock.owner !== this) {
64+
continue;
65+
}
66+
67+
const lock = sib.__libreact_lock;
68+
69+
sib.inert = lock.inert;
70+
sibling.style.setProperty('pointer-events', lock.pointerEvents),
71+
sibling.style.setProperty('user-select', lock.userSelect),
72+
6573
sibling.removeAttribute('aria-hidden');
74+
delete sib.__libreact_lock;
6675
}
6776

6877
// Focus previously active element.
@@ -90,12 +99,24 @@ export class Modal extends Component<IModalProps, IModalState> {
9099
continue;
91100
}
92101

93-
if ((sibling as any).__modal_lock) {
102+
if ((sibling as any).__libreact_lock) {
103+
continue;
104+
}
105+
106+
if (sibling.hasAttribute('aria-hidden')) {
94107
continue;
95108
}
96109

97-
(sibling as any).__modal_lock = this;
98-
(sibling as any).inert = true;
110+
const sib = sibling as any;
111+
112+
sib.__libreact_lock = {
113+
owner: this,
114+
inert: sib.inert,
115+
pointerEvents: sibling.style.getPropertyValue('pointer-events'),
116+
userSelect: sibling.style.getPropertyValue('user-select'),
117+
};
118+
119+
sib.inert = true;
99120
sibling.style.setProperty('pointer-events', 'none');
100121
sibling.style.setProperty('user-select', 'none');
101122
sibling.setAttribute('aria-hidden', 'true');
@@ -111,17 +132,20 @@ export class Modal extends Component<IModalProps, IModalState> {
111132
};
112133

113134
render () {
114-
const {color, time, onClick} = this.props;
135+
const {color, dontLockFocus, time, onClick} = this.props;
136+
let element = render(this.props, this.state);
137+
138+
if (!dontLockFocus) {
139+
element = h(FocusLock, null, element);
140+
}
115141

116142
return h(Overlay, {
117143
color,
118144
time,
119145
onClick,
120146
onElement: this.onElement,
121147
},
122-
h(FocusLock, null,
123-
render(this.props, this.state)
124-
)
148+
element
125149
);
126150
}
127151
}

0 commit comments

Comments
 (0)