Skip to content

Feature request: allow the use of a stylesheet as adoptedStylesheets in component libraries #1967

Closed

Description

@JMDidon

Stencil version:

I'm submitting a:

[ ] bug report
[x] feature request
[ ] support request

Current behaviour:
Context: I'm working on a web component library.

Stencil allow the inclusion of globalStyles in the stencil.config.ts, which then is included in the dist after compilation. While this stylesheet is useful, a very common need in component libraries would be to be able to set some rules globally for all components (think fonts, reset.css, box-sizing: border-box, etc.).

Right now, the way to do it would be either to:

  • manually include the global stylesheet in all components of the library, which means that all components will now include the stylesheet itself which in turn can cause a very quick augmentation of the bundle size.
  • manually configure a Constructable Stylesheet system, in order to make all your components adopt the stylesheet.

The second option seems better, and I feel that Stencil could handle this at build time instead of letting the developers implement a custom solution 😄

Activity

  1. christophsaile commented on Apr 3, 2024

    @christophsaile

    Hey, are there any plans to include this feature? @JMDidon did you manage to implement a custom soltuion that you can share?:)

  2. added
    Feature: Want this? Upvote it!
    and removed on Apr 3, 2024
  3. christian-bromann commented on Apr 3, 2024

    @christian-bromann
    Member

    @christophsaile thanks for nudging on this issue. I've updated the label for it which slots it into our bucket of community features we consider to include in future roadmaps. Please subscribe to this issue to receive updates.

  4. JMDidon commented on Apr 30, 2024

    @JMDidon
    Author

    Hey @christophsaile!

    I changed company kinda recently and haven't seen your message until today 😅
    Basically we haven't worked on it as there were other prios, and as we had decided to drop support for ShadowDOM for accessibility reasons this feature lost a bit of its purpose to us.

    Still, I think being able to declare an adoptedStylesheet for a component library would be very useful 🙂

  5. christian-bromann commented on Jan 21, 2025

    @christian-bromann
    Member

    @JMDidon some good news: we are planning to add this to our 2025 roadmap. Can you help us understand your desired solution a bit better and be more clear about how applying this feature would look like? How would some enhance the e.g. Stencil config to apply this?

    Are you suggesting that the contents of the css file that is set in the globalStyle configuration is beeing applied as adoptedStylesheet to the shadow DOM of each Stencil component?

  6. michaelrgalloway commented on Mar 28, 2025

    @michaelrgalloway

    Our team would also like this, we generate a set of classes using Tailwind which also initializes a set of css variables, currently this works but it is duplicated across components which makes them bloaty. If we could use a single stylesheet and adopt it to the shadowRoot of components, sharing the single constructable stylesheet, this would not only reduce the bloat and payload, but it will also allow us to modify/add/delete rules at runtime and have all component styles be reflected of such updates.

  7. christian-bromann commented on May 30, 2025

    @christian-bromann
    Member

    @michaelrgalloway @christophsaile

    A dev release was made: @stencil/[email protected]
    Docs PR was raised: stenciljs/site#1526
    A preview is available at: https://stencil-docs-git-cb-constructable-stylesheet-docs-ionic1.vercel.app/docs/next/styling#constructable-stylesheets

    Please test the feature out and let us know what you think. We plan to publish it as new minor version next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Feature: Want this? Upvote it!

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions