Styling Brainstorming #2003
Replies: 41 comments 7 replies
-
I think the system for styling would be best as a separate system, where individual properties are ideally strictly typed. Then the library could be integrated here. |
Beta Was this translation helpful? Give feedback.
-
I agree with @ThomasdenH and think this could start out as a separate crate but could eventually get added to @jkelleyrtp are you thinking of taking approach like https://cssinjs.org/? I would love having a way to use declarative styles like this. Then the integration with |
Beta Was this translation helpful? Give feedback.
-
@jstarry I was definitely feeling inline CSS in the same way that JSS works - I like having the state of the component directly tied to its appearance. I'm going to approach this in the same way that styled-components work so we can just drop existing stylesheets into components but also be able to modify them on component updates. |
Beta Was this translation helpful? Give feedback.
-
Personally I just use bootstrap. Then apply the class attributes as needed: https://hackerthemes.com/bootstrap-cheatsheet/ I don't use the JS part of it; just the stylesheets. I can imagine possibly a high level API that just applies those, but I think I would prefer to just apply them myself. The question is where does the scope of yew end ? perhaps what's needed is a separate yew-styling plugin/library ? |
Beta Was this translation helpful? Give feedback.
-
@jkelleyrtp Have you made any progress towards this? |
Beta Was this translation helpful? Give feedback.
-
Nevermind, I've got a prototype in the works here in case anyone wants to check it out: |
Beta Was this translation helpful? Give feedback.
-
Here is my thoughts on building styling system. I think there are two different places that we need to handle style:
A default style for every componentEvery component can have it's own default style, let's say a
At this point we need to defined the behavior of the styling system, thus we need to answer these questions:
A unique style for every component instateSome times we change style for component instate, by adding, removing .. etc styles to the component instate, thus we need to defined a how the styling system would allow us to do that. possible answer for the 1st question
|
Beta Was this translation helpful? Give feedback.
-
I'm having a play with writing a css parser from rust tokens, and it seems to be going well. It's available at https://github.com/derekdreery/style. The idea is that you can do things like <div style={{font-size: 10px; flex-direction: column}}></div> from within a macro in rust code, making using css/styles feel really natural. |
Beta Was this translation helpful? Give feedback.
-
Wow, awesome @derekdreery! Looks like the yew html macro could depend on your style macros under the hood. Is that what you had in mind? |
Beta Was this translation helpful? Give feedback.
-
I agree with
from #533 (comment). |
Beta Was this translation helpful? Give feedback.
-
Happy to help out however you want to use them. There are a couple of small caveats:
The lib I'm working on does typecheck your CSS, so only valid types of property values are allowed for a given property name. |
Beta Was this translation helpful? Give feedback.
-
My preference is to use css modules, or css that's localised to the component, keeping components from conflicting with each other automatically. To this end there is the css-modules crate, which simply aliases all of the class names in a css file and imports a list of those aliases into rust. The author of the crate was recently harassed into deleting their GitHub account by a group of transphobes but everything still works just fine. Edit: I forgot to say that I also use it with rollup for post processing. |
Beta Was this translation helpful? Give feedback.
-
First off, New to rust & New to yew... Figure it would be a great place to learn both! Recently been using a lot of Svelte and I have really enjoyed their take on styling. <style>
p {
color: purple;
font-family: 'Comic Sans MS', cursive;
font-size: 2em;
}
</style>
<p>Styled!</p> They do stuff under the hood of generating classes and attaching them to the elements but it avoids concepts of theming and leaves it to the developer/css. Ideally (for me) an API like so would exist like so where I could leverage css variables to do the theming/customization of a child component in CSS. impl Component for Model {
fn style(&self) -> Css {
css! {
div {
background: black;
--main-text-color: white;
}
}
}
fn view(&self) -> Html {
html! {
<div>
<MyComponent /> <!-- changes child's --main-text-color by setting the var -->
</div>
}
} It may not be possible (still learning) but I would be interested if it does. |
Beta Was this translation helpful? Give feedback.
-
@bl42 I like the look of that. Minor detail: the function signature wouldn't quite work like that if the CSS was generated at compile time since we wouldn't have an instance of the component then.
I don't quite understand your vision here, could you elaborate a bit more? |
Beta Was this translation helpful? Give feedback.
-
I have developed trait with similar functionality, Component should declare their Style struct that will be used to style the component (e.g. Entry) instead of using some sort of collection that doesn't help at checking the used style if it works with the Component or not. My project using Seed, but I'm pretty sure this idea is applicable in Yew too. |
Beta Was this translation helpful? Give feedback.
-
CSSinRust is now compatible with yew 0.17.* (https://crates.io/crates/css-in-rust). Check out https://github.com/lukidoescode/yew-fullstack-boilerplate on how to use it. I'd appreciate any PRs. |
Beta Was this translation helpful? Give feedback.
-
This looks great. Is there a way of providing a sass or css file that overrides these defaults? It would be great to give to an artist for them to tweak. |
Beta Was this translation helpful? Give feedback.
-
How about the way vueJS does it: https://vue-loader.vuejs.org/guide/scoped-css.html#deep-selectors You could add an unique id to every component, modify the css which should be pretty simple, and recursively add an attribute to every DOM element in the component, perhaps at render time. |
Beta Was this translation helpful? Give feedback.
-
I have one possible solution: https://github.com/lovelace-ed/lovelace/blob/main/utils/mercutio/src/lib.rs Note that it depends on Malvolio (another crate I've written) and I can't publish the next version of that until Rocket cuts its next release 🤷🏼 |
Beta Was this translation helpful? Give feedback.
-
Reading this conversation, the bs-css was the best implementation example, I think. One more thing to consider is that the browser downloads the whole wasm code at once. As far as I know, yew cannot split bundles currently. So maybe it's not the best performance to keep the CSS in rust right now. Also, CSS in WASM requires two compilations, one for WASM, one for CSS. As I see, currently the css-modules is the best performing option with in-rust syntax checking. |
Beta Was this translation helpful? Give feedback.
-
I think I'm approaching the "styling" question from a slightly different angle. "CSS-in-<language>" may be a good fit for modular aesthetic/layout styling, but it seems a bit heavy-handed for data-driven styles where object-identity of an element/component matters more than its semantic-identity. For example, I'm currently working on a The obvious workaround is to use Handling a |
Beta Was this translation helpful? Give feedback.
-
Composable styles are becoming popular, via https://tailwindcss.com/ Today I can use the twind shim in a Yew app and get Tailwind support for free, but a proper CSS compiler that generates only the classes in use inside the |
Beta Was this translation helpful? Give feedback.
-
Tailwindcss JIT compiler |
Beta Was this translation helpful? Give feedback.
-
Since there have been no commits on the css-in-rust crate for over a year, I have decided to create a fork to provide Yew 0.18 support and fix a couple known issues that I had when using the crate as well as some improvement on the internals. https://github.com/futursolo/stylist-rs I would appreciate any feedback on this. |
Beta Was this translation helpful? Give feedback.
-
I think we should keep the css in plain .scss files. 1 for each component preferably named as <component_name>.scss. and 1 global .scss file can be named anything. The global .scss file can be used for common styling requirements like button styling or list styling for complete app. The component scss file will be used for styling particular component and scoped for that component only. so that two components scss won't affect each other. } impl Component for MainPage { the style method can give the compiled .scss file. or user can write the css in style function directly also.
With about design I will also propose to separate the html code in separate file with <component_name>.html. and add 1 macro to include the complete file in view() function returning Html. Let me know any feedback regarding this approach. |
Beta Was this translation helpful? Give feedback.
-
https://github.com/unocss/unocss, this is soo goood. |
Beta Was this translation helpful? Give feedback.
-
For those wanting to use Tailwind (v3) and using |
Beta Was this translation helpful? Give feedback.
-
I'm currently working on railwind which is a tailwind compiler ported to rust and I'm wondering if it could be used here as a sort of plugin or extension if that's possible. |
Beta Was this translation helpful? Give feedback.
-
any update on this? |
Beta Was this translation helpful? Give feedback.
-
Regarding bundling stylesheets of different (component library) crates: I think this is mostly the domain of bundlers and build tools like Trunk, where there are plenty of investigatory issues that look promising: trunk-rs/trunk#759, trunk-rs/trunk#710, and even trunk-rs/trunk#3 from way back then. If Yew component library crates can support whatever direction they take, even if by convention, I think that would be a huge win. If bundling external component library crate SCSS can already be achieved in some convenient way using their current Dart-Sass support, it might be worth writing a guide or recommendation for that. Anyone got experience with that? Regarding dynamic styling and full-blown theming, that is where we might look for changes and improvements to the current macro's and traits, that feel as if they could use some ergonomics improvements at times. Is any of the CSS-in-Rust tools still under active development? I feel that some ergonomics are missing when writing my |
Beta Was this translation helpful? Give feedback.
-
I'd like to add a styling system to yew - not having a built-in way of styling components seems a bit unfinished to me. If we want to build better, bigger, and faster web apps, then we need a way of managing styles that doesn't clobber a global namespace and makes sense when looking at the components.
I'd like to see a Stylable trait where we can pull in an existing stylesheet (using css-parser for the servo project), modify it (override the defaults), and then set that as the element style.
We could also have a special style property for each element where we can inject style into even the subdivs of elements.
I personally favor parsing an external stylesheet and doing inline modification because of autocompletes working with .css files, but there is also room for a css! macro and reserve a keyword for the element that the style is being applied to, to access subdivs in that element.
Curious what the thoughts of the community is before I try pull my changes into the project.
Beta Was this translation helpful? Give feedback.
All reactions