svelte-reparent
svelte's missing reparenting utility | alternative to react-reparent.
GitHub | npm
Demo
Container A
Container B
Explanation
svelte-reparent uses a limbo to store the component to be moved around, and different portals that the component can move to.
limbo is an invisible component, and any thing inside of it will be hidden. portals are the visible components that the component can move to.
When portals are destroyed, they will move the component back to limbo. When limbo is destroyed, it will completely destroy the component, no matter where it is.
A component has portal ID metadata attatched to it, allowing the component to be owned by portals, creating a safe way to move components around.
As mentioned in the example, the input has an "unkept state". This is state that isn't stored by svelte, demonstrating that the original element is only moved, not re-rendered.
Example Code
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
made with ❤️ by leo