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
<p>Build-in elements provided by user agents have certain “states” that can change over time depending on user interaction and other factors, and are exposed to web authors through pseudo classes. For example, some form controls have the “invalid” state, which is exposed through the :invalid pseudo-class.</p>
1347
+
1348
+
<p>Like built-in elements, custom elements can have various states to be in too, and custom element authors want to expose these states in a similar fashion as the built-in elements.</p>
1349
+
</blockquote>
1333
1350
</section>
1334
1351
<section>
1335
1352
<h3>Status</h3>
1336
1353
<ul>
1337
-
<li>---</li>
1354
+
<li>A <ahref="https://wicg.github.io/custom-state-pseudo-class/#exposing">draft of the spec has been written in WICG</a>.</li>
1338
1355
</ul>
1339
1356
</section>
1340
1357
<section>
1341
1358
<h3>Initial API Summary/Quick API Proposal</h3>
1342
-
<p>Summary or proposal based on current status; paragraph(s) and code.</p>
1359
+
<p>The <code>CustomStateSet</code> API allows component authors to expose internal component state for use in styling or other element-matching operations (such as <code>querySelector</code></p>
1360
+
1361
+
<p>This is different from a custom element sprouting a class (via <code>this.classList.add</code> in any state added to the custom element can be seen as internal (similar to the <code>:checked</code> pseud-selector for input elements).</code></p>
1362
+
1363
+
<p>To allow for this operation, a set-like API is exposed at <code>ElementInternals.prototype.states</code>, meaning that only custom elements can apply custom states. An example might look like the following:</p>
1364
+
1365
+
<pre>
1366
+
<code>
1367
+
class FancyElement extends HTMLElement {
1368
+
#internals = this.attachInternals();
1369
+
1370
+
constructor() {
1371
+
super();
1372
+
1373
+
const root = this.attachShadow({ mode: 'open' });
1374
+
const button = document.createElement('button');
1375
+
button.innerText = 'Add clicked state';
1376
+
button.setAttribute('part', 'btn');
1377
+
root.append(button);
1378
+
1379
+
this.addEventListener('click', function wasClicked() {
<p>Consumers of the <code>fancy-element</code> code can now take advantage of the <code>:--clicked</code> state in CSS or in any DOM querying API to modify or select the relevant element once clicked.</p>
1390
+
1391
+
<p>For example, to change the background of the element's <code>btn</code> part, a consuming developer could apply the following CSS:</p>
1392
+
1393
+
<pre>
1394
+
<code>
1395
+
:--clicked::part(btn) {
1396
+
background: rebeccapurple;
1397
+
}
1398
+
</code>
1399
+
</pre>
1400
+
1401
+
<p>Alternatively, a consuming developer could call <code>document.querySelectorAll(':--clicked')</code> to target all elements with the custom state.</p>
1343
1402
</section>
1344
1403
<section>
1345
1404
<h3>Key Scenarios</h3>
1346
-
<p>---</p>
1347
-
</section>
1348
-
<section>
1349
-
<h3>Concerns</h3>
1350
1405
<ul>
1351
-
<li>---</li>
1406
+
<li>Allow custom element authors to expose internal state for use in CSS</li>
1407
+
<li>Allow custom element authros to expose internal state for use in DOM selection operations</li>
1352
1408
</ul>
1353
1409
</section>
1354
1410
<section>
1355
-
<h3>Dissenting Opinion</h3>
1411
+
<h3>Concerns</h3>
1356
1412
<ul>
1357
-
<li>---</li>
1413
+
<li>Most of the concerns around this topic are related to syntax rather than functionality. For example, some prefer the selector to match state to be something along the lines of <code>:state(--state)</code>; however, it appears as if the current status is fairly accepted right now.</li>
0 commit comments