-
Notifications
You must be signed in to change notification settings - Fork 142
Create README.md for css-typed-om #622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # CSS Typed OM Explained | ||
|
|
||
| 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. | ||
|
|
||
| ## Motivation | ||
|
|
||
| The benefits of CSS Typed OM include: | ||
| * Allows the performant manipulation of values assigned to CSS properties. | ||
| * Being able to write more maintainable code: | ||
| * Your code is easier to understand, | ||
| * Easier to write more generic code. | ||
|
|
||
| ### Examples | ||
|
|
||
| ### Getting a property's value | ||
|
|
||
| In CSSOM land: | ||
| 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. | ||
|
|
||
| In Typed OM: | ||
| We get them of `StylePropertyMaps` on elements. | ||
|
|
||
| To get the specified height from an element we would use the following JS: | ||
| ``` | ||
| element.attributeStyleMap.get('height'); // returns a string | ||
| ``` | ||
|
|
||
| Similarly to get the computed value of a CSS property we would do the following: | ||
| ``` | ||
| element.computedStyleMap().get('height'); // returns a CSSUnitValue | ||
| ``` | ||
|
|
||
| ### Changing a property's value | ||
|
|
||
| To understand the power of Typed OM take a look at the example in CSSOM below: | ||
|
|
||
| ``` | ||
| let heightValue = target.style.height.slice(0,-2); | ||
| heightValue++; | ||
| target.style.height = heightValue + 'px'; | ||
| ``` | ||
|
|
||
| In the above example, we are manipulating the `height` CSS property by: | ||
| * extract the numeric portion of a string, | ||
| * performing mathematical operation, | ||
| * converting a number to a string, | ||
| * appending a unit to the resulting string. | ||
|
|
||
| The CSS parser will then parse the final string and convert it back to a number for the CSS engine. | ||
|
|
||
| With Typed OM you can manipulate typed Javascript objects thereby eliminating the CSS Parser altogether. The above example will now read as follows: | ||
|
|
||
| ``` | ||
| let heightValue = element.attributeStyleMap.get('height'); | ||
| heightValue.value++; | ||
| target.attributeStyleMap.set('height', heightValue); | ||
| ``` | ||
|
|
||
| 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. | ||
|
|
||
| ## Spec references | ||
|
|
||
| ### What is a CSSStyleValue? | ||
|
|
||
| [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. | ||
|
|
||
| ### What are the different CSSStyleValue subclasses? | ||
|
|
||
| #### CSSKeywordValue | ||
|
|
||
| [CSSKeywordValue](https://drafts.css-houdini.org/css-typed-om-1/#csskeywordvalue) objects represent CSS keywords and other identifiers. An example would be: | ||
|
|
||
| ``` | ||
| element.attributeStyleMap.set("display", new CSSKeywordValue("none"))); | ||
| ``` | ||
|
|
||
| #### CSSNumericValue | ||
|
|
||
| [CSSNumericValue](https://drafts.css-houdini.org/css-typed-om-1/#cssnumericvalue) objects represent CSS values that are numeric in nature. | ||
|
|
||
| The class can be broken down into two subclasses: | ||
| * CSSUnitValues represent simple numbers with units - `CSSUnitValue(10, 'px')` for example is the typed representation of `10px` | ||
| * CSSMathValues represent more complex expressions - `CSSMathSum(CSS.em(1), CSS.px(5))` for example is the typed representation of `calc(1em + 5px)` | ||
|
|
||
| #### CSSTransformValue | ||
|
|
||
| [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. | ||
|
|
||
| #### CSSPositionValue | ||
|
|
||
| [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). | ||
|
|
||
| #### CSSResourceValue | ||
|
|
||
| [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) | ||
|
|
||
| ### What about Custom Properties? | ||
|
|
||
| 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). | ||
|
|
||
| ## Future Capabilities | ||
|
|
||
| The current specification doesn't have support for the following, however support for the following is under consideration for Typed OM Level 2: | ||
| * Colors, | ||
| * URLs (that aren't images), | ||
| * Shapes, | ||
| * Strings, | ||
| * Counters, | ||
| * etc. | ||
|
|
||
| ## Go ahead - try it! | ||
|
|
||
| Currently you can try out CSS Typed OM in Chromium based browsers (it is behind a flag though.) | ||
|
|
||
| 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). | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extraneous line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done