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

Dynamically Controlling Components #794

Closed
pele1410 opened this issue Dec 19, 2024 · 2 comments
Closed

Dynamically Controlling Components #794

pele1410 opened this issue Dec 19, 2024 · 2 comments
Labels
question Further information is requested

Comments

@pele1410
Copy link

My application contains several components (overhead map, guidance panel) that are known apriori by the App object that renders the <DockviewReact>. I create it like so:

<DockviewReact
    onReady={onReady}
    components={components}
    className={'dockview-theme-light'}
/>

where {components} is a list of two:

const components = {
    default: (props: IDockviewPanelProps) => {
        return <MainComponent />;
    },
    guidance_panel: (props: IDockviewPanelProps) => {
        return <GuidancePanelComponent />;
    },
};

What I am interested in is having the ability to add new components arbitrarily. I want to be able to press a button and call something like:

dockviewApi.addPanel({
            id: 'camera',
            component: 'camera',
            renderer: 'always',
            title: 'Camera',
            floating: {
                position: {left: 10, bottom: 10},
                width: 400,
                height: 300,
            },
        });

The catch is I don't know apriori to the <DockviewApi> object creation that this camera component needs to exist. I'd like to be able to just add it and then call addPanel.

I'm not sure if I'm just missing some understanding of what the components are in <DockviewReact> or if it's just not possible. Any help would be appreciated.

@mathuo
Copy link
Owner

mathuo commented Dec 29, 2024

In the case where you need dynamic components the approach I would recommend is to register a single component with dockview and have that component call into a dynamic collection of components that you maintain. You can pass that dynamic components name into the panel at the time of creation using the params property.

Anything in params is persisted with the dockview state.

Let me know if that's useful?

const dynamicComponents: Record<string, React.FC> = {
    component_1: () => {
        return <div>{'component_1'}</div>;
    },
};

const components = {
    default: (
        props: IDockviewPanelProps<{ arbitraryComponentName: string }>
    ) => {
        const name = props.params.arbitraryComponentName;

        const Component = dynamicComponents[name];

        return Component ? <Component /> : null;
    },
};

const RootComponent = () => {
    const onReady = (event: DockviewReadyEvent) => {
        event.api.addPanel({
            id: 'panel_1',
            component: 'default',
            params: { arbitraryComponentName: 'component_1' },
        });
    };

    return <DockviewReact components={components} onReady={onReady} />;
};

@mathuo mathuo added the question Further information is requested label Dec 29, 2024
@pele1410
Copy link
Author

This worked for me. Appreciate the help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants