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

[Core] Remove click-awayable mixin #3360

Merged
merged 1 commit into from
Feb 19, 2016
Merged
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
57 changes: 57 additions & 0 deletions src/ClickAwayListener.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react';
import ReactDOM from 'react-dom';
import events from './utils/events';

const isDescendant = (el, target) => {
if (target !== null) {
return el === target || isDescendant(el, target.parentNode);
}
return false;
};

const clickAwayEvents = ['mousedown', 'touchstart'];
const bind = (callback) => clickAwayEvents.forEach((event) => events.on(document, event, callback));
const unbind = (callback) => clickAwayEvents.forEach((event) => events.off(document, event, callback));

export default class ClickAwayListener extends React.Component {

static propTypes = {
children: React.PropTypes.node,
onClickAway: React.PropTypes.any,
};

componentDidMount() {
if (this.props.onClickAway) {
bind(this.handleClickAway);
}
}

componentDidUpdate(prevProps) {
if (prevProps.onClickAway !== this.props.onClickAway) {
unbind(this.handleClickAway);
if (this.props.onClickAway) {
bind(this.handleClickAway);
}
}
}

componentWillUnmount() {
unbind(this.handleClickAway);
}

handleClickAway = (event) => {
if (event.defaultPrevented) {
return;
}

const el = ReactDOM.findDOMNode(this);

if (document.documentElement.contains(event.target) && !isDescendant(el, event.target)) {
this.props.onClickAway(event);
}
};

render() {
return this.props.children;
}
}
39 changes: 17 additions & 22 deletions src/menus/menu.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import ReactDOM from 'react-dom';
import update from 'react-addons-update';
import ClickAwayable from '../mixins/click-awayable';
import ClickAwayListener from '../ClickAwayListener';
import autoPrefix from '../styles/auto-prefix';
import Transitions from '../styles/transitions';
import KeyCode from '../utils/key-code';
Expand Down Expand Up @@ -132,10 +132,6 @@ const Menu = React.createClass({
muiTheme: React.PropTypes.object,
},

mixins: [
ClickAwayable,
],

getDefaultProps() {
return {
autoWidth: true,
Expand Down Expand Up @@ -564,24 +560,23 @@ const Menu = React.createClass({
});

return (
<div
onKeyDown={this._handleKeyDown}
style={prepareStyles(mergedRootStyles)}
>
<Paper
ref="scrollContainer"
style={styles.paper}
zDepth={zDepth}
>
<List
{...other}
ref="list"
style={mergedListStyles}
<ClickAwayListener onClickAway={this.componentClickAway}>
<div onKeyDown={this._handleKeyDown} style={prepareStyles(mergedRootStyles)}>
<Paper
ref="scrollContainer"
style={styles.paper}
zDepth={zDepth}
>
{newChildren}
</List>
</Paper>
</div>
<List
{...other}
ref="list"
style={mergedListStyles}
>
{newChildren}
</List>
</Paper>
</div>
</ClickAwayListener>
);
},

Expand Down
43 changes: 0 additions & 43 deletions src/mixins/click-awayable.js

This file was deleted.

3 changes: 0 additions & 3 deletions src/mixins/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import ClickAwayable from './click-awayable';
import StylePropable from './style-propable';
import StyleResizable from './style-resizable';

export {ClickAwayable};
export {StylePropable};
export {StyleResizable};

export default {
ClickAwayable,
StylePropable,
StyleResizable,
};
48 changes: 25 additions & 23 deletions src/snackbar.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import Transitions from './styles/transitions';
import ClickAwayable from './mixins/click-awayable';
import ClickAwayListener from './ClickAwayListener';
import FlatButton from './flat-button';
import getMuiTheme from './styles/getMuiTheme';
import StyleResizable from './mixins/style-resizable';
Expand Down Expand Up @@ -138,7 +138,6 @@ const Snackbar = React.createClass({

mixins: [
StyleResizable,
ClickAwayable,
],

getInitialState() {
Expand All @@ -158,12 +157,8 @@ const Snackbar = React.createClass({

componentDidMount() {
if (this.state.open) {
this._setAutoHideTimer();

//Only Bind clickaway after transition finishes
this.timerTransitionId = setTimeout(() => {
this._bindClickAway();
}, 400);
this.setAutoHideTimer();
this.setTransitionTimer();
}
},

Expand Down Expand Up @@ -200,15 +195,10 @@ const Snackbar = React.createClass({
componentDidUpdate(prevProps, prevState) {
if (prevState.open !== this.state.open) {
if (this.state.open) {
this._setAutoHideTimer();

//Only Bind clickaway after transition finishes
this.timerTransitionId = setTimeout(() => {
this._bindClickAway();
}, 400);
this.setAutoHideTimer();
this.setTransitionTimer();
} else {
clearTimeout(this.timerAutoHideId);
this._unbindClickAway();
}
}
},
Expand All @@ -217,7 +207,6 @@ const Snackbar = React.createClass({
clearTimeout(this.timerAutoHideId);
clearTimeout(this.timerTransitionId);
clearTimeout(this.timerOneAtTheTimeId);
this._unbindClickAway();
},

manuallyBindClickAway: true,
Expand All @@ -227,14 +216,17 @@ const Snackbar = React.createClass({
timerOneAtTheTimeId: undefined,

componentClickAway() {
if (this.timerTransitionId) return; // If transitioning, don't close snackbar

if (this.props.open !== null && this.props.onRequestClose) {
this.props.onRequestClose('clickaway');
} else {
this.setState({open: false});
}
},

_setAutoHideTimer() {
// Timer that controls delay before snackbar auto hides
setAutoHideTimer() {
const autoHideDuration = this.props.autoHideDuration;

if (autoHideDuration > 0) {
Expand All @@ -249,6 +241,13 @@ const Snackbar = React.createClass({
}
},

// Timer that controls delay before click-away events are captured (based on when animation completes)
setTransitionTimer() {
this.timerTransitionId = setTimeout(() => {
this.timerTransitionId = undefined;
}, 400);
},

render() {
const {
onActionTouchTap,
Expand All @@ -263,6 +262,7 @@ const Snackbar = React.createClass({
muiTheme: {
prepareStyles,
},
open,
} = this.state;

const styles = getStyles(this.props, this.state);
Expand All @@ -276,14 +276,16 @@ const Snackbar = React.createClass({
);

return (
<div {...others} style={prepareStyles(Object.assign(styles.root, style))}>
<div style={prepareStyles(Object.assign(styles.body, bodyStyle))}>
<div style={prepareStyles(styles.content)}>
<span>{message}</span>
{actionButton}
<ClickAwayListener onClickAway={open && this.componentClickAway}>
<div {...others} style={prepareStyles(Object.assign(styles.root, style))}>
<div style={prepareStyles(Object.assign(styles.body, bodyStyle))}>
<div style={prepareStyles(styles.content)}>
<span>{message}</span>
{actionButton}
</div>
</div>
</div>
</div>
</ClickAwayListener>
);
},

Expand Down
14 changes: 6 additions & 8 deletions src/table/table-body.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import Checkbox from '../checkbox';
import TableRowColumn from './table-row-column';
import ClickAwayable from '../mixins/click-awayable';
import ClickAwayListener from '../ClickAwayListener';
import getMuiTheme from '../styles/getMuiTheme';

const TableBody = React.createClass({
Expand Down Expand Up @@ -121,10 +121,6 @@ const TableBody = React.createClass({
muiTheme: React.PropTypes.object,
},

mixins: [
ClickAwayable,
],

getDefaultProps() {
return {
allRowsSelected: false,
Expand Down Expand Up @@ -419,9 +415,11 @@ const TableBody = React.createClass({
const rows = this._createRows();

return (
<tbody className={className} style={prepareStyles(Object.assign({}, style))}>
{rows}
</tbody>
<ClickAwayListener onClickAway={this.componentClickAway}>
<tbody className={className} style={prepareStyles(Object.assign({}, style))}>
{rows}
</tbody>
</ClickAwayListener>
);
},

Expand Down