Vue playground

Nova v3.37.0

Preview

Vue

Working with Slots

Working with Slots and Dynamic Content

Introduction

Nova components are built with Stencil.js and wrapped for Vue. Because Nova uses the light DOM rather than native shadow DOM, Stencil polyfills slot behavior by physically moving slotted elements into the correct positions within the component's rendered markup. Vue has no awareness that this DOM manipulation has occurred, which can lead to unexpected behavior when slot content changes dynamically.

This guide shows you how to work with dynamic slot content correctly.

How Slots Work in the Light DOM

When you render children inside a Nova component's slot, Stencil relocates those DOM nodes to match where the <slot> appears in the component's template. From Vue's perspective, those elements still exist where it originally placed them — but in the actual DOM, they've been moved.

This mismatch is harmless when the slot content is static. This requires extra attention when slot content changes dynamically — for example, when you filter a list, add or remove items, or reorder children. Vue tries to patch the real DOM to match its virtual DOM, but the real DOM no longer matches what Vue expects. This can result in:

  • Items not appearing or disappearing when they should
  • Duplicate or stale items rendering
  • Console errors or visual glitches
  • Event handlers becoming detached

A Concrete Example

Consider a dropdown where the items are filtered based on user input:

vue<!-- This can produce unexpected results -->
<script setup lang="ts">
import { ref, computed } from 'vue';
import { NvFielddropdown, NvFielddropdownitem } from '@nova-design-system/nova-vue';

const filter = ref('');
const items = ref([
  { label: 'Berlin', value: 'berlin' },
  { label: 'Hamburg', value: 'hamburg' },
  { label: 'Munich', value: 'munich' },
]);

const filteredItems = computed(() =>
  items.value.filter((item) =>
    item.label.toLowerCase().includes(filter.value.toLowerCase())
  )
);
</script>

<template>
  <NvFielddropdown
    filterable
    controlled-filter
    @filter-text-changed="filter = $event.detail"
  >
    <ul slot="content">
      <NvFielddropdownitem
        v-for="item in filteredItems"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </ul>
  </NvFielddropdown>
</template>

In this example, when the user types a filter and the list of <NvFielddropdownitem> elements changes, Vue will try to patch the DOM — but Stencil has already moved those nodes. The result is unpredictable rendering.

Keeping Slot Content in Sync

The approach is straightforward: give the parent web component a key that changes whenever its slot content changes. When the key changes, Vue destroys the old component instance and creates a fresh one, which allows Stencil to correctly slot the new children from scratch.

vue<!-- This works correctly -->
<script setup lang="ts">
import { ref, computed } from 'vue';
import { NvFielddropdown, NvFielddropdownitem } from '@nova-design-system/nova-vue';

const filter = ref('');
const items = ref([
  { label: 'Berlin', value: 'berlin' },
  { label: 'Hamburg', value: 'hamburg' },
  { label: 'Munich', value: 'munich' },
]);

const filteredItems = computed(() =>
  items.value.filter((item) =>
    item.label.toLowerCase().includes(filter.value.toLowerCase())
  )
);

const dropdownKey = computed(() =>
  filteredItems.value.map((i) => i.value).join(',')
);
</script>

<template>
  <NvFielddropdown
    :key="dropdownKey"
    filterable
    controlled-filter
    @filter-text-changed="filter = $event.detail"
  >
    <ul slot="content">
      <NvFielddropdownitem
        v-for="item in filteredItems"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
    </ul>
  </NvFielddropdown>
</template>

The critical addition is the :key on <NvFielddropdown>:

vue:key="dropdownKey"

where dropdownKey is a computed property derived from the filtered list:

tsconst dropdownKey = computed(() =>
  filteredItems.value.map((i) => i.value).join(',')
);

Every time the filtered list changes, the key changes, Vue tears down the old component and creates a new one, and Stencil slots everything correctly.

Key Generation Strategies

The key you choose should change whenever the slot content changes. Here are common approaches:

Joining item identifiers

Best when items have stable, unique IDs:

tsconst dropdownKey = computed(() =>
  items.value.map((i) => i.value).join(',')
);

Using the array length

Sufficient when items are only added or removed (not reordered):

tsconst dropdownKey = computed(() => items.value.length);

Using a counter

Useful when you want explicit control over when to re-mount:

tsconst revision = ref(0);

// Call this whenever slot content changes
function handleItemsChanged(newItems) {
  items.value = newItems;
  revision.value++;
}
vue<NvFielddropdown :key="revision">
  <!-- ... -->
</NvFielddropdown>

Hashing or serializing

For complex cases where the content shape matters:

tsconst dropdownKey = computed(() => JSON.stringify(items.value));

Tip: Prefer the simplest key that captures meaningful changes. Overly granular keys (like JSON.stringify) cause more re-mounts than necessary, while overly coarse keys (like items.length) may miss reorder changes.

Full Example: Filtered Dropdown

This is a complete working pattern using NvFielddropdown with dynamic filtering. See the Advanced NvFielddropdown Examples for interactive demonstrations.

vue<script setup lang="ts">
import { ref, computed } from 'vue';
import { NvFielddropdown, NvFielddropdownitem } from '@nova-design-system/nova-vue';

const filter = ref('');
const value = ref('');
const dropdownRef = ref<HTMLNvFielddropdownElement | null>(null);

const items = ref([
  { label: 'Berlin', value: 'berlin' },
  { label: 'Hamburg', value: 'hamburg' },
  { label: 'Munich', value: 'munich' },
  { label: 'Frankfurt', value: 'frankfurt' },
  { label: 'Cologne', value: 'cologne' },
]);

const filteredItems = computed(() => {
  if (!filter.value.trim()) return items.value;
  const lower = filter.value.toLowerCase();
  return items.value.filter((item) =>
    item.label.toLowerCase().includes(lower)
  );
});

// Key changes whenever the filtered results change
const dropdownKey = computed(() =>
  filteredItems.value.map((i) => i.value).join(',')
);
</script>

<template>
  <NvFielddropdown
    :key="dropdownKey"
    ref="dropdownRef"
    filterable
    controlled-filter
    :value="value"
    label="Select a City"
    :options="filteredItems"
    @filter-text-changed="filter = $event.detail"
    @value-changed="value = $event.detail"
  >
    <ul slot="content">
      <NvFielddropdownitem
        v-if="!filteredItems.length"
        disabled
        data-empty
      >
        No results found
      </NvFielddropdownitem>
    </ul>
  </NvFielddropdown>
</template>

When Is This Necessary?

You only need this pattern when slot content changes dynamically. Specifically:

ScenarioKey needed?
Static list of itemsNo
Filtering / searching itemsYes
Adding or removing itemsYes
Reordering itemsYes
Changing props on existing items onlyNo
Toggling visibility of a slot childYes

If your component's slotted children never change after the initial render, no special handling is required.

Caveats

  • Component state resets on re-mount. Because changing the key destroys and recreates the component, any internal state (e.g., open/closed state, scroll position, filter text) will be lost. Lift any state you need to preserve into your component's ref or reactive state above the keyed component.
  • Template refs are reassigned. If you hold a template ref to the component, it will point to the new instance after re-mount. This is generally fine, but be aware of it if you're caching the ref value.
  • Performance. Re-mounting is more expensive than patching. For most use cases this is negligible, but avoid key patterns that change on every render if slot content hasn't actually changed.

Summary

Because Stencil's light-DOM slot polyfill moves DOM nodes in ways that Vue cannot track, the recommended approach for dynamic slot content is:

When slot content changes dynamically, set a :key on the parent web component that reflects the current content. This forces Vue to re-mount the component, keeping Vue and the web component in sync.

Related Documentation