dumdom icon indicating copy to clipboard operation
dumdom copied to clipboard

Fragments a la React https://react.dev/reference/react/Fragment

Open olavfosse opened this issue 2 years ago • 6 comments

Hei hallo

Loving this library!

One thing I'm missing is fragments like React's <> syntax.

<div>
  <>
    <span>this will be a direct child of our div</span>
    <span>this too will be a direct child of our div</span> 
  </>
</div>

Snabbdom has an experimental API for this: https://github.com/snabbdom/snabbdom?tab=readme-ov-file#fragment-experimental.

This would be extremely handy for use-cases where it is absolutely required that child elements are direct children of some parent; specifically CSS grid requires grid children to be direct children of the grid parent, no way around it. I have a very dynamic CSS grid and I would love to have one component for the grid header and one component for the grid rows.

I think the syntax should be [:<> ...] because it's similar to React and it's also the same syntax Rum and other hiccup libs use.

God jul! 🎅

olavfosse avatar Dec 21 '23 05:12 olavfosse

Hi! Yes, I wanted fragments a while ago, but it wasn't possible in snabbdom then IIRC. I was thinking that maybe the syntax could just be a list? But then I though, hey, lists are already supported... So maybe what you want is already possible? The only limitation at the moment is that component functions can't return a list. Was that your use case?

cjohansen avatar Dec 22 '23 09:12 cjohansen

Yep, that is my use case :^)

olavfosse avatar Dec 28 '23 03:12 olavfosse

Does it have to be a component? If you can make do with a regular function, it should work fine.

cjohansen avatar Jan 03 '24 10:01 cjohansen

I can use regular functions. I just wanted to use components for performance :^).

olavfosse avatar Jan 03 '24 11:01 olavfosse

(def PerformantFragment (memoize render-fragment)) will achieve pretty much the same 😊

cjohansen avatar Jan 03 '24 12:01 cjohansen

For the record: Fragments are now available in snabbdom as (very) experimental: https://github.com/snabbdom/snabbdom?tab=readme-ov-file#fragment-experimental

PEZ avatar Feb 09 '24 13:02 PEZ