-
-
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
Closed
Closed
Changes from all commits
Commits
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 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
| 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; |
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
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
| 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', | ||||||
|
|
||||||
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.