Rework Pressable to handle ripple on android

This commit is contained in:
Zoe Roux 2023-01-08 21:22:20 +09:00
parent b407a257c3
commit bf73f0ce5c
No known key found for this signature in database
GPG Key ID: B2AB52A2636E5C46
3 changed files with 23 additions and 64 deletions

View File

@ -19,10 +19,11 @@
*/ */
import React, { ComponentProps, ComponentType, ForwardedRef, forwardRef } from "react"; import React, { ComponentProps, ComponentType, ForwardedRef, forwardRef } from "react";
import { Pressable, Platform, PressableProps, ViewStyle } from "react-native"; import { Platform, PressableProps, ViewStyle } from "react-native";
import { SvgProps } from "react-native-svg"; import { SvgProps } from "react-native-svg";
import { YoshikiStyle } from "yoshiki/dist/type"; import { YoshikiStyle } from "yoshiki/dist/type";
import { px, useYoshiki } from "yoshiki/native"; import { px, useYoshiki } from "yoshiki/native";
import { PressableFeedback } from "./links";
import { ts } from "./utils"; import { ts } from "./utils";
declare module "react" { declare module "react" {
@ -71,7 +72,7 @@ export const IconButton = forwardRef(function _IconButton<AsProps = PressablePro
) { ) {
const { css } = useYoshiki(); const { css } = useYoshiki();
const Container = as ?? Pressable; const Container = as ?? PressableFeedback;
return ( return (
<Container <Container

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 { 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,20 @@ 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>({ android_ripple={{ foreground: true, color: alpha(theme.contrast, 0.5) as any }}
android: { useForeground: true, ...pressProps }, {...props}
default: { ref, ...props }, >
})} {children}
> </Pressable>
{Platform.select<ReactNode>({
android: <View {...noPressProps}>{children}</View>,
ios: <View {...noPressProps}>{children}</View>,
default: children,
})}
</InnerPressable>
</Wrapper>
); );
}); });
@ -114,7 +71,7 @@ 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} href={href}

View File

@ -27,6 +27,7 @@ import {
IconButton, IconButton,
Link, Link,
Poster, Poster,
PressableFeedback,
Skeleton, Skeleton,
Slider, Slider,
tooltip, tooltip,
@ -34,7 +35,7 @@ import {
} from "@kyoo/primitives"; } from "@kyoo/primitives";
import { Chapter, Font, Track } from "@kyoo/models"; import { Chapter, Font, Track } from "@kyoo/models";
import { useAtomValue, useSetAtom, useAtom } from "jotai"; import { useAtomValue, useSetAtom, useAtom } from "jotai";
import { Pressable, View, ViewProps } from "react-native"; import { View, ViewProps } from "react-native";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { percent, rem, useYoshiki } from "yoshiki/native"; import { percent, rem, useYoshiki } from "yoshiki/native";
import { useRouter } from "solito/router"; import { useRouter } from "solito/router";
@ -177,7 +178,7 @@ export const Back = ({
> >
<IconButton <IconButton
icon={ArrowBack} icon={ArrowBack}
{...(href ? { as: Link as any, href: href } : { as: Pressable, onPress: router.back })} {...(href ? { as: Link as any, href: href } : { as: PressableFeedback, onPress: router.back })}
{...tooltip(t("player.back"))} {...tooltip(t("player.back"))}
/> />
<Skeleton> <Skeleton>