Comments (7)
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.
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.
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.
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.
Note: This is also relevant to SVG attributes, they have exact same typing problems as CSS properties.
from scala-dom-types.
I'm wondering if we can borrow anything from ScalaCSS.
from scala-dom-types.
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)
- Unescaped text entity -- raw() HOT 9
- Separation of reflected attributes and attributes with complex keys HOT 5
- Automatically generate dom types HOT 4
- Key renaming proposals HOT 4
- Discussion about lazy val definitions HOT 8
- implicit def eventPropToEventPropTransformation interferes sometimes HOT 2
- img loading attribute not supported HOT 2
- Support fullscreenchange event HOT 2
- Merge colliding html and svg attr names into shared namespace. HOT 3
- More complete code example HOT 1
- Aria Attributes should be usable from SVG too, not just HTML HOT 3
- Add flow-relative CSS properties HOT 2
- Server Side Rendering HOT 4
- Support HTML Dialog Element HOT 6
- Switch definitions from lazy vals to vals? HOT 2
- RFC: Use concrete types instead of generic type params? HOT 20
- Add info about supported tags to props/attrs HOT 6
- Publish defs (and generators?) for popular web component libraries, e.g. UI5, Material HOT 10
- Add ff4s to list of UI libs that use scala-dom-types HOT 13
- How to add a new property to a img? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scala-dom-types.