|
| 1 | +--- |
| 2 | +title: CustomStateSet |
| 3 | +slug: Web/API/CustomStateSet |
| 4 | +tags: |
| 5 | + - API |
| 6 | + - Interface |
| 7 | + - Reference |
| 8 | + - CustomStateSet |
| 9 | +browser-compat: api.CustomStateSet |
| 10 | +--- |
| 11 | +{{DefaultAPISidebar("DOM")}} |
| 12 | + |
| 13 | +The **`CustomStateSet`** interface of the {{domxref('Document_Object_Model','','',' ')}} stores a list of possible states for a custom element to be in, and allows states to be added and removed from the set. |
| 14 | + |
| 15 | +## Description |
| 16 | + |
| 17 | +An HTML form element, such as a checkbox has different _states_, "checked" and "unchecked". Likewise, developers creating custom elements need to assign possible states to these elements. The `CustomStateList` allows these states to be stored, and accessed from the custom element. |
| 18 | + |
| 19 | +A instance of `CustomStateList` is returned by {{domxref("ElementInternals.states")}}. The `CustomStateList` object is described as _setlike_, and therefore the methods behave in a similar manner to those on a {{jsxref("Set")}}. |
| 20 | + |
| 21 | +Each value stored in a `CustomStateList` is a `<dashed-ident>`, in the format `--mystate`. |
| 22 | + |
| 23 | +### Interaction with CSS |
| 24 | + |
| 25 | +States are stored as a `<dashed-ident>` as this format can then be accessed from CSS using the _custom state pseudo-class_. |
| 26 | +In the same way that you can use CSS to determine if a checkbox is checked using the {{cssxref(":checked")}} pseudo-class, |
| 27 | +you can use a custom state pseudo-class to select a custom element that is in a certain state. |
| 28 | + |
| 29 | +## Properties |
| 30 | + |
| 31 | +- {{domxref("CustomStateSet.size")}} |
| 32 | + - : Returns the number of values in the `CustomStateSet`. |
| 33 | + |
| 34 | +## Methods |
| 35 | + |
| 36 | +- {{domxref("CustomStateSet.add()")}} |
| 37 | + - : Adds a value to the set, first checking that the _value_ is a `<dashed-ident>`. |
| 38 | +- {{domxref("CustomStateSet.clear()")}} |
| 39 | + - : Removes all elements from the `CustomStateSet` object. |
| 40 | +- {{domxref("CustomStateSet.delete()")}} |
| 41 | + - : Removes one value from the `CustomStateSet` object. |
| 42 | +- {{domxref("CustomStateSet.entries()")}} |
| 43 | + - : Returns a new iterator with the values for each element in the `CustomStateSet` in insertion order. |
| 44 | +- {{domxref("CustomStateSet.forEach()")}} |
| 45 | + - : Executes a provided function for each value in the `CustomStateSet` object. |
| 46 | +- {{domxref("CustomStateSet.has()")}} |
| 47 | + - : Returns a {{jsxref("Boolean")}} asserting whether an element is present with the given value. |
| 48 | +- {{domxref("CustomStateSet.keys()")}} |
| 49 | + - : An alias for {{domxref("CustomStateSet.values()")}}. |
| 50 | +- {{domxref("CustomStateSet.values()")}} |
| 51 | + - : Returns a new iterator object that yields the values for each element in the `CustomStateSet` object in insertion order. |
| 52 | + |
| 53 | +## Examples |
| 54 | + |
| 55 | +The following function adds and removes the state `--checked` to a `CustomStateSet`, then prints to the console `true` or `false` as the custom checkbox is checked or unchecked. |
| 56 | + |
| 57 | +The state of the element can be accessed from CSS using the custom state pseudo-class `--checked`. |
| 58 | + |
| 59 | +```js |
| 60 | +set checked(flag) { |
| 61 | + if (flag) { |
| 62 | + this._internals.states.add('--checked'); |
| 63 | + } else { |
| 64 | + this._internals.states.delete('--checked'); |
| 65 | + } |
| 66 | + |
| 67 | + console.log(this._internals.states.has('--checked')); |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +```css |
| 72 | +labeled-checkbox { border: dashed red; } |
| 73 | +labeled-checkbox:--checked { border: solid; } |
| 74 | +``` |
| 75 | + |
| 76 | +## Specifications |
| 77 | + |
| 78 | +{{Specifications}} |
| 79 | + |
| 80 | +## Browser compatibility |
| 81 | + |
| 82 | +{{Compat}} |
| 83 | + |
0 commit comments