Skip to content

Instantly share code, notes, and snippets.

@espretto
Created November 8, 2022 09:35
Show Gist options
  • Save espretto/f745ce8e2631c672fbf8659b4ccd2621 to your computer and use it in GitHub Desktop.
Save espretto/f745ce8e2631c672fbf8659b4ccd2621 to your computer and use it in GitHub Desktop.
ts: stringify css declaration
const kebabCase = (camelCased: string) =>
camelCased.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase();
export const toCssText = (styles: object) =>
Object.keys(styles)
// @ts-ignore: trust input is a CSSStyleDeclaration
.flatMap(prop => [kebabCase(prop), ":", styles[prop], ";"])
.join("");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment