File tree Expand file tree Collapse file tree 4 files changed +62
-2
lines changed
src/internal/client/dom/elements
tests/runtime-browser/custom-elements-samples/propagate-prop-changes Expand file tree Collapse file tree 4 files changed +62
-2
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' svelte ' : patch
3
+ ---
4
+
5
+ fix: propagate custom element component prop changes
Original file line number Diff line number Diff line change 1
1
import { createClassComponent } from '../../../../legacy/legacy-client.js' ;
2
2
import { destroy_effect , render_effect } from '../../reactivity/effects.js' ;
3
3
import { append } from '../template.js' ;
4
- import { define_property , object_keys } from '../../../shared/utils.js' ;
4
+ import { define_property , get_descriptor , object_keys } from '../../../shared/utils.js' ;
5
5
6
6
/**
7
7
* @typedef {Object } CustomElementPropDefinition
@@ -305,7 +305,18 @@ export function create_custom_element(
305
305
set ( value ) {
306
306
value = get_custom_element_value ( prop , value , props_definition ) ;
307
307
this . $$d [ prop ] = value ;
308
- this . $$c ?. $set ( { [ prop ] : value } ) ;
308
+ var component = this . $$c ;
309
+
310
+ if ( component ) {
311
+ // // If the instance has an accessor, use that instead
312
+ var setter = get_descriptor ( component , prop ) ?. get ;
313
+
314
+ if ( setter ) {
315
+ component [ prop ] = value ;
316
+ } else {
317
+ component . $set ( { [ prop ] : value } ) ;
318
+ }
319
+ }
309
320
}
310
321
} ) ;
311
322
} ) ;
Original file line number Diff line number Diff line change
1
+ import { flushSync } from 'svelte' ;
2
+ import { test } from '../../assert' ;
3
+ const tick = ( ) => Promise . resolve ( ) ;
4
+
5
+ export default test ( {
6
+ async test ( { assert, target } ) {
7
+ target . innerHTML = '<custom-element></custom-element>' ;
8
+ await tick ( ) ;
9
+ await tick ( ) ;
10
+
11
+ /** @type {any } */
12
+ const el = target . querySelector ( 'custom-element' ) ;
13
+ const button = el . shadowRoot . querySelector ( 'button' ) ;
14
+
15
+ assert . equal ( button . textContent , '0' ) ;
16
+ assert . equal ( el . count , 0 ) ;
17
+
18
+ button . click ( ) ;
19
+
20
+ flushSync ( ) ;
21
+
22
+ assert . equal ( button . textContent , '1' ) ;
23
+ assert . equal ( el . count , 1 ) ;
24
+
25
+ el . count = 0 ;
26
+
27
+ assert . equal ( button . textContent , '0' ) ;
28
+ assert . equal ( el . count , 0 ) ;
29
+
30
+ button . click ( ) ;
31
+
32
+ flushSync ( ) ;
33
+
34
+ assert . equal ( button . textContent , '1' ) ;
35
+ assert . equal ( el . count , 1 ) ;
36
+ }
37
+ } ) ;
Original file line number Diff line number Diff line change
1
+ <svelte:options customElement =" custom-element" />
2
+
3
+ <script >
4
+ export let count = 0 ;
5
+ </script >
6
+
7
+ <button onclick ={() => count ++ }>{count }</button >
You can’t perform that action at this time.
0 commit comments