Skip to content

Commit fdecad6

Browse files
authored
chore: better bind_property implementation (#11904)
- better name for bidirectional bindings - cleanup implementation
1 parent ba017db commit fdecad6

File tree

3 files changed

+21
-26
lines changed

3 files changed

+21
-26
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/template.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2747,10 +2747,9 @@ export const template_visitors = {
27472747
'$.bind_property',
27482748
b.literal(node.name),
27492749
b.literal(property.event),
2750-
b.literal(property.type ?? 'get'),
27512750
state.node,
2752-
getter,
2753-
setter
2751+
setter,
2752+
property.bidirectional && getter
27542753
);
27552754
} else {
27562755
// special cases

packages/svelte/src/compiler/phases/bindings.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @typedef BindingProperty
33
* @property {string} [event] This is set if the binding corresponds to the property name on the dom element it's bound to
44
* and there's an event that notifies of a change to that property
5-
* @property {string} [type] Set this to `set` if updates are written to the dom property
5+
* @property {boolean} [bidirectional] Set this to `true` if updates are written to the dom property
66
* @property {boolean} [omit_in_ssr] Set this to true if the binding should not be included in SSR
77
* @property {string[]} [valid_elements] If this is set, the binding is only valid on the given elements
88
* @property {string[]} [invalid_elements] If this is set, the binding is invalid on the given elements
@@ -175,7 +175,7 @@ export const binding_properties = {
175175
// checkbox/radio
176176
indeterminate: {
177177
event: 'change',
178-
type: 'set',
178+
bidirectional: true,
179179
valid_elements: ['input'],
180180
omit_in_ssr: true // no corresponding attribute
181181
},
@@ -200,7 +200,7 @@ export const binding_properties = {
200200
},
201201
open: {
202202
event: 'toggle',
203-
type: 'set',
203+
bidirectional: true,
204204
valid_elements: ['details']
205205
},
206206
value: {

packages/svelte/src/internal/client/dom/elements/bindings/universal.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,40 +33,36 @@ export function bind_content_editable(property, element, get_value, update) {
3333
/**
3434
* @param {string} property
3535
* @param {string} event_name
36-
* @param {'get' | 'set'} type
3736
* @param {Element} element
38-
* @param {() => unknown} get_value
39-
* @param {(value: unknown) => void} update
37+
* @param {(value: unknown) => void} set
38+
* @param {() => unknown} [get]
4039
* @returns {void}
4140
*/
42-
export function bind_property(property, event_name, type, element, get_value, update) {
43-
var target_handler = () => {
41+
export function bind_property(property, event_name, element, set, get) {
42+
var handler = () => {
4443
// @ts-ignore
45-
update(element[property]);
44+
set(element[property]);
4645
};
4746

48-
element.addEventListener(event_name, target_handler);
47+
element.addEventListener(event_name, handler);
4948

50-
if (type === 'set') {
49+
if (get) {
5150
render_effect(() => {
5251
// @ts-ignore
53-
element[property] = get_value();
52+
element[property] = get();
5453
});
54+
} else {
55+
handler();
5556
}
5657

57-
if (type === 'get') {
58-
// @ts-ignore
59-
update(element[property]);
60-
}
61-
62-
render_effect(() => {
63-
// @ts-ignore
64-
if (element === document.body || element === window || element === document) {
58+
// @ts-ignore
59+
if (element === document.body || element === window || element === document) {
60+
render_effect(() => {
6561
return () => {
66-
element.removeEventListener(event_name, target_handler);
62+
element.removeEventListener(event_name, handler);
6763
};
68-
}
69-
});
64+
});
65+
}
7066
}
7167

7268
/**

0 commit comments

Comments
 (0)