Skip to content

Commit c9f713d

Browse files
committed
rework toolbar to single active tool
1 parent 5f84c2a commit c9f713d

File tree

1 file changed

+12
-46
lines changed

1 file changed

+12
-46
lines changed

packages/svelte/src/toolbar/ToolBar.svelte

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
} = $props();
1010
let open = $state(false); // Default to closed
1111
12-
/** @type {SvelteMap<string, Record<string, any>>} */
13-
let active_tools = $state(new SvelteMap());
12+
/** @type {import('svelte').Component} */
13+
let ActiveComponent = $state(null);
1414
/** @type {HTMLElement} */
1515
let toolbar;
1616
/** @type {HTMLElement} */
@@ -29,54 +29,16 @@
2929
* @param {import('./public').Tool} tool
3030
*/
3131
function toggle_tool(tool) {
32-
const active = active_tools.has(tool.name);
33-
if (!active) {
34-
let mounted_component;
35-
if (tool.component) mounted_component = mountTool(tool.component, tool.name, { tool });
36-
37-
active_tools.set(tool.name, mounted_component);
38-
if (tool.activate) tool.activate();
32+
if (tool.component === ActiveComponent) {
33+
ActiveComponent = undefined;
3934
} else {
40-
const mounted_component = active_tools.get(tool.name);
41-
if (tool.component && mounted_component) unmountTool(mounted_component, tool.name);
42-
43-
if (tool.deactivate) tool.deactivate();
44-
active_tools.delete(tool.name);
35+
ActiveComponent = tool.component;
4536
}
4637
47-
if (active_tools.size === 0) toolbarPanels.style.display = 'none';
38+
if (!ActiveComponent) toolbarPanels.style.display = 'none';
4839
else toolbarPanels.style.display = 'block';
4940
}
5041
51-
/**
52-
* @param {import('svelte').Component} component
53-
* @param {string} id
54-
* @param {Record<string, any>} props
55-
*/
56-
function mountTool(component, id, props) {
57-
if (document.getElementById(id) != null) {
58-
throw Error(`${id} already exists, skipping`);
59-
}
60-
61-
const el = document.createElement('div');
62-
el.setAttribute('id', `svelte-toolbar-${id}`);
63-
toolbarPanels.appendChild(el);
64-
const mounted_component = mount(component, { target: el, props });
65-
66-
return mounted_component;
67-
}
68-
69-
/**
70-
* @param {string} id
71-
* @param {Record<string, any>} component
72-
*/
73-
async function unmountTool(component, id) {
74-
await unmount(component);
75-
76-
const el = document.getElementById(`svelte-toolbar-${id}`);
77-
if (el) el.remove();
78-
}
79-
8042
/**
8143
* @param {DragEvent} event
8244
*/
@@ -129,7 +91,7 @@
12991
{#if open}
13092
<ul class="svelte-toolbar-tools">
13193
{#each config.tools as tool}
132-
<li class:active={active_tools.has(tool.name)}>
94+
<li class:active={tool.component === ActiveComponent}>
13395
<button onclick={() => toggle_tool(tool)} aria-label={tool.name}>{@html tool.icon}</button
13496
>
13597
</li>
@@ -140,7 +102,11 @@
140102
<Icon />
141103
</button>
142104
</div>
143-
<div class="svelte-toolbar-panels" bind:this={toolbarPanels}></div>
105+
<div class="svelte-toolbar-panels" bind:this={toolbarPanels}>
106+
{#if ActiveComponent}
107+
<ActiveComponent />
108+
{/if}
109+
</div>
144110
145111
<style>
146112
.svelte-toolbar {

0 commit comments

Comments
 (0)