Skip to content

Commit 3e59359

Browse files
Add story to showcase swapping nodes
1 parent 088033e commit 3e59359

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

stories/html.stories.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,40 @@ storiesOf('Portals', module)
226226
<Parent />
227227
</div>;
228228
})
229+
.add("swap nodes between different locations", () => {
230+
const numberOfNodes = 5;
231+
const initialOrder = [];
232+
for (let i = 0; i < numberOfNodes; i++) {
233+
initialOrder[i] = i;
234+
}
235+
236+
const ExampleContent = ({ content }) => String(content);
237+
238+
const ChangeLayoutWithoutUnmounting = () => {
239+
const nodes = React.useMemo(
240+
() => initialOrder.map(createHtmlPortalNode),
241+
[]
242+
);
243+
const [order, setOrder] = React.useState(initialOrder);
244+
return (
245+
<div>
246+
<button onClick={() => setOrder(order.slice().reverse())}>
247+
Click to reverse the order
248+
</button>
249+
{nodes.map((node, index) => (
250+
<InPortal node={node}>
251+
<ExampleContent content={index} />
252+
</InPortal>
253+
))}
254+
{order.map((position) => (
255+
<OutPortal node={nodes[position]} />
256+
))}
257+
</div>
258+
);
259+
};
260+
261+
return <ChangeLayoutWithoutUnmounting />;
262+
})
229263
.add('Example from README', () => {
230264
const MyExpensiveComponent = () => 'expensive!';
231265

0 commit comments

Comments
 (0)