reagent

Fredrik Andersson 2025-07-15T08:55:55.858009Z

I'm using reagent 1.3.0 and DaisyUI. DaisyUI has a Menu-component with foldable menu items. The folding state seems to be maintained with a "slot". I have never heard about slots before and I wonder how I can keep the slot state in a atom. But I don't understand how the slot really works. Anyone have any experience with this?

p-himik 2025-07-15T09:04:18.015339Z

Where are the slots documented?

p-himik 2025-07-15T09:04:48.432709Z

And why do you want to keep the slot state in an atom? (I assume you actually mean a ratom.)

Fredrik Andersson 2025-07-15T09:09:28.331589Z

What do you mean about where the slots are documented? I wan't to keep them in a ratom because when data change in my state ratom the menu is re-rendered and the slot states is reset.

p-himik 2025-07-15T09:11:30.895589Z

> What do you mean about where the slots are documented? Well, I meant precisely what I asked. :) Not sure how else to describe it. The Menu component seems to be well documented, but those docs don't mention slots. Or even state. So I have no clue where "state is maintained via slots" comes from, or what a "slot" even means.

Fredrik Andersson 2025-07-15T09:13:25.910379Z

Yes, you are right. The DaisyUI documentation does not mention slots. The reason I say slot is because chrome gives me a hint of a slot when I inspect the elements.

p-himik 2025-07-15T09:13:46.610869Z

I still have no clue what it means. Can you share a screenshot?

Fredrik Andersson 2025-07-15T09:13:57.747109Z

Fredrik Andersson 2025-07-15T09:14:33.079479Z

and this is what the menu looks like

p-himik 2025-07-15T09:15:08.428969Z

It's about web components: https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/slot Doesn't really mean that you have to use web components. I really doubt it's necessary for state tracking.

Fredrik Andersson 2025-07-15T09:16:53.884309Z

yes, I read that reference. But I didn't get any wiser. I was hoping someone had experience with similar problem.

p-himik 2025-07-15T09:17:31.542409Z

Their "Collapsible submenu" section in the Menu component documentation shows that they handle it with <details> and an open attribute. I would just try something like

[:details {:open (:open? @state-ratom)}
 ...]

Fredrik Andersson 2025-07-15T09:18:04.310769Z

yes, that makes it possible to make it default to open

Fredrik Andersson 2025-07-15T09:18:14.850099Z

but I also need to be able to track the change

p-himik 2025-07-15T09:19:16.365759Z

> yes, that makes it possible to make it default to open It should also be possible to use it to alter the current state - not just the initial one. > but I also need to be able to track the change You mean something like :on-open?

Fredrik Andersson 2025-07-15T09:20:18.597929Z

yes, precisely. I have tried :on-change and :on-toggle but I will try :on-open

p-himik 2025-07-15T09:20:41.004259Z

Wait, that was just for the sake of discussion - that's not what necessarily should work.

Fredrik Andersson 2025-07-15T09:21:04.360509Z

yes, I suspected that

p-himik 2025-07-15T09:23:27.645769Z

How exactly did you use the :on-toggle event handler? There's a discussion about <details> in the React issue tracker, with this being the most interesting comment among the most recent ones: https://github.com/facebook/react/issues/15486#issuecomment-1537155358

Fredrik Andersson 2025-07-15T09:24:21.969319Z

(defn menu-folder-item
  [name icon children]
  (let 
    [open? (r/atom false)]
    (fn []
     [:li
      [:details 
       {:open @open? 
        :on-toggle #(reset! open? (not @open?))}

       [:summary
        [:> icon {:class [:size-4]}]
        name]

       (into [:ul] children)]]))) 

p-himik 2025-07-15T09:26:38.089839Z

Your code works for me just fine. Although I would still follow the advice in the linked comment.

p-himik 2025-07-15T09:26:53.336009Z

What exactly did not work? In what scenario?

Fredrik Andersson 2025-07-15T09:27:11.100099Z

ok, let me do some more tests

Fredrik Andersson 2025-07-15T09:32:38.391239Z

I moved the state from a component ratom to a global ratom and now it works. I should have tested if the :on-toggle event was triggered first. Thank you for helping out!

p-himik 2025-07-15T09:33:44.595489Z

That shouldn't have affected anything. But just in case - if you tried e.g. changing children, and it didn't work, and you thought that's because of that state, then no, it's because you use (fn [] ...) without passing children to the inner function. Also, #(reset! open? (not @open?)) could be written as #(swap! open? not).

Fredrik Andersson 2025-07-15T09:34:10.022889Z

actually, now I have a recusive loop

p-himik 2025-07-15T09:34:22.340029Z

Have you read the comment that I linked?

Fredrik Andersson 2025-07-15T09:34:38.826079Z

yes, I will read it again

Fredrik Andersson 2025-07-15T09:40:33.796449Z

Yes, now it works! Thanks!

👍 1