Git Product home page Git Product logo

Comments (7)

raquo avatar raquo commented on May 20, 2024 2

I think I might actually get this done. Kinda need it for raquo/Laminar#95.

Looking into it now. So far it looks like the style props will look like val color: SP[String] with ColorStyle[Setter[String]] = colorStyle("color") where both SP ("style prop") and Setter are abstract types, and ColorStyle is a trait that looks a lot like the current object color's type does. Then in the consuming library you'd need to implement the colorStyle builder (well, a bunch of them for all the specialized types), returning concrete instances of Style or its library-specific replacement (similar to how Attrs and Props are done, but a lot more specialized builders).

This pattern removes any instantiation of the Style class from Scala DOM Types, abstracting away that type in a similar manner to how Attrs and Props are organized. This also does not require crazy numbers of generic params for the Styles trait, just a couple.

from scala-dom-types.

raquo avatar raquo commented on May 20, 2024

Agreed.

However, I would like to evaluate other options of dealing with styles before spending too much time polishing the current setup. What we have now I inherited from ScalaTags, and I haven't given it much thought yet.

For one thing, I don't like that we're extending the Style class, feels like it should not be required, that styles should work similar to attrs and props.

I'm thinking maybe to rewrite the styles as e.g.

lazy val backgroundColor: S[CSSColor] = style("color")

where

trait CSSColor { val str: String }
object Red extends CSSColor { override val str: String = "red"}
object Black extends CSSColor { override val str: String = "black"}

With desired usage like this:

div(backgroundColor := Red)

Were we to adopt this kind of API, my biggest concern is discoverability of available values. I gotta check how helpful would IntelliJ be in this scenario.

from scala-dom-types.

raquo avatar raquo commented on May 20, 2024

Alternatively, with a different set of tradeoffs:

lazy val backgroundColor: S[Color] = style(name = "color")

// ------

class Color(val colorStr: String) {
  override def toString(): colorStr // this method will be used to get raw CSS value
}

object Color {
  def apply(colorStr: String): new Color(colorStr) // maybe memoize this function

  lazy val red: new Color("red")
  lazy val black: new Color("black")
}

// -------

backgroundColor := Color("#fff")
backgroundColor := Color.red

from scala-dom-types.

fdietze avatar fdietze commented on May 20, 2024

Interesting! This would allow for implicits for different color-libraries.

And in the worst case one could even provide an implicit from String to Color.

from scala-dom-types.

raquo avatar raquo commented on May 20, 2024

Note: This is also relevant to SVG attributes, they have exact same typing problems as CSS properties.

from scala-dom-types.

fdietze avatar fdietze commented on May 20, 2024

I'm wondering if we can borrow anything from ScalaCSS.

https://github.com/japgolly/scalacss/blob/7fca2dafd93e017f64a34ea61e3a7290767daf45/core/shared/src/main/scala/scalacss/internal/Values.scala

from scala-dom-types.

raquo avatar raquo commented on May 20, 2024

Yeah, I'll need to look at how ScalaCSS organizes its types. One of the reasons I'm not delegating the entirety of SDT's CSS business to ScalaCSS is that it's apparently quite bulky in terms of bundle size (low hundreds of KBs in fullopt for 200LoC of CSS, as alleged here: http://scalapro.net/scalacss-is-it-time-for-using-it/). I'm hoping all that size is coming not from the Value/ValueT traits but from all the other irrelevant-to-us machinery required for style generation.

from scala-dom-types.

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.