Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-comics-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: `append_styles` in an effect to make them available on mount
4 changes: 2 additions & 2 deletions packages/svelte/src/internal/client/dom/css.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { DEV } from 'esm-env';
import { queue_micro_task } from './task.js';
import { register_style } from '../dev/css.js';
import { effect } from '../reactivity/effects.js';

/**
* @param {Node} anchor
* @param {{ hash: string, code: string }} css
*/
export function append_styles(anchor, css) {
// Use `queue_micro_task` to ensure `anchor` is in the DOM, otherwise getRootNode() will yield wrong results
queue_micro_task(() => {
effect(() => {
var root = anchor.getRootNode();

var target = /** @type {ShadowRoot} */ (root).host
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { test } from '../../assert';
const tick = () => Promise.resolve();

export default test({
async test({ assert, target }) {
target.innerHTML = '<custom-element></custom-element>';
/** @type {any} */
const el = target.querySelector('custom-element');

/** @type {string} */
let html = '';
const handle_evt = (e) => (html = e.detail);
el.addEventListener('html', handle_evt);

await tick();
await tick();
await tick();

assert.ok(html.includes('<style'));
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<svelte:options css="injected" customElement="custom-element"/>

<script lang="ts">
$effect(() => {
$host().dispatchEvent(new CustomEvent("html", { detail: $host().shadowRoot?.innerHTML }));
})
</script>

<button class="btn">btn</button>

<style >
.btn {
width: 123px;
height: 123px;
}
</style>
Loading