-
Notifications
You must be signed in to change notification settings - Fork 168
CSS-in-JS Support? #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
An example of it with macros: // macro source
export default function Styled(initialCSS, ...values) {
return "";
}
Styled.macro = {
transform(initialCSS, ...values) {
let outputCSS = "";
let valueCounter = 0;
const id = Math.random().toString(36).slice(2);
initialCSS.forEach(intial => {
outputCSS += initial;
outputCSS += values[valueCounter];
valueCounter++;
});
return `
(() => {
const styleElem = document.createElement("style");
styleElem.textContent = ".styled-${id}{${intialCSS}}";
document.head.appendChild(styleElem);
return "styled-${id}";
})();
`
}
} // source code
const className = styled`
color: red;
background-color: grey;
`; // compiled code
const className = (() => {
const styleElem = document.createElement("style");
styleElem.textContent = ".styled-asdfi4788837{color: red;background-color:grey;}";
document.head.appendChild(styleElem);
return "styled-asdfi4788837";
})(); |
0.3 will support inline css like: export default function App() {
return (
<>
<h1>Hello World</h1>
<style>{`
h1 {
color: #d63369;
}
`}</style>
</>
)
} |
@ije Cool, but as far as I am aware that is very basic CSS-in-JS. Most CSS-in-JS frameworks support stuff like theming and dynamic styles too. |
I'm using the framework agonistic version of Emotion (CSS-in-JS) in Aleph.js without any effort: https://emotion.sh/docs/@emotion/css using the esm.sh import in my import_map.json:
then in my calling code:
also the global css in the app.tsx file:
Didn't know where to begin with getting the react version working (https://emotion.sh/docs/@emotion/react) and it's custom jsx transform, without babel. Not sure if the new JSX transform in React will help or hinder this: https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html In any case this framework agnostic CSS-in-JS solution is already a massive improvement for me over SASS/LESS/CSS. |
@thegaryroberts Thanks for sharing how you do it, I think Emotions React pkg has out of the box support with |
So the main hurdle i faced without @jsx directive support, is when I tried this (as per emotion documented example):
the jsx value gets clean up/removed from the import by the deno fmt as it assumes it is not referenced. |
@thegaryroberts I think you could open an issue for that because afaik |
It turns out it was my IDE (VS Code) removing it. The directive is not removed if it is the very first line in the file. However this just led onto other issues getting emotion/react to work. Tried using the esm.sh import bundle options, but is still delivering emotion with inbuilt react version which leads to multiple react version warnings (and probable errors). Anyway giving up and sticking to emotion/css for now. (A shame as emotion/react is a true component based approach to CSS. In my opinion modern projects should consider it as the starting point for CSS integration, instead of separate file css/sass approaches. We benefit from a consolidated componentisation approach to JS & HTML, e.g. React. Why exclude CSS from the componentisation paradigm). |
Hey @thegaryroberts, I've been trying to get emotion to work but keep running into the same error when calling css:
using deno 1.11 and aleph 0.3.0-alpha.33 Is it still working for you? |
A ton of people like CSS-in-JS and I think we should add support for it. We have to figure out how we will do it. I see 2 options:
styled-components
, though we would have to write our own SSR compiler thingThe text was updated successfully, but these errors were encountered: