-
I didn't find an example of defining style for a component scope, is this possible? I want to create components with scoped-style classes |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 12 replies
-
I looked into how Vue does that and it seems it basically parses out the css and then prefixes the class names to "scope" them. That seemed like an awful lot of work and code for a feature that (at least to me?) seemed niche. What I have been doing is just using inline styles like this. class MyComponent(Component):
lg_style = "font-size: 200%"
def populate(self):
t.span("Hello", style=self.lg_style) Admittedly that's not quite the same. I'd be open to other ideas? The issue is mostly that parsing css isn't easy in python. Especially micropython. 😬 |
Beta Was this translation helpful? Give feedback.
-
When I started using the ltk, I thought that having I mention this ask you to consider that maybe there is a lib that you might consider that has already resolved an efficient implementation to solve this css issue. |
Beta Was this translation helpful? Give feedback.
-
We could solve this by generating a common "hash class name" and applying it to every tag that belongs to the component, i.e. ...
def scoped_styles(self):
return dict(
_button= dict(
border-radius= "5px",
),
container= dict(
display= "flex",
flex_direction= self.flex_dir,
_hover= dict(
background_color= "#AAFFFF",
)
)
)
def populate(self):
with t.div(classes=[self.styles["container"]]):
...
... Then in css // generated CSS by the component
// These styles only apply to buttons with the component's unique class identifier
button.some_unique_class_component {
border-radius: 5px;
}
// Then for the generated "container" class we might not need a new prefix
some_unique_class_component.container {
display: flex;
flex-direction: column;
}
some_unique_class_component.container:hover {
background-color: #AAFFFF;
} |
Beta Was this translation helpful? Give feedback.
-
Here's what I came up with so far in a working prototype. My prototype is generating some imperfect css classes at runtime, but I think strongly this is the direction to take: #
# These can be defined at a class level, or at a top-level
container_class = ScopedClass(
padding="1em",
margin="1em",
border="solid 2px #333",
background_color="#efefef",
)
@t.component()
class Message(Component):
# Class-level ones are fine
title_class = ScopedClass(
color="#333",
font_size="1.5em",
)
# You can use the ScopedClass instances directly
default_classes = [container_class]
def populate(self):
# You can use the ScopedClass instances inside populate() too
with t.h2(classes=[self.title_class]):
t.h1("Title") There are several benefits here:
What this doesn't solve is how you might apply a style to all buttons in a component. I'll give that some thought. |
Beta Was this translation helpful? Give feedback.
-
I haven't updated the docs yet, and the next couple of weekends I have family stuff, but I thought I'd let you know I merged this and pushed a release with it. In 0.4.5, you can see it in the components.py example here: https://github.com/kkinder/puepy/blob/release/0.4.5/examples/tutorial/06_components/components.py My general thinking is that the way the docs work needs to be redone a bit. This should be its own chapter, but I'll want to insert it somewhere and maybe rethink the flow of the whole tutorial so each chapter builds on the last a bit. At any rate, if you upgrade to 0.4.5, you can try this out. |
Beta Was this translation helpful? Give feedback.
I haven't updated the docs yet, and the next couple of weekends I have family stuff, but I thought I'd let you know I merged this and pushed a release with it. In 0.4.5, you can see it in the components.py example here:
https://github.com/kkinder/puepy/blob/release/0.4.5/examples/tutorial/06_components/components.py
My general thinking is that the way the docs work needs to be redone a bit. This should be its own chapter, but I'll want to insert it somewhere and maybe rethink the flow of the whole tutorial so each chapter builds on the last a bit.
At any rate, if you upgrade to 0.4.5, you can try this out.