Using Houdini to Extend CSS

Houdini: The JavaScript API to Extend CSS

We will be finally able to extend CSS in JavaScript via Houdini

As we all know, CSS is a style sheet language that enhances the presentations written by HTML, like mark-up languages. Many requirements come from the community in the fast-growing tech world, such as CSS native nesting requirements, getting parent selectors. Getting rid of SASS, get better styling options for select and input options, more filter options, more display options, etc.

It seems impossible for CSS specifications to keep up with the various feature demands from the industry.

The possible solution is a native way of extending CSS using various APIs. However, Google developers have introduced a method to overcome this issue and powerup the developers when styling.

Introduction to Houdini

Houdini introduces low-level JavaScript APIs for the browser’s render engines.

Houdini is a hypernym that describes a series of low-level browser APIs, making it easier for authors to access the CSS object model and extend CSS by hooking into the browser’s rendering engine’s styling and layout process. This means that developers now have much more control and power over the styles they write.

Need for Houdini over CSS Polyfills?

There are three steps in the rendering pipeline; converting HTML, CSS, and JavaScript into pixels on the browser screen.

Step 01: Browser reads page content and builds Render Tree, which designs the page layout

Step 02: Painting will take place to convert the page layout into pixels.

Step 03: Composite all the painted elements into one page.

To increase the performance of a webpage, it is crucial to optimize the critical render path.

One of CSS’s critical issues is that it takes quite a considerable amount of time to improve or implement a new CSS feature to its fully-supported level.

Currently, developers use JavaScript Polyfills as a substitute for the lack of browser support for the newest CSS features. However, there are critical drawbacks to this approach.

  • It requires extra code. The simplest CSS polyfills require more than just rewriting individual property values.
  • It does not work with cross-origin (non-CORS) stylesheets.
  • It performs poorly when changes are needed (DOM changes, scroll/resize handlers, etc.)
  • Need to re-rewrite in any DOM change, which means it requires knowledge of the DOM.
  • Performance issues: After Polyfill makes changes to the DOM styles, it causes the render process to run again, and the whole page re-renders.

Tip: Share your reusable components between projects using Bit (Github).

Bit makes it simple to share, document, and reuse independent components between projects. Use it to maximize code reuse, keep a consistent design, speed-up delivery, and build apps that scale.

Bit supports Node, TypeScript, React, Vue, Angular, and more.

Example: exploring reusable React components shared on Bit.dev

Key Houdini APIs

Houdini Typed Object Model

Developers already use the CSS Object Model for a long time which the function is to extract the computed style. It returns a string and is a performance-wise drawback.

document.body.style.width // returns the width as a string ("3px")
window.getComputedStyle(document.body).width

Typed Object models work the same but convert strings into meaningfully typed JS representations using two methods.

document.body.attributeStyleMap.get("width") //returns with the type of the value (3px)
document.body.computedStyleMap().get("width")

Painting API

When it comes to styling web pages, the appearance can be changed due to many scenarios such as when using URL(), applying gradient effects, etc.

Painting API enhances the complaisance of CSS. Function in the API is to draw directly into HTML elements by writing custom functions using paint() and <image> CSS functions. These functions create an image which the rendering engine can use on behalf of the property against which it is used.

For example, developers can use these functions to set complex custom backgrounds on HTML elements.

canvas {
background-image: paint(TestPaintedImg);
}

Houdini Worklets

The Worklet is almost similar to Web Workers(light-weighted), enabling access to the rendering pipeline. The main advantage of Worklets is that developers can use Web Assembly and JavaScript code to perform complex audio processing and graphics rendering. Worklets extend the rendering engine.

04 Worklet types

  • PaintWorklet: Generate images programmatically where a CSS property expects an image/file.
  • AudioWorklet: Use for audio processing using custom AudioNodes.
  • AnimationWorklet: To implement high-performance animations.
  • LayoutWorklet: Defines positioning and dimensions of custom elements.

Other than these four main APIs, there are two APIs that are still under development (CSS Parser API, CSS Layout API)

Advantages of Houdini

Houdini considers mainly all about providing a flexible developer environment and enhances performance in the page rendering cycle. Other than that, there are some key advantages as well.

  • Houdini enables faster parse times than using JavaScript for style changes. Most importantly, Houdini does not wait until the completion of the first rendering cycle. The reason is that Houdini consists of understandable styles for a readable first cycle creation.
  • Working with CSS values in JavaScript made easier with Houdini’s Object-based API.
  • Using string-based CSS manipulation, HTMLElement style is replaced with Houdini’s CSS object model (Typed Object Model), which exposes values as JavaScript objects to better CSS manipulation.
  • Houdini’s key feature, Worklets: Has access to the custom properties of elements. Also, Worklets enables developers to create modular CSS using a single line to import configurable components.
//acessing custom elements
li{
background-image: paint(testComponent, fill, 12px);
--highlights: red;
--lowlights: blue;
}
//import configureable components
<script>
CSS.paintWorklet.addModule('csscomponent.js');
</script>
  • Browser compatibility: Chrome is leading the way with most of the Houdini APIs. However, most of the APIs are still under development.
Screenshot by Author

Conclusion

Even though developers can invent their grids, regional implementations, custom elements, etc., using Houdini APIs, it is not the best idea for now because developers need to consider privacy, accessibility, and security concerns. So, my opinion is it is better to start small before moving into more advanced projects.

Thank you for reading the article. Cheers!

Learn More


Using Houdini to Extend CSS was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Charuka Herath

Houdini: The JavaScript API to Extend CSS

We will be finally able to extend CSS in JavaScript via Houdini

As we all know, CSS is a style sheet language that enhances the presentations written by HTML, like mark-up languages. Many requirements come from the community in the fast-growing tech world, such as CSS native nesting requirements, getting parent selectors. Getting rid of SASS, get better styling options for select and input options, more filter options, more display options, etc.

It seems impossible for CSS specifications to keep up with the various feature demands from the industry.

The possible solution is a native way of extending CSS using various APIs. However, Google developers have introduced a method to overcome this issue and powerup the developers when styling.

Introduction to Houdini

Houdini introduces low-level JavaScript APIs for the browser’s render engines.

Houdini is a hypernym that describes a series of low-level browser APIs, making it easier for authors to access the CSS object model and extend CSS by hooking into the browser’s rendering engine’s styling and layout process. This means that developers now have much more control and power over the styles they write.

Need for Houdini over CSS Polyfills?

There are three steps in the rendering pipeline; converting HTML, CSS, and JavaScript into pixels on the browser screen.

Step 01: Browser reads page content and builds Render Tree, which designs the page layout

Step 02: Painting will take place to convert the page layout into pixels.

Step 03: Composite all the painted elements into one page.

To increase the performance of a webpage, it is crucial to optimize the critical render path.

One of CSS’s critical issues is that it takes quite a considerable amount of time to improve or implement a new CSS feature to its fully-supported level.

Currently, developers use JavaScript Polyfills as a substitute for the lack of browser support for the newest CSS features. However, there are critical drawbacks to this approach.

  • It requires extra code. The simplest CSS polyfills require more than just rewriting individual property values.
  • It does not work with cross-origin (non-CORS) stylesheets.
  • It performs poorly when changes are needed (DOM changes, scroll/resize handlers, etc.)
  • Need to re-rewrite in any DOM change, which means it requires knowledge of the DOM.
  • Performance issues: After Polyfill makes changes to the DOM styles, it causes the render process to run again, and the whole page re-renders.

Tip: Share your reusable components between projects using Bit (Github).

Bit makes it simple to share, document, and reuse independent components between projects. Use it to maximize code reuse, keep a consistent design, speed-up delivery, and build apps that scale.

Bit supports Node, TypeScript, React, Vue, Angular, and more.

Example: exploring reusable React components shared on Bit.dev

Key Houdini APIs

Houdini Typed Object Model

Developers already use the CSS Object Model for a long time which the function is to extract the computed style. It returns a string and is a performance-wise drawback.

document.body.style.width // returns the width as a string ("3px")
window.getComputedStyle(document.body).width

Typed Object models work the same but convert strings into meaningfully typed JS representations using two methods.

document.body.attributeStyleMap.get("width") //returns with the type of the value (3px)
document.body.computedStyleMap().get("width")

Painting API

When it comes to styling web pages, the appearance can be changed due to many scenarios such as when using URL(), applying gradient effects, etc.

Painting API enhances the complaisance of CSS. Function in the API is to draw directly into HTML elements by writing custom functions using paint() and <image> CSS functions. These functions create an image which the rendering engine can use on behalf of the property against which it is used.

For example, developers can use these functions to set complex custom backgrounds on HTML elements.

canvas {
background-image: paint(TestPaintedImg);
}

Houdini Worklets

The Worklet is almost similar to Web Workers(light-weighted), enabling access to the rendering pipeline. The main advantage of Worklets is that developers can use Web Assembly and JavaScript code to perform complex audio processing and graphics rendering. Worklets extend the rendering engine.

04 Worklet types

  • PaintWorklet: Generate images programmatically where a CSS property expects an image/file.
  • AudioWorklet: Use for audio processing using custom AudioNodes.
  • AnimationWorklet: To implement high-performance animations.
  • LayoutWorklet: Defines positioning and dimensions of custom elements.

Other than these four main APIs, there are two APIs that are still under development (CSS Parser API, CSS Layout API)

Advantages of Houdini

Houdini considers mainly all about providing a flexible developer environment and enhances performance in the page rendering cycle. Other than that, there are some key advantages as well.

  • Houdini enables faster parse times than using JavaScript for style changes. Most importantly, Houdini does not wait until the completion of the first rendering cycle. The reason is that Houdini consists of understandable styles for a readable first cycle creation.
  • Working with CSS values in JavaScript made easier with Houdini’s Object-based API.
  • Using string-based CSS manipulation, HTMLElement style is replaced with Houdini’s CSS object model (Typed Object Model), which exposes values as JavaScript objects to better CSS manipulation.
  • Houdini’s key feature, Worklets: Has access to the custom properties of elements. Also, Worklets enables developers to create modular CSS using a single line to import configurable components.
//acessing custom elements
li{
background-image: paint(testComponent, fill, 12px);
--highlights: red;
--lowlights: blue;
}
//import configureable components
<script>
CSS.paintWorklet.addModule('csscomponent.js');
</script>
  • Browser compatibility: Chrome is leading the way with most of the Houdini APIs. However, most of the APIs are still under development.
Screenshot by Author

Conclusion

Even though developers can invent their grids, regional implementations, custom elements, etc., using Houdini APIs, it is not the best idea for now because developers need to consider privacy, accessibility, and security concerns. So, my opinion is it is better to start small before moving into more advanced projects.

Thank you for reading the article. Cheers!

Learn More


Using Houdini to Extend CSS was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Bits and Pieces - Medium and was authored by Charuka Herath


Print Share Comment Cite Upload Translate Updates
APA

Charuka Herath | Sciencx (2021-02-23T21:59:06+00:00) Using Houdini to Extend CSS. Retrieved from https://www.scien.cx/2021/02/23/using-houdini-to-extend-css/

MLA
" » Using Houdini to Extend CSS." Charuka Herath | Sciencx - Tuesday February 23, 2021, https://www.scien.cx/2021/02/23/using-houdini-to-extend-css/
HARVARD
Charuka Herath | Sciencx Tuesday February 23, 2021 » Using Houdini to Extend CSS., viewed ,<https://www.scien.cx/2021/02/23/using-houdini-to-extend-css/>
VANCOUVER
Charuka Herath | Sciencx - » Using Houdini to Extend CSS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/23/using-houdini-to-extend-css/
CHICAGO
" » Using Houdini to Extend CSS." Charuka Herath | Sciencx - Accessed . https://www.scien.cx/2021/02/23/using-houdini-to-extend-css/
IEEE
" » Using Houdini to Extend CSS." Charuka Herath | Sciencx [Online]. Available: https://www.scien.cx/2021/02/23/using-houdini-to-extend-css/. [Accessed: ]
rf:citation
» Using Houdini to Extend CSS | Charuka Herath | Sciencx | https://www.scien.cx/2021/02/23/using-houdini-to-extend-css/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.