Skip to content

Commit d9dc95d

Browse files
committed
Slot: Add lazy component stories
1 parent 71f8531 commit d9dc95d

File tree

1 file changed

+40
-20
lines changed

1 file changed

+40
-20
lines changed

apps/storybook/stories/slot.stories.tsx

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import * as React from 'react';
2-
import { Slot as SlotPrimitive } from 'radix-ui';
3-
4-
const Slot = SlotPrimitive.Root;
5-
const Slottable = SlotPrimitive.Slottable;
2+
import { Slot } from 'radix-ui';
63

74
export default { title: 'Utilities/Slot' };
85

@@ -81,6 +78,25 @@ export const ButtonAsLink = () => (
8178
</>
8279
);
8380

81+
const LazyButton = React.lazy(async () => {
82+
await wait(1000);
83+
return {
84+
default: ({ children, ...props }: React.ComponentProps<'button'>) => (
85+
<button {...props}>{children}</button>
86+
),
87+
};
88+
});
89+
90+
export const WithLazyComponent = () => {
91+
return (
92+
<React.Suspense fallback={<div>Loading...</div>}>
93+
<Slot.Root data-slot-root onClick={() => console.log('click')}>
94+
<LazyButton data-slot-lazy>Click me</LazyButton>
95+
</Slot.Root>
96+
</React.Suspense>
97+
);
98+
};
99+
84100
export const Chromatic = () => (
85101
<>
86102
<h1>Without Slottable</h1>
@@ -232,19 +248,19 @@ export const Chromatic = () => (
232248
<h1>With callback-dependent rendering</h1>
233249
<h2>Component not passing callback</h2>
234250
<p>Should NOT have delete button next to component</p>
235-
<Slot>
251+
<Slot.Root>
236252
<MockTag>Component</MockTag>
237-
</Slot>
253+
</Slot.Root>
238254
<h2>Component passing `undefined` callback</h2>
239255
<p>Should NOT have delete button next to component</p>
240-
<Slot>
256+
<Slot.Root>
241257
<MockTag onDelete={undefined}>Component</MockTag>
242-
</Slot>
258+
</Slot.Root>
243259
<h2>Component passing callback</h2>
244260
<p>Should have delete button next to component</p>
245-
<Slot>
261+
<Slot.Root>
246262
<MockTag onDelete={() => alert('Delete')}>Component</MockTag>
247-
</Slot>
263+
</Slot.Root>
248264
</>
249265
);
250266
Chromatic.parameters = { chromatic: { disable: false } };
@@ -271,26 +287,26 @@ class ErrorBoundary extends React.Component<any, { hasError: boolean }> {
271287

272288
/* Also verifying that props and ref types don't error */
273289
const SlotWithoutSlottable = (props: React.ComponentPropsWithRef<'div'>) => (
274-
<Slot {...props} className="test" />
290+
<Slot.Root {...props} className="test" />
275291
);
276292

277293
const SlotWithSlottable = ({ children, ...props }: any) => (
278-
<Slot {...props}>
279-
<Slottable>{children}</Slottable>
294+
<Slot.Root {...props}>
295+
<Slot.Slottable>{children}</Slot.Slottable>
280296
<span>world</span>
281-
</Slot>
297+
</Slot.Root>
282298
);
283299

284300
const SlotWithFalseInternalChild = ({ children, ...props }: any) => (
285-
<Slot {...props}>{false && children}</Slot>
301+
<Slot.Root {...props}>{false && children}</Slot.Root>
286302
);
287303

288304
const SlotWithNullInternalChild = ({ children, ...props }: any) => (
289-
<Slot {...props}>{false ? children : null}</Slot>
305+
<Slot.Root {...props}>{false ? children : null}</Slot.Root>
290306
);
291307

292308
const SlotWithPreventableEvent = (props: any) => (
293-
<Slot
309+
<Slot.Root
294310
{...props}
295311
onClick={(event) => {
296312
props.onClick?.(event);
@@ -302,7 +318,7 @@ const SlotWithPreventableEvent = (props: any) => (
302318
);
303319

304320
const SlotWithoutPreventableEvent = (props: any) => (
305-
<Slot
321+
<Slot.Root
306322
{...props}
307323
onClick={(event) => {
308324
props.onClick?.(event);
@@ -322,7 +338,7 @@ const Button = ({
322338
iconLeft?: React.ReactNode;
323339
iconRight?: React.ReactNode;
324340
}) => {
325-
const Comp = asChild ? Slot : 'button';
341+
const Comp = asChild ? Slot.Root : 'button';
326342
return (
327343
<Comp
328344
{...props}
@@ -340,7 +356,7 @@ const Button = ({
340356
}}
341357
>
342358
{iconLeft}
343-
<Slottable>{children}</Slottable>
359+
<Slot.Slottable>{children}</Slot.Slottable>
344360
{iconRight}
345361
</Comp>
346362
);
@@ -371,3 +387,7 @@ const MockTag = ({
371387
</div>
372388
);
373389
};
390+
391+
async function wait(ms: number) {
392+
return new Promise((resolve) => setTimeout(resolve, ms));
393+
}

0 commit comments

Comments
 (0)