Skip to content

Commit 2a3d688

Browse files
author
Nikolai Lopin
committed
docs: migrate Offcanvas docs to csf3
1 parent 5bb2814 commit 2a3d688

File tree

3 files changed

+98
-124
lines changed

3 files changed

+98
-124
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import React, { useState } from 'react';
2+
import { Meta, StoryObj } from '@storybook/react';
3+
import { Offcanvas, useOffcanvasDismiss } from '../Offcanvas';
4+
import { Button } from '../../Button/Button';
5+
6+
const meta: Meta = {
7+
title: 'Components/Offcanvas',
8+
component: Offcanvas,
9+
decorators: [
10+
Story => (
11+
<div style={{ willChange: 'transform', height: '150px', border: '1px dashed gray' }}>
12+
<Story />
13+
</div>
14+
)
15+
],
16+
args: {
17+
children: 'Offcanvas Content'
18+
}
19+
};
20+
21+
export default meta;
22+
23+
type Story = StoryObj<typeof Offcanvas>;
24+
25+
export const Default: Story = {
26+
render: ({ onClose, ...args }) => {
27+
const [open, setOpen] = useState(false);
28+
return (
29+
<>
30+
<Button size="small" mr={1} onClick={() => setOpen(true)}>
31+
Open Offcanvas
32+
</Button>
33+
{open && (
34+
<Offcanvas
35+
{...args}
36+
onClose={() => {
37+
onClose?.();
38+
setOpen(false);
39+
}}
40+
/>
41+
)}
42+
</>
43+
);
44+
}
45+
};
46+
47+
export const WithoutAutomaticDismiss: Story = {
48+
...Default,
49+
args: {
50+
dismissible: false
51+
}
52+
};
53+
54+
export const FromRightSide: Story = {
55+
...Default,
56+
args: {
57+
side: 'right'
58+
}
59+
};
60+
61+
export const DismissedWithRenderProps: Story = {
62+
render: args => <Offcanvas {...args}>{dismiss => <Button onClick={dismiss}>Close</Button>}</Offcanvas>
63+
};
64+
65+
const Content = () => {
66+
const dismiss = useOffcanvasDismiss();
67+
return <Button onClick={dismiss}>Close</Button>;
68+
};
69+
70+
export const DismissedWithHook: Story = {
71+
render: args => (
72+
<Offcanvas {...args}>
73+
<Content />
74+
</Offcanvas>
75+
)
76+
};
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
1-
import { useState } from 'react';
2-
import { Canvas, Story, ArgsTable, Meta } from '@storybook/blocks';
3-
import { Offcanvas } from '../Offcanvas';
4-
import { OffcanvasCreator, offcanvasContent } from './OffcanvasCreator';
1+
import { Primary, Stories, ArgTypes, Meta } from '@storybook/blocks';
52
import { StyledSystemLinks } from '../../../docs/StyledSystemLinks';
6-
import { Button } from '../..';
3+
import * as OffcanvasStories from './Offcanvas.stories';
74

8-
<Meta title="Components/Offcanvas" component={Offcanvas} />
9-
10-
export const Template = args => {
11-
const [open, setOpen] = useState(false);
12-
const openCanvas = () => setOpen(true);
13-
const closeCanvas = () => setOpen(false);
14-
return (
15-
<>
16-
<Button size="small" mr={1} onClick={openCanvas}>
17-
Open Offcanvas
18-
</Button>
19-
{open && <Offcanvas {...args} onClose={closeCanvas} />}
20-
</>
21-
);
22-
};
5+
<Meta of={OffcanvasStories} />
236

247
# Offcanvas
258

@@ -30,20 +13,17 @@ content.
3013
Offcanvas is responsive per default but the width can be adjusted (the default is 600px or 37.5rem). Content on the Offcanvas
3114
is scrollable and has a space5 bounding box.
3215

33-
<Canvas withSource="none">
34-
<Story
35-
name="Default"
36-
args={{
37-
children: offcanvasContent
38-
}}
39-
>
40-
{Template.bind({})}
41-
</Story>
42-
</Canvas>
16+
<Primary />
4317

44-
### Example Code
18+
## Properties
4519

46-
The following contains a simple example, with a resulting Offcanvas similar to the live examples above.
20+
<ArgTypes of={OffcanvasStories} />
21+
22+
<StyledSystemLinks component="Offcanvas" supportedProps={['width']} />
23+
24+
## Usage
25+
26+
### Basic use
4727

4828
```jsx
4929
const [showOffcanvas, setShowOffcanvas] = useState(true);
@@ -62,31 +42,24 @@ const [showOffcanvas, setShowOffcanvas] = useState(true);
6242
</Offcanvas>;
6343
```
6444

65-
### More examples
66-
67-
<Canvas withSource="none">
68-
<OffcanvasCreator />
69-
</Canvas>
70-
71-
## API
72-
73-
<ArgsTable of={Offcanvas} />
74-
75-
<StyledSystemLinks component="Offcanvas" supportedProps={['width']} />
76-
77-
### Dismiss
45+
### Dismissing the Offcanvas
7846

7947
Offcanvas provides a function to dismiss the component with an animation. The
8048
dismiss function is available either through render props or a hook. After the
8149
animation has finished the `onClose` callback will be called.
8250

83-
#### Render Props
51+
#### Via Render Props
52+
53+
Pass render function as a child to `Offcanvas`. Call the function passed as an argument to close the Offcanvas:
8454

8555
```jsx
8656
<Offcanvas>{dismiss => <Button onClick={dismiss}>Close</Button>}</Offcanvas>
8757
```
8858

89-
#### Hook
59+
#### Via Hook
60+
61+
Use the `useOffcanvasDismiss` hook to get the dismiss function. It is only available inside the `Offcanvas` component because
62+
it uses React context to get the dismiss function.
9063

9164
```jsx
9265
const InsideOffcanvas = () => {
@@ -100,3 +73,5 @@ const OffcanvasWrapper = () => (
10073
</Offcanvas>
10174
);
10275
```
76+
77+
<Stories includePrimary={false} />

src/components/Offcanvas/docs/OffcanvasCreator.tsx

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)