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
- Resolve open question on automatically exposing attributes and properties on the element rather than on the behavior.
- Resolve to propose the current API shape where web developers would hold direct references to behaviors.
- `behaviors` property on `ElementInternals` is a read-only `FrozenArray`.
239
239
- Developers hold direct references to their behavior instances.
240
240
241
241
*Note: An ordered array is preferred over a set because order may be significant for [conflict resolution](#behavior-composition-and-conflict-resolution). `behaviors` uses a `FrozenArray` because behaviors are immutable after attachment.*
242
242
243
-
**Pros:**
244
-
- Single property name.
245
-
- No array lookup or `instanceof` checks needed as developers hold direct references.
246
-
- *Future* developer-defined behaviors are simpler: just instantiate and attach.
247
-
- Consistent mental model: behaviors are objects you create and manage.
248
-
249
-
**Cons:**
250
-
- Requires developers to manage behavior instances themselves.
251
-
- More setup code compared to passing class references directly.
243
+
Developers hold direct references to their behavior instances. This aligns with the [W3C design principle that classes should have constructors](https://www.w3.org/TR/design-principles/#constructors) that allow authors to create and configure instances, and it extends naturally to future developer-defined behaviors that follow the same `new` + attach pattern.
- Named access via `this._internals.behaviors.<behaviorName>` requires no iteration.
287
-
- Less setup code as developers don't manage behavior instances.
288
-
289
-
**Cons:**
290
-
- Platform instantiates the behavior, so constructor parameters aren't available. This conflicts with the [design principle that classes should have constructors](https://www.w3.org/TR/design-principles/#constructors) that allow authors to create and configure instances.
291
-
- Requires a `behaviors` interface for named access.
292
-
- *Future* developer-defined behaviors would need a way to name their behaviors.
293
-
294
265
### Behavior composition and conflict resolution
295
266
296
267
When multiple behaviors are attached to an element, they may provide overlapping capabilities. This section discusses strategies for resolving such conflicts.
@@ -839,17 +810,6 @@ Although this proposal currently focuses on custom elements, the behavior patter
839
810
840
811
## Open questions
841
812
842
-
### Should behavior properties be automatically exposed on the element?
843
-
844
-
The current proposal uses manual property delegation: developers create getters/setters that delegate to the stored behavior instance. This gives authors full control over their public API, avoids naming conflicts, and allows validation or side effects in setters. The tradeoff is boilerplate for each exposed property.
845
-
846
-
Two alternatives have been considered:
847
-
848
-
- **Automatic property exposure:** The platform adds behavior properties directly to the custom element (e.g., `btn.disabled=true` works without any getter/setter). This matches how native elements work but introduces naming conflicts, reduces API control, and feels "magical."
849
-
- **Opt-in automatic exposure:** Authors choose per-element whether properties are auto-exposed via an option like `exposeProperties:true`. This offers flexibility but adds API complexity.
850
-
851
-
Future behaviors like `HTMLCheckboxBehavior` or `HTMLInputBehavior` would require developers to write boilerplate for key properties (`checked`, `value`) that external code needs to access. A consistent approach across all behaviors should be decided before additional behaviors ship.
852
-
853
813
### Is there a better name than "behavior" for this concept?
854
814
855
815
The American English spelling of behavior throughout this proposal follows the [WHATWG spec style guidelines](https://wiki.whatwg.org/wiki/Specs/style#:~:text=Use%20standard%20American%20English%20spelling). However, the word "behavior" has some drawbacks:
`HTMLSubmitButtonBehavior` could itself be designed as a decorator, but decorators can't easily access `ElementInternals` or instance state during application. Decorators would need to coordinate with `attachInternals()` timing. Additionally, getting a reference to the behavior instance for property access (e.g., `behavior.formAction`) would require additional wiring.
1044
1004
1005
+
### Alternative to API design: Class references
1006
+
1007
+
Pass behavior classes (not instances) to `attachInternals()`:
1008
+
1009
+
```javascript
1010
+
// Attach a behavior during initialization (class reference).
- Named access via `this._internals.behaviors.<behaviorName>` requires no iteration.
1019
+
- Less setup code as developers don't manage behavior instances.
1020
+
1021
+
**Cons:**
1022
+
- Platform instantiates the behavior, so constructor parameters aren't available. This conflicts with the [design principle that classes should have constructors](https://www.w3.org/TR/design-principles/#constructors) that allow authors to create and configure instances.
1023
+
- Requires a `behaviors` interface for named access.
1024
+
- *Future* developer-defined behaviors would need a way to name their behaviors.
1025
+
1045
1026
## Accessibility, security, and privacy considerations
0 commit comments