Using tailwindcss in shadow DOM #37
Replies: 3 comments 7 replies
-
Hey there! Shadow DOM provides 3 things
The first two are the ones that conflict with what you want to try, when you use Shadow DOM you basically close your component to external manipulation by default which means external stylesheets won't work at all with your encapsulated components, it also means that anything like There are two ways you can overcome this situation
[<LitElement("my-button")>]
let MyButton() =
let _, props =
LitElement.init(fun init ->
init.useShadwoDom <- false
init.props <- {| children = Prop.Of("" :?> TemplateResult, attribute = "content") |} )
html $"""<button class="mx-4"><div class="bg-purple-800 rounded border-2">{pops.children.Value}</div></button>"""
// usage
let content = $"<span class='red'>This</span> is my <span class='blue'>content</span>!"
html $"<my-button .content={content}></my-button>" this will not use shadow DOM and therefore it will be like any other HTML element out there with the extra benefit that your component will actually have an instance
[<HookComponent>]
let myButton(content: TempalteResult) =
// with HookComponent you can use hooks like useState
html $"""<button class="mx-4"><div class="bg-purple-800 rounded border-2">{content}</div></button>"""
// or
let myButton(content: TempalteResult) =
// no hooks available here
html $"""<button class="mx-4"><div class="bg-purple-800 rounded border-2">{content}</div></button>"""
let content = $"<span class='red'>This</span> is my <span class='blue'>content</span>!"
html $"<section>This is a button: {myButton content}</section>" This won't create a new component but it will just rather return just "Html" that can be used like any other function The downsides for these two approaches are that you will lose the 3 benefits from shadow dom and content projection. Also I want to mention that you can's style the slot element itself you can style the sorrounding elements (parent or siblings) though As for extra context this discussion may be of help for you #33 |
Beta Was this translation helpful? Give feedback.
-
I'm not sure whether using CSS modules is another possibility - I haven't been able to get it working. More here: https://twitter.com/buildWithLit/status/1503400707667513359 My attempt looks something like this: module Lit.TodoMVC.Fresh
open Lit
open Fable.Core
let register() = ()
[<Emit("import tailwindStyles from '/src/out/style.module.css' assert { type: 'css' }")>]
let importTailwindStyles() = jsNative
importTailwindStyles()
[<Emit("tailwindStyles")>]
let tailwindStyles() = jsNative
[<LitElement("fresh-component")>]
let FreshComponent() =
let _ = LitElement.init(fun init ->
init.useShadowDom <- true
init.styles <- [
yield! tailwindStyles()
css $"""
p {{
color: red;
}}
"""
]
)
html $"""
<div class="bg-white rounded-lg">
Fresh Component
</div>
""" Unfortunately this doesn't work for me when starting from the Lit.TodoMVC project as a starting point (i.e. using Vite). I get a browser error Also when I try and build, it doesn't seem to like the
|
Beta Was this translation helpful? Give feedback.
-
BTW Fable.Lit implements a custom mechanism to scope CSS at the component level which should work very similar to CSS modules. So this is when you don't want to use shadow Dom in order to have global styles, but at the same time you want to have locally scoped rules for your components: https://fable.io/Fable.Lit/docs/hook-components.html#scoped-css |
Beta Was this translation helpful? Give feedback.
-
Hi,
I am playing with a lot of new (for me they are new 😅) technologies: vite, fable.lit, tailwindcss.
Fable.Lit + vite + tailwindcss already works, but I got into problems with tailwindcss in components.
Here is a small repro: https://github.com/ThisFunctionalTom/test-lit
Do you know why it is not working?
I think I do not understand how shadow DOM works, or maybe it has something to do with how tailwind generates css on the fly?
Beta Was this translation helpful? Give feedback.
All reactions