Coder Social home page Coder Social logo

Impl Add for Style? about kayak_ui HOT 5 CLOSED

CaioLC avatar CaioLC commented on June 12, 2024
Impl Add for Style?

from kayak_ui.

Comments (5)

StarArawn avatar StarArawn commented on June 12, 2024 2

I think having both has merits 👍 I think the operator internally would just use with_style? I've also been thinking of having a way of defining "classes" or at least some sort of reusable style similar to JSS.

from kayak_ui.

CaioLC avatar CaioLC commented on June 12, 2024 1

with_style works exactly as needed. Still new to kayak_ui so thanks for pointing out the functions 😃

There might be some cosmetic value in having the "+" operator to concatenate different styles, though. For instance, this code:

<Background styles={Some(container_style
    .with_style(center_align)
    .with_style(main_background_color)
    .with_style(two_players)
)}>
</Backgroud>

would become:

<Background styles={Some(container_style + center_align + main_background_color + two_players)}>
</Backgroud>

Some food for thought, in case you want to copy/paste the with_style method as the implementation for the Add<Style> operator.

from kayak_ui.

MrGVSV avatar MrGVSV commented on June 12, 2024 1

Interesting... Would you be willing to draft up a PR so we can take a look at it in real examples? It might be a nice QoL feature. Any thoughts on this @StarArawn?

One hesitation here is that it hides the ordering considerations (A + B ≠ B + A) a bit more than with_style already does.

from kayak_ui.

StarArawn avatar StarArawn commented on June 12, 2024 1

Add PR has been merged so I will be closing this. 👍

from kayak_ui.

MrGVSV avatar MrGVSV commented on June 12, 2024

Check out the methods defined on Style here:

/// Returns a `Style` object where all fields are set to [`StyleProp::Default`]
///
/// This should only be used when default properties are required or desired. Otherwise, you
/// may be better off using `Style::default` (which sets all properties to [`StyleProp::Unset`]).
pub fn new_default() -> Self {
Self {
$($field: StyleProp::Default),*
}
}
/// If any field is set to [`StyleProp::Inherit`], its value will be taken from `other`
pub fn inherit(&mut self, other: &Self) {
$(
if matches!(self.$field, StyleProp::Inherit) {
self.$field = other.$field.clone();
}
)*
}
/// Applies a `Style` over this one
///
/// Values from `other` are applied to any field in this one that is marked as [`StyleProp::Unset`]
pub fn apply<T: AsRefOption<Style>>(&mut self, other: T) {
if let Some(other) = other.as_ref_option() {
$(
if matches!(self.$field, StyleProp::Unset) {
self.$field = other.$field.clone();
}
)*
}
}
/// Applies the given style and returns the updated style
///
/// This is simply a builder-like wrapper around the [`Style::apply`] method.
pub fn with_style<T: AsRefOption<Style>>(mut self, other: T) -> Self {
self.apply(other);
self
}

It sounds like you want to use apply (or with_style for builder-like syntax). If a style has any StyleProp::Unset values, these methods will attempt to apply the value from the given style. Hopefully that helps! 😄

from kayak_ui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.