Skip to content

Support any scopedStyleStrategy - #4190

Merged
delucis merged 17 commits into
mainfrom
chris/scoped-style-strategy
Nov 13, 2026
Merged

delucis merged 17 commits into
mainfrom
chris/scoped-style-strategy

Conversation

@delucis

@delucis delucis commented Sep 9, 2026 •

Copy link
Copy Markdown
Member

Background

  • Since Support Astro v3 #615 and Starlight v0.9.0, we have forced scopedStyleStrategy: "where". At the time, the higher specificity of the "attribute" default scoping strategy caused a lot of issues so we avoided it by sticking with the Astro v2 default of "where".
  • Since we released Cascade Layers #2322 the issues with specificity are much reduced: user styles will always override our @layer styles making support for different scoping strategies more viable.

This PR

  • This PR currently removes the line where we set scopedStyleStrategy and leave it to user discretion. This means by default users will switch to Astro’s default of "attribute".
  • Various small changes to styles were needed to correctly support the different scoping strategies with one case being the most common:
    • icons that had a class name passed down — these do not receive the parent scope when using the "attribute" strategy, so needed to be updated to use a parent selector and :global().
  • Because of some specificity/ordering issues within our starlight.components layer, I had to extract the <Icon> styles and move them to a global .sl-icon class name instead of just depending on an Astro-scoped svg element selector. I also added this class name to the icons generated by Markdown asides for consistency.
  • The icon component is updated to match the Astro docs guidance for passing classes down to child components and I did the same for one component in our docs which also needed this.
  • I tested all English docs pages and two Farsi docs pages with starlight-visual-diff to ensure consistency between all strategies and the existing appearance, but some more testing and thinking about the impact of this change is still very welcome.

Open questions

  • It could be less disruptive to continue setting scopedStyleStrategy for users in the cases where they do not set it themselves. It would make Starlight’s default different from Astro though, so for now I went with removing it and documenting how to restore it in the changeset.
  • Supporting multiple strategies may potentially mean there are visual differences between strategies, which we don’t notice in testing for changes made in the future. Might be worth us considering how best to handle that to ensure we maintain consistency.

Using the `"attribute"` scoping strategy (the default), Astro does not currently pass down a parent scoping attribute like it does with the `"where"` strategy’s class name. Unclear if that’s intentional on Astro’s part or not but instead we have to use a parent selector + `:global()` for the child instead.
@changeset-bot

changeset-bot Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 7a10d5d

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

This PR includes changesets to release 1 package
Name Type
@astrojs/starlight Minor

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

@netlify

netlify Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for astro-starlight ready!

Name Link
🔨 Latest commit 7a10d5d
🔍 Latest deploy log https://app.netlify.com/projects/astro-starlight/deploys/6af7412b013faeb8859951c7
😎 Deploy Preview https://deploy-preview-4190--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
🤖 Make changes Run an agent on this branch

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

@github-actions github-actions Bot added 📚 docs Documentation website changes 🌟 core Changes to Starlight’s main package labels Sep 9, 2026
@delucis delucis added this to the 0.43 milestone Sep 9, 2026
@astrobot-houston

astrobot-houston commented Sep 9, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
/index.html 6.16 KB (+0.69% 🔺)
/guides/example/index.html 5.61 KB (+0.62% 🔺)
/_astro/*.js 25.51 KB (0%)
/_astro/*.css 14.89 KB (+0.2% 🔺)

@delucis
delucis marked this pull request as ready for review September 24, 2026 17:06
@delucis

delucis commented Sep 24, 2026

Copy link
Copy Markdown
Member Author

Just went through this again and marked it ready for review. I think it should be a reasonably safe change thanks to @layer insulating users from our styles. And for anyone it does cause issues for, the path back to the previous behaviour is very simple.

Supporting multiple strategies may potentially mean there are visual differences between strategies, which we don’t notice in testing for changes made in the future. Might be worth us considering how best to handle that to ensure we maintain consistency.

On this point I still don’t have an answer and it may be OK? AFAICT the increase in specificity of switching to the default strategy was riskier than the decrease of using "where". At least it seems that currently switching back to "where" was still totally fine without any specific effort or care to do so.

Any approach to asserting this in tests would presumably mean running something like starlight-visual-diff in CI and that feels like a pretty big hammer for something that very often won’t apply (a small % of PRs touch styling). So maybe it’s fine to leave that as a manual thing to bear in mind.

@HiDeoo HiDeoo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the pull request 🙌

It definitely took me a while to review it as I spent a lot of time thinking about it, testing things out, etc.

Not gonna lie, this is a scary change. Not at the Starlight level (I think we can manage it, even tho the PR proves it's tricky and super easy to miss tiny issues) but more at the scale of the entire ecosystem (plugins, themes, components) and user components. Styles applied to child components using a class prop would now silently stop matching unless the child forward the data-astro-cid-* attribute (and scoped styles get a higher specificity as you mentioned). It's not something that has obvious errors, can be very subtle/easy to miss and we can't really tell people to use a tool like starlight-visual-diff on all their pages.

It could be less disruptive to continue setting scopedStyleStrategy for users in the cases where they do not set it themselves.

After initially reading the PR and starting to review it, this was one of the points that kinda reassured me about the approach and that I wanted to bring up. Altho, after thinking more about it, I don't think this is something we can do as scopedStyleStrategy defaults to attribute and we would have no way to differentiate when it was set by the user or not (unless Astro starts exposing userConfig or something like that).

I'm posting this review to share my current thoughts and the issues I spotted so far, but I'm still a bit torn on the overall change.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not sure we can keep the current approach. Like you said, one of the most common issues you hit during the PR was icons no longer receiving the parent scope with the "attribute" strategy, but this also means that every user would also hit the same issue.

Let's say a user has the following code:

---
import { Icon } from '@astrojs/starlight/components';
---

<Icon name="star" size="10rem" class="my-icon" />

<style>
	.my-icon {
		color: red;
	}
</style>

This would no longer work and they would need to switch to :global(.my-icon) which is a worse DX imo (we would need to document it too). In this example, without a container to scope the selector, the style would also apply to any .my-icon element on the page.

I think we would need to pass the data-astro-cid-* attribute as documented:

<svg
	{...a11yAttrs}
-	class:list={['sl-icon', Astro.props.class]}
+ {...attrs}
+	class:list={['sl-icon', className]}
	style={{ '--sl-icon-color': color, '--sl-icon-size': size }}
	width="16"
	height="16"
	viewBox="0 0 24 24"
	fill="currentColor"
	set:html={Icons[name]}
/>

With this approach, I think the previous selectors would work again with all strategies, which also means we could basically revert a lot of :global() changes in the PR.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Great, great call! And this PR is much smaller with this approach 😁

I actually didn’t realise it was possible to get the component ID data attribute this way — I’m pretty sure all these edge details of the API were not 100% intentional/planned.

I applied this approach in fbf2677 which allows us to remove all the various changes around icons and keep benefiting from scoping in components.

Comment thread .changeset/dirty-worms-throw.md Outdated

⚠️ **BREAKING CHANGE:** Adds support for any Astro [`scopedStyleStrategy`](https://docs.astro.build/en/reference/configuration-reference/#scopedstylestrategy) configuration.

Previously, Starlight forced the `"where"` strategy, resulting in CSS selectors like `:where(astro-HASH)`. Starlight now respects the strategy set in `astro.config.mjs` and will default to Astro’s default `"attribute"` strategy, resulting in attribute selectors like `[data-astro-cid-HASH]`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
Previously, Starlight forced the `"where"` strategy, resulting in CSS selectors like `:where(astro-HASH)`. Starlight now respects the strategy set in `astro.config.mjs` and will default to Astro’s default `"attribute"` strategy, resulting in attribute selectors like `[data-astro-cid-HASH]`.
Previously, Starlight forced the `"where"` strategy, resulting in CSS selectors like `:where(.astro-HASH)`. Starlight now respects the strategy set in `astro.config.mjs` and will default to Astro’s default `"attribute"` strategy, resulting in attribute selectors like `[data-astro-cid-HASH]`.

Tiny fix, also if we keep the default Astro strategy, I wonder if we should also link to https://docs.astro.build/en/guides/styling/#passing-a-class-to-a-child-component as this may be required for some of their components which is a new behavior?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Took a pass in 797b66a to add more details. I think it’s definitely required given how annoying this change can be.

@delucis delucis added the 🌟 minor Change that triggers a minor release label Nov 2, 2026
delucis and others added 5 commits November 3, 2026 16:45
…ehaviour

Co-Authored-By: HiDeoo <494699+HiDeoo@users.noreply.github.com>
Co-authored-by: HiDeoo <494699+HiDeoo@users.noreply.github.com>
Co-Authored-By: HiDeoo <494699+HiDeoo@users.noreply.github.com>
@delucis
delucis requested a review from HiDeoo November 10, 2026 18:03
Instead of using `:global()` to fix the `<ThemeGrid>` component, we pass down the parent’s scoping attribute to `<ThemeImage>` to match our recommended migration path from the changeset.
@delucis

delucis commented Nov 10, 2026 •

Copy link
Copy Markdown
Member Author

Thanks for the great review — improved things a lot!

It could be less disruptive to continue setting scopedStyleStrategy for users in the cases where they do not set it themselves.

After initially reading the PR and starting to review it, this was one of the points that kinda reassured me about the approach and that I wanted to bring up. Altho, after thinking more about it, I don't think this is something we can do as scopedStyleStrategy defaults to attribute and we would have no way to differentiate when it was set by the user or not (unless Astro starts exposing userConfig or something like that).

Yeah, it’s a shame we can’t do that. I also don’t recall the reasoning for the switch from :where() to [data-*] as the default in Astro itself. Personally, I prefer the lower specificity option although at least we’re now more insulated from that inside Starlight itself thanks to cascade layers.

I'm still a bit torn on the overall change.

Let me know how you’re feeling after the most recent update. I’m also not super happy to change this for all the reasons you mentioned, but I don’t love that we block users from choosing if they do have a preference. Either way, it’s now or not until v2 of Starlight at the earliest, so we can decide what we want!

@HiDeoo HiDeoo added the pr-preview Trigger a preview release for this PR using pkg.pr.new label Nov 11, 2026
@github-actions github-actions Bot removed the pr-preview Trigger a preview release for this PR using pkg.pr.new label Nov 11, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 11, 2026

Copy link
Copy Markdown
pnpm add https://pkg.pr.new/@astrojs/starlight@4190

commit: faa2597

@delucis

delucis commented Nov 11, 2026

Copy link
Copy Markdown
Member Author

@HiDeoo HiDeoo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Tested the new preview release on multiple projects using the visual diffing tool as well and found no issues.

I also wanted to try it on starlight-themes with a more advanced setup: 22 themes, both light and dark variants, desktop and mobile, on 5 pages including the kitchen sink to catch as many issues as possible.

This ended up comparing 460 pairs of screenshots and only 3 themes reported differences caused by 3 kinds of issues:

  • A component styled by its parent component's scoped styles only forwarding the class attribute instead of all of the attributes, so it needs to be updated to forward them (like we did for our <Icon> component in this PR). I expect this to be one of the most common issues encountered.
  • A selector relying on a:not([class]) which now matches elements that previously received an Astro scoping class. This should be an easy fix and I'm not even sure this specific selector should have been used in the first place.
  • A dark mode selector in the scoped styles of an Astro component not using :global() so the rule never actually applied and the theme only looked right thanks to duplicated global styles. This is a real bug in the theme itself.

Of course, I cannot test all pages, all states, and all possible interactions, but considering the size of the test matrix, this makes me even more confident in the change (and the latest changes were already giving me more confidence).

I also expect a few issues in plugins (and I cannot realistically test all of them either) but I would expect a similar kinds of errors at a very low rate. Nothing that a few small fixes couldn't resolve and we will catch up on them as they arise.

Considering all this, I now feel condident in releasing this change. Thanks for all the work and effort you have put into this. 🚀

@delucis delucis added the ✅ approved Pull requests that have been approved and are ready to merge when next cutting a release label Nov 13, 2026
@delucis
delucis merged commit 3f2336b into main Nov 13, 2026
21 checks passed
@delucis
delucis deleted the chris/scoped-style-strategy branch November 13, 2026 17:01
@astrobot-houston astrobot-houston mentioned this pull request Nov 13, 2026
HiDeoo added a commit to HiDeoo/starlight that referenced this pull request Nov 16, 2026
* main: (62 commits)
  docs: point the changeset link at the current Changesets docs (withastro#4387)
  i18n(ko-KR): update `configuration`, `overrides` (withastro#4399)
  i18n(ja): update plugins.mdx (withastro#4403)
  i18n(ja): update overrides.md (withastro#4401)
  i18n(ja): update configuration.mdx (withastro#4402)
  i18n(ja): update site-search.mdx (withastro#4400)
  i18n(de): update `resources/plugins` (withastro#4407)
  i18n(de): update `overrides.mdx` (withastro#4406)
  i18n(de): update `configuration.mdx` (withastro#4405)
  i18n(de): update `site-search.mdx` (withastro#4404)
  [ci] release (withastro#4398)
  fix: restore aside component icon size (withastro#4396)
  i18n(es): update project-structure.mdx (withastro#4393)
  [ci] release (withastro#4391)
  Update peter-evans/create-pull-request action to v8.1.3 (withastro#4392)
  Allow passing user options to `pagefind.createIndex` (withastro#3674)
  Refactor `ThemeSelect` to use CSS instead of JS for icon updates (withastro#4219)
  Support any `scopedStyleStrategy` (withastro#4190)
  Update i18next to v27 (withastro#4385)
  Fix asides accepting Object.prototype property names as custom icons (withastro#4390)
  ...
HiDeoo added a commit that referenced this pull request Nov 17, 2026
* main: (187 commits)
  docs: point the changeset link at the current Changesets docs (#4387)
  i18n(ko-KR): update `configuration`, `overrides` (#4399)
  i18n(ja): update plugins.mdx (#4403)
  i18n(ja): update overrides.md (#4401)
  i18n(ja): update configuration.mdx (#4402)
  i18n(ja): update site-search.mdx (#4400)
  i18n(de): update `resources/plugins` (#4407)
  i18n(de): update `overrides.mdx` (#4406)
  i18n(de): update `configuration.mdx` (#4405)
  i18n(de): update `site-search.mdx` (#4404)
  [ci] release (#4398)
  fix: restore aside component icon size (#4396)
  i18n(es): update project-structure.mdx (#4393)
  [ci] release (#4391)
  Update peter-evans/create-pull-request action to v8.1.3 (#4392)
  Allow passing user options to `pagefind.createIndex` (#3674)
  Refactor `ThemeSelect` to use CSS instead of JS for icon updates (#4219)
  Support any `scopedStyleStrategy` (#4190)
  Update i18next to v27 (#4385)
  Fix asides accepting Object.prototype property names as custom icons (#4390)
  ...
dadezzz pushed a commit to dadezzz/university_notes that referenced this pull request Nov 18, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [@astrojs/starlight](https://starlight.astro.build) ([source](https://github.com/withastro/starlight/tree/HEAD/packages/starlight)) | [`0.42.11` → `0.43.1`](https://renovatebot.com/diffs/npm/@astrojs%2fstarlight/0.42.11/0.43.1) | ![age](https://developer.mend.io/api/mc/badges/age/npm/@astrojs%2fstarlight/0.43.1?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/@astrojs%2fstarlight/0.42.11/0.43.1?slim=true) |

---

### Release Notes

<details>
<summary>withastro/starlight (@&#8203;astrojs/starlight)</summary>

### [`v0.43.1`](https://github.com/withastro/starlight/blob/HEAD/packages/starlight/CHANGELOG.md#0431)

[Compare Source](https://github.com/withastro/starlight/compare/@astrojs/starlight@0.43.0...@astrojs/starlight@0.43.1)

##### Patch Changes

- [#&#8203;4396](withastro/starlight#4396) [`46a5020`](withastro/starlight@46a5020) Thanks [@&#8203;trueberryless](https://github.com/trueberryless)! - Fixes a regression where the icon of the `<Aside>` component was rendered smaller than the icon of Markdown asides.

### [`v0.43.0`](https://github.com/withastro/starlight/blob/HEAD/packages/starlight/CHANGELOG.md#0430)

[Compare Source](https://github.com/withastro/starlight/compare/@astrojs/starlight@0.42.11...@astrojs/starlight@0.43.0)

##### Minor Changes

- [#&#8203;4190](withastro/starlight#4190) [`3f2336b`](withastro/starlight@3f2336b) Thanks [@&#8203;delucis](https://github.com/delucis)! - ⚠️ **BREAKING CHANGE:** Adds support for any Astro [`scopedStyleStrategy`](https://docs.astro.build/en/reference/configuration-reference/#scopedstylestrategy) configuration.

  Previously, Starlight forced the `"where"` strategy, resulting in CSS selectors like `:where(.astro-HASH)`. Starlight now respects the strategy set in `astro.config.mjs` and will default to Astro’s default `"attribute"` strategy, resulting in attribute selectors like `[data-astro-cid-HASH]`.

  The new default can change some styling behaviour, so we recommend checking for visual changes in your site when updating. In particular, note the following:

  - Attribute selectors have a higher specificity than `:where()` selectors. Check to ensure styles still apply and no CSS changes are needed to ensure the relative specificity of declarations remains the same.
  - Astro’s `"attribute"` strategy does not automatically pass along a parent component’s scope when passing a `class` down to a child component. If you have components that depended on this, you will need to update the child component to spread the parent’s data attribute where you are reusing the class name, for example:

    ```astro
    ---
    // Parent.astro
    ---

    <Child class="special-case" />

    <style>
      .special-case {
        --accent-color: red;
      }
    </style>
    ```

    ```diff
    ---
    // Child.astro
    -const { class: className } = Astro.props;
    +const { class: className, ...attributes } = Astro.props;
    ---

    -<div class={className}>
    +<div class={className} {...attributes}>
      <!-- ... -->
    </div>
    ```

    See [“Passing a `class` to a child component”](https://docs.astro.build/en/guides/styling/#passing-a-class-to-a-child-component) in the Astro docs for more details.

  Alternatively, if you wish to continue using the previous scoping strategy in your site, set it explicitly in your Astro configuration file:

  ```diff
  // astro.config.mjs
  import { defineConfig } from 'astro/config';

  export default defineConfig({
  + scopedStyleStrategy: "where",
    // ...
  });
  ```

- [#&#8203;3674](withastro/starlight#3674) [`993ac27`](withastro/starlight@993ac27) Thanks [@&#8203;controversial](https://github.com/controversial)! - Adds support for Pagefind’s [`excludeSelectors`](https://pagefind.app/docs/config-options/#exclude-selectors) and [`includeCharacters`](https://pagefind.app/docs/config-options/#include-characters) configuration to customize how search indexes are built

- [#&#8203;4219](withastro/starlight#4219) [`35abe4f`](withastro/starlight@35abe4f) Thanks [@&#8203;delucis](https://github.com/delucis)! - Refactors Starlight’s theme select menu to reduce its JavaScript use

  ⚠️ **Potentially breaking change:** If your project or theme plugin overrides the `ThemeSelect` component, check that it still works after updating. The global API previously used by theme select components has been removed and calling `StarlightThemeProvider.updatePickers()` will no longer work.

  See [`ThemeProvider.astro`](https://github.com/withastro/starlight/blob/main/packages/starlight/src/components/ThemeProvider.astro), [`ThemeSelect.astro`](https://github.com/withastro/starlight/blob/main/packages/starlight/src/components/ThemeSelect.astro), and [`LanguageSelect.astro`](https://github.com/withastro/starlight/blob/main/packages/starlight/src/components/LanguageSelect.astro) on GitHub for the full source code of the updated components.

- [#&#8203;4385](withastro/starlight#4385) [`c130349`](withastro/starlight@c130349) Thanks [@&#8203;delucis](https://github.com/delucis)! - Updates dependency `i18next` to v27

  ⚠️ **Potentially breaking change:** If you use Starlight’s [translation APIs](https://starlight.astro.build/guides/i18n/#using-ui-translations) directly, see [i18next’s migration guide](https://www.i18next.com/misc/migration-guide) for details. We don’t expect any changes to impact Starlight users, but [let us know](https://github.com/withastro/starlight/issues/new/choose) if they do.

##### Patch Changes

- [#&#8203;4390](withastro/starlight#4390) [`f8b072a`](withastro/starlight@f8b072a) Thanks [@&#8203;Toyenzo](https://github.com/Toyenzo)! - Errors if an aside is passed an icon name that matches `Object.prototype` properties, such as `constructor` or `toString`

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate CLI](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0NC4yNDAuMCIsInVwZGF0ZWRJblZlciI6IjQ0LjI0MC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants