-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RNMobile] Add ripple effect to BottomSheet cells on Android (#22169)
- Loading branch information
Showing
3 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
83 changes: 83 additions & 0 deletions
83
packages/components/src/mobile/bottom-sheet/ripple.native.js
This file contains 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,83 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { | ||
Platform, | ||
TouchableOpacity, | ||
TouchableNativeFeedback, | ||
View, | ||
} from 'react-native'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { withPreferredColorScheme } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import rippleStyles from './ripple.native.scss'; | ||
|
||
const ANDROID_VERSION_LOLLIPOP = 21; | ||
const ANDROID_VERSION_PIE = 28; | ||
|
||
const TouchableRipple = ( { | ||
style, | ||
onPress, | ||
disabled: disabledProp, | ||
children, | ||
activeOpacity, | ||
getStylesFromColorScheme, | ||
borderless = false, | ||
...touchableProps | ||
} ) => { | ||
const isTouchableNativeSupported = | ||
Platform.OS === 'android' && | ||
Platform.Version >= ANDROID_VERSION_LOLLIPOP; | ||
|
||
const disabled = disabledProp || ! onPress; | ||
const rippleColor = getStylesFromColorScheme( | ||
rippleStyles.ripple, | ||
rippleStyles.rippleDark | ||
).backgroundColor; | ||
|
||
if ( isTouchableNativeSupported ) { | ||
// A workaround for ripple on Android P is to use useForeground + overflow: 'hidden' | ||
// https://github.com/facebook/react-native/issues/6480 | ||
const useForeground = | ||
Platform.OS === 'android' && | ||
Platform.Version >= ANDROID_VERSION_PIE && | ||
borderless; | ||
|
||
return ( | ||
<TouchableNativeFeedback | ||
{ ...touchableProps } | ||
onPress={ onPress } | ||
disabled={ disabled } | ||
useForeground={ useForeground } | ||
background={ TouchableNativeFeedback.Ripple( | ||
rippleColor, | ||
borderless | ||
) } | ||
> | ||
<View style={ [ borderless && rippleStyles.overflow, style ] }> | ||
{ children } | ||
</View> | ||
</TouchableNativeFeedback> | ||
); | ||
} | ||
|
||
return ( | ||
<TouchableOpacity | ||
{ ...touchableProps } | ||
onPress={ onPress } | ||
disabled={ disabled } | ||
activeOpacity={ activeOpacity } | ||
style={ style } | ||
> | ||
{ children } | ||
</TouchableOpacity> | ||
); | ||
}; | ||
|
||
export default withPreferredColorScheme( TouchableRipple ); |
11 changes: 11 additions & 0 deletions
11
packages/components/src/mobile/bottom-sheet/ripple.native.scss
This file contains 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,11 @@ | ||
.ripple { | ||
background-color: rgba(0, 0, 0, 0.2); | ||
} | ||
|
||
.rippleDark { | ||
background-color: rgba(255, 255, 255, 0.2); | ||
} | ||
|
||
.overflow { | ||
overflow: hidden; | ||
} |