Pass in scoped class name to child components (#579)
* Pass in scoped class name to child components If a class is being passed into child components, pass in the scoped class name as well. * Adds the changeset
This commit is contained in:
parent
f721275f33
commit
1e735bb331
5 changed files with 34 additions and 3 deletions
5
.changeset/eighty-dogs-notice.md
Normal file
5
.changeset/eighty-dogs-notice.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Allows passing in a class to a child component which will be scoped
|
|
@ -196,8 +196,15 @@ export default function transformStyles({ compileOptions, filename, fileID }: Tr
|
|||
html: {
|
||||
InlineComponent: {
|
||||
enter(node) {
|
||||
if (node.name !== 'Markdown') return;
|
||||
injectScopedClassAttribute(node, scopedClass, '$scope');
|
||||
if (node.name === 'Markdown') {
|
||||
injectScopedClassAttribute(node, scopedClass, '$scope');
|
||||
}
|
||||
for(let attr of node.attributes) {
|
||||
if(attr.name === 'class') {
|
||||
injectScopedClassAttribute(node, scopedClass, 'class');
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
Element: {
|
||||
|
|
|
@ -133,4 +133,17 @@ StylesSSR('Astro scoped styles skipped without <style>', async ({ runtime }) =>
|
|||
assert.type($('#no-scope').attr('class'), 'undefined', `Astro component without <style> should not include scoped class`);
|
||||
});
|
||||
|
||||
StylesSSR('Astro scoped styles can be passed to child components', async ({ runtime }) => {
|
||||
const result = await runtime.load('/');
|
||||
const $ = doc(result.contents);
|
||||
|
||||
let scopedClass;
|
||||
$('style').html().replace(/outer\.(astro-[A-Za-z0-9-]+)/, (match, p1) => {
|
||||
scopedClass = p1;
|
||||
return match;
|
||||
});
|
||||
|
||||
assert.match($('#passed-in').attr('class'), `outer ${scopedClass}`);
|
||||
});
|
||||
|
||||
StylesSSR.run();
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
---
|
||||
let blue = true;
|
||||
let visible = true;
|
||||
|
||||
const { class: cn } = Astro.props;
|
||||
---
|
||||
|
||||
<style>
|
||||
|
@ -21,3 +23,4 @@ let visible = true;
|
|||
<div id="dynamic-class" class={blue ? 'blue' : 'notblue'}>I change colors</div>
|
||||
{visible && <div id="dynamic-vis" class="visible">I disappear</div>}
|
||||
<div id="colon-class" class="color:blue">I am blue</div>
|
||||
<div id="passed-in" class={cn}>This was passed in</div>
|
|
@ -18,11 +18,14 @@ import SvelteScoped from '../components/SvelteScoped.svelte';
|
|||
margin-right: auto;
|
||||
max-width: 1200px;
|
||||
}
|
||||
.outer {
|
||||
color: red;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="wrapper">
|
||||
<AstroComponent />
|
||||
<AstroComponent class="outer" />
|
||||
<AstroComponentNone />
|
||||
<ReactCSS />
|
||||
<ReactModules />
|
||||
|
|
Loading…
Reference in a new issue