Helper for creating tappable components.
It allows to handle pressed, hovered and focused states consistently across different input methods - mouse, touch and keyboard.
- Fixes sticky hover and pressed state for touch
- Prevents focus when using mouse and touch, but still allows to focus using keyboard
npm install taply
import Taply from 'taply'
const tappableElement = (
<Taply
onTap={() => console.log('Tap')}
onChangeTapState={tapState => {
// tapState is an object with keys 'isHovered', 'isPressed' and 'isFocused'
this.setState(tapState)
}}
>
<div>Tap me</div>
</Taply>
)
// Any component can be a child (React components should forward ref to inner DOM-element)
const tappableComponent = (
<Taply {...props}>
<SomeComponent />
</Taply>
)
// Also, you can use function that takes tapState and returns element
const tappableFn = (
<Taply {...props}>
{(tapState, ref) => (
<div style={{ color: tapState.hovered ? 'red' : 'black' }} ref={ref}>Tap me</div>
)}
</Taply>
)
Type: element|(tapState: object, ref) => element
Tap state is an object with following properties: isHovered
, isPressed
and isFocused
.
Type: (event) => void
Tap event handler.
Type: (tapState) => void
Handler of state changes.
Type: (tap: object) => void
Type: (tap: object) => void
Type: (event: object) => void
Focus event handler.
Type: (event: object) => void
Blur event handler.
Type: bool
Disables events handling.
Type: bool
Default: true
Type: string|number
Default: 0
HTML tabindex
attribute.
Type: bool
Default: true
Type: bool
Default: true
Taply sets tabIndex
and disabled
attributes on the html-element.
When you have multiple nested Taply
components, it is ambigious which one
should control attributes, so you can disable it.