This repository has been archived by the owner on Jan 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
feat(framework-examples): Add ripple support to React checkbox example #233
Merged
traviskaufman
merged 3 commits into
material-components:master
from
codesuki:react-checkbox-ripple
Feb 3, 2017
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,13 +23,23 @@ | |
*/ | ||
|
||
import React, {PureComponent, PropTypes} from 'react'; | ||
import {Set as ImmutableSet} from 'immutable'; | ||
import {Set as ImmutableSet, Map as ImmutableMap} from 'immutable'; | ||
// Temporarily using relative reference until we publish on npm. | ||
import {getCorrectEventName} from '@material/animation/dist/mdc.animation'; | ||
import {MDCRipple, MDCRippleFoundation} from '@material/ripple/dist/mdc.ripple'; | ||
import {MDCCheckboxFoundation} from '@material/checkbox/dist/mdc.checkbox'; | ||
import '@material/checkbox/dist/mdc.checkbox.css'; | ||
|
||
function getMatchesProperty(HTMLElementPrototype) { | ||
return [ | ||
'webkitMatchesSelector', 'msMatchesSelector', 'matches', | ||
].filter((p) => p in HTMLElementPrototype).pop(); | ||
} | ||
|
||
const {ANIM_END_EVENT_NAME} = MDCCheckboxFoundation.strings; | ||
|
||
const MATCHES = getMatchesProperty(HTMLElement.prototype); | ||
|
||
export default class Checkbox extends PureComponent { | ||
static propTypes = { | ||
id: PropTypes.string, | ||
|
@@ -47,11 +57,10 @@ export default class Checkbox extends PureComponent { | |
|
||
state = { | ||
classes: new ImmutableSet(), | ||
css: new ImmutableMap(), | ||
checkedInternal: false, | ||
indeterminateInternal: false | ||
} | ||
classesToAdd = new ImmutableSet(); | ||
classesToRemove = new ImmutableSet(); | ||
|
||
// Here we initialize a foundation class, passing it an adapter which tells it how to | ||
// work with the React component in an idiomatic way. | ||
|
@@ -64,12 +73,12 @@ export default class Checkbox extends PureComponent { | |
})), | ||
registerAnimationEndHandler: handler => { | ||
if (this.refs.root) { | ||
this.refs.root.addEventListener(ANIM_END_EVENT_NAME, handler); | ||
this.refs.root.addEventListener(getCorrectEventName(window, 'animationend'), handler); | ||
} | ||
}, | ||
deregisterAnimationEndHandler: handler => { | ||
if (this.refs.root) { | ||
this.refs.root.removeEventListener(ANIM_END_EVENT_NAME, handler); | ||
this.refs.root.removeEventListener(getCorrectEventName(window, 'animationend'), handler) | ||
} | ||
}, | ||
registerChangeHandler: handler => { | ||
|
@@ -101,6 +110,45 @@ export default class Checkbox extends PureComponent { | |
isAttachedToDOM: () => Boolean(this.refs.nativeCb), | ||
}); | ||
|
||
// For browser compatibility we extend the default adapter which checks for css variable support. | ||
rippleFoundation = new MDCRippleFoundation(Object.assign(MDCRipple.createAdapter(this), { | ||
isUnbounded: () => true, | ||
isSurfaceActive: () => this.refs.root[MATCHES](':active'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what you actually want to check is that the input element is active, rather than the root. See #241 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense! I was wondering why it's the root. I was using the native js version that you fixed as my guide. Thanks for clearing that up. |
||
addClass: className => { | ||
this.setState(prevState => ({ | ||
classes: prevState.classes.add(className) | ||
})); | ||
}, | ||
removeClass: className => { | ||
this.setState(prevState => ({ | ||
classes: prevState.classes.remove(className) | ||
})); | ||
}, | ||
registerInteractionHandler: (evtType, handler) => { | ||
this.refs.nativeCb.addEventListener(evtType, handler); | ||
}, | ||
deregisterInteractionHandler: (evtType, handler) => { | ||
this.refs.nativeCb.removeEventListener(evtType, handler); | ||
}, | ||
updateCssVariable: (varName, value) => { | ||
this.setState(prevState => ({ | ||
css: prevState.css.set(varName, value) | ||
})); | ||
}, | ||
computeBoundingRect: () => { | ||
const {left, top} = this.refs.root.getBoundingClientRect(); | ||
const DIM = 40; | ||
return { | ||
top, | ||
left, | ||
right: left + DIM, | ||
bottom: top + DIM, | ||
width: DIM, | ||
height: DIM, | ||
}; | ||
}, | ||
})); | ||
|
||
render() { | ||
// Within render, we generate the html needed to render a proper MDC-Web checkbox. | ||
return ( | ||
|
@@ -138,8 +186,10 @@ export default class Checkbox extends PureComponent { | |
// so that proper work can be performed. | ||
componentDidMount() { | ||
this.foundation.init(); | ||
this.rippleFoundation.init(); | ||
} | ||
componentWillUnmount() { | ||
this.rippleFoundation.destroy(); | ||
this.foundation.destroy(); | ||
} | ||
|
||
|
@@ -160,5 +210,11 @@ export default class Checkbox extends PureComponent { | |
if (this.refs.nativeCb) { | ||
this.refs.nativeCb.indeterminate = this.state.indeterminateInternal; | ||
} | ||
// To make the ripple animation work we update the css properties after React finished building the DOM. | ||
if (this.refs.root) { | ||
this.state.css.forEach((v, k) => { | ||
this.refs.root.style.setProperty(k, v); | ||
}); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perhaps it would be better to call this
rippleCss?