Skip to content

Commit

Permalink
Update style attribute behavior for better object support (#7542)
Browse files Browse the repository at this point in the history
  • Loading branch information
natemoo-re authored Jul 3, 2023
1 parent 19c2d43 commit cdc2832
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-crews-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix bug when using `define:vars` with a `style` object
2 changes: 1 addition & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
"test:e2e:match": "playwright test -g"
},
"dependencies": {
"@astrojs/compiler": "^1.5.0",
"@astrojs/compiler": "^1.5.3",
"@astrojs/internal-helpers": "^0.1.1",
"@astrojs/language-server": "^1.0.0",
"@astrojs/markdown-remark": "^2.2.1",
Expand Down
9 changes: 7 additions & 2 deletions packages/astro/src/runtime/server/render/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ Make sure to use the static attribute syntax (\`${key}={value}\`) instead of the
}

// support object styles for better JSX compat
if (key === 'style' && !(value instanceof HTMLString) && typeof value === 'object') {
return markHTMLString(` ${key}="${toAttributeString(toStyleString(value), shouldEscape)}"`);
if (key === 'style' && !(value instanceof HTMLString)) {
if (Array.isArray(value) && value.length === 2) {
return markHTMLString(` ${key}="${toAttributeString(`${toStyleString(value[0])};${value[1]}`, shouldEscape)}"`);
}
if (typeof value === 'object') {
return markHTMLString(` ${key}="${toAttributeString(toStyleString(value), shouldEscape)}"`);
}
}

// support `className` for better JSX compat
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/astro-directives.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ describe('Directives', async () => {
expect($('h1').attr('style').toString()).to.include('--textColor: red;');
});

it('Properly handles define:vars on style elements with style object', async () => {
const html = await fixture.readFile('/define-vars/index.html');
const $ = cheerio.load(html);

// All styles should be bundled
expect($('style')).to.have.lengthOf(0);

// Inject style attribute on top-level element in page
expect($('#compound-style').attr('style').toString()).to.include('color:var(--fg);--fg: black;--bg: white;');
});

it('set:html', async () => {
const html = await fixture.readFile('/set-html/index.html');
const $ = cheerio.load(html);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let undef: undefined;
<script id="inline-5" define:vars={{ undef }}>
console.log(undef);
</script>

<div id="compound-style" style={{ color: `var(--fg)` }}></div>
<Title />
</body>
</html>
14 changes: 7 additions & 7 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cdc2832

Please sign in to comment.