Skip to content

Commit

Permalink
feat: add SwitchTransition, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
iamyoki committed Dec 23, 2021
1 parent 2e7039d commit 18b08e9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
52 changes: 46 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
- [useTransition](#usetransition)
- [useSwitchTransition](#useswitchtransition)
- [Transition](#transition)
- [SwitchTransition](#switchtransition)
- [API Reference](#api-reference)
- [useTransition(state, timeout)](#usetransitionstate-timeout)
- [useSwitchTransition(state, timeout, mode)](#useswitchtransitionstate-timeout-mode)
- [Transition](#transition-1)
- [SwitchTransition](#switchtransition-1)
- [License](#license)

## Installation
Expand Down Expand Up @@ -137,6 +139,29 @@ return <div>
</div>
```

### SwitchTransition

FaCC pattern version of useSwitchTransition

```jsx
<SwitchTransition state={count} timeout={300} mode='default'>
{(state, stage) => (
<h1
style={{
transition: '.3s',
opacity: stage === 'enter' ? 1 : 0,
transform: {
from: 'translateX(-100%) scale(1.2)',
enter: 'translateX(0)',
leave: 'translateX(100%) scale(0)'
}[stage]
}}>
{state} {stage} hello
</h1>
)}
</SwitchTransition>
```

## API Reference

### useTransition(state, timeout)
Expand All @@ -163,17 +188,17 @@ return <div>
const transition = useSwitchTransition(onOff, 300, 'default')
```

| Parameters | Type | Description |
| :--------- | :--------------------------------- | :------------------------------------------------------------ |
| `state` | `any` | **Required**. Your state, which triggers animation |
| `timeout` | `number` | **Required**. How long before the animation ends and unmounts |
| `mode` | `default` \| `out-in` \| `int-out` | **Optional**. Default to `default` mode |
| Parameters | Type | Description |
| :--------- | :-------------------------------- | :------------------------------------------------------------ |
| `state` | `any` | **Required**. Your state, which triggers animation |
| `timeout` | `number` | **Required**. How long before the animation ends and unmounts |
| `mode` | `default` \| `out-in` \| `in-out` | **Optional**. Default to `default` mode |

### Transition

```jsx
<Transition state={onOff} timeout={300}>
{(stage, shouldMount) => shouldMount && <div style={...}>hello</div>}
{(stage, shouldMount) => shouldMount && <div style={{...}}>hello</div>}
</Transition>
```

Expand All @@ -183,6 +208,21 @@ return <div>
| `timeout` | `number` | **Required**. How long before the animation ends and unmounts |
| `children` | `(stage: Stage, shouldMount: boolean)=>React.ReactNode` | **Required**. FaCC pattern. |

### SwitchTransition

```jsx
<SwitchTransition state={count} timeout={300}>
{(state, stage) => <div style={{...}}>{state} hello</div>}
</SwitchTransition>
```

| Props | Type | Description |
| :--------- | :-------------------------------------------- | :-------------------------------------------------------------------- |
| `state` | `any` | **Required**. Your boolean state, which controls animation in and out |
| `timeout` | `number` | **Required**. How long before the animation ends and unmounts |
| `mode` | `default` \| `out-in` \| `in-out` | **Optional**. Default to `default` mode |
| `children` | `(state: any, stage: Stage)=>React.ReactNode` | **Required**. FaCC pattern. |

## License

[MIT](https://choosealicense.com/licenses/mit/)
20 changes: 20 additions & 0 deletions src/SwitchTransition/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {Stage, useSwitchTransition} from '..';
import {Mode} from '../useSwitchTransition/types';

type SwitchTransitionProps<S = any> = {
state: S;
timeout: number;
mode: Mode;
children: (state: S, stage: Stage) => React.ReactNode;
};

export function SwitchTransition<S>({
state,
timeout,
mode,
children,
}: SwitchTransitionProps<S>) {
const transition = useSwitchTransition(state, timeout, mode);

return transition(children);
}
2 changes: 1 addition & 1 deletion src/Transition/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Stage, useTransition} from '../useTransition';
import {Stage, useTransition} from '..';

type TransitionProps = {
state: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useTransition';
export * from './useSwitchTransition';
export * from './Transition';
export * from './SwitchTransition';

0 comments on commit 18b08e9

Please sign in to comment.