Skip to content

Commit

Permalink
Add option to disable double tap to zoom on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
antonKalinin committed Jan 1, 2020
1 parent ab1f053 commit d74a497
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,18 @@ const [visible, setIsVisible] = useState(false);

## Props

| Prop name | Description | Type | Required |
| --------------------- | ----------------------------------------------------- | ----------------------- | -------- |
| `images` | Array of images to display | ImageSource[] | true |
| `imageIndex` | Current index of image to display | number | true |
| `visible` | Is modal shown or not | boolean | true |
| `onRequestClose` | Function called to close the modal | function | true |
| `animationType` | Animation modal presented with: default `fade` | `none`, `fade`, `slide` | false |
| `backgroundColor` | Background color of the modal in HEX (#000000EE) | string | false |
| `swipeToCloseEnabled` | Close modal with swipe up or down: default `true` | boolean | false |
| `HeaderComponent` | Header component, gets current `imageIndex` as a prop | component, function | false |
| `FooterComponent` | Footer component, gets current `imageIndex` as a prop | component, function | false |
| Prop name | Description | Type | Required |
| ------------------------ | ------------------------------------------------------------- | ----------------------- | -------- |
| `images` | Array of images to display | ImageSource[] | true |
| `imageIndex` | Current index of image to display | number | true |
| `visible` | Is modal shown or not | boolean | true |
| `onRequestClose` | Function called to close the modal | function | true |
| `animationType` | Animation modal presented with: default `fade` | `none`, `fade`, `slide` | false |
| `backgroundColor` | Background color of the modal in HEX (#000000EE) | string | false |
| `swipeToCloseEnabled` | Close modal with swipe up or down: default `true` | boolean | false |
| `doubleTapToZoomEnabled` | Zoom image by double tap on it (Android only): default `true` | boolean | false |
| `HeaderComponent` | Header component, gets current `imageIndex` as a prop | component, function | false |
| `FooterComponent` | Footer component, gets current `imageIndex` as a prop | component, function | false |

- ImageSource is an object like { uri: '<http location || file path>' }

Expand Down
7 changes: 5 additions & 2 deletions src/ImageItem/ImageItem.android.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ type Props = {
onRequestClose: () => void;
onZoom: (isZoomed: boolean) => void;
swipeToCloseEnabled?: boolean;
doubleTapToZoomEnabled?: boolean;
};

const ImageItem = ({
imageSrc,
onZoom,
onRequestClose,
swipeToCloseEnabled = true
swipeToCloseEnabled = true,
doubleTapToZoomEnabled = true
}: Props) => {
const imageContainer = React.createRef();
const imageDimensions = useImageDimensions(imageSrc);
Expand All @@ -62,7 +64,8 @@ const ImageItem = ({
const [panHandlers, scaleValue, translateValue] = useZoomPanResponder({
initialScale: scale || 1,
initialTranslate: translate || { x: 0, y: 0 },
onZoom: onZoomPerformed
onZoom: onZoomPerformed,
doubleTapToZoomEnabled
});

const imagesStyles = getImageStyles(
Expand Down
1 change: 1 addition & 0 deletions src/ImageItem/ImageItem.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ declare type Props = {
onRequestClose: () => void;
onZoom: (isZoomed: boolean) => void;
swipeToCloseEnabled?: boolean;
doubleTapToZoomEnabled?: boolean;
};

declare const _default: React.MemoExoticComponent<({
Expand Down
3 changes: 3 additions & 0 deletions src/ImageViewing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Props = {
animationType?: "none" | "fade" | "slide";
backgroundColor?: string;
swipeToCloseEnabled?: boolean;
doubleTapToZoomEnabled?: boolean;
HeaderComponent?: ComponentType<{ imageIndex: number }>;
FooterComponent?: ComponentType<{ imageIndex: number }>;
};
Expand All @@ -49,6 +50,7 @@ function ImageViewing({
animationType = DEFAULT_ANIMATION_TYPE,
backgroundColor = DEFAULT_BG_COLOR,
swipeToCloseEnabled,
doubleTapToZoomEnabled,
HeaderComponent,
FooterComponent
}: Props) {
Expand Down Expand Up @@ -112,6 +114,7 @@ function ImageViewing({
imageSrc={imageSrc}
onRequestClose={onRequestCloseEnhanced}
swipeToCloseEnabled={swipeToCloseEnabled}
doubleTapToZoomEnabled={doubleTapToZoomEnabled}
/>
)}
onMomentumScrollEnd={onScroll}
Expand Down
8 changes: 5 additions & 3 deletions src/hooks/useZoomPanResponder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ type Props = {
initialScale: number;
initialTranslate: Position;
onZoom: (isZoomed: boolean) => void;
doubleTapToZoomEnabled: boolean;
};

const useZoomPanResponder = ({
initialScale,
initialTranslate,
onZoom
onZoom,
doubleTapToZoomEnabled
}: Props): Readonly<[
GestureResponderHandlers,
Animated.Value,
Expand Down Expand Up @@ -129,7 +131,7 @@ const useZoomPanResponder = ({
lastTapTS && tapTS - lastTapTS < DOUBLE_TAP_DELAY
);

if (isDoubleTapPerformed) {
if (doubleTapToZoomEnabled && isDoubleTapPerformed) {
const isScaled = currentTranslate.x !== initialTranslate.x; // currentScale !== initialScale;
const { pageX: touchX, pageY: touchY } = event.nativeEvent.touches[0];
const targetScale = SCALE_MAX;
Expand Down Expand Up @@ -184,7 +186,7 @@ const useZoomPanResponder = ({
gestureState: PanResponderGestureState
) => {
// Don't need to handle move because double tap in progress (was handled in onStart)
if (isDoubleTapPerformed) return;
if (doubleTapToZoomEnabled && isDoubleTapPerformed) return;

if (
numberInitialTouches === 1 &&
Expand Down

0 comments on commit d74a497

Please sign in to comment.