Skip to content

Add possibility to disable drawer from context #238

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ This module supports a wide range of drawer styles, and hence has *a lot* of pro
- `useInteractionManager` (Boolean) `false` - if true will run InteractionManager for open/close animations.
- `elevation` (Number) `0` - (Android-only) Sets the elevation of the drawer using Android's underlying [elevation API](https://developer.android.com/training/material/shadows-clipping.html#Elevation)

### External Functions
- `open()` (Function) - Trigger drawer open.
- `close()` (Function) - Trigger drawer close.
- `disableDrawer(disabled: Boolean)` (Function) - Disable or Enable the drawer.

### Tween Handler
You can achieve pretty much any animation you want using the tween handler with the transformMatrix property. E.G.
```js
Expand Down Expand Up @@ -162,6 +167,7 @@ Three options:
contextTypes = {drawer: React.PropTypes.object}
// later...
this.context.drawer.open()
this.context.drawer.disableDrawer(true)
```

### Demo
Expand Down
27 changes: 19 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ export default class Drawer extends Component {
}
};

state = {
viewport: deviceScreen
};

static propTypes = {
acceptDoubleTap: PropTypes.bool,
acceptPan: PropTypes.bool,
Expand Down Expand Up @@ -107,6 +103,15 @@ export default class Drawer extends Component {
getChildContext = () => ({ drawer: this });
/*** END CONTEXT ***/

constructor(props) {
super(props);

this.state = {
viewport: deviceScreen,
disabled: props.disabled,
};
}

_registerChildDrawer(drawer) {
// Store child drawer for gesture disambiguation with multi drawer
// @TODO make cleaner, generalize to work with 3+ drawers, prop to disable/configure
Expand Down Expand Up @@ -234,6 +239,12 @@ export default class Drawer extends Component {
}
};

disableDrawer(disabled) {
this.setState({
disabled,
});
}

shouldOpenDrawer(dx) {
let hasActiveHeading = this._open ^ dx > 0 ^ this.props.side === 'right'
if (!hasActiveHeading) return this._open
Expand All @@ -244,7 +255,7 @@ export default class Drawer extends Component {
this._panning = false
this.shouldOpenDrawer(gestureState.dx) ? this.open() : this.close()
};

onStartShouldSetPanResponderCapture = (e, gestureState) => {
if (this.shouldCaptureGestures()) return this.processShouldSet(e, gestureState)
return false
Expand Down Expand Up @@ -307,7 +318,7 @@ export default class Drawer extends Component {
if (!inMask) return false
if (!this.props.acceptPan) return false

if (!this.props.negotiatePan || this.props.disabled || !this.props.acceptPan || this._panning) return false
if (!this.props.negotiatePan || this.state.disabled || !this.props.acceptPan || this._panning) return false
let swipeToLeft = (gestureState.dx < 0) ? true : false
let swipeToRight = (gestureState.dx > 0) ? true : false
let swipeUpDown = (Math.abs(gestureState.dy) >= Math.abs(gestureState.dx)) ? true : false
Expand Down Expand Up @@ -346,7 +357,7 @@ export default class Drawer extends Component {
}

testPanResponderMask = (e, gestureState) => {
if (this.props.disabled) return false
if (this.state.disabled) return false

// Disable if parent or child drawer exist and are open
// @TODO make cleaner, generalize to work with 3+ drawers, prop to disable/configure
Expand Down Expand Up @@ -398,7 +409,7 @@ export default class Drawer extends Component {
if(typeof type === 'function') {
type() // this is actually a callback
} else cb && cb()

}
})
};
Expand Down