Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: add hover support to web#3751
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import type * as React from 'react'; | ||
| import type { | ||
| PressableProps as PressableNativeProps, | ||
| StyleProp, | ||
| View, | ||
| ViewStyle, | ||
| } from 'react-native'; | ||
| import { Pressable as PressableNative } from 'react-native'; | ||
| // This component is added to support type-safe hover and focus states on web | ||
| // https://necolas.github.io/react-native-web/docs/pressable/ | ||
| export type PressableStateCallbackType = { | ||
| pressed: boolean; | ||
| focused: boolean; | ||
| hovered: boolean; | ||
| }; | ||
| export type PressableProps = Omit< | ||
| PressableNativeProps, | ||
| 'children' | 'style' | ||
| > & { | ||
| children: | ||
| | React.ReactNode | ||
| | ((state: PressableStateCallbackType) => React.ReactNode) | ||
| | undefined; | ||
| style?: | ||
| | StyleProp<ViewStyle> | ||
| | ((state: PressableStateCallbackType) => StyleProp<ViewStyle>) | ||
| | undefined; | ||
| }; | ||
| export const Pressable: React.ForwardRefExoticComponent< | ||
| PressableProps & React.RefAttributes<View> | ||
| > = PressableNative as any; |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -2,17 +2,20 @@ import * as React from 'react'; | ||||||
| import { | ||||||
| GestureResponderEvent, | ||||||
| Platform, | ||||||
| Pressable, | ||||||
| StyleProp, | ||||||
| StyleSheet, | ||||||
| ViewStyle, | ||||||
| } from 'react-native'; | ||||||
| import Color from 'color'; | ||||||
| import { useInternalTheme } from '../../core/theming'; | ||||||
| import type { ThemeProp } from '../../types'; | ||||||
| import type { PressableProps, PressableStateCallbackType } from './Pressable'; | ||||||
| import { Pressable } from './Pressable'; | ||||||
| import { getTouchableRippleColors } from './utils'; | ||||||
| export type Props = React.ComponentPropsWithRef<typeof Pressable> & { | ||||||
| export type Props = PressableProps & { | ||||||
| /** | ||||||
| * Whether to render the ripple outside the view bounds. | ||||||
| */ | ||||||
| @@ -49,8 +52,13 @@ export type Props = React.ComponentPropsWithRef<typeof Pressable> & { | ||||||
| /** | ||||||
| * Content of the `TouchableRipple`. | ||||||
| */ | ||||||
| children: React.ReactNode; | ||||||
| style?: StyleProp<ViewStyle>; | ||||||
| children: | ||||||
| | ((state: PressableStateCallbackType) => React.ReactNode) | ||||||
| | React.ReactNode; | ||||||
| style?: | ||||||
| | StyleProp<ViewStyle> | ||||||
| | ((state: PressableStateCallbackType) => StyleProp<ViewStyle>) | ||||||
| | undefined; | ||||||
| /** | ||||||
| * @optional | ||||||
| */ | ||||||
| @@ -100,16 +108,16 @@ const TouchableRipple = ({ | ||||||
| ...rest | ||||||
| }: Props) => { | ||||||
| const theme = useInternalTheme(themeOverrides); | ||||||
| const { calculatedRippleColor } = getTouchableRippleColors({ | ||||||
| theme, | ||||||
| rippleColor, | ||||||
| }); | ||||||
| const hoverColor = Color(calculatedRippleColor).fade(0.5).rgb().string(); | ||||||
Member 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.
Suggested change
| ||||||
| const handlePressIn = (e: any) => { | ||||||
| const { centered, onPressIn } = rest; | ||||||
| onPressIn?.(e); | ||||||
| const { calculatedRippleColor } = getTouchableRippleColors({ | ||||||
| theme, | ||||||
| rippleColor, | ||||||
| }); | ||||||
| const button = e.currentTarget; | ||||||
| const style = window.getComputedStyle(button); | ||||||
| const dimensions = button.getBoundingClientRect(); | ||||||
| @@ -236,9 +244,20 @@ const TouchableRipple = ({ | ||||||
| onPressIn={handlePressIn} | ||||||
| onPressOut={handlePressOut} | ||||||
| disabled={disabled} | ||||||
| style={[styles.touchable, borderless && styles.borderless, style]} | ||||||
| style={(state) => [ | ||||||
| styles.touchable, | ||||||
| borderless && styles.borderless, | ||||||
| // focused state is not ready yet: https://github.com/necolas/react-native-web/issues/1849 | ||||||
| // state.focused && { backgroundColor: ___ }, | ||||||
| state.hovered && { backgroundColor: hoverColor }, | ||||||
| typeof style === 'function' ? style(state) : style, | ||||||
| ]} | ||||||
| > | ||||||
| {React.Children.only(children)} | ||||||
| {(state) => | ||||||
| React.Children.only( | ||||||
| typeof children === 'function' ? children(state) : children | ||||||
| ) | ||||||
| } | ||||||
Member 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. Please add unit test for introduced changes in styles and rendering the children. | ||||||
| </Pressable> | ||||||
| ); | ||||||
| }; | ||||||
| @@ -251,7 +270,10 @@ TouchableRipple.supported = true; | ||||||
| const styles = StyleSheet.create({ | ||||||
| touchable: { | ||||||
| position: 'relative', | ||||||
| ...(Platform.OS === 'web' && { cursor: 'pointer' }), | ||||||
| ...(Platform.OS === 'web' && { | ||||||
| cursor: 'pointer', | ||||||
| transition: '150ms background-color', | ||||||
| }), | ||||||
| }, | ||||||
| borderless: { | ||||||
| overflow: 'hidden', | ||||||
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.