|
| 1 | +# CSS Typed OM Explained |
| 2 | + |
| 3 | +CSS Typed OM is an extensible API for the CSSOM that reduces the burden of manipulating a CSS property's value via string manipulation. It does so by exposing CSS values as typed JavaScript objects rather than strings. |
| 4 | + |
| 5 | +## Motivation |
| 6 | + |
| 7 | +The benefits of CSS Typed OM include: |
| 8 | +* Allows the performant manipulation of values assigned to CSS properties. |
| 9 | +* Being able to write more maintainable code: |
| 10 | + * Your code is easier to understand, |
| 11 | + * Easier to write more generic code. |
| 12 | + |
| 13 | +### Examples |
| 14 | + |
| 15 | +### Getting a property's value |
| 16 | + |
| 17 | +In CSSOM land: |
| 18 | +We get specified and computed values for CSS properties of an element in CSSOM via the `.style` attribute on an element and the `getComputedStyle()` object respectively. |
| 19 | + |
| 20 | +In Typed OM: |
| 21 | +We get them of `StylePropertyMaps` on elements. |
| 22 | + |
| 23 | +To get the specified height from an element we would use the following JS: |
| 24 | +``` |
| 25 | +element.attributeStyleMap.get('height'); // returns a string |
| 26 | +``` |
| 27 | + |
| 28 | +Similarly to get the computed value of a CSS property we would do the following: |
| 29 | +``` |
| 30 | +element.computedStyleMap().get('height'); // returns a CSSUnitValue |
| 31 | +``` |
| 32 | + |
| 33 | +### Changing a property's value |
| 34 | + |
| 35 | +To understand the power of Typed OM take a look at the example in CSSOM below: |
| 36 | + |
| 37 | +``` |
| 38 | +let heightValue = target.style.height.slice(0,-2); |
| 39 | +heightValue++; |
| 40 | +target.style.height = heightValue + 'px'; |
| 41 | +``` |
| 42 | + |
| 43 | +In the above example, we are manipulating the `height` CSS property by: |
| 44 | +* extract the numeric portion of a string, |
| 45 | +* performing mathematical operation, |
| 46 | +* converting a number to a string, |
| 47 | +* appending a unit to the resulting string. |
| 48 | + |
| 49 | +The CSS parser will then parse the final string and convert it back to a number for the CSS engine. |
| 50 | + |
| 51 | +With Typed OM you can manipulate typed Javascript objects thereby eliminating the CSS Parser altogether. The above example will now read as follows: |
| 52 | + |
| 53 | +``` |
| 54 | +let heightValue = element.attributeStyleMap.get('height'); |
| 55 | +heightValue.value++; |
| 56 | +target.attributeStyleMap.set('height', heightValue); |
| 57 | +``` |
| 58 | + |
| 59 | +In the above code, `heightValue` gets assigned a `CSSUnitValue` with a value set to the current value of the height and the unit set to 'px'. You can then modify the unit as you would do an integer and not have to incur the cost of manipulating a string. |
| 60 | + |
| 61 | +## Spec references |
| 62 | + |
| 63 | +### What is a CSSStyleValue? |
| 64 | + |
| 65 | +[CSSStyleValue](https://drafts.css-houdini.org/css-typed-om-1/#cssstylevalue) is the base class through which all CSS values are expressed. For a detailed list of what CSSStyleValue subclasses each CSS property maps to please see [here](PLACEHOLDER). Values that aren't supported yet will also be considered as CSSStyleValue objects. |
| 66 | + |
| 67 | +### What are the different CSSStyleValue subclasses? |
| 68 | + |
| 69 | +#### CSSKeywordValue |
| 70 | + |
| 71 | +[CSSKeywordValue](https://drafts.css-houdini.org/css-typed-om-1/#csskeywordvalue) objects represent CSS keywords and other identifiers. An example would be: |
| 72 | + |
| 73 | +``` |
| 74 | +element.attributeStyleMap.set("display", new CSSKeywordValue("none"))); |
| 75 | +``` |
| 76 | + |
| 77 | +#### CSSNumericValue |
| 78 | + |
| 79 | +[CSSNumericValue](https://drafts.css-houdini.org/css-typed-om-1/#cssnumericvalue) objects represent CSS values that are numeric in nature. |
| 80 | + |
| 81 | +The class can be broken down into two subclasses: |
| 82 | +* CSSUnitValues represent simple numbers with units - `CSSUnitValue(10, 'px')` for example is the typed representation of `10px` |
| 83 | +* CSSMathValues represent more complex expressions - `CSSMathSum(CSS.em(1), CSS.px(5))` for example is the typed representation of `calc(1em + 5px)` |
| 84 | + |
| 85 | +#### CSSTransformValue |
| 86 | + |
| 87 | +[CSSTransformValue](https://drafts.css-houdini.org/css-typed-om-1/#csstransformvalue) objects represent via [CSSTransformComponents](https://drafts.css-houdini.org/css-typed-om-1/#csstransformcomponent) values, the different functions used by the `transform` property. |
| 88 | + |
| 89 | +#### CSSPositionValue |
| 90 | + |
| 91 | +[CSSPositionValue](https://drafts.css-houdini.org/css-typed-om-1/#positionvalue-objects) help express the position of an object (e.g. background image) inside a positioning area (e.g. a div). |
| 92 | + |
| 93 | +#### CSSResourceValue |
| 94 | + |
| 95 | +[CSSResourceValue](https://drafts.css-houdini.org/css-typed-om-1/#resourcevalue-objects) objects represent CSS values that require an asynchronous network fetch. Hence, they also describe the status the resource is in. Properties with image values (e.g. `background-image`), are represented by [CSSImageValues](https://drafts.css-houdini.org/css-typed-om-1/#cssimagevalue) |
| 96 | + |
| 97 | +### What about Custom Properties? |
| 98 | + |
| 99 | +Unregistered custom properties are represented by [CSSUnparsedValues](https://drafts.css-houdini.org/css-typed-om-1/#cssunparsedvalue) in the Typed OM API. `var()` references are represented using [CSSVariableReferenceValues](https://drafts.css-houdini.org/css-typed-om-1/#cssvariablereferencevalue). |
| 100 | + |
| 101 | +## Future Capabilities |
| 102 | + |
| 103 | +The current specification doesn't have support for the following, however support for the following is under consideration for Typed OM Level 2: |
| 104 | +* Colors, |
| 105 | +* URLs (that aren't images), |
| 106 | +* Shapes, |
| 107 | +* Strings, |
| 108 | +* Counters, |
| 109 | +* etc. |
| 110 | + |
| 111 | +## Go ahead - try it! |
| 112 | + |
| 113 | +Currently you can try out CSS Typed OM in Chromium based browsers (it is behind a flag though.) |
| 114 | + |
| 115 | +Do let us know if you are having trouble with Typed OM or are missing capabilities by filing an issue on [GitHub](https://github.com/w3c/css-houdini-drafts/issues/new). |
0 commit comments