Skip to main content

registerToolbarItem

Description

Registers custom components as elements in the toolbar

Usage

registerToolbarItem(type: string, handler: Component<any>, config?: { menu?: boolean }): void;

Parameters

The function accepts the following parameters:

  • type – (required) the unique name that identifies the custom toolbar component

  • handler – (required) the Svelte component that implements the custom toolbar item

  • config - (optional) a configuration object with the following property:

    • menu - (optional) if true, registeres a separate renderer for the overflow menu

Examples

Register custom component as toolbar's element

<script>
import { Toolbar, registerToolbarItem } from "@svar-ui/svelte-toolbar";
import { Text } from "@svar-ui/svelte-core";

// register custom component
registerToolbarItem("text", Text);
</script>

<Toolbar
items={[
{ id: "label", text: "Toolbar with text input" },
{ id: "search", comp: "text" },
]}
/>

Create custom button control for toolbar

In myControl.svelte

<script>
export let menu;
export let text;
</script>

<button class:menu onclick>{text}</button>

<style>
.menu {
font-weight: bold;
}
</style>
  • custom control can fire onclick event on activation
  • menu property will be set to true, when component is inside of popup menu
  • any other property set in the item object will be available

In App.svelte:

<script>
import { Toolbar, registerToolbarItem } from "@svar-ui/svelte-toolbar";
import MyControl from "./MyControl.svelte";

// register custom component
registerToolbarItem("cb", MyControl);
</script>

<Toolbar
items={[
{ id: "label", text: "Toolbar with custom button" },
{ id: "search", comp: "cb" },
]}
/>

Create custom input control for toolbar

In myControl.svelte:

<script>
export let value;
export let type = "text";
</script>

<input type="text" bind:value />
  • value property will contain initial value
  • changes to value will be reflected in toolbars state
  • any other property set in the item object will be available

In App.svelte:

<script>
import { Toolbar, registerToolbarItem } from "@svar-ui/svelte-toolbar";
import MyControl from "./MyControl.svelte";

// register custom component
registerToolbarItem("ci", MyControl);
</script>

<Toolbar
items={[
{ id: "label", text: "Toolbar with custom input" },
{ id: "search", comp: "ci", type: "search" },
]}
/>

Registering a separate renderer for the overflow menu

You can register two different components under the same type:

  • the first one, Segmented, for the default toolbar mode
  • the second one, ButtonList, specifically for the menu mode, by passing { menu: true } as the config argument

If no menu-specific component is registered, the default component is reused for both.

<script>
import { Toolbar, ButtonList, registerToolbarItem } from "@svar-ui/svelte-toolbar";
import { Segmented } from "@svar-ui/svelte-core";

// register component for the `toolbar` mode
registerToolbarItem("segmented", Segmented);
// register a separate component for the `menu` mode
registerToolbarItem("segmented", ButtonList, { menu: true });

let items = [
// ... more toolbar items
{
id: "mode",
comp: "segmented",
value: "1",
options: [
{ id: "1", label: "All" },
{ id: "2", label: "Active" },
],
},
];
</script>

<Toolbar {items} />

The choice which component (Segmented or ButtonList) to render is resolved automatically based on the item's current location:

  • horizontal Segmented component is used when the item is rendered directly in the toolbar
  • the ButtonList component is used if the same item collapses into the overflow menu