Skip to content
This repository was archived by the owner on Sep 11, 2019. It is now read-only.

Commit 7446057

Browse files
author
Vladimir Varankin
committed
Overlay: Inherit zIndexGroupLevel from ancestors
fix #123
1 parent c016479 commit 7446057

File tree

5 files changed

+40
-31
lines changed

5 files changed

+40
-31
lines changed

src/Modal/examples.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import React from 'react';
2-
32
import Button from '../Button';
43
import Modal from '../Modal';
4+
import Select from '../Select';
5+
import Item from '../Item';
56

67
class ModalExampleControlled extends React.Component {
78
constructor(props) {
@@ -15,8 +16,13 @@ class ModalExampleControlled extends React.Component {
1516
return (
1617
<div className="example">
1718
<Button theme="islands" size="s" onClick={() => this.onModalAnchorClick()}>Open controlled modal</Button>
18-
<Modal theme="islands" visible={this.state.visible}>
19+
<Modal theme="islands" visible={this.state.visible} zIndexGroupLevel={40}>
1920
<p>I can do this all day.</p>
21+
<Select theme="islands" size="l" placeholder="Select days" value={[12]}>
22+
<Item value="12" checkedText="Dec">December</Item>
23+
<Item value="1" checkedText="Jan">January</Item>
24+
<Item value="2" checkedText="Feb">February</Item>
25+
</Select>
2026
<p>Because we can!</p>
2127
<Button theme="islands" size="l" onClick={() => this.onModalCloseClick()}>Close modal</Button>
2228
</Modal>

src/Overlay/Portal/index.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class Portal extends React.Component {
55
constructor(props) {
66
super(props);
77
this.portalNode = null;
8-
this.isAttachedToPortal = false;
98
}
109

1110
componentDidMount() {
@@ -30,7 +29,6 @@ class Portal extends React.Component {
3029
this.mountPortal();
3130
// NOTE: `ReactDOM.unstable_renderSubtreeIntoContainer` to store intermediate contexts
3231
ReactDOM.unstable_renderSubtreeIntoContainer(this, child, this.portalNode);
33-
this.isAttachedToPortal = true;
3432
} else {
3533
this.unmountPortal();
3634
}
@@ -44,11 +42,8 @@ class Portal extends React.Component {
4442
}
4543

4644
unmountPortal() {
47-
if (this.isAttachedToPortal) {
48-
ReactDOM.unmountComponentAtNode(this.portalNode);
49-
this.isAttachedToPortal = false;
50-
}
5145
if (this.portalNode) {
46+
ReactDOM.unmountComponentAtNode(this.portalNode);
5247
this.getPortalRootNode().removeChild(this.portalNode);
5348
this.portalNode = null;
5449
}

src/Overlay/index.js

+19-15
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,39 @@ class Overlay extends Component {
2828

2929
getChildContext() {
3030
return {
31+
zIndexGroupLevel: this.context.zIndexGroupLevel || this.props.zIndexGroupLevel,
3132
isParentLayerVisible: this.isVisible,
3233
preventParentLayerClickOutside: this.preventClickOutside,
3334
};
3435
}
3536

36-
componentWillMount() {
37+
componentDidMount() {
3738
if (this.props.visible) {
38-
this.layerWillBecomeVisible();
39+
this.layerBecomeVisible();
3940
}
4041
}
4142

42-
componentWillUpdate(nextProps) {
43+
componentDidUpdate({ visible }) {
4344
this.handleParentLayerHide();
44-
// NOTE(narqo@): do this only when visible is going to be changed
45-
if (this.props.visible !== nextProps.visible) {
46-
if (nextProps.visible) {
47-
this.layerWillBecomeVisible();
45+
// NOTE(narqo@): do this only when visible was changed
46+
if (this.props.visible !== visible) {
47+
if (this.props.visible) {
48+
this.layerBecomeVisible();
4849
} else {
49-
this.layerWillBecomeHidden();
50+
this.layerBecomeHidden();
5051
}
5152
}
5253
}
5354

5455
componentWillUnmount() {
5556
this.requestHide(null, false);
56-
this.layerWillBecomeHidden();
57+
this.layerBecomeHidden();
5758
}
5859

59-
layerWillBecomeVisible() {
60+
layerBecomeVisible() {
6061
visibleLayersStack.unshift(this);
6162

62-
this.captureZIndex();
63+
this.acquireZIndex();
6364

6465
document.addEventListener('keydown', this.onDocumentKeyPress);
6566
// NOTE(narqo@): we have to use `nextTick` or nested layer will be closed immediately after being opened
@@ -71,7 +72,7 @@ class Overlay extends Component {
7172
});
7273
}
7374

74-
layerWillBecomeHidden() {
75+
layerBecomeHidden() {
7576
const idx = visibleLayersStack.indexOf(this);
7677
if (idx > -1) {
7778
visibleLayersStack.splice(idx, 1);
@@ -151,8 +152,8 @@ class Overlay extends Component {
151152
}
152153
}
153154

154-
captureZIndex() {
155-
const level = this.props.zIndexGroupLevel;
155+
acquireZIndex() {
156+
const level = this.context.zIndexGroupLevel || this.props.zIndexGroupLevel;
156157

157158
let zIndexes = visibleLayersZIndexes[level];
158159
if (!zIndexes) {
@@ -168,7 +169,8 @@ class Overlay extends Component {
168169
}
169170

170171
releaseZIndex() {
171-
const zIndexes = visibleLayersZIndexes[this.props.zIndexGroupLevel];
172+
const level = this.context.zIndexGroupLevel || this.props.zIndexGroupLevel;
173+
const zIndexes = visibleLayersZIndexes[level];
172174
const idx = zIndexes.indexOf(this.zIndex);
173175
if (idx > -1) {
174176
zIndexes.splice(idx, 1);
@@ -177,12 +179,14 @@ class Overlay extends Component {
177179
}
178180

179181
Overlay.childContextTypes = Overlay.contextTypes = {
182+
zIndexGroupLevel: React.PropTypes.number,
180183
isParentLayerVisible: React.PropTypes.func,
181184
preventParentLayerClickOutside: React.PropTypes.func,
182185
};
183186

184187
Overlay.propsTypes = {
185188
visible: React.PropTypes.bool.isRequired,
189+
zIndexGroupLevel: React.PropTypes.number,
186190
onClick: React.PropTypes.func,
187191
onRequestHide: React.PropTypes.func,
188192
onOrderChange: React.PropTypes.func,

src/Popup/examples.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ class PopupExampleScroll extends React.Component {
119119
};
120120
}
121121

122+
getChildContext() {
123+
return {
124+
zIndexGroupLevel: this.props.zIndexGroupLevel,
125+
};
126+
}
127+
122128
render() {
123129
return (
124130
<div className="example">
@@ -179,14 +185,18 @@ class PopupExampleScroll extends React.Component {
179185
}
180186
}
181187

188+
PopupExampleScroll.childContextTypes = {
189+
zIndexGroupLevel: React.PropTypes.number,
190+
};
191+
182192
function Example() {
183193
return (
184194
<div className="examples">
185195
<PopupExampleSimple />
186196

187197
<PopupExampleNested />
188198

189-
<PopupExampleScroll />
199+
<PopupExampleScroll zIndexGroupLevel={20} />
190200
</div>
191201
);
192202
}

src/Popup/index.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,7 @@ class Popup extends Component {
125125
}
126126

127127
onLayerRequestHide(e, reason) {
128-
if (this.props.visible && this.props.onRequestHide) {
129-
if (reason === 'clickOutside') {
130-
const anchor = this.getAnchor();
131-
if (anchor instanceof Element && anchor.contains(e.target)) {
132-
return;
133-
}
134-
}
128+
if (this.props.visible) {
135129
this.props.onRequestHide(e, reason, this.props);
136130
}
137131
}

0 commit comments

Comments
 (0)