[Question] How do I create new panel after onReady? #310
-
Hi! I want to create a new panel for my panel view after onReady has run for example later on button press. How can I achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I do something like the following const components: PanelCollection<IDockviewPanelProps> = {
default: (props: IDockviewPanelProps) => {
return <div>{"Hello world"}</div>;
},
};
function useDockviewApiRef() {
const dockviewApi = useRef<DockviewApi | null>(null);
return {
onReady: (event: DockviewReadyEvent) => {
dockviewApi.current = event.api;
},
get: () => {
if (dockviewApi.current == null) {
throw new Error("Dockview api is not ready");
}
return dockviewApi.current;
},
};
}
export function Layout() {
const api = useDockviewApiRef();
return (
<>
<button
onClick={() => {
api.get().addPanel({
id: "my-window",
title: "my-window",
component: "default",
});
}}
>
Open
</button>
<DockviewReact className="dockview dockview-theme-dark" components={components} onReady={api.onReady} />
</>
);
} I can't say if this is the best practice, but it has worked great for me so far. |
Beta Was this translation helpful? Give feedback.
-
The How you then store that https://codesandbox.io/s/vigorous-cori-7ms6xh?file=/src/app.tsx:583-601 Let me know if you have any further questions! |
Beta Was this translation helpful? Give feedback.
I do something like the following