Replies: 13 comments 8 replies
-
extra benefit: you can transform classlists to selectors as part of the template transform instead of afterwards inside the object keys |
Beta Was this translation helpful? Give feedback.
-
Personally, I'm not a fan of this being part of the core API. However I think it's nice to be able to convert from one to the other quickly. Someone has actually already built a CSS to VE converter btw: https://css-to-vanilla-extract.netlify.app/. |
Beta Was this translation helpful? Give feedback.
-
That convertor is nice but coming from styled-components it would still be nice to have the css syntax in the css.ts file. @mattcompiles I've been playing with Stylis and I think I was able to make it. See this StackBlitz I think this will work? I'm just not sure about converting classlists to selectors. When I encounter an interpolation in a selector, I check if it is a string of one or more words ending with 6 alphanumeric characters followed by digits separated by spaces. Is that always correct? I'd rather not have to dig into VE internals to grab the known identifiers. Using my function, this css: // ignore me
padding: 0.5em;
border: thin solid var(${'--color-hint'});
border-bottom: none;
:focus {
background: unsupported(123);
background: gold;
}
@media screen and (min-width: 768px) {
whatever: hello;
:hover { color: green }
}
@media printer {
// use up all the ink
background-color: black
}
${'header_header__1wihje60 oowjoq42'} + & {
border-left: none;
}
@supports (display: grid) {
display: grid;
} converts into {
"padding": "0.5em",
"border": "thin solid var(--color-hint)",
"border-bottom": "none",
"selectors": {
":focus": {
"background": [
"unsupported(123)",
"gold"
]
},
".header_header__1wihje60+&": {
"border-left": "none"
}
},
"@media": {
"screen and (min-width: 768px)": {
"whatever": "hello",
"selectors": {
":hover": {
"color": "green"
}
}
},
"printer": {
"background-color": "black"
}
},
"@supports": {
"(display: grid)": {
"display": "grid"
}
}
} Is that correct? |
Beta Was this translation helpful? Give feedback.
-
I released a library, probably there's some edge cases, but it works for me. It would be nice if #895 were merged 😀 |
Beta Was this translation helpful? Give feedback.
-
@wmertens @mattcompiles This is high value, if not essential functionality from my perspective. |
Beta Was this translation helpful? Give feedback.
-
It would be great if someone more knowledgeable about vanilla-extract could take a look at https://github.com/wmertens/qwik-styled-ve/blob/main/src/css.ts Perhaps this could be included into VE? It's still 0 runtime... |
Beta Was this translation helpful? Give feedback.
-
kinda related https://github.com/macaron-css/macaron |
Beta Was this translation helpful? Give feedback.
-
So, is the |
Beta Was this translation helpful? Give feedback.
-
@mattcompiles It works nicely for me, and it's really not much code - would you be open to a PR that adds css template string support? |
Beta Was this translation helpful? Give feedback.
-
Are there any updates on this? Looking for a new CSS solution, but the object syntax makes this cumbersome to write. |
Beta Was this translation helpful? Give feedback.
-
Alternative LibraryLooks like the new zero-runtime Panda CSS already implemented this feature! Maybe a good reason to switch from Vanilla Extract over there 👍 |
Beta Was this translation helpful? Give feedback.
-
KISS workaround: import { GlobalStyleRule, globalStyle, style } from '@vanilla-extract/css';
/** Adds free-form CSS as a globalStyle */
export const vanillaGlobal = (css: string) =>
globalStyle('x', { x: `} ${css} x{x:` } as GlobalStyleRule);
/** Spread the return value into a style object, to inject free-form CSS properties */
export const vanillaProps = (css: string) =>
({ x: `; ${css}` } as GlobalStyleRule);
/** Returns a scoped cssClassName styled with free-form CSS */
export function vanillaClass(css: string): string;
export function vanillaClass(debugId: string, css: string): string;
export function vanillaClass(cssOrDebugId: string, css?: string): string {
const debugId = css != null ? cssOrDebugId : undefined;
css = css != null ? css : cssOrDebugId;
return style(vanillaProps(css), debugId);
} Usage: import { style } from '@vanilla-extract/css';
export const page = style({
borderTop: '1px solid blue',
...vanillaProps(`
/* Plain CSS that's inject into the "page" style block */
border-bottom: 1px solid red;
color: ${theme.color.primary}; /* I can still use typesafe values */
random-css-prop-normally-rejected-by-vanilla-extract: 'YOLO!';
`)
});
export const myClass = vanillaClass('myClass', `
background-color: #ccc;
padding: .5em 1em;
`);
vanillaGlobal(`
/* I'm free to write my own CSS!! */
body {
--fruit: 'banana!'
}
`) By adding the styled-components VSCode extension and writing a tiny tagged-template literal called
FAQ:
|
Beta Was this translation helpful? Give feedback.
-
If the goal is to write plain CSS, wouldn't a |
Beta Was this translation helpful? Give feedback.
-
For DX, it would be nice to be able to directly copy CSS snippets, for example from the browser devtools, so it would be nice to have a helper function that converts actual CSS to ComplexCss:
Is that desirable? What would the special properties like
selectors
andvars
look like inside a template?Looks like https://stylis.js.org could do it with a middleware
Beta Was this translation helpful? Give feedback.
All reactions