feat: add support for object-based styles
This commit is contained in:
parent
9532d56b50
commit
47941c4d7e
2 changed files with 11 additions and 0 deletions
5
.changeset/sweet-dodos-mix.md
Normal file
5
.changeset/sweet-dodos-mix.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Add support for object-based styles
|
|
@ -405,6 +405,8 @@ export function createAstro(
|
|||
};
|
||||
}
|
||||
|
||||
const kebab = (k: string) => k.toLowerCase() === k ? k : k.replace(/[A-Z]/g, match => `-${match.toLowerCase()}`);
|
||||
const toStyleString = (obj: Record<string, any>) => Object.entries(obj).map(([k, v]) => `${kebab(k)}:${v}`).join(';')
|
||||
const toAttributeString = (value: any, shouldEscape = true) =>
|
||||
shouldEscape ? String(value).replace(/&/g, '&').replace(/"/g, '"') : value;
|
||||
|
||||
|
@ -437,6 +439,10 @@ Make sure to use the static attribute syntax (\`${key}={value}\`) instead of the
|
|||
return markHTMLString(` ${key.slice(0, -5)}="${toAttributeString(serializeListValue(value))}"`);
|
||||
}
|
||||
|
||||
if (key === 'style' && typeof value === 'object') {
|
||||
return markHTMLString(` ${key}="${toStyleString(value)}"`);
|
||||
}
|
||||
|
||||
// Boolean values only need the key
|
||||
if (value === true && (key.startsWith('data-') || htmlBooleanAttributes.test(key))) {
|
||||
return markHTMLString(` ${key}`);
|
||||
|
|
Loading…
Reference in a new issue