Performance: Passing styles to components (--style-props)
This affects rendering speed and visual stability by controlling how styles are applied and updated in component trees.
<ChildComponent style={{ color: 'red', fontSize: '1.2rem' }} /><ChildComponent style="color: red; font-size: 1.2rem;" />| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Inline style string props | Minimal DOM nodes | Triggers 1 reflow per update | Medium paint cost | [X] Bad |
| Style object props | Minimal DOM nodes | Single reflow per batch update | Lower paint cost | [!] OK |
| CSS class usage | Minimal DOM nodes | No reflows from style changes | Lowest paint cost | [OK] Good |
export let to declare props that components accept.export let style; uses correct Svelte syntax. Options B and C use React or other framework styles. function style() {} is a function, not a prop declaration.<ChildComponent style="color: red; font-weight: bold;" />
<script>
export let style;
</script>
<div style={style}>Hello</div>ChildComponent?<script>
let style;
</script>
<div style={style}>Content</div>export let to receive values from parents.let style; without export, so it stays local and undefined.Button component that accepts custom styles from its parent. Which approach correctly passes and applies styles while keeping default styles intact?Button.svelte component:<script>
export let style = '';
</script>
<button style={`background: blue; color: white; ${style}`}>Click me</button> sets a default style string and appends the passed style, combining both. <script>
let style;
</script>
<button style={style}>Click me</button> misses export and default styles. <script>
export let style;
</script>
<button class={style}>Click me</button> uses class binding incorrectly for styles. <script> export let style; </script> <button>Click me</button>ignores the style prop.
Pass the test and this pattern moves unknown