-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmixins-exploration.html
More file actions
1402 lines (1240 loc) · 49.8 KB
/
mixins-exploration.html
File metadata and controls
1402 lines (1240 loc) · 49.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Mixins & Custom Attributes: An Interactive Exploration</title>
<style>
:root {
--primary: #1976d2;
--secondary: #dc004e;
--success: #4caf50;
--warning: #ff9800;
--bg: #ffffff;
--surface: #f5f5f5;
--text: #212121;
--text-secondary: #757575;
--border: #e0e0e0;
--code-bg: #2d2d2d;
--code-text: #f8f8f2;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #121212;
--surface: #1e1e1e;
--text: #ffffff;
--text-secondary: #b0b0b0;
--border: #333333;
}
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
line-height: 1.6;
color: var(--text);
background: var(--bg);
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
header {
text-align: center;
padding: 40px 20px;
background: linear-gradient(135deg, var(--primary), var(--secondary));
color: white;
border-radius: 12px;
margin-bottom: 40px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
}
h1 {
font-size: 2.5rem;
margin-bottom: 10px;
}
.subtitle {
font-size: 1.2rem;
opacity: 0.9;
}
nav {
background: var(--surface);
padding: 20px;
border-radius: 8px;
margin-bottom: 30px;
position: sticky;
top: 20px;
z-index: 100;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
nav ul {
list-style: none;
display: flex;
flex-wrap: wrap;
gap: 15px;
justify-content: center;
}
nav a {
color: var(--primary);
text-decoration: none;
padding: 8px 16px;
border-radius: 4px;
transition: all 0.2s;
font-weight: 500;
}
nav a:hover {
background: var(--primary);
color: white;
}
section {
background: var(--surface);
padding: 30px;
margin-bottom: 30px;
border-radius: 8px;
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
}
h2 {
color: var(--primary);
margin-bottom: 20px;
font-size: 2rem;
border-bottom: 3px solid var(--primary);
padding-bottom: 10px;
}
h3 {
color: var(--secondary);
margin: 25px 0 15px;
font-size: 1.5rem;
}
h4 {
color: var(--text);
margin: 20px 0 10px;
font-size: 1.2rem;
}
a {
color: var(--primary);
text-decoration: none;
position: relative;
transition: color 0.2s ease;
}
a:hover {
color: #1565c0;
text-decoration: underline;
}
a:visited {
color: #7b1fa2;
}
a:focus {
outline: 2px solid var(--primary);
outline-offset: 2px;
border-radius: 2px;
}
a:active {
color: var(--secondary);
}
/* List styling */
ul, ol {
margin-left: 50px;
padding-left: 0;
}
/* Style for external links */
a[href^="http"]::after,
a[href^="https://"]::after {
content: "↗";
font-size: 0.8em;
margin-left: 3px;
opacity: 0.6;
}
/* Links inside paragraphs and lists */
p a,
li a {
font-weight: 500;
border-bottom: 1px solid transparent;
}
p a:hover,
li a:hover {
border-bottom-color: var(--primary);
}
/* Code-styled links */
code a {
font-family: inherit;
background: rgba(25, 118, 210, 0.1);
padding: 2px 4px;
border-radius: 3px;
}
.comparison-table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
background: var(--bg);
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.comparison-table th,
.comparison-table td {
padding: 15px;
text-align: left;
border-bottom: 1px solid var(--border);
}
.comparison-table th {
background: var(--primary);
color: white;
font-weight: 600;
}
.comparison-table tr:last-child td {
border-bottom: none;
}
.comparison-table tr:hover {
background: rgba(25, 118, 210, 0.05);
}
.pro {
color: var(--success);
font-weight: 500;
}
.con {
color: var(--secondary);
font-weight: 500;
}
.code-block {
background: var(--code-bg);
color: var(--code-text);
padding: 20px;
border-radius: 8px;
overflow-x: auto;
margin: 15px 0;
font-family: 'Courier New', Courier, monospace;
font-size: 0.9rem;
line-height: 1.5;
box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);
}
/* Pre and code elements */
pre {
background: var(--code-bg);
color: var(--code-text);
padding: 20px;
border-radius: 8px;
overflow-x: auto;
margin: 15px 0;
box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);
}
code {
font-family: 'Courier New', Courier, monospace;
font-size: 0.9rem;
line-height: 1.5;
}
pre code {
background: none;
padding: 0;
border-radius: 0;
display: block;
color: inherit;
}
/* Inline code */
p code,
li code,
td code {
background: rgba(25, 118, 210, 0.1);
color: var(--primary);
padding: 2px 6px;
border-radius: 3px;
font-size: 0.85em;
font-weight: 500;
}
.demo-area {
background: var(--bg);
border: 2px solid var(--border);
border-radius: 8px;
padding: 20px;
margin: 20px 0;
}
.demo-controls {
display: flex;
gap: 10px;
flex-wrap: wrap;
margin-bottom: 15px;
}
button {
background: var(--primary);
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 1rem;
transition: all 0.2s;
font-weight: 500;
}
button:hover {
background: #1565c0;
transform: translateY(-1px);
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
}
button:active {
transform: translateY(0);
}
.output {
background: var(--surface);
padding: 15px;
border-radius: 4px;
min-height: 50px;
margin-top: 15px;
font-family: monospace;
white-space: pre-wrap;
word-break: break-all;
}
.status-badge {
display: inline-block;
padding: 4px 12px;
border-radius: 12px;
font-size: 0.85rem;
font-weight: 600;
margin-left: 10px;
}
.status-proposed {
background: var(--warning);
color: white;
}
.status-experimental {
background: var(--secondary);
color: white;
}
.status-partial {
background: #ff9800;
color: white;
}
.info-box {
background: rgba(25, 118, 210, 0.1);
border-left: 4px solid var(--primary);
padding: 15px;
margin: 15px 0;
border-radius: 4px;
}
.warning-box {
background: rgba(255, 152, 0, 0.1);
border-left: 4px solid var(--warning);
padding: 15px;
margin: 15px 0;
border-radius: 4px;
}
.use-case {
background: var(--bg);
padding: 15px;
margin: 10px 0;
border-radius: 6px;
border-left: 3px solid var(--success);
}
.resources {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 15px;
margin: 20px 0;
}
.resource-card {
background: var(--bg);
padding: 15px;
border-radius: 6px;
border: 1px solid var(--border);
transition: all 0.2s;
}
.resource-card:hover {
border-color: var(--primary);
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.resource-card h4 {
margin-top: 0;
}
.resource-card a {
color: var(--primary);
text-decoration: none;
word-break: break-all;
}
.resource-card a:hover {
text-decoration: underline;
}
footer {
text-align: center;
padding: 30px;
color: var(--text-secondary);
margin-top: 50px;
}
@media (max-width: 768px) {
h1 {
font-size: 2rem;
}
nav ul {
flex-direction: column;
}
.comparison-table {
font-size: 0.9rem;
}
.comparison-table th,
.comparison-table td {
padding: 10px;
}
}
/* Custom element styles for demos */
custom-button {
display: inline-block;
padding: 10px 20px;
background: var(--success);
color: white;
border-radius: 4px;
cursor: pointer;
user-select: none;
}
custom-button:hover {
background: #45a049;
}
custom-label {
display: inline-block;
font-weight: 600;
margin-bottom: 5px;
color: var(--text);
}
.demo-form {
display: flex;
flex-direction: column;
gap: 15px;
max-width: 400px;
}
.form-group {
display: flex;
flex-direction: column;
gap: 5px;
}
input[type="text"],
input[type="email"] {
padding: 8px 12px;
border: 1px solid var(--border);
border-radius: 4px;
font-size: 1rem;
background: var(--bg);
color: var(--text);
}
input:focus {
outline: 2px solid var(--primary);
outline-offset: 2px;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>Mixins: Exploration</h1>
<p class="subtitle">Ana Sollano Kim</p>
<p class="date">November 2025</p>
</header>
<nav>
<ul>
<li><a href="#introduction">Why?</a></li>
<li><a href="#problem">The Problem</a></li>
<li><a href="#proposals">Potential solutions</a></li>
<li><a href="#comparison">Comparison</a></li>
<li><a href="#future-proposals">Future</a></li>
<li><a href="#demos">Demos</a></li>
<li><a href="#resources">Resources</a></li>
</ul>
</nav>
<section id="introduction">
<h2>Why?</h2>
<p>
Custom element authors frequently need custom elements to leverage platform behaviors that are currently
exclusive to native HTML elements. These behaviors include
<a href="https://github.com/WICG/webcomponents/issues/814">form submission</a>,
<a href="https://github.com/whatwg/html/issues/9110">popover invocation</a>,
<a href="https://github.com/whatwg/html/issues/5423#issuecomment-1517653183">label behaviors</a>,
<a href="https://github.com/whatwg/html/issues/10220">custom elements acting as forms</a>,
<a href="https://github.com/whatwg/html/issues/11061#issuecomment-3250415103">radio button grouping</a>,
and more. This document explores various proposals and approaches to solve this problem.
</p>
<div class="info-box">
<strong>Core use case:</strong> Enable autonomous custom elements to acquire behaviors already
implemented in the platform (like submit buttons, labels, form participation) without requiring
excessive JavaScript or reimplementation of platform logic.
</div>
</section>
<section id="problem">
<h2>The Problem</h2>
<h3>Why can't custom elements do everything native elements can?</h3>
<div class="use-case">
<h4>Use case 1: Custom submit button</h4>
<p>
Custom button that can submit a form, just like <code><button type="submit"></code>
or <code><input type="submit"></code>.
</p>
<pre><code><custom-button>Submit</custom-button></code></pre>
<p>❌ Can't submit forms.</p>
</div>
<div class="use-case">
<h4>Use case 2: Custom label</h4>
<p>Custom element that can have the role of a label.</p>
<pre><code><custom-label for="myInput">Name</custom-label>
<input id="myInput"></code></pre>
<p>❌ Clicking the custom label doesn't focus the input and can't actually have "label" role.</p>
</div>
<div class="use-case">
<h4>Use case 3: Custom button can't invoke popovers or commands</h4>
<p>
You want a custom button to invoke popovers using the
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Popover_API">Popover API</a>
or trigger commands with the
<a href="https://developer.mozilla.org/en-US/docs/Web/API/Invoker_Commands_API">Invoker Commands API</a>.
</p>
<pre><code><custom-button popovertarget="my-popover">Open Popover</custom-button>
<div id="my-popover" popover>Popover content</div>
<custom-button command="show-dialog" commandfor="my-dialog">Show Dialog</custom-button>
<dialog id="my-dialog">Dialog content</dialog></code></pre>
<p>
❌ <code>popovertarget</code> and <code>popovertargetaction</code> only work on <code><button></code>
and <code><input></code> elements.<br>
❌ <code>command</code> and <code>commandfor</code> attributes only work on native buttons.<br>
</p>
<div class="info-box">
These are declarative platform features that let web authors control popovers and dialogs without JavaScript.
Custom elements can't use them, forcing developers to write custom JavaScript event handlers and manage
state manually.
</div>
</div>
<div class="use-case">
<h4>Use case 4: Custom element as a form</h4>
<p>
You want a custom element to act like a <code><form></code> element,
collecting and submitting data from its descendant form controls.
</p>
<pre><code><custom-form action="/submit" method="post">
<input name="username">
<input name="password" type="password">
<button type="submit">Login</button>
</custom-form></code></pre>
<p>❌ Custom elements can't act as form containers or participate in form submission.</p>
<div class="info-box">
This would enable custom components to encapsulate form behavior while maintaining
standard form semantics and accessibility. See
<a href="https://github.com/whatwg/html/issues/10220">WHATWG HTML issue #10220</a>.
</div>
</div>
<div class="use-case">
<h4>Use case 5: Custom radio button groups</h4>
<p>
You want custom elements to participate in radio button grouping behavior,
where selecting one deselects others in the same group.
</p>
<pre><code><custom-radio name="theme" value="light">Light Theme</custom-radio>
<custom-radio name="theme" value="dark">Dark Theme</custom-radio>
<custom-radio name="theme" value="auto">Auto Theme</custom-radio></code></pre>
<p>❌ Custom elements can't participate in radio button grouping or mutual exclusion behavior.</p>
<div class="info-box">
Radio button semantics require platform-level coordination between elements with the same
<code>name</code> attribute. This can't be fully polyfilled with JavaScript alone.
</div>
</div>
<h3>Current solutions & their limitations</h3>
<h4>1. Customized built-ins (<code>is/extends</code> syntax)</h4>
<pre><code>class FancyButton extends HTMLButtonElement {
constructor() {
super();
}
}
customElements.define('fancy-button', FancyButton, { extends: 'button' });
// Usage:
<button is="fancy-button">Click me</button></code></pre>
<p><span class="pro">✅</span></p>
<ul>
<li>Inherits all native button behaviors and styling.</li>
<li>Works with form submission and accessibility.</li>
</ul>
<p><span class="con">❌</span></p>
<ul>
<li>
Not supported in
<a href="https://github.com/WebKit/standards-positions/issues/97#issuecomment-1328880274">WebKit</a>
(deal-breaker for interoperability).
</li>
<li>Can't attach shadow DOM to most customized built-ins.</li>
<li>
Can't use the
<a href="https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals">Element Internals API</a>.
</li>
<li>Logically different from extended element but can't use <code>ElementInternals</code> for a11y customizations.</li>
<li>"Awkward" <code>is=</code> syntax feels unnatural in HTML.</li>
</ul>
<h4>2. Form-associated custom elements (FACE)</h4>
<pre><code>class CustomInput extends HTMLElement {
static formAssociated = true;
constructor() {
super();
this.internals_ = this.attachInternals();
}
formResetCallback() { /* ... */ }
formStateRestoreCallback() { /* ... */ }
}
customElements.define('custom-input', CustomInput);</code></pre>
<p><span class="pro">✅</span></p>
<ul>
<li>Supported in all modern browsers.</li>
</ul>
<p><span class="con">❌</span></p>
<ul>
<li>Can't trigger form submission.</li>
<li>Can't use <code>popovertarget</code>, <code>commandfor</code>, etc.</li>
<li>Can't act as a label for other controls.</li>
</ul>
<div class="warning-box">
There's no way for autonomous custom elements to acquire specific platform
behaviors like button activation, label association, or other capabilities that
native elements have.
</code></pre>
</section>
<section id="proposals">
<h2>Potential solutions</h2>
<h3>1. Custom attributes <span class="status-badge status-proposed">Proposed</span></h3>
<p>
Allow developers to define custom attributes with lifecycle callbacks that can add behavior to
any element.
</p>
<pre><code>// Define a custom attribute
class LogAttribute extends Attribute {
connectedCallback() {
this.ownerElement.addEventListener('click',
() => console.log(this.value)
);
}
disconnectedCallback() {
// Cleanup
}
changedCallback() {
// Value changed
}
}
HTMLElement.attributeRegistry.define('log-to-console', LogAttribute);
// Usage:
<div log-to-console="Button clicked!">Click me</div></code></pre><span class="pro">✅</span></p>
<ul>
<li>Works with native and custom elements.</li>
<li>Can use multiple attributes (composability).</li>
<li>Lifecycle model similar to custom elements.</li>
</ul>
<p><span class="con">❓</span></p>
<ul>
<li>
Authors must implement all behavior in JavaScript.
The browser only provides the mechanism (lifecycle hooks, attribute registry). For example, to
implement popover behavior on a custom element, authors would create a custom attribute but
still need to manually implement all the popover logic that native buttons get automatically with
<code>popovertarget</code>.
</li>
<li>
There are performance concerns with <code>Attr</code> node creation. Implementations optimize attributes,
they're not <code>Attr</code> objects.
</li>
<li>
If behaviors depend on pairs or groups of attributes, like <code>popovertarget</code> and
<code>popovertargetaction</code>, and each attribute is implemented as an independent
<code>Attr</code> class, developers need a way to make them cooperate without duplicating
logic.
</li>
<li>
Namespace/naming conflicts need resolution to avoid collisions with standard attributes, future
HTML attributes, or third-party libraries.
</li>
<li>Property reflection not currently considered for MVP.</li>
</ul>
<h3>2. ES Decorators <span class="status-badge status-experimental">Stage 3</span></h3>
<p>
TC39 proposal for decorator syntax that could be used with custom elements.
</p>
<pre><code>// Decorator function
function formSubmitter(value, { kind, name }) {
return class extends value {
connectedCallback() {
super.connectedCallback?.();
this.addEventListener('click', () => {
const form = this.closest('form');
if (form) {
form.requestSubmit(this);
}
});
}
};
}
// Usage
@formSubmitter
class SubmitButton extends HTMLElement {
// Implementation
}
</code></pre>
<p><span class="pro">✅</span></p>
<ul>
<li>Works well with classes.</li>
<li>A class can have multiple decorators (composability).</li>
<li>Stage 3 in TC39 (will be implemented by browsers eventually).</li>
<li>Can decorate classes, methods, fields, accessors.</li>
</ul>
<p><span class="con">❌</span></p>
<ul>
<li>Authors must implement all behavior in JavaScript (no declarative way of specifying behavior).</li>
<li>Web authors can't access native behaviors.</li>
<li>Not yet available in browsers.</li>
</ul>
<h3>3. JavaScript class mixins <span class="status-badge status-experimental">Available Today</span></h3>
<p>
Use JavaScript to create composable class mixins (subclass factories).
</p>
<pre><code>// Mixin as a subclass factory
const ButtonBehaviorMixin = (superclass) => class extends superclass {
connectedCallback() {
super.connectedCallback?.();
this.addEventListener('click', this.handleActivation);
}
handleActivation() {
// Button activation logic
}
};
const FormSubmitMixin = (superclass) => class extends superclass {
connectedCallback() {
super.connectedCallback?.();
// Form submission logic
}
};
// Compose mixins
class CustomButton extends ButtonBehaviorMixin(FormSubmitMixin(HTMLElement)) {
constructor() {
super();
// Custom button implementation
}
}
</code></pre>
<span class="pro">✅</span></p>
<ul>
<li>
Available today, no platform changes needed. Related proposal to standarize
a more readable syntax for mixins <a href="https://github.com/tc39/proposal-mixins">
Maximally Minimal Mixins</a>.
</li>
<li>Composable.</li>
<li>Standard JavaScript patterns.</li>
<li>Good for sharing behavior between custom elements.</li>
</ul>
<p><span class="con">❌</span></p>
<ul>
<li>Doesn't give access to platform behaviors.</li>
<li>Requires userland implementation of everything.</li>
<li>Complex inheritance chains can be hard to debug.</li>
</ul>
</section>
<section id="comparison">
<h2>Feature comparison</h2>
<table class="comparison-table">
<thead>
<tr>
<th>Feature</th>
<th>Custom Attributes</th>
<th>JS Mixins</th>
<th>Decorators</th>
<th>FACEs</th>
<th>Customized Built-ins</th>
</tr>
</thead>
<tbody>
<tr>
<td>Browser Support</td>
<td>❌ Proposed</td>
<td>✅</td>
<td>⚠️ Transpile (Chromium soon)</td>
<td>✅</td>
<td>⚠️ Not Webkit</td>
</tr>
<tr>
<td>Platform Behaviors</td>
<td>Potential</td>
<td>Potential</td>
<td>Potential</td>
<td>⚠️ Form only</td>
<td>✅</td>
</tr>
<tr>
<td>Works with Native Elements</td>
<td>✅</td>
<td>❌</td>
<td>❌</td>
<td>❌</td>
<td>✅</td>
</tr>
<tr>
<td>Shadow DOM Support</td>
<td>✅</td>
<td>✅</td>
<td>✅</td>
<td>✅</td>
<td>⚠️ Limited</td>
</tr>
<tr>
<td>Composability</td>
<td>✅</td>
<td>✅</td>
<td>✅</td>
<td>⚠️ Single element</td>
<td>❌</td>
</tr>
<tr>
<td>Performance</td>
<td>⚠️ Concerns</td>
<td>✅</td>
<td>✅</td>
<td>✅</td>
<td>✅</td>
</tr>
<tr>
<td>Developer Ergonomics</td>
<td>✅ Similar to CE</td>
<td>⚠️ Complex</td>
<td>✅ Clean syntax</td>
<td>✅ Simple</td>
<td>⚠️ "Awkward"</td>
</tr>
<tr>
<td>Framework Patterns</td>
<td>Matches Vue/Alpine</td>
<td>Common JS</td>
<td>Python-like</td>
<td>Custom Elements</td>
<td>Unique</td>
</tr>
</tbody>
</table>
</section>
<section id="future-proposals">
<h2>🔮 Platform-provided mixins</h2>
<p>
There's emerging discussion about the platform itself providing reusable behavior classes that custom
elements could adopt.
</p>
<h3> Platform behavior mixins via ElementInternals</h3>
<p>
As discussed in
<a href="https://github.com/whatwg/html/issues/11061#issuecomment-2673966134" target="_blank">this comment</a>
and in
<a href="https://github.com/WICG/webcomponents/issues/814#issuecomment-3392480397" target="_blank">
WICG issue #814</a>, the platform could expose behavior classes that developers attach to custom elements
through <code>ElementInternals</code>.
</p>
<pre><code>import {CustomMixin} from './my-mixin.js'
class CustomButton extends HTMLElement {
static formAssociated = true;
constructor() {
super();
const internals = this.attachInternals();
// Add the built-in mixin for button activation behavior
// This grants popovertarget, commandfor, click activation, etc.
internals.addBehavior(HTMLButtonActivationBehavior);
// Could also add form submission behavior.
internals.addBehavior(HTMLSubmitBehavior);
// Or label behavior.
internals.addBehavior(HTMLLabelBehavior);
// Can also adopt custom userland behaviors.
internals.addBehavior(CustomMixin);
}
}</code></pre>
<h3>How it would work</h3>
<div class="info-box">
The browser provides behavior classes (like <code>HTMLButtonActivationBehavior</code>) that encapsulate
specific platform capabilities. Custom elements can mix and match these behaviors to get exactly the
platform integration they need.
</div>
<h4>Examples of platform-provided behavior classes</h4>
<ul>
<li>
<code>HTMLButtonActivationBehavior</code>: click/keyboard activation, <code>popovertarget</code>,
<code>commandfor</code> support, implicit ARIA <code>role="button"</code>.
</li>
<li>
<code>HTMLSubmitBehavior</code>: the FACE can be selected by <code>:default</code>, can be
automatically clicked by the implicit submission of other form elementsm, and has implicit ARIA
<code>role="button"</code>.
</li>
<li><code>HTMLResetBehavior</code>: the FACE can trigger the associated form to reset.</li>
<li><code>HTMLLabelBehavior</code>: has label association via 'for' attribute and click delegation.</li>
<li><code>HTMLFormBehavior</code>: can act as a form container and collect descendant controls.</li>
<li><code>HTMLRadioGroupBehavior</code>: can provide radio button mutual exclusion by name.</li>
</ul>
<h4>Each behavior class would have to provide</h4>
<ul>
<li><strong>Event handlers:</strong> Automatic wiring of events like click, keydown, etc.</li>
<li><strong>Attributes:</strong> Handling of related attributes (disabled, for, popovertarget, etc.)</li>
<li><strong>CSS pseudo-classes:</strong> Matching of relevant selectors (:disabled, :default, etc.)</li>
<li><strong>ARIA:</strong> Default roles and properties.</li>
</ul>
<h3>Spec changes needed (WIP)</h3>
<p><a href="https://html.spec.whatwg.org/dev/custom-elements.html">HTML Standard's Custom Elements section</a>:</p>
<ol>
<li>
<strong>Define behavior interface:</strong> Create a <code>HTMLElementBehavior</code> base interface
that all platform behaviors implement.
</li>
<li>
<strong>Extend <code>ElementInternals</code>:</strong> Add <code>addBehavior()</code>❓, <code>removeBehavior()</code>❓,