-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement scopedStyleStrategy (#6771)
* Implement scopedStyleStrategy * Add changeset * Update compiler * Specify the eswalker version * Update compiler * Update .changeset/green-cups-hammer.md Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * Update .changeset/green-cups-hammer.md Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * Update packages/astro/src/@types/astro.ts Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update packages/astro/src/@types/astro.ts Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> * Update .changeset/green-cups-hammer.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
- Loading branch information
1 parent
49514e4
commit 3326492
Showing
9 changed files
with
2,231 additions
and
2,068 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--- | ||
'astro': minor | ||
--- | ||
|
||
Implements a new class-based scoping strategy | ||
|
||
This implements the [Scoping RFC](https://github.com/withastro/roadmap/pull/543), providing a way to opt in to increased style specificity for Astro component styles. | ||
|
||
This prevents bugs where global styles override Astro component styles due to CSS ordering and the use of element selectors. | ||
|
||
To enable class-based scoping, you can set it in your config: | ||
|
||
```js | ||
import { defineConfig } from 'astro/config'; | ||
|
||
export default defineConfig({ | ||
scopedStyleStrategy: 'class' | ||
}); | ||
``` | ||
|
||
Note that the 0-specificity `:where` pseudo-selector is still the default strategy. The intent is to change `'class'` to be the default in 3.0. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/astro/test/fixtures/scoped-style-strategy/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@test/scoped-style-strategy", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/astro/test/fixtures/scoped-style-strategy/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<html> | ||
<head> | ||
<title>scopedStyleStrategy</title> | ||
<style> | ||
h1 { | ||
color: green; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>scopedStyleStrategy</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { expect } from 'chai'; | ||
import * as cheerio from 'cheerio'; | ||
import { loadFixture } from './test-utils.js'; | ||
|
||
describe('scopedStyleStrategy', () => { | ||
describe('default', () => { | ||
/** @type {import('./test-utils').Fixture} */ | ||
let fixture; | ||
let stylesheet; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/scoped-style-strategy/', | ||
}); | ||
await fixture.build(); | ||
|
||
const html = await fixture.readFile('/index.html'); | ||
const $ = cheerio.load(html); | ||
const $link = $('link[rel=stylesheet]'); | ||
const href = $link.attr('href'); | ||
stylesheet = await fixture.readFile(href); | ||
}); | ||
|
||
it('includes :where pseudo-selector', () => { | ||
expect(stylesheet).to.match(/:where/); | ||
}); | ||
|
||
it('does not includes the class name directly in the selector', () => { | ||
expect(stylesheet).to.not.match(/h1\.astro/); | ||
}); | ||
}); | ||
|
||
describe('scopedStyleStrategy: "class"', () => { | ||
/** @type {import('./test-utils').Fixture} */ | ||
let fixture; | ||
let stylesheet; | ||
|
||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/scoped-style-strategy/', | ||
scopedStyleStrategy: 'class' | ||
}); | ||
await fixture.build(); | ||
|
||
const html = await fixture.readFile('/index.html'); | ||
const $ = cheerio.load(html); | ||
const $link = $('link[rel=stylesheet]'); | ||
const href = $link.attr('href'); | ||
stylesheet = await fixture.readFile(href); | ||
}); | ||
|
||
it('does not include :where pseudo-selector', () => { | ||
expect(stylesheet).to.not.match(/:where/); | ||
}); | ||
|
||
it('includes the class name directly in the selector', () => { | ||
expect(stylesheet).to.match(/h1\.astro/); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.