npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

styled-components-custom-properties-theme-provider

v1.0.0-beta.3

Published

Custom styled-components theme provider which uses CSS custom properties in browsers that support them. Will fallback to using styled components theme provider implementation in browsers that don't support CSS custom properties.

Downloads

29

Readme

styled-components-custom-properties-theme-provider

Custom styled-components theme provider which uses CSS custom properties in browsers that support them. Will fallback to using styled components theme provider implementation in browsers that don't support CSS custom properties.

This project is in initial concept development stages and may not be ready for production use.

Install

With npm:

npm install styled-components-custom-properties-theme-provider

With yarn:

yarn add styled-components-custom-properties-theme-provider

Demo

https://bjankord.github.io/styled-components-custom-properties-theme-provider/

Usage

Edit Styled Components CSS Custom Properties Theming

Constraints

The CSS Custom Property Theme Proivder returns its children when rendering, so it must only wrap a single child node, similar to how the <ThemeProvider> component works from styled-components.

This single child node must be a DOM element, e.g. (this can't be a React.Fragment node), as this element is used to set inline styles for the CSS custom properties.

Another constraint is that your keys in your theme need to be a flat, single level with the key names being values that can be uses as CSS custom properties. eg. "button-background-color": "mediumseagreen"

Performance Notes

Originally, I started exploring this idea thinking this would be faster for theming as the styled component wouldn't need to read multiple items off of the theme object.

I've set up 3 pages to test 3 implementations.

In testing I've found that using the CSS custom properties based theme provider (without including fallback support for browsers that don't support CSS properties) is faster compared to the default styled-components theme provider.

However, the implementation that uses the CSS custom properties based theme provider and falls back to using the the default styled-components theme provider is noticeably slower when throttling the CPU to 4x slowdown in Chrome compared to using the default styled-components theme provider. This can be seen in the demo when changing themes.

Testing with React.unstable_Profiler

I've wrapped the theme provider in each demo with a Profiler component from React.unstable_Profiler;

Below is the mean average from 20 test runs for each demo collected from running create-react-app in development mode.

Tested in production mode in CRA thanks to @bvaughn's gist here: https://gist.github.com/bvaughn/25e6233aeb1b4f0cdb8d8366e54a3977

Production Mode

MOUNT phase:

| Measure | Styled Components ThemeProvider | CSS Custom Properties Theme Provider | CSS Custom Properties Theme Provider With Fallback | |---|---|---|---| | Actual time: | 114.67500089202076 ms | 65.54000021424145 ms | 122.44500109227374 ms | | Base time: | 93.18000212078914 ms | 44.91499869618565 ms | 99.71500077517703 ms |

UPDATE phase:

| Measure | Styled Components ThemeProvider | CSS Custom Properties Theme Provider | CSS Custom Properties Theme Provider With Fallback | |---|---|---|---| | Actual time: | 42.78000054182485 ms | 18.06499983649701 ms | 52.99499997636303 ms | | Base time: | 39.890000072773546 ms | 15.694999718107283 ms | 50.529999658465385 ms |

Update phase was tested by supplying a new theme with entirely different values for each theme property.

Development Mode

MOUNT phase:

| Measure | Styled Components ThemeProvider | CSS Custom Properties Theme Provider | CSS Custom Properties Theme Provider With Fallback | |---|---|---|---| | Actual time: | 243.0450000683777 ms | 234.49500190326944 ms | 272.86500280024484 ms | | Base time: | 182.36999865621328 ms | 177.84500279230997 ms | 209.2250018613413 ms |

UPDATE phase:

| Measure | Styled Components ThemeProvider | CSS Custom Properties Theme Provider | CSS Custom Properties Theme Provider With Fallback | |---|---|---|---| | Actual time: | 97.6450014859438 ms | 89.05499934917316 ms | 108.44500467646867 ms | | Base time: | 87.12000423111022 ms | 79.05499776825309 ms | 99.06000515911728 ms |

Update phase was tested by supplying a new theme with entirely different values for each theme property.

Testing with puppeteer

| Measure | Styled Components ThemeProvider | CSS Custom Properties Theme Provider | CSS Custom Properties Theme Provider With Fallback | |---|---|---|---| | Total page time from start to load: | 428ms | 398ms | 489ms | | Time spent constructing the DOM tree: | 179ms | 165ms | 179ms | | Time spent rendering: | 409ms | 374ms | 466ms |

tl;dr

The CSS custom properties theme provider is a bit faster compared to the the default styled-components theme provider.

If you do need to support browsers that don't support CSS custom properties, the default styled-components theme provider will be faster compared to the CSS Custom Properties Theme Provider which offers fallback support.

License

CSS Custom Properties Browser Support

CSS Custom Properties Browser Support

How it looks in browsers that support CSS custom properties

Chrome

Chrome

Firefox

Firefox

Safari

Safari

Edge

Edge

Opera

Opera

How it looks in browser that don't natively support CSS custom properties

Edge 15

Edge15

IE 11

IE11

IE 10

IE10

IE 9

IE9

Android 4.4

Android 4.4