-
Notifications
You must be signed in to change notification settings - Fork 21
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
Proposal/RFC scoped styles with custom elements #1764
base: develop
Are you sure you want to change the base?
Conversation
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.
If I understood correctly, this solution is mainly to reduce/avoid the style naming conflicts with upstream or other extensions, and this brings a further benefit that the children's style naming within a scoped component will be easier, right?
If I remember rightly, the style conflicts that had/have happened in practice were probably these situations:
- Conflicts between WP and WC components themselves due to the current use of DEWP
- GLA's style affects others
- Others' style affects GLA
In the first case, after WP and WC packages are externalized, it should not occur because of GLA's DEWP setting. In the second case, the only direct importing of node_modules/@wordpress/components/src/panel/style
will also be removed soon, and with the .gla-
prefix and .gla-admin-page
scoping support already in place, this is unlikely to happen in the future.
However, the third one is still not effectively addressed, since most GLA components are heavily based on WP and WC components, there seems no way to completely avoid style conflicts from outside on its own CSS name (e.g. .components-button
). This solution does not seem to be applicable to solving this problem either?
In addition, the Fast Refresh development mode (npm run start:hot
) seems to conflict with this approach. Once a code change occurs within a component that uses defineShadowStylesHost
, subsequent code changes will not make Fast Refresh update the page. Must refresh the page to resume.
Not quite. This PRs aims to improve the DevX to make our developer life merrier, help to avoid bugs (incl. style conflicts), and speed up debugging. The improvements are listed in bullet points in OP. The ones I'd highlight:
Also, I'd like to emphasize one thing: this solution is mostly to specify and encapsulate the styles for an HTML Element ( So I do not aim to fix the issues you link.
As long as we do not have the ambition to change WP & WC conflict, this proposal does not aim to fix that problem. As you said DEWPing all the things should be the solution.
As you mentioned,
The solution proposed here is to style only the specific, GLA-defined host element (
So "This solution does not seem to be applicable to solving" others' styles styling elements like But the aim here is to encapsulate the styles of |
I didn't use Fast Refresh. I'll take a look and check what happens there |
Thanks for clarifying. I originally thought it was related to those style conflict cases because this PR came up in this slack thread: p1668008895895869/1668000686.809249-slack-C0410KV3YLW
After a quick browsing of the .~/components directory, there are probably only a few components completely hosted by GLA:
Considering that
Currently, I think this is probably still an experimental-stage method and a bit far from the actual adoption. Or, maybe try |
Changes proposed in this Pull Request:
This is another proposal to use Shadow DOM to encapsulate at least some of our styles.
This is an alternative to #539 that joins #538
And has all the pros and cons of both :)
Screenshots:
Declutters HTML
Declutters CSS
Coding flexibility
Detailed test instructions:
npm run build
/wp-admin/admin.php?page=wc-admin&path=%2Fgoogle%2Fsettings
Additional details:
<style>
tag to every root to play safe.css
tagged template, or replace it with something from an external library, like lit.css
</code> highlighted in VSCode, you need a plugin like https://marketplace.visualstudio.com/items?itemName=bashmish.es6-string-css or
lit-html`<gla-connected-icon-label>
or we want to use two different versions of the same component. What we currently cannot with@wordpress/
and@woocommerce/components
we can simply add generated suffix todefineShadowStylesHost
and make it return the name. Styles will keep their isolation thanks to Shadow DOM.Changelog entry