Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Popover] absolute positioning #3144

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/dialog.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,37 @@ const Dialog = React.createClass({
};
},

getInitialState() {
return {
open: this.props.open,
closing: false,
};
},

componentWillReceiveProps(nextProps, nextContext) {
let newMuiTheme = nextContext.muiTheme ? nextContext.muiTheme : this.state.muiTheme;

if (nextProps.open !== this.state.open) {
if (nextProps.open) {
this.setState({
open: true,
closing: false,
muiTheme: newMuiTheme,
});
} else {
this.setState({closing: true});
this._timeout = setTimeout(() => {
if (this.isMounted()) {
this.setState({
open: false,
muiTheme: newMuiTheme,
});
}
}, 500);
}
}
},

renderLayer() {
return (
<DialogInline {...this.props} />
Expand All @@ -520,7 +551,7 @@ const Dialog = React.createClass({

render() {
return (
<RenderToLayer render={this.renderLayer} open={true} useLayerForClickAway={false} />
<RenderToLayer render={this.renderLayer} open={this.state.open} useLayerForClickAway={false} />
);
},

Expand Down
2 changes: 1 addition & 1 deletion src/popover/popover-animation-from-top.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getStyles(props, state) {
opacity: open ? 1 : 0,
transform: open ? 'scaleY(1)' : 'scaleY(0)',
transformOrigin: `${horizontal} ${targetOrigin.vertical}`,
position: 'fixed',
position: 'absolute',
zIndex: zIndex.popover,
transition: Transitions.easeOut('450ms', ['transform', 'opacity']),
maxHeight: '100%',
Expand Down
2 changes: 1 addition & 1 deletion src/popover/popover-default-animation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function getStyles(props, state) {
opacity: open ? 1 : 0,
transform: open ? 'scale(1, 1)' : 'scale(0, 0)',
transformOrigin: `${horizontal} ${targetOrigin.vertical}`,
position: 'fixed',
position: 'absolute',
zIndex: zIndex.popover,
transition: Transitions.easeOut('250ms', ['transform', 'opacity']),
maxHeight: '100%',
Expand Down
4 changes: 2 additions & 2 deletions src/popover/popover.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ const Popover = React.createClass({
const a = {
top: rect.top,
left: rect.left,
width: el.offsetWidth,
height: el.offsetHeight,
width: rect.width,
height: rect.height,
};

a.right = rect.right || a.left + a.width;
Expand Down
28 changes: 22 additions & 6 deletions src/render-to-layer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React from 'react';
import ReactDOM from 'react-dom';
import Dom from './utils/dom';
import getMuiTheme from './styles/getMuiTheme';
import WindowListenable from './mixins/window-listenable';
import throttle from 'lodash.throttle';

// heavily inspired by https://github.com/Khan/react-components/blob/master/js/layered-component-mixin.jsx
const RenderToLayer = React.createClass({
Expand All @@ -22,13 +24,18 @@ const RenderToLayer = React.createClass({
muiTheme: React.PropTypes.object,
},

mixins: [
WindowListenable,
],

getDefaultProps() {
return {
useLayerForClickAway: true,
};
},

getInitialState() {
this._renderLayerThrottled = throttle(this._renderLayer, 100);
return {
muiTheme: this.context.muiTheme || getMuiTheme(),
};
Expand Down Expand Up @@ -61,6 +68,11 @@ const RenderToLayer = React.createClass({
this._unrenderLayer();
},

windowListeners: {
resize: '_renderLayerThrottled',
scroll: '_renderLayerThrottled',
},

onClickAway(event) {
if (event.defaultPrevented) {
return;
Expand Down Expand Up @@ -113,16 +125,12 @@ const RenderToLayer = React.createClass({
if (open) {
if (!this._layer) {
this._layer = document.createElement('div');
this._layer.style.position = 'absolute';
document.body.appendChild(this._layer);

if (this.props.useLayerForClickAway) {
this._layer.addEventListener('touchstart', this.onClickAway);
this._layer.addEventListener('click', this.onClickAway);
this._layer.style.position = 'fixed';
this._layer.style.top = 0;
this._layer.style.bottom = 0;
this._layer.style.left = 0;
this._layer.style.right = 0;
this._layer.style.zIndex = this.state.muiTheme.zIndex.layer;
} else {
setTimeout(() => {
Expand All @@ -131,7 +139,15 @@ const RenderToLayer = React.createClass({
}, 0);
}
}

const {documentElement, body} = document;
const top = window.pageYOffset || documentElement.scrollTop || body.scrollTop || 0;
const left = window.pageXOffset || documentElement.scrollLeft || body.scrollLeft || 0;

this._layer.style.top = `${top}px`;
this._layer.style.height = `${body.clientHeight}px`;
this._layer.style.left = `${left}px`;
this._layer.style.width = `${body.clientWidth}px`;

// By calling this method in componentDidMount() and
// componentDidUpdate(), you're effectively creating a "wormhole" that
// funnels React's hierarchical updates through to a DOM node on an
Expand Down