react-native-paper
Version:
Material design for React Native
94 lines (92 loc) • 3.52 kB
JavaScript
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
import * as React from 'react';
import { Platform, StyleSheet, View } from 'react-native';
import { Pressable } from './Pressable';
import { getTouchableRippleColors } from './utils';
import { SettingsContext } from '../../core/settings';
import { useInternalTheme } from '../../core/theming';
import { forwardRef } from '../../utils/forwardRef';
import hasTouchHandler from '../../utils/hasTouchHandler';
const ANDROID_VERSION_LOLLIPOP = 21;
const ANDROID_VERSION_PIE = 28;
const TouchableRipple = ({
style,
background,
borderless = false,
disabled: disabledProp,
rippleColor,
underlayColor,
children,
theme: themeOverrides,
...rest
}, ref) => {
const theme = useInternalTheme(themeOverrides);
const {
rippleEffectEnabled
} = React.useContext(SettingsContext);
const {
onPress,
onLongPress,
onPressIn,
onPressOut
} = rest;
const hasPassedTouchHandler = hasTouchHandler({
onPress,
onLongPress,
onPressIn,
onPressOut
});
const disabled = disabledProp || !hasPassedTouchHandler;
const {
calculatedRippleColor,
calculatedUnderlayColor
} = getTouchableRippleColors({
theme,
rippleColor,
underlayColor
});
// Keep the static form when possible: `Pressable` only tracks the pressed
// state when `children` or `style` is a function.
const resolvedStyle = typeof style === 'function' ? state => [borderless && styles.overflowHidden, style(state)] : [borderless && styles.overflowHidden, style];
const resolveChildren = state => React.Children.only(typeof children === 'function' ? children(state) : children);
// 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;
if (TouchableRippleComponent.supported) {
const androidRipple = rippleEffectEnabled ? background ?? {
color: calculatedRippleColor,
borderless,
foreground: useForeground
} : undefined;
return /*#__PURE__*/React.createElement(Pressable, _extends({}, rest, {
ref: ref,
disabled: disabled,
style: resolvedStyle,
android_ripple: androidRipple
}), typeof children === 'function' ? resolveChildren : React.Children.only(children));
}
return /*#__PURE__*/React.createElement(Pressable, _extends({}, rest, {
ref: ref,
disabled: disabled,
style: resolvedStyle
}), state => /*#__PURE__*/React.createElement(React.Fragment, null, state.pressed && rippleEffectEnabled && /*#__PURE__*/React.createElement(View, {
testID: "touchable-ripple-underlay",
style: [styles.underlay, {
backgroundColor: calculatedUnderlayColor
}]
}), resolveChildren(state)));
};
const styles = StyleSheet.create({
overflowHidden: {
overflow: 'hidden'
},
underlay: {
...StyleSheet.absoluteFill,
zIndex: 2
}
});
const Component = forwardRef(TouchableRipple);
const TouchableRippleComponent = Component;
TouchableRippleComponent.supported = Platform.OS === 'android' && Platform.Version >= ANDROID_VERSION_LOLLIPOP;
export default TouchableRippleComponent;
//# sourceMappingURL=TouchableRipple.native.js.map