[Feature discussion] Allow keeping some elements mounted offscreen #584
Replies: 1 comment 6 replies
-
Hey, I'm not sure that I understand the feature in your chat (text selection being synced), but I am not your PM nor your users, so there's not much of a point to drill further. To start with your question, I don't think that there's a realistic path towards making such a feature part of the library, especially in the shape you've outlined. People will use it for the wrong reasons. Let's see why: First, you're probably aware of this yourself; using the DOM as a stateful source of truth for the state in React (especially with virtualization, but not only that) is a path of pain and bugs. I would focus on finding a way to "fix" that, perhaps by emulating a selection that can be kept in React state. Next, as you have already guessed, what you're doing is not good for the Virtuoso internals, causes a huge performance hit, and, unless I am missing something, pointless. You're disabling the virtualization - why do you use virtualized components at all? The internet is full of tutorials for infinite, non-virtualized React lists. Maybe I don't see the full picture here, though. Finally, here's an idea on how to hack the component more gracefully - use the Hope this helps. My day job is also doing chats. It's one of those surprisingly complex domains - good luck with it. |
Beta Was this translation helpful? Give feedback.
-
I’m working on integrating virtuoso into our chat history. We have a feature where text selection is synced across all participants in the room. Whenever a currently selected element is unmounted, the selection state is lost. FF fires a
selectionchange
event. Chrome and Safari don’t fire an event but the selection object becomes mangled.The only reliable way to make this work with virtualization is by leaving the highlighted elements mounted when they go offscreen. If the key and the order of the persisted elements remains stable, React will not touch them, preserving selection.
I came up with a workaround that gives
Virtuoso
customList
andItem
components and modifieschildren
directly.It looks something like this.
By keeping the component and the key same, React doesn’t move or touch the components that go offscreen in any way, preserving selection.
This is obviously pretty hacky. For example, Virtuoso complained about 0-sized elements so I made them 1px, and force data- props on them.
I don’t know if there is more child introspection happening internally which could be subtly broken by injecting children like this.
Would really appreciate if you could tell whether there is anything else to watch out.
Considering the case described above, would you be interested in having this officially supported by the library?
I am happy to make a PR but figured something like this would require discussing the API, scope and specifics of integration.
I prototyped this locally, adding a
parkedItems
prop toVirtuoso
List mode and got children rendering in a similar fashion, but it was of course a quick demo and fully fleshed out implementation would need more thought on how to wire it with all the systems of emitters.It’s a bit different from top items feature or footer items request seen in a different issue. In my proposal the items have to stay precisely within the “main” item collection to ensure React never moves the corresponding DOM nodes around.
Moving the DOM, even if it’s not explicitly removed but just reparented still causes loss of selection.
Let me know if you need more examples or an interactive demo to better understand what I’m trying to achieve here.
Thank you.
Beta Was this translation helpful? Give feedback.
All reactions