Skip to content

Commit

Permalink
feat: add prop to opt into zooming with mouse wheel without the alt k…
Browse files Browse the repository at this point in the history
…ey (#131)
  • Loading branch information
stropitek authored Mar 7, 2024
1 parent 2bac0ee commit c79e779
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/components/container/ContainerComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ interface ContainerProps<TData = unknown> {
noUnselection?: boolean;
lockZoom: boolean;
lockPan: boolean;
zoomWithoutModifierKey?: boolean;
minNewRoiSize?: number;
getNewRoiData?: () => TData;
}
Expand All @@ -62,6 +63,7 @@ export function ContainerComponent<TData = unknown>(
noUnselection,
lockZoom,
lockPan,
zoomWithoutModifierKey = false,
minNewRoiSize = 1,
} = props;

Expand Down Expand Up @@ -147,7 +149,7 @@ export function ContainerComponent<TData = unknown>(
}, 25);

function onWheel(event: WheelEvent) {
if (!event.altKey || lockZoom) return;
if ((!event.altKey && !zoomWithoutModifierKey) || lockZoom) return;
event.preventDefault();
event.stopPropagation();
onZoom(event);
Expand All @@ -173,6 +175,7 @@ export function ContainerComponent<TData = unknown>(
noUnselection,
stateRef,
actions,
zoomWithoutModifierKey,
]);

const lockContextValue = useMemo<LockContext>(() => {
Expand Down
5 changes: 5 additions & 0 deletions src/components/container/RoiContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ export interface RoiContainerProps<TData = unknown> {
* Get the data of a new ROI as it is being drawn.
*/
getNewRoiData?: () => TData;
/**
* If enabled, the user will be able to zoom into the target just by using the mouse wheel
* Discouraged in a scrollable container
*/
zoomWithoutModifierKey?: boolean;
}

export type AfterDrawCallback<TData = unknown> = (
Expand Down
42 changes: 42 additions & 0 deletions stories/panzoom/no-alt-key.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Meta } from '@storybook/react';

import { RoiContainer, RoiList, RoiProvider, TargetImage } from '../../src';
import { Layout } from '../utils/Layout';

export default {
title: 'Pan and zoom',
} as Meta;

export function ZoomWithoutAltKey() {
return (
<Layout>
<RoiProvider>
<RoiContainer
target={<TargetImage src="/barbara.jpg" />}
zoomWithoutModifierKey
>
<RoiList />
</RoiContainer>
</RoiProvider>
</Layout>
);
}

export function ZoomWithoutAltKeyScroll() {
return (
<>
<div style={{ height: 400 }} />
<Layout>
<RoiProvider>
<RoiContainer
target={<TargetImage src="/barbara.jpg" />}
zoomWithoutModifierKey
>
<RoiList />
</RoiContainer>
</RoiProvider>
</Layout>
<div style={{ height: 400 }} />
</>
);
}

0 comments on commit c79e779

Please sign in to comment.