Skip to content

Cascade Layers #2322

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

Merged
merged 55 commits into from
Apr 16, 2025
Merged

Cascade Layers #2322

merged 55 commits into from
Apr 16, 2025

Conversation

HiDeoo
Copy link
Member

@HiDeoo HiDeoo commented Sep 11, 2024

Description

This PR is a WIP that refactors all Starlight CSS to use cascade layers.

At the moment, Starlight will internally uses the following nested layers:

  • starlight.base
  • starlight.reset
  • starlight.core
  • starlight.content
  • starlight.components
  • starlight.utils

This slightly reduces browser support by 0.3% (Chrome >= 88Chrome >= 99).

Testing

As I quickly realized that manually inspecting would not be practical for this PR to ensure there are no regressions, I created a new temporary package to help test the changes (packages/local-prod-visual-diff) that can be run using pnpm test from that directory.

For a list of routes, the tool will:

  • Grab an entire page screenshot of the production version of the route (if it doesn't already exist locally)
  • Start a local server and grab an entire page screenshot of the local version of the route
  • Visually diff the two screenshots
  • If there are significant differences, a diff image with differences in yellow/red will be generated and the test will fail

This has been very helpful to spot some tiny issues during the refactor, e.g. the following diff image that shows a tiny difference in the header <Select> component icons:

header

Another one showing an issue with the content of the last step in a set of <Steps>:

steps

Of course, this is only a bonus and this PR would still be a good Talking & Doc'ing candidate with many different eyes/browsers.

Remaining tasks

  • Tailwind v4 support
  • Figure out which layers we want to use (I mostly maximized the number of layers in the first implementation and we can reduce them during the review)
  • Figure out if we should namespace the Tailwind layers, e.g. tailwind.base like we do at the moment for Starlight or if we should just use the default layers (not doing that)
  • Update the documentation theme editor Tailwind output
  • Address and remove the TODO(HiDeoo) comment in the docs/astro.config.mjs file
  • Remove the packages/local-prod-visual-diff package
  • See if we should remove the default scoped style strategy enforced by Starlight? (not doing that)
    • We would need to refactor various components to handle data-astro-cid-* attributes. Not confident this PR is the right place to do that.
  • Improve documentation
  • Add changesets for all impacted packages.

Copy link

changeset-bot bot commented Sep 11, 2024

🦋 Changeset detected

Latest commit: 6928271

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@astrojs/starlight Minor
@astrojs/starlight-tailwind Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

netlify bot commented Sep 11, 2024

Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 6928271
🔍 Latest deploy log https://app.netlify.com/sites/astro-starlight/deploys/67fe9610f0ba0c0008bb618d
😎 Deploy Preview https://deploy-preview-2322--astro-starlight.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 100 (no change from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 100 (no change from production)
PWA: -
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

@github-actions github-actions bot added 📚 docs Documentation website changes 🌟 core Changes to Starlight’s main package labels Sep 11, 2024
@astrobot-houston
Copy link
Collaborator

astrobot-houston commented Sep 11, 2024

Lunaria Status Overview

🌕 This pull request will trigger status changes.

Learn more

By default, every PR changing files present in the Lunaria configuration's files property will be considered and trigger status changes accordingly.

You can change this by adding one of the keywords present in the ignoreKeywords property in your Lunaria configuration file in the PR's title (ignoring all files) or by including a tracker directive in the merged commit's description.

Tracked Files

Locale File Note
en guides/css-and-tailwind.mdx Source changed, localizations will be marked as outdated.
Warnings reference
Icon Description
🔄️ The source for this localization has been updated since the creation of this pull request, make sure all changes in the source have been applied.

@delucis
Copy link
Member

delucis commented Sep 11, 2024

Copying @mayank99 in on this PR! Will try to take a look soon.

Copy link
Contributor

@mayank99 mayank99 left a comment

Choose a reason for hiding this comment

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

Excited to see this!

I'll try to take a deeper look when I have some time. For now I've just left one question.

@HiDeoo
Copy link
Member Author

HiDeoo commented Nov 22, 2024

Small note that Tailwind CSS v4.0 Beta adds native CSS cascade layers support.

@mayank99
Copy link
Contributor

Tailwind v4's layer implementation has good and bad parts.

The bad: they implicitly create very generic layer names (i.e. not namespaced).

The good: they allow control over importing their stylesheets(s) so you can put their layers inside your own layer or even change the names.

@import "tailwindcss" layer(💨);

/* or */

@import "tailwindcss/base" layer(💨base);
@import "tailwindcss/theme" layer(💨theme);
@import "tailwindcss/utilities" layer(💨utilities);

* main: (493 commits)
  i18n(de): update `sidebar.mdx` (withastro#2900)
  Adding all samples for badges in sidebar as example (withastro#2881)
  i18n(fr): fix highlighting in `reference/plugins.md` (withastro#2898)
  i18n(fr): update `reference/plugins` & `guides/i18n` (withastro#2886)
  i18n(fr): update `reference/overrides` & `guides/sidebar` (withastro#2887)
  i18n(fr): add `guides/route-data` and `reference/route-data` (withastro#2884)
  i18n(fr): update `guides/overriding-components` (withastro#2885)
  i18n(fr): update `reference/configuration` (withastro#2888)
  i18n(de): update German translation with withastro#2578 (withastro#2882)
  [ci] format
  i18n(ru): update some translations (withastro#2895)
  i18n(de): update `reference/configuration.mdx` (withastro#2883)
  [ci] format
  i18n(ko-KR): update `configuration.mdx` (withastro#2892)
  i18n(ko-KR): update `overrides.md` (withastro#2893)
  i18n(ko-KR): update `overriding-components.mdx` (withastro#2890)
  i18n(ko-KR): update `i18n.mdx` and `plugins.md` (withastro#2889)
  [ci] format
  i18n(ko-KR): create `guides/route-data.mdx` and `reference/route-data.mdx` (withastro#2896)
  i18n(ko-KR): update `/resources/plugins.mdx` (withastro#2894)
  ...
Copy link
Contributor

@mayank99 mayank99 left a comment

Choose a reason for hiding this comment

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

Finally got the chance to try it out, and I'm happy to report it's working great! 🥳

When I used customCss, I found that the stylesheet was linked before Starlight's own CSS. I'm not sure if this behavior is intentional and will remain this way, but it works out great for the layer use-case.

For extra surety, I also tried overriding Starlight's Head component and it worked as expected for placing an inline <style> tag with my predefined layer order before all other <head> content.

HiDeoo and others added 4 commits April 14, 2025 19:17
* main: (31 commits)
  [ci] release (withastro#3091)
  i18n(de): update `environmental-impact` (withastro#3112)
  feat: update environment comparisons data (withastro#3097)
  chore(deps): update actions/setup-node action to v4.4.0 (withastro#3111)
  Update Expressive Code and related packages (withastro#3109)
  i18n(de): update `reference/overrides.md` (withastro#3093)
  i18n(de): update `reference/route-data.mdx` (withastro#3094)
  i18n(de): update `resources/themes.mdx` (withastro#3092)
  [ci] format
  i18n(zh-cn): Update configuration.mdx (withastro#3102)
  i18n(zh-cn): Update overrides.mdx (withastro#3103)
  [ci] format
  i18n(zh-cn): Add route-data.mdx (withastro#3106)
  [ci] format
  i18n(zh-cn): Add themes.mdx (withastro#3105)
  [ci] format
  i18n(zh-cn): Update plugins.md (withastro#3104)
  [ci] format
  i18n(zh-cn): Update customization.mdx (withastro#3101)
  i18n(zh-cn): Update badges.mdx (withastro#3100)
  ...
Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

I’m too sleepy to fully tackle the docs — will do those tomorrow morning — but getting in at least a review on all the code changes.

Amazing work as always @HiDeoo and I haven’t spotted anything major I don’t think!

  • See if we should remove the default scoped style strategy enforced by Starlight?

I was also wondering that the other day, but I agree with you that this PR already has enough going on 😁

We could definitely reuse your visual diff script for that PR when the time comes though!

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

Last few remarks on the changesets. Will looks at docs now.

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

I think this PR is in great shape to merge in our next minor! Many, many thanks to @HiDeoo for iterating over this and getting us to this point. In some ways this is a big architectural change but you’ve carried it out with such care that I think most people will hardly notice, while unlocking lots of improvements for those who need it.

@delucis delucis added the ✅ approved Pull requests that have been approved and are ready to merge when next cutting a release label Apr 15, 2025
@delucis delucis merged commit f14eb0c into withastro:main Apr 16, 2025
16 checks passed
@astrobot-houston astrobot-houston mentioned this pull request Apr 16, 2025
HiDeoo added a commit to HiDeoo/starlight that referenced this pull request Apr 16, 2025
* main:
  [ci] release (withastro#3124)
  Fix E2E tests broken by formatter
  [ci] format
  Add built-in heading anchor link support (withastro#3033)
  i18n(fr): update `environmental-impact` (withastro#3123)
  Cascade Layers (withastro#2322)
  Fix image metadata validation in StarlightPage schema (withastro#3118)
  Don’t set defaults for `attrs` and `content` in head entries (withastro#3122)
  [ci] format
  i18n(es) Translate `route-data` (withastro#3071)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✅ approved Pull requests that have been approved and are ready to merge when next cutting a release 🌟 core Changes to Starlight’s main package 📚 docs Documentation website changes 🌟 minor Change that triggers a minor release 🌟 tailwind Changes to Starlight’s Tailwind package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants