Update pressable's feedback ripple

This commit is contained in:
Zoe Roux 2023-01-28 02:24:04 +09:00 committed by Zoe Roux
parent d2d736a7ec
commit 80be960f29

View File

@ -18,20 +18,11 @@
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>. * along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { ComponentProps, ComponentType, forwardRef, Fragment, ReactNode } from "react"; import { forwardRef, ReactNode } from "react";
import { import { Platform, Pressable, TextProps, View, PressableProps } from "react-native";
Platform,
Pressable,
TextProps,
TouchableOpacity,
TouchableNativeFeedback,
View,
ViewProps,
StyleSheet,
PressableProps,
} from "react-native";
import { LinkCore, TextLink } from "solito/link"; import { LinkCore, TextLink } from "solito/link";
import { useYoshiki } from "yoshiki/native"; import { useTheme, useYoshiki } from "yoshiki/native";
import { alpha } from "./themes";
export const A = ({ export const A = ({
href, href,
@ -59,54 +50,23 @@ export const A = ({
); );
}; };
export const PressableFeedback = forwardRef< export const PressableFeedback = forwardRef<View, PressableProps>(function _Feedback(
unknown, { children, ...props },
ViewProps & { ref,
onFocus?: PressableProps["onFocus"]; ) {
onBlur?: PressableProps["onBlur"]; const theme = useTheme();
onPressIn?: PressableProps["onPressIn"];
onPressOut?: PressableProps["onPressOut"];
onPress?: PressableProps["onPress"];
WebComponent?: ComponentType;
}
>(function _Feedback({ children, WebComponent, ...props }, ref) {
const { onBlur, onFocus, onPressIn, onPressOut, onPress, ...noPressProps } = props;
const pressProps = { onBlur, onFocus, onPressIn, onPressOut, onPress };
const wrapperProps = Platform.select<ViewProps & { ref?: any }>({
android: {
style: {
borderRadius: StyleSheet.flatten(props?.style)?.borderRadius,
alignContent: "center",
justifyContent: "center",
overflow: "hidden",
},
ref,
},
default: {},
});
const Wrapper = wrapperProps.style ? View : Fragment;
const InnerPressable = Platform.select<ComponentType<{ children?: any }>>({
web: WebComponent ?? Pressable,
android: TouchableNativeFeedback,
ios: TouchableOpacity,
default: Pressable,
});
return ( return (
<Wrapper {...wrapperProps}> <Pressable
<InnerPressable ref={ref}
{...Platform.select<object>({ // TODO: Enable ripple on tv. Waiting for https://github.com/react-native-tvos/react-native-tvos/issues/440
android: { useForeground: true, ...pressProps }, {...(Platform.isTV
default: { ref, ...props }, ? {}
})} : { android_ripple: { foreground: true, color: alpha(theme.contrast, 0.5) as any } })}
> {...props}
{Platform.select<ReactNode>({ >
android: <View {...noPressProps}>{children}</View>, {children}
ios: <View {...noPressProps}>{children}</View>, </Pressable>
default: children,
})}
</InnerPressable>
</Wrapper>
); );
}); });
@ -114,13 +74,9 @@ export const Link = ({
href, href,
children, children,
...props ...props
}: { href: string } & Omit<ComponentProps<typeof PressableFeedback>, "WebComponent">) => { }: { href: string; children?: ReactNode } & PressableProps) => {
return ( return (
<LinkCore <LinkCore href={href} Component={PressableFeedback} componentProps={props}>
href={href}
Component={PressableFeedback}
componentProps={{ WebComponent: View, ...props }}
>
{children} {children}
</LinkCore> </LinkCore>
); );