Skip to content

Commit

Permalink
docs: components
Browse files Browse the repository at this point in the history
  • Loading branch information
Daydreamer-riri committed Apr 8, 2024
1 parent e2d7bac commit e9c7d05
Show file tree
Hide file tree
Showing 11 changed files with 174 additions and 6 deletions.
32 changes: 32 additions & 0 deletions docs/pages/docs/ListTransition.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# ListTransition

## Example

```tsx
<ul>
<ListTransition list={list}>
{(item, { simpleStatus }) => (
<li
style={{
transition: '.3s',
opacity: simpleStatus === 'enter' ? 1 : 0,
transform: {
from: 'translateX(-100%) scale(1.2)',
enter: 'translateX(0)',
leave: 'translateX(100%) scale(0)',
}[simpleStatus],
}}
>
{item}
</li>
)}
</ListTransition>
</ul>
```

## Type

```ts
function ListTransition<T>(props: ListTransitionProps<T>): JSX.Element

```
30 changes: 30 additions & 0 deletions docs/pages/docs/SwitchTransition.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SwitchTransition

## Example

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

## Type

```ts
function SwitchTransition<T>(props: SwitchTransitionProps<T>): JSX.Element

```
32 changes: 32 additions & 0 deletions docs/pages/docs/Transition.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Transition

## Example

```tsx
const [onOff, setOnOff] = useState(true)

return (
<div>
<button onClick={() => setOnOff(!onOff)}>toggle</button>
<Transition state={onOff} timeout={300}>
{({ simpleStatus, shouldMount }) => shouldMount && (
<p style={{
transition: '.3s',
opacity: simpleStatus === 'enter' ? 1 : 0,
}}
>
I'm fading
</p>
)}
</Transition>
</div>
)

```

## Type

```ts
function Transition(props: TransitionProps): React.ReactNode

```
36 changes: 36 additions & 0 deletions docs/pages/docs/useListTransition.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,39 @@ export function BasicUseListTransition() {
:::

If you want to use `view transition`, the keyExtractor argument must be set.

## API

### `useListTransition`

```ts
function useListTransition<Item>(list: Array<Item>, options?: ListTransitionOptions<Item>): {
transitionList: (renderCallback: ListRenderCallback<Item>) => JSX.Element[]
isResolved: boolean
}
```

### `ListTransitionOptions`

```ts
interface ListTransitionOptions<Item> {
timeout: Timeout
entered?: boolean
keyExtractor?: (item: Item) => string | number
viewTransition?: (fn: () => void) => void
}
```

### `ListRenderCallback`

```ts
export type ListRenderCallback<Item> = (
item: Item,
stage: StatusState & { key: string | number }
) => React.ReactNode

```

### `StatusState`

See [useTransition#StatusState](/docs/useTransition#statusstate)
10 changes: 9 additions & 1 deletion docs/pages/docs/useSwitchTransition.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,13 @@ interface SwitchTransitionOptions {
### `SwitchRenderCallback`

```ts
export type SwitchRenderCallback<S> = (state: S, statusState: StatusState & { prevState?: S, nextState?: S }) => React.ReactNode
export type SwitchRenderCallback<S> = (
state: S,
statusState: StatusState & { prevState?: S, nextState?: S }
) => React.ReactNode

```

### `StatusState`

See [useTransition#StatusState](/docs/useTransition#statusstate)
2 changes: 2 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export default ririd(
rules: {
'style/no-multiple-empty-lines': 'off',
'unused-imports/no-unused-vars': 'off',
'react-hooks/rules-of-hooks': 'off',
'react/no-unescaped-entities': 'off',
},
},
)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"release": "bumpp && npm publish",
"typecheck": "tsc --noEmit",
"docs:dev": "vocs dev",
"docs:build": "vocs build",
"docs:build": "npm run build && vocs build",
"docs:preview": "vocs preview"
},
"peerDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions src/components/ListTransition.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { ListRenderCallback, ListTransitionOptions } from '../hooks/useListTransition'
import { useListTransition } from '../hooks/useListTransition'

interface SwitchTransitionProps<T> {
interface ListTransitionProps<T> {
list: T[]
transitionOptions?: ListTransitionOptions<T>
children: ListRenderCallback<T>
}

export function SwitchTransition<T>(props: SwitchTransitionProps<T>) {
export function ListTransition<T>(props: ListTransitionProps<T>) {
const { transitionList } = useListTransition(props.list, props.transitionOptions)

return transitionList(props.children)
return <>{transitionList(props.children)}</>
}
2 changes: 1 addition & 1 deletion src/components/SwitchTransition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ interface SwitchTransitionProps<T> {
export function SwitchTransition<T>(props: SwitchTransitionProps<T>) {
const { transition } = useSwitchTransition(props.state, props.transitionOptions)

return transition(props.children)
return <>{transition(props.children)}</>
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@ export type { Mode, SwitchRenderCallback, SwitchTransitionOptions } from './hook
export { useListTransition } from './hooks/useListTransition'
export type { ListRenderCallback, ListTransitionOptions } from './hooks/useListTransition'
export { Transition } from './components/Transition'
export { SwitchTransition } from './components/SwitchTransition'
export { ListTransition } from './components/ListTransition'
export * from './types'
export { getSimpleStatus } from './status'
26 changes: 26 additions & 0 deletions vocs.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ export default defineConfig({
pattern: 'https://github.com/Daydreamer-riri/transition-hooks/edit/main/docs/pages/:path',
text: 'Suggest changes to this page',
},
socials: [
{
icon: 'github',
link: 'https://github.com/Daydreamer-riri/transition-hooks',
},
],
topNav: [
{ text: 'Guide & API', link: '/docs', match: '/docs' },
],
sidebar: [
{
text: 'Getting Started',
Expand Down Expand Up @@ -34,5 +43,22 @@ export default defineConfig({
},
],
},
{
text: 'Components',
items: [
{
text: 'Transition',
link: '/docs/Transition',
},
{
text: 'SwitchTransition',
link: '/docs/SwitchTransition',
},
{
text: 'ListTransition',
link: '/docs/ListTransition',
},
],
},
],
})

0 comments on commit e9c7d05

Please sign in to comment.