Skip to content
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

fix: CSS custom properties not defined when a component first renders #2999

Merged

Conversation

nicholasrice
Copy link
Contributor

@nicholasrice nicholasrice commented Apr 23, 2020

Description

Fixes a rendering bug related to the design-system-provider where CSS custom properties have not been written by the time a component first paints.

I also organized the methods / property order and added more code comments, so apologies if this looks more complicated than it should be.

Motivation & context

This change changes the behavior of the design-system-provider to synchronously create
CSS custom properties instead of batching them asynchronously. This is being done to prevent flashes of un-styled content and un-desired transitions that result from the CSS custom property not being defined when the component using the CSS custom property first paints.

This change also introduces an optimization where it will use a CSSStyleSheet attached via adoptedStyleSheets to create the custom properties when that API is available. There are significant performance benefits to this approach over using the HTMLElement.style.setProperty API: https://jsbench.me/hck9d4noez/1. Inline styles will be the fallback behavior if adopted stylesheets are not supported.

Issue type checklist

  • Chore: A change that does not impact distributed packages.
  • Bug fix: A change that fixes an issue, link to the issue above.
  • New feature: A change that adds functionality.

Is this a breaking change?

  • This change causes current functionality to break.

Process & policy checklist

  • I have added tests for my changes.
  • I have tested my changes.
  • I have updated the project documentation to reflect my changes.
  • I have read the CONTRIBUTING documentation and followed the standards for this project.

@nicholasrice nicholasrice self-assigned this Apr 23, 2020
sheet,
];

this.customPropertyTarget = (sheet.rules[0] as CSSStyleRule).style;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever!

@nicholasrice nicholasrice merged commit db4b694 into master Apr 24, 2020
@nicholasrice nicholasrice deleted the users/nirice/synchronous-custom-property-creation branch April 24, 2020 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants