-
-
Notifications
You must be signed in to change notification settings - Fork 96
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
Parse classNames to inline styles. #96
Comments
For the let rules
tw(({ css }) => {
rules = css('text-center')
/// rules = css([apply`...`, 'text-center text-xl', css`...`])
return ''
})
// => rules = { textAlign: center } Then we need to convert the object to styles (hyphenate, and some other stuff). The functionality is already internally available. Maybe we could define a new module (twind/style) to do that. |
With #101 in place we could expose the css context as tw.style`text(center white) bg-black`
// => { align: "center", color: "#fff", ...} @tw-in-js/contributors Thoughts? |
I don't know enough about this problem/ use case, but that seems really nice for those who might need it. I might lean towards I wonder if it is possible, or useful, to optionally generate the styles in raw format: |
The raw variant is the idea behind this issue. I was thinking to use twind for styling email templates, but than I'd need to end up with inline styles, in string format.
|
That’s kind of what I figured. The <p id="title">
Hello, friend.
</p>
<script type="module">
import { tw } from 'https://cdn.skypack.dev/twind';
const el = document.getElementById("title");
el.style = tw.style`text(blue-500 uppercase) p-2`;
</script> I’m sure there are a plenty of considerations and challenges that I haven’t thought of but it seems like a neat idea. |
@smeijer Do you want to use this feature for https://github.com/rakered/rakered/tree/main/packages/email? I agree that What do we do about child selectors, pseudo classes, global styles? The only reasonable approach I see is to throw an error because we can not add a class or something like that. |
That was the idea indeed. That way I could clean up this mess: https://github.com/rakered/rakered/blob/main/packages/email/src/templates/themes/default.ts 😬. Some things would indeed not be possible, that's why I mentioned:
IF this is something that you'd want to support, the inline style support would be very basic. Some things won't be supported, or would still end up in the So I think that's a decision that should be made first. Not everything can end up in the |
After thinking about A separate module like After looking at Email Client CSS Support @smeijer Wouldn't the |
I found a similar issue discussed on the styled-components repo, which pointed to tools like |
Unfortunately, that's a big one 😞 Yes, the What would it look like when using it together with the shim? |
@mattrossman That makes things easier for us. Something like the following should work already:
Based on a react app: import { renderToString } from 'react-dom/server'
import inlineCss from 'inline-css'
import { setup } from 'twind'
import { virtualSheet, getStyleTag } from 'twind/sheets'
import App from './app'
const sheet = virtualSheet()
setup({ ...sharedOptions, sheet })
function ssr() {
// 1. Reset the sheet for a new rendering
sheet.reset()
// 2. Render the app
const body = renderToString(<App />)
// 3. Create the style tag with all generated CSS rules
const styleTag = getStyleTag(sheet)
// 4. Generate the html
const html = `<!DOCTYPE html>
<html lang="en">
<head>${styleTag}</head>
<body>${body}</body>
</html>
`
// 5. Inline the CSS – returns a Promise
return inlineCss(html, { /* options */ })
} If this works it is an extension to the existing SSR examples (incl the shim) as only the last line is specific to email. We could add a recipe or ad it to the FAQs. |
@smeijer Does the example work as you want it? Then I would add a section to the docs and close this one. |
@sastan, I just tried it, and the output is sufficient for my needs! Thanks. Just good to know, the non-supported classes don't seem to end up in |
Documentation will be added via #125 |
It would be nice if we could use the server side rendering part, to style emails with this library.
The thing is, not all mail clients know how to handle style tags. The recommended approach is still to use inline styles (
<div style="color: red">
).I do realize that it's not possible to support everything with that approach, but even partial support could be of immense value when creating mail templates.
Things that are unsupported (like media queries) could still go to the head style sheet for those clients that do support it.
It would also mean that css variables should be resolved.
Thoughts?
The text was updated successfully, but these errors were encountered: