Repository navigation
Support any scopedStyleStrategy - #4190
Conversation
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 detectedLatest commit: 7a10d5d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
✅ Deploy Preview for astro-starlight ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
size-limit report 📦
|
|
Just went through this again and marked it ready for review. I think it should be a reasonably safe change thanks to
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 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
left a comment
There was a problem hiding this comment.
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
scopedStyleStrategyfor 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
|
||
| ⚠️ **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]`. |
There was a problem hiding this comment.
| 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?
There was a problem hiding this comment.
Took a pass in 797b66a to add more details. I think it’s definitely required given how annoying this change can be.
…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>
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.
|
Thanks for the great review — improved things a lot!
Yeah, it’s a shame we can’t do that. I also don’t recall the reasoning for the switch from
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! |
commit: |
|
I tested the preview release on some pages of Astro’s docs with @HiDeoo’s visual diffing tool and found no differences which is reassuring. |
HiDeoo
left a comment
There was a problem hiding this comment.
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
classattribute 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. 🚀
* 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) ...
* 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) ...
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) |  |  | --- ### Release Notes <details> <summary>withastro/starlight (@​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 - [#​4396](withastro/starlight#4396) [`46a5020`](withastro/starlight@46a5020) Thanks [@​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 - [#​4190](withastro/starlight#4190) [`3f2336b`](withastro/starlight@3f2336b) Thanks [@​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", // ... }); ``` - [#​3674](withastro/starlight#3674) [`993ac27`](withastro/starlight@993ac27) Thanks [@​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 - [#​4219](withastro/starlight#4219) [`35abe4f`](withastro/starlight@35abe4f) Thanks [@​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. - [#​4385](withastro/starlight#4385) [`c130349`](withastro/starlight@c130349) Thanks [@​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 - [#​4390](withastro/starlight#4390) [`f8b072a`](withastro/starlight@f8b072a) Thanks [@​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-->

Background
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".@layerstyles making support for different scoping strategies more viable.This PR
scopedStyleStrategyand 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 ourstarlight.componentslayer, I had to extract the<Icon>styles and move them to a global.sl-iconclass name instead of just depending on an Astro-scopedsvgelement selector. I also added this class name to the icons generated by Markdown asides for consistency.Open questions
scopedStyleStrategyfor 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.