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

autoResize option enabled #24

Closed
wants to merge 2 commits into from
Closed
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
30 changes: 1 addition & 29 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const defaultOptions = {
physics: {
stabilization: false,
},
autoResize: false,
autoResize: true,
edges: {
smooth: false,
color: '#000000',
Expand All @@ -103,25 +103,6 @@ const defaultOptions = {
},
};

function useResizeObserver(
ref: React.MutableRefObject<HTMLElement | null>,
callback: ResizeObserverCallback
): void {
useEffect(() => {
// Create an observer instance linked to the callback function
if (ref.current) {
const observer = new ResizeObserver(callback);

// Start observing the target node for configured mutations
observer.observe(ref.current);

return () => {
observer.disconnect();
};
}
return;
}, [callback, ref]);
}
function shallowClone<T>(array: T[]): T[] {
return array.map((value) => ({ ...value }));
}
Expand Down Expand Up @@ -238,15 +219,6 @@ const VisGraph = forwardRef<
};
}, [edges, initialOptions, nodes]);

//resize network on window resize
function onContainerResize() {
if (network) {
network.redraw();
}
}

useResizeObserver(container, onContainerResize);

return (
<div style={{ width: '100%', height: '100%' }} ref={container} {...props} />
);
Expand Down
21 changes: 21 additions & 0 deletions src/stories/VisNetwork.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Template: Story<StoryProps> = (args) => {

export const BasicNetwork = Template.bind({});
BasicNetwork.args = {
options: { autoResize: false },
graph: {
nodes: [{ id: 1 }, { id: 2 }, { id: 3 }],
edges: [
Expand Down Expand Up @@ -66,3 +67,23 @@ ZoomKey.args = {
...BasicNetwork.args,
zoomKey: 'ctrlKey',
};

const ResizeTemplate: Story<StoryProps> = (args) => {
return (
<div
style={{
resize: 'both',
overflow: 'auto',
border: '1px solid black',
}}
>
<VisGraph {...args} />
</div>
);
};

export const ResizeNetwork = ResizeTemplate.bind({});
ResizeNetwork.args = {
...BasicNetwork.args,
options: { autoResize: true },
};