Last active
January 1, 2024 17:32
-
-
Save necolas/1c494e44e23eb7f8c5864a2fac66299a to your computer and use it in GitHub Desktop.
Hover styles in React Native for Web
This file contains hidden or 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
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment'; | |
/** | |
* Touch devices emulate mouse events. This functions makes it possible to know | |
* if the current modality supports hover (including for multi-modality | |
* devices). | |
*/ | |
const createHoverMonitor = () => { | |
let isHoverEnabled = false; | |
let lastTouchTime = 0; | |
function enableHover() { | |
if (isHoverEnabled || Date.now() - lastTouchTime < 500) { | |
return; | |
} | |
isHoverEnabled = true; | |
} | |
function disableHover() { | |
lastTouchTime = new Date(); | |
if (isHoverEnabled) { | |
isHoverEnabled = false; | |
} | |
} | |
if (canUseDOM) { | |
document.addEventListener('touchstart', disableHover, true); | |
document.addEventListener('mousemove', enableHover, true); | |
} | |
return { | |
get isEnabled() { | |
return isHoverEnabled; | |
} | |
}; | |
}; | |
export default createHoverMonitor; |
This file contains hidden or 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
import createHoverMonitor from './createHoverMonitor'; | |
import { element, func, oneOfType } from 'prop-types'; | |
import React, { Component } from 'react'; | |
const hover = createHoverMonitor(); | |
/** | |
* Use: | |
* <Hoverable> | |
* {(hover) => <View style={hover && styles.hovered} />} | |
* </Hoverable> | |
* | |
* Example: https://imaginary-lycra.glitch.me/ | |
* Example source: https://glitch.com/edit/#!/imaginary-lycra | |
*/ | |
class Hoverable extends Component { | |
static displayName = 'Hoverable'; | |
static propTypes = { | |
children: oneOfType([func, element]).isRequired, | |
onHoverIn: func, | |
onHoverOut: func | |
}; | |
state = { isHovered: false }; | |
_handleMouseEnter = e => { | |
if (hover.isEnabled && !this.state.isHovered) { | |
const { onHoverIn } = this.props; | |
if (onHoverIn) { | |
onHoverIn(); | |
} | |
this.setState(() => ({ isHovered: true })); | |
} | |
}; | |
_handleMouseLeave = e => { | |
if (this.state.isHovered) { | |
const { onHoverOut } = this.props; | |
if (onHoverOut) { | |
onHoverOut(); | |
} | |
this.setState(() => ({ isHovered: false })); | |
} | |
}; | |
render() { | |
const { | |
children, | |
/* eslint-disable */ | |
onHoverIn, | |
onHoverOut | |
/* eslint-enable */ | |
} = this.props; | |
const child = typeof children === 'function' ? children(this.state.isHovered) : children; | |
return React.cloneElement(React.Children.only(child), { | |
onMouseEnter: this._handleMouseEnter, | |
onMouseLeave: this._handleMouseLeave | |
}); | |
} | |
} | |
export default Hoverable; |
I think onHoverIn/Out is a better version of that which accounts for other conditions.
@nandorojo If you look closely at the gist, you will see that the hover is accomplished using the same approach. Only instead of appending the onMouseEnter
and onMouseLeave
props to the child and rendering the child using the render props pattern, the example I shared adds onMouseEnter
and onMouseLeave
explicitly.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In the code I'm working in, the render props pattern is something that is used seldomly. Instead a Pressable is used as a controlled component like so:
In this case, I was able to replace the
onHoverIn
andonHoverOut
props withonMouseEnter
andonMouseLeave
and it works like a charm on react-native-web.