Experiments >

Code animation: Selection integration

Experiment #14821st April, 2021by Joshua Nussbaum

After exploring how to maintain global selection with a store, it’s ready to be integrated in the app.

There are 4 parts to this:

  1. Toggling the selection from multiple places in the app.
  2. Adjusting the style of objects once they’re selected.
  3. Making the property editor component conditional based on the selection.
  4. Clearing all selections when the user clicks on the background.

Toggling selection

The selection is initiatied by clicking on element:

  import { toggle } from '$lib/selection'

<div on:click={() => toggle('selection-type', id)}>

Styling the selection

Components can watch for selection changes using the derived store and adjust css classes.

  import { watchSelection } from '$lib/selection'

  // returns a `derived` store
  const selected = watchSelection('selection-type', id)

<div class:selected={$selected}>

  .selected {

Conditional property editor

The component displayed in the property editor sidebar is based on the selection:

  import { selection } from '$lib/selection'

{#if $selection.type == 'settings'}
{:else if $selection.type == 'frame'}
{:else if $selection.type == 'window'}
{:else if $selection.type == 'event'}
  <EventEditor id={$selection.id}/>

Clearing all selections

All selections can be cleared by clicking the background:

  import { clear } from '$lib/selection'

<main on:click={clear}>





  • Should selection support on:focus events? That would allow focusing with just keyboard tab key. Probably overkill though.
  • Instead of using {#if $selection.type} to display components conditionally, an alternative is to use a dictionary, ie <svelte:component this={dictionary[type]}/>
view all experiments

Stay tuned in

Learn how to add more experimentation to your workflow