Skip to content

How can I programmatically set a parameterless Svelte component as the default value of the default slot in Svelte 5? #13732

@i18nsite

Description

@i18nsite

I have a virtual scrollbar, In svelte4 I can programmatically set a svelte component to the default value of the slot.

How can I achieve the same effect in Svelte 5?

@2-/svelte_slot/index.js

  import { detach, mount_component, noop } from "svelte/internal"

  // https://github.com/sveltejs/svelte/issues/2588#issuecomment-1134612872
  export default (svelte_component) => () => {
    let c
    return {
      c: noop,
      // mount
      m: (target, anchor) => {
        mount_component(
         (c = new svelte_component({
            target,
          })),
          target,
          anchor,
          null,
        )
      },
      // destroy
      d: (detaching) => {
        if (detaching) {
          detach(c)
        }
      },
      l: noop,
    }
  }

scrollbar.js

  import slot from "@2-/svelte_slot"
  import Mfs from "@8p/topbar/Mfs.svelte"

  export default (svelte) => {
    return class extends Mfs {
      constructor(opts) {
        let { props } = opts
        if (!props) {
          opts.props = props = {}
        }
        props.$$scope = {}
        props.$$slots = { default: [slot(svelte)] }
        super(opts)
      }
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions