Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(schedule): refactor storybook #431

Merged
merged 3 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
244 changes: 0 additions & 244 deletions packages/pagination/pagination.stories.tsx

This file was deleted.

27 changes: 15 additions & 12 deletions packages/schedule/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,14 @@ npm install @zendeskgarden/container-schedule

## Usage

For live examples check out our [storybook](https://zendeskgarden.github.io/react-containers?path=/story/schedule-container--useschedule).

### As a Render Prop Component

```jsx static
import { ScheduleContainer } from '@zendeskgarden/container-schedule';

<ScheduleContainer duration={1000} delayMS={0}>
{elapsed => <p>Percentage: {(elapsed * 100).toFixed(0)}%</p>}
</ScheduleContainer>;
```
Check out [storybook](https://zendeskgarden.github.io/react-containers) for live
examples.

### As a hook

```jsx static
The `useSchedule` hook implements a schedule (timer) and communicates when it has elapsed.

```jsx
import { useSchedule } from '@zendeskgarden/container-schedule';

const Animation = () => {
Expand All @@ -38,6 +31,16 @@ const Animation = () => {
};
```

### As a Render Prop Component

```jsx
import { ScheduleContainer } from '@zendeskgarden/container-schedule';

<ScheduleContainer duration={1000} delayMS={0}>
{elapsed => <p>Percentage: {(elapsed * 100).toFixed(0)}%</p>}
</ScheduleContainer>;
```

## Info

See [react-loaders][loaders link] component as a non-trivial use of this.
Expand Down
6 changes: 6 additions & 0 deletions packages/schedule/demo/#readme.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Meta, Description } from '@storybook/addon-docs';
import README from '../README.md';

<Meta title="Packages/Schedule/README" />

<Description>{README}</Description>
23 changes: 23 additions & 0 deletions packages/schedule/demo/schedule.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Meta, ArgsTable, Canvas, Story } from '@storybook/addon-docs';
import { ScheduleContainer } from '@zendeskgarden/container-schedule';
import { ScheduleStory } from './stories/ScheduleStory';

<Meta title="Packages/Schedule" component={ScheduleContainer} />

# API

<ArgsTable />

# Demo

<Canvas>
<Story
name="Schedule"
args={{ as: 'hook', delayMS: 750, duration: 1250, loop: true }}
argTypes={{
as: { options: ['container', 'hook'], control: 'radio', table: { category: 'Story' } }
}}
>
{args => <ScheduleStory {...args} />}
</Story>
</Canvas>
64 changes: 64 additions & 0 deletions packages/schedule/demo/stories/ScheduleStory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/

import React from 'react';
import { Story } from '@storybook/react';
import {
IScheduleContainerProps,
IUseScheduleProps,
IUseScheduleReturnValue,
ScheduleContainer,
useSchedule
} from '@zendeskgarden/container-schedule';

interface IComponentProps extends IUseScheduleReturnValue {
duration?: IUseScheduleProps['duration'];
loop?: IUseScheduleProps['loop'];
}

const Component = ({ delayMS, delayComplete, elapsed, loop, duration = 1250 }: IComponentProps) => (
<div>
<label>
<span>
Schedule {delayMS / 1000}s delay followed by {duration / 1000}s{' '}
{loop ? 'looped' : 'elapsed'} progress
</span>
<progress className="block w-full" value={delayComplete ? elapsed : undefined} />
</label>
</div>
);

const Container = ({ loop, ...props }: IScheduleContainerProps) => (
<ScheduleContainer loop={loop} {...props}>
{containerProps => <Component {...containerProps} loop={loop} />}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't containerProps also pass thru loop from the container with no need to manually pass loop to Component?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, the loop prop is not reconstituted in the containerProps. (In going through this exercise, I'm noting API discontinuity between the packages – this is one area where we could improve consistency.) So, I'm feeding it through. But worth noting that a) it is simply for string display, and b) these particular (overly) abstracted examples are a bit contrived since IRL one would either use the container or the hook and all the props would likely be available in one place.

</ScheduleContainer>
);

const Hook = (props: IUseScheduleProps) => {
const hookProps = useSchedule(props);

return <Component {...hookProps} {...props} />;
};

interface IArgs extends IScheduleContainerProps {
as: 'hook' | 'container';
}

export const ScheduleStory: Story<IArgs> = ({ as, ...props }) => {
const Schedule = () => {
switch (as) {
case 'container':
return <Container {...props} />;

case 'hook':
default:
return <Hook {...props} />;
}
};

return <Schedule />;
};
Loading