A state container which provides an interface for applying CSS will-change
styles to its child components when given events fire. This is incredibly useful
because browsers recommend that you use this CSS property sparingly, as it's
a GPU-hog. You should really only be applying it directly before the event
occurs.
yarn add @render-props/will-change
or npm i @render-props/will-change
import WillChange from '@render-props/will-change'
const WillChangeButton = props => (
<WillChange properties={['padding', 'font-size']} onHover>
{({
willChangeRef,
willChange,
style
}) => (
<button
ref={willChangeRef}
style={
{
...style,
transition: 'padding 0.16s ease-out, font-size 0.16s ease-out'
}
}
>
Hover me
</button>
)}
</WillChange>
)
properties {array}
- array of CSS properties that
will-change
when the event occurs
- array of CSS properties that
all {bool}
- if
true
,all
will be the value of thewill-change
property
- if
eventTypes {array}
- names of events to listen for. When these events fire,
will-change
will be applied to the style object
- names of events to listen for. When these events fire,
onTouchStart {bool}
- makes the component listen to the
ontouchstart
event
- makes the component listen to the
onTouchEnd {bool}
- makes the component listen to the
touchend
event
- makes the component listen to the
onTouchMove {bool}
- makes the component listen to the
touchmove
event
- makes the component listen to the
onMouseMove {bool}
- makes the component listen to the
mousemove
event
- makes the component listen to the
onClick {bool}
- makes the component listen to the
mousedown
event
- makes the component listen to the
onHover {bool}
- makes the component listen to the
mouseenter
event
- makes the component listen to the
onMouseEnter {bool}
- makes the component listen to the
mouseenter
event
- makes the component listen to the
onMouseLeave {bool}
- makes the component listen to the
mouseleave
event
- makes the component listen to the
onScroll {bool}
- makes the component listen to the
scroll
event
- makes the component listen to the
onResize {bool}
- makes the component listen to the
resize
event
- makes the component listen to the
onFocus {bool}
- makes the component listen to the
focus
event
- makes the component listen to the
onBlur {bool}
- makes the component listen to the
blur
event
- makes the component listen to the
onDrag {bool}
- makes the component listen to the
dragstart
event
- makes the component listen to the
onDrop {bool}
- makes the component listen to the
drop
event
- makes the component listen to the
staleTimeout {milliseconds}
- after this amount a time
will-change
will be removed from the style property. Defaults to1000
, 1-second.
- after this amount a time
willChangeRef
- This
ref
must be provided to whatever element you are trying to observe the the events of. e.g.<div ref={willChangeRef}>
- This
willChange
()
- turns on the
will-change
style property, regardless of prop-defined events firing<WillChange properties={['background-color']}> {({willChange}) => <div onClick={willChange}/>} </WillChange>
- turns on the
style {object}
- the style object with or without the
will-change
property depending on if it is turned on
- the style object with or without the