You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am having difficulties getting the slot feature working for Svelte Components that are compiled as Custom Elements. When I compile the component into a custom element and use it as follows
I expected "blue world" to replace the default slot content of <counter-component>. However, the default slot content (i.e. <span>This is the default slot </span> still gets rendered:
In addition, I can't seem to get the extend keyword to work with the Custom Element API:
Specifically, I extended the customElementConstructor with a class that simply prints "hello world" in its constructor body. However, "hello world" doesn't show up in the console whenever the custom element <counter-component> is mounted on the web page.
Check out the reproduction instructions below for details. The bottom of the instructions includes a .zip of the repository that you can use as well if it's more convenient.
Am I doing something wrong, or is this a bug?
Thanks for your time!
Reproduction
# Create a new project. Specify "SvelteKit demo app", "Typescript", and "Prettier" when prompted.
npm create svelte@latest my-app
cd my-app
npm install
npm install \
@rollup/plugin-commonjs \
@rollup/plugin-node-resolve \
@rollup/plugin-replace \
@rollup/plugin-typescript \
rollup-plugin-css-only \
rollup-plugin-scss \
rollup-plugin-svelte
In the included src/routes/Counter.svelte, insert a <svelte:options> element to mark the component as a custom element with a custom element constructor that simply prints "hello world". In addition, add an arbitrary <slot> element in the component's template:
Create a rollup.config.js file in the root of the repository, which is used to compile counter-component-wc.ts into a standalone counter-component-wc.js file:
// rollup.config.js importresolvefrom'@rollup/plugin-node-resolve';importsveltefrom'rollup-plugin-svelte';importscssfrom'rollup-plugin-scss'importtypescriptfrom'@rollup/plugin-typescript';importcommonjsfrom'@rollup/plugin-commonjs';importsveltePreprocessfrom'svelte-preprocess';exportdefault{input: 'src/counter-component-wc.ts',output: {file: 'public/counter-component-wc.js',format: 'iife',name: 'app',},plugins: [resolve({// Allow browser-specific functionality like onMount() to be // included in bundle.browser: true}),commonjs(),scss(),typescript(),svelte({preprocess: sveltePreprocess({scss: true}),compilerOptions: {customElement: true}}),]};
Run npx rollup -c rollup.config.js. counter-component-wc.js should be generated in the public/ directory.
Afterwards, create a simple index.html file that uses the custom element that we just compiled. The <counter-component> has an arbitrary <p> nested with a slot="header" attribute.
<!-- public/index.html --><!doctype html><html><head><title>Web Component Sandbox</title><metaname="viewport" content="width=device-width, initial-scale=1"><scriptsrc="./counter-component-wc.js"></script></head><body><h1>Hello World. This is a Web Component Demo.</h1><br/><counter-component><pslot="header">blue world</p></counter-component/></body></html>
Then open public/index.html in your browser.
I also have a .zip of the repository with the above edits. Extract the zip, run npm install, npx rollup -c rollup.config.js, and then open public/index.html in your browser.
It seems that browser resolve "await Promise.resolve()" extra fast (close to synchronous) so the DOM isn't actually updated further. This fixes an issue where web component slots don't appear when the script registering the web components is invoked before them being added to the DOM
fixes#8997
The problem with the slot is that the DOM for the slot isn't available when Svelte loads the inner component and determines whether or not a slot is present. You can work around it by moving the script tag to the bottom of the body tag.
Describe the bug
Hello,
I am having difficulties getting the slot feature working for Svelte Components that are compiled as Custom Elements. When I compile the component into a custom element and use it as follows
I expected "blue world" to replace the default slot content of
<counter-component>
. However, the default slot content (i.e.<span>This is the default slot </span>
still gets rendered:In addition, I can't seem to get the
extend
keyword to work with the Custom Element API:Specifically, I extended the customElementConstructor with a class that simply prints "hello world" in its constructor body. However, "hello world" doesn't show up in the console whenever the custom element
<counter-component>
is mounted on the web page.Check out the reproduction instructions below for details. The bottom of the instructions includes a .zip of the repository that you can use as well if it's more convenient.
Am I doing something wrong, or is this a bug?
Thanks for your time!
Reproduction
In the included
src/routes/Counter.svelte
, insert a<svelte:options>
element to mark the component as a custom element with a custom element constructor that simply prints "hello world". In addition, add an arbitrary<slot>
element in the component's template:Create a file called
counter-component-wc.ts
in thesrc/
directory that imports the .svelte file that we want to compile into a custom element:Create a rollup.config.js file in the root of the repository, which is used to compile
counter-component-wc.ts
into a standalonecounter-component-wc.js
file:Run
npx rollup -c rollup.config.js
.counter-component-wc.js
should be generated in thepublic/
directory.Afterwards, create a simple index.html file that uses the custom element that we just compiled. The
<counter-component>
has an arbitrary<p>
nested with aslot="header"
attribute.Then open public/index.html in your browser.
I also have a .zip of the repository with the above edits. Extract the zip, run
npm install
,npx rollup -c rollup.config.js
, and then openpublic/index.html
in your browser.my-app.zip
Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: