Tokamak icon indicating copy to clipboard operation
Tokamak copied to clipboard

Collapse multiple modifiers into a single DOM element if possible

Open MaxDesiatov opened this issue 5 years ago • 4 comments

In cases such as

Text("foo")
  .padding(5)
  .padding(10)
  .padding(42)

three new divs are created each with a separate padding. I wonder if there's a good (probably renderer-specific) way to collapse these into a single div with the modifiers merged in a sensible way. The merge logic could easily get complicated, so we need to be sure that this optimization is actually warranted.

MaxDesiatov avatar Aug 03 '20 18:08 MaxDesiatov

Interesting connection I see here is that the domAttributes modifier discussed in #231 would essentially require this collapse/merge logic to work, so maybe it would be best to start with that one first to understand how this would be implemented.

MaxDesiatov avatar Aug 03 '20 18:08 MaxDesiatov

We already do some flattening in ModifiedContent, but it could definitely be improved.

One thing to be careful of is:

.padding()
.background(Color.red)

should become:

padding: 8px;
background-color: red;

Whereas the reverse

.background(Color.red)
.padding()

should become:

margin: 8px;
background-color: red;

Getting the order right is going to be the trickiest part IMO.

carson-katri avatar Aug 03 '20 20:08 carson-katri

Since margins can collapse against each other in some circumstances I think it would be best to stick to padding. Maybe have a way to tag modifiers as either dependent on or causing a change to view metrics? Then it would be possible to combine, for example, background and border modifiers, and a wrapped padding modifier would be added to a new HTML element.

j-f1 avatar Aug 03 '20 20:08 j-f1

Maybe we could try something similar to operator fusion in Combine: https://jasdev.me/fusion-primer

MaxDesiatov avatar Aug 03 '20 20:08 MaxDesiatov