jss icon indicating copy to clipboard operation
jss copied to clipboard

Atomic generator

Open behnammodi opened this issue 5 years ago • 17 comments

We generate a lot of common style somethings like this:

.a1-0-1-105 {
  display: none;
}

.a2-0-1-106 {
  display: none;
}

And style sheet size is increasing in a large project

We can use an atomic style generator, Some of the libraries did it like:

https://github.com/robinweser/fela

And

https://github.com/styletron/styletron

Are you willing to implement it? Yes

behnammodi avatar Mar 08 '21 08:03 behnammodi

We discussed this thing a bit, there are certain downsides coming with atomic approach, but I encourage you to create an AtomicDomRenderer that would render out the rules in an atomic way (one css rule - one property).

See DomRenderer for the current rendering implementation example.

kof avatar Mar 09 '21 10:03 kof

@kof Thank you, I will try to do that, but maybe somewhere I need your help

behnammodi avatar Mar 09 '21 12:03 behnammodi

@kof Hi man, I investigate the entire project to create an Atomic generator, I think we need a new class of BaseStyleRule for that.

That is right?

behnammodi avatar Apr 22 '21 14:04 behnammodi

I was hoping its possible to do by just supplying a different DomRenderer implementation, without modifying antything else, renderer can be passed to Jss as an option.

Or alternatively, which is eventually a simpler option, but would not work with vanilla JSS and not with any other integrations and would require to fork react-jss - creating a new rule per property from createUseStyles.

kof avatar Apr 22 '21 15:04 kof

I will investigate more about DomRenderer.

behnammodi avatar Apr 22 '21 15:04 behnammodi

I hard worked on DomRenderer, so I have to say it not possible to have Atomic Generator just by changing DomRenderer

I think this future is very important because the new approach of CSS in JS is about Atomic generation. I could find a new repo about that: https://github.com/johanholmerin/style9

and a video of Facebook on youtube about CSS in JS: https://www.youtube.com/watch?v=9JZHodNR184

behnammodi avatar Apr 23 '21 09:04 behnammodi

Can you show me what you did and what was impossible?

Regarding atomic - it's absolutely not new. It's been originally in react-native-web, then they migrated to partially using regular rules and partially atomic. There is fela and a bunch of others which do atomic. There is a number of downsides on this approach and it should be up to the user what they choose. What I am saying is that it's not a new trend but still a good thing to have as an option.

kof avatar Apr 23 '21 13:04 kof

@kof I said it not possible by DomRenderer

We need to compute on Rule before giving it to DomRenderer. I create a flow for that, so we have to have a process like this:

Screen Shot 2021-04-23 at 6 38 35 PM

So we need these green boxes

behnammodi avatar Apr 23 '21 14:04 behnammodi

Woo, I found new one :)

https://stitches.dev/

behnammodi avatar Apr 23 '21 15:04 behnammodi

Stitches just recently switched from atomic to regular rules.

kof avatar Apr 23 '21 15:04 kof

Stitches just recently switched from atomic to regular rules.

Why? have they any notes about that?

behnammodi avatar Apr 23 '21 15:04 behnammodi

I haven't found any, feel free to ask them.

kof avatar Apr 23 '21 15:04 kof

I haven't found any, feel free to ask them.

Ok, did you see my process flow?

behnammodi avatar Apr 23 '21 15:04 behnammodi

I think the biggest problem here is classes map, because we currently create a class name per jss rule, but if we were to render it to multiple css rules, each jss rule would need a new class and they would have to land in classes something like this: classes = {myButton: 'a b c d'}.

We have a way to pass a custom id generator to jss, it's createGenerateId function (just in case). But it's designed around a single rule and is being called in StyleRule and KeyframesRule.

So it seems you would need 3 things:

  1. create class name for the atomic rule
  2. ability to register a compound class name in sheet.classes
  3. make sure that atomic rules from media queries are not mixed up with the global once, because inside media query they can only be reused between rules of the same media query

What else am I missing?

kof avatar Apr 23 '21 18:04 kof

Now nothing, may in the implementation phase we have another concern

behnammodi avatar Apr 24 '21 05:04 behnammodi

Maybe a combination of a AtomicDomRenderer and a plugin that uses the hooks api could do the job.

kof avatar Apr 24 '21 11:04 kof

But I think we have to forget AtomicDomRenderer and focusing on AtomicStyleSheet

behnammodi avatar Apr 24 '21 11:04 behnammodi