Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/BeforeClip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Image, type ImageProps, type ImageSource } from "expo-image";
import { StyleSheet } from "react-native";
import Animated, { useAnimatedStyle, type DerivedValue } from "react-native-reanimated";

type PassThroughImageProps = Omit<ImageProps, "source" | "style" | "contentFit">;

interface BeforeClipProps {
source: ImageSource | number;
contentFit?: ImageProps["contentFit"];
imageProps?: PassThroughImageProps;
measuredWidth: number;
splitPx: DerivedValue<number>;
}

export function BeforeClip(props: BeforeClipProps) {
const { measuredWidth, splitPx, source, contentFit, imageProps } = props;

const clipStyle = useAnimatedStyle(() => ({ width: splitPx.get() }));

return (
<Animated.View style={[styles.clip, clipStyle]}>
{measuredWidth > 0 && (
<Image
{...imageProps}
accessible={false}
source={source}
contentFit={contentFit}
style={[styles.leftImage, { width: measuredWidth }]}
/>
)}
</Animated.View>
);
}

const styles = StyleSheet.create({
clip: {
position: "absolute",
left: 0,
top: 0,
bottom: 0,
overflow: "hidden",
},
leftImage: {
position: "absolute",
left: 0,
top: 0,
height: "100%",
},
});
56 changes: 56 additions & 0 deletions src/Handle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import type { ReactNode } from "react";
import { StyleSheet, View } from "react-native";
import Animated, { useAnimatedStyle, type DerivedValue } from "react-native-reanimated";

const HANDLE_KNOB = 40;
const LINE_WIDTH = 3;

interface HandleProps {
knobContent?: ReactNode;
renderLine?: ReactNode;
splitPx: DerivedValue<number>;
}

export function Handle(props: HandleProps) {
const { knobContent, renderLine, splitPx } = props;

const handleGroupStyle = useAnimatedStyle(() => ({
transform: [{ translateX: splitPx.get() - HANDLE_KNOB / 2 }],
}));

return (
<Animated.View style={[styles.handleColumn, handleGroupStyle]} pointerEvents="none">
{renderLine ?? <View style={styles.line} />}
<View style={styles.knob}>{knobContent}</View>
</Animated.View>
);
}

const styles = StyleSheet.create({
handleColumn: {
position: "absolute",
left: 0,
top: 0,
bottom: 0,
width: HANDLE_KNOB,
alignItems: "center",
justifyContent: "center",
},
line: {
position: "absolute",
top: 0,
bottom: 0,
width: LINE_WIDTH,
alignSelf: "center",
backgroundColor: "#ffffff",
},
knob: {
width: HANDLE_KNOB,
height: HANDLE_KNOB,
borderRadius: HANDLE_KNOB / 2,
overflow: "hidden",
alignItems: "center",
justifyContent: "center",
backgroundColor: "#ffffff",
},
});
77 changes: 11 additions & 66 deletions src/ImageComparisonSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { Image, type ImageProps, type ImageSource } from "expo-image";
import { type ReactNode } from "react";
import { StyleSheet, View, type StyleProp, type ViewStyle } from "react-native";
import { GestureDetector } from "react-native-gesture-handler";
import Animated, { useAnimatedStyle, useDerivedValue } from "react-native-reanimated";
import { useDerivedValue } from "react-native-reanimated";

import { BeforeClip } from "./BeforeClip";
import { Handle } from "./Handle";
import { useComparisonSlider } from "./useComparisonSlider";

const HANDLE_KNOB = 40;
const LINE_WIDTH = 3;

type PassThroughImageProps = Omit<ImageProps, "source" | "style" | "contentFit">;

/** Props for {@link ImageComparisonSlider}. */
Expand Down Expand Up @@ -77,12 +76,6 @@ export function ImageComparisonSlider({

const splitPx = useDerivedValue(() => splitRatio.get() * containerWidth.get());

const clipStyle = useAnimatedStyle(() => ({ width: splitPx.get() }));

const handleGroupStyle = useAnimatedStyle(() => ({
transform: [{ translateX: splitPx.get() - HANDLE_KNOB / 2 }],
}));

return (
<GestureDetector gesture={pan}>
<View
Expand All @@ -105,26 +98,17 @@ export function ImageComparisonSlider({
style={styles.fullImage}
/>
{enabled && (
<Animated.View style={[styles.clip, clipStyle]}>
{measuredWidth > 0 && (
<Image
{...beforeImageProps}
accessible={false}
source={before}
contentFit={contentFit}
style={[styles.leftImage, { width: measuredWidth }]}
/>
)}
</Animated.View>
<BeforeClip
source={before}
contentFit={contentFit}
imageProps={beforeImageProps}
splitPx={splitPx}
measuredWidth={measuredWidth}
/>
)}
</View>

{enabled && (
<Animated.View style={[styles.handleColumn, handleGroupStyle]} pointerEvents="none">
{renderLine ?? <View style={styles.line} />}
<View style={styles.knob}>{knobContent}</View>
</Animated.View>
)}
{enabled && <Handle knobContent={knobContent} renderLine={renderLine} splitPx={splitPx} />}
</View>
</GestureDetector>
);
Expand All @@ -143,43 +127,4 @@ const styles = StyleSheet.create({
width: "100%",
height: "100%",
},
clip: {
position: "absolute",
left: 0,
top: 0,
bottom: 0,
overflow: "hidden",
},
leftImage: {
position: "absolute",
left: 0,
top: 0,
height: "100%",
},
handleColumn: {
position: "absolute",
left: 0,
top: 0,
bottom: 0,
width: HANDLE_KNOB,
alignItems: "center",
justifyContent: "center",
},
line: {
position: "absolute",
top: 0,
bottom: 0,
width: LINE_WIDTH,
alignSelf: "center",
backgroundColor: "#ffffff",
},
knob: {
width: HANDLE_KNOB,
height: HANDLE_KNOB,
borderRadius: HANDLE_KNOB / 2,
overflow: "hidden",
alignItems: "center",
justifyContent: "center",
backgroundColor: "#ffffff",
},
});
Loading