Skip to content

Commit 49ae330

Browse files
calebdwilliamsWestbrook
authored andcommitted
feat: add CustomStateSet
1 parent 65aba37 commit 49ae330

File tree

1 file changed

+69
-19
lines changed

1 file changed

+69
-19
lines changed

reports/2022.html

Lines changed: 69 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,51 +1322,101 @@ <h3>Links</h3>
13221322
<dt>Previous WCCG Report(s)</dt>
13231323
<dd>N/A</dd>
13241324
<dt>GitHub issues:</dt>
1325-
<dd>---</dd>
1325+
<dd>https://github.com/WICG/webcomponents/issues/738</dd>
13261326
<dt>Browser positions:</dt>
1327-
<dd>---</dd>
1327+
<dd>
1328+
<ul>
1329+
<li>
1330+
<strong>Chrome</strong> &mdash; shipped
1331+
</li>
1332+
<li>
1333+
<strong>Firefox</strong> &mdash;
1334+
</li>
1335+
<li>
1336+
<strong>Webkit</strong> &mdash; <a href="https://github.com/WebKit/standards-positions/issues/56">WebKit position</a>
1337+
</li>
1338+
</ul>
1339+
</dd>
13281340
</dl>
13291341
</section>
13301342
<section>
13311343
<h3>Description</h3>
1332-
<p>---</p>
1344+
1345+
<blockquote cite="https://wicg.github.io/custom-state-pseudo-class/#motivation">
1346+
<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>
13331350
</section>
13341351
<section>
13351352
<h3>Status</h3>
13361353
<ul>
1337-
<li>---</li>
1354+
<li>A <a href="https://wicg.github.io/custom-state-pseudo-class/#exposing">draft of the spec has been written in WICG</a>.</li>
13381355
</ul>
13391356
</section>
13401357
<section>
13411358
<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() {
1380+
this.#internals.states.add('--clicked');
1381+
this.removeEventListener('click', wasClicked);
1382+
});
1383+
}
1384+
}
1385+
customElements.define('fancy-element', FancyElement);
1386+
</code>
1387+
</pre>
1388+
1389+
<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>
13431402
</section>
13441403
<section>
13451404
<h3>Key Scenarios</h3>
1346-
<p>---</p>
1347-
</section>
1348-
<section>
1349-
<h3>Concerns</h3>
13501405
<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>
13521408
</ul>
13531409
</section>
13541410
<section>
1355-
<h3>Dissenting Opinion</h3>
1411+
<h3>Concerns</h3>
13561412
<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>
13581414
</ul>
13591415
</section>
13601416
<section>
13611417
<h3>Related Specs</h3>
13621418
<ul>
1363-
<li>---</li>
1364-
</ul>
1365-
</section>
1366-
<section>
1367-
<h3>Open Questions</h3>
1368-
<ul>
1369-
<li>---</li>
1419+
<li><a href="https://html.spec.whatwg.org/multipage/custom-elements.html#custom-elements-face-example">Form-associated custom elements</a></li>
13701420
</ul>
13711421
</section>
13721422
</section>

0 commit comments

Comments
 (0)