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

disble mouse scroll #437

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module system it is exported as `GraphQLVoyager` global variable.
- `hideDocs` [`boolean`, default `false`] - hide the docs sidebar
- `hideSettings` [`boolean`, default `false`] - hide settings panel
- `hideVoyagerLogo` [`boolean`, default `true`] - hide voyager logo
- `disableMouseWheelZoom` [`boolean`, default `false`] - disables scroll handler

## Using pre-bundled version

Expand Down
7 changes: 7 additions & 0 deletions src/components/GraphViewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ interface GraphViewportProps {

onSelectNode: (id: string | null) => void;
onSelectEdge: (id: string) => void;

disableMouseWheelZoom?: boolean | null;
}

interface GraphViewportState {
Expand Down Expand Up @@ -106,6 +108,11 @@ export default class GraphViewport extends Component<
this._containerRef.current!,
onSelectNode,
onSelectEdge,
{
mouseWheelZoomEnabled: this.props.disableMouseWheelZoom
? false
: true,
},
);
this.setState({ svgViewport });
})
Expand Down
3 changes: 3 additions & 0 deletions src/components/Voyager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface VoyagerProps {
hideDocs?: boolean;
hideSettings?: boolean;
hideVoyagerLogo?: boolean;
disableMouseWheelZoom?: boolean;

children?: ReactNode;
}
Expand Down Expand Up @@ -120,6 +121,7 @@ export default function Voyager(props: VoyagerProps) {
hideSettings = false,
// TODO: switch to false in the next major version
hideVoyagerLogo = true,
disableMouseWheelZoom = false,
} = props;

const viewportRef = useRef<GraphViewport>(null);
Expand Down Expand Up @@ -223,6 +225,7 @@ export default function Voyager(props: VoyagerProps) {
onSelectNode={handleSelectNode}
onSelectEdge={handleSelectEdge}
ref={viewportRef}
disableMouseWheelZoom={disableMouseWheelZoom}
/>
</Box>
);
Expand Down
6 changes: 4 additions & 2 deletions src/graph/viewport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class Viewport {
public container: HTMLElement,
onSelectNode: (id: string | null) => void,
onSelectEdge: (id: string) => void,
options: SvgPanZoom.Options = {},
) {
this.onSelectNode = onSelectNode;
this.onSelectEdge = onSelectEdge;
Expand All @@ -51,7 +52,7 @@ export class Viewport {

// Allow the SVG dimensions to be computed
// Quick fix for SVG manipulation issues.
setTimeout(() => this.enableZoom(), 0);
setTimeout(() => this.enableZoom(options), 0);
this.bindClick();
this.bindHover();

Expand All @@ -66,13 +67,14 @@ export class Viewport {
this.resizeObserver.observe(this.container);
}

enableZoom() {
enableZoom(options: SvgPanZoom.Options = {}) {
const svgHeight = this.$svg['height'].baseVal.value;
const svgWidth = this.$svg['width'].baseVal.value;
const bbRect = this.container.getBoundingClientRect();
this.maxZoom = Math.max(svgHeight / bbRect.height, svgWidth / bbRect.width);

this.zoomer = svgPanZoom(this.$svg, {
...options,
zoomScaleSensitivity: 0.25,
minZoom: 0.95,
maxZoom: this.maxZoom,
Expand Down
Loading