Files
jellyfin-audio-player/src/components/TouchableHandler.tsx

42 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-06-21 10:30:41 +02:00
import React, { useCallback } from 'react';
import { Pressable, ViewStyle } from 'react-native';
2020-06-21 10:30:41 +02:00
interface TouchableHandlerProps {
id: string;
onPress: (id: string) => void;
onLongPress?: (id: string) => void;
}
function TouchableStyles({ pressed }: { pressed: boolean }): ViewStyle {
if (pressed) {
return { opacity: 0.5 };
} else {
return { opacity: 1 };
}
2020-06-21 10:30:41 +02:00
}
/**
* This is a generic handler that accepts id as a prop, and return it when it is
* pressed. This comes in handy with lists in which albums / tracks need to be selected.
*/
const TouchableHandler: React.FC<TouchableHandlerProps> = ({ id, onPress, onLongPress, children }) => {
2020-06-21 10:30:41 +02:00
const handlePress = useCallback(() => {
return onPress(id);
}, [id, onPress]);
2020-06-21 10:30:41 +02:00
const handleLongPress = useCallback(() => {
return onLongPress ? onLongPress(id) : undefined;
}, [id, onLongPress]);
2020-06-21 10:30:41 +02:00
return (
<Pressable
onPress={handlePress}
onLongPress={handleLongPress}
style={TouchableStyles}
>
2020-06-21 10:30:41 +02:00
{children}
</Pressable>
2020-06-21 10:30:41 +02:00
);
};
export default TouchableHandler;