Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Gallery] feat: Added onZoomBegin and onZoomEnd callbacks for gallery #38

Merged
merged 4 commits into from
Aug 1, 2024
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
24 changes: 19 additions & 5 deletions docs/docs/components/gallery.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,13 @@ This property is useful for instance to animate the background color based on th

Callback triggered when the user swipes up, down, left or right.

### onScroll
| Type | Default | Additional Info |
|------|---------|----------------|
| `(scroll: number, contentOffset: number) => void` | `undefined` | see [worklets](https://docs.swmansion.com/react-native-reanimated/docs/2.x/fundamentals/worklets/) |

Worklet callback triggered as the user scrolls the gallery.

### onPanStart
| Type | Default | Additional Info |
|------|---------|-----------------|
Expand Down Expand Up @@ -277,12 +284,19 @@ Callback triggered when the pinch gesture starts.

Callback triggered as soon as the user lifts their fingers off the screen after pinching.

### onScroll
| Type | Default | Additional Info |
|------|---------|----------------|
| `(scroll: number, contentOffset: number) => void` | `undefined` | see [worklets](https://docs.swmansion.com/react-native-reanimated/docs/2.x/fundamentals/worklets/) |
### onZoomBegin
| Type | Default |
|------|---------|
| `(index: number) => void` | `undefined` |

Worklet callback triggered as the user scrolls the gallery.
Callback triggered when component is zoomed from its base state (scale value at one).

### onZoomEnd
| Type | Default |
|------|---------|
| `(index: number) => void` | `undefined` |

Callback triggered when component returns back to its original state (scale value at one).

### customTransition

Expand Down
5 changes: 5 additions & 0 deletions src/commons/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,8 @@ export type TapGestureCallbacks = Partial<{
onTap: TapGestureEventCallback;
onDoubleTap: TapGestureEventCallback;
}>;

export type ZoomEventCallbacks = Partial<{
onZoomBegin: (index: number) => void;
onZoomEnd: (index: number) => void;
}>;
17 changes: 17 additions & 0 deletions src/components/gallery/Gallery.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const Gallery = <T extends unknown>(props: GalleryPropsWithRef<T>) => {
onPinchStart,
onPinchEnd,
onSwipe,
onZoomBegin,
onZoomEnd,
onVerticalPull,
} = props;

Expand All @@ -62,6 +64,7 @@ const Gallery = <T extends unknown>(props: GalleryPropsWithRef<T>) => {
scroll,
translate,
scale,
hasZoomed,
} = useContext(GalleryContext);

const scrollDirection = useDerivedValue(() => {
Expand Down Expand Up @@ -118,6 +121,20 @@ const Gallery = <T extends unknown>(props: GalleryPropsWithRef<T>) => {
[vertical, activeIndex, rootSize]
);

useAnimatedReaction(
() => scale.value,
(value, previousValue) => {
if (value !== 1 && !hasZoomed.value) {
hasZoomed.value = true;
onZoomBegin && runOnJS(onZoomBegin)(activeIndex.value);
} else if (value === 1 && previousValue !== 1 && hasZoomed.value) {
hasZoomed.value = false;
onZoomEnd && runOnJS(onZoomEnd)(activeIndex.value);
}
},
[scale]
);

// Reference handling
const setIndex = (index: number) => {
const clamped = clamp(index, 0, data.length);
Expand Down
3 changes: 3 additions & 0 deletions src/components/gallery/GalleryProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const GalleryProvider = <T extends unknown>(
const resetIndex = useSharedValue<number>(startIndex);
const fetchIndex = useSharedValue<number>(startIndex);

const hasZoomed = useSharedValue<boolean>(false);

const context: GalleryContextType = {
rootSize,
rootChildSize,
Expand All @@ -45,6 +47,7 @@ const GalleryProvider = <T extends unknown>(
fetchIndex,
isScrolling,
scale,
hasZoomed,
};

return (
Expand Down
1 change: 1 addition & 0 deletions src/components/gallery/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type GalleryContextType = {
resetIndex: SharedValue<number>;
fetchIndex: SharedValue<number>;
isScrolling: SharedValue<boolean>;
hasZoomed: SharedValue<boolean>;
};

export const GalleryContext = React.createContext<GalleryContextType>(
Expand Down
4 changes: 3 additions & 1 deletion src/components/gallery/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
SizeVector,
SwipeDirection,
TapGestureEvent,
ZoomEventCallbacks,
} from '../../commons/types';
import type { ResumableZoomState } from '../resumable/types';

Expand Down Expand Up @@ -44,7 +45,8 @@ export type GalleryProps<T = unknown> = {
onScroll?: (scroll: number, contentOffset: number) => void;
onVerticalPull?: (translateY: number, released: boolean) => void;
} & PinchGestureCallbacks &
PanGestureCallbacks;
PanGestureCallbacks &
ZoomEventCallbacks;

export type GalleryType = {
setIndex: (index: number) => void;
Expand Down
Loading