Skip to content

Commit

Permalink
docs: init
Browse files Browse the repository at this point in the history
  • Loading branch information
Daydreamer-riri committed Apr 2, 2024
1 parent 1a58827 commit 124f69a
Show file tree
Hide file tree
Showing 12 changed files with 248 additions and 13 deletions.
27 changes: 27 additions & 0 deletions docs/components/BasicUseTransition/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useState } from 'react'
import { useTransition } from 'transition-hooks'
import { Button } from '../Button'

export function BasicUseTransition() {
const [show, setShow] = useState(false)
const { status, shouldMount } = useTransition(show)

return (
<div>
{shouldMount
? (
<div
style={{
transition: 'opacity 0.3s',
opacity: status === 'entering' || status === 'entered' ? 1 : 0,
}}
>
Hello Word
</div>
)
: null}
<Button onClick={() => setShow(!show)}>toggle</Button>
<p>status: { status }</p>
</div>
)
}
4 changes: 4 additions & 0 deletions docs/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function Button(props: React.HTMLProps<HTMLButtonElement>) {
const { type: _, ...rest } = props
return <button {...rest} className="vocs_Button_button vocs_Link vocs_Link_styleless" />
}
55 changes: 55 additions & 0 deletions docs/pages/docs/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Introduction

Transition hooks is a headless UI utility for transition scene in React.
It is simpler and easier to use than react-transition-group, And because it is state-driven, it can be used with react-dom and react-native

## Status

The heart of transition-hooks is the state machine.

### Status graph

![status graph](/status.png)

Here is just a quick example to get you started:

import { BasicUseTransition } from "../../components/BasicUseTransition"

:::code-group
<div data-title="Preview">
<BasicUseTransition />
</div>

```tsx [BasicUseTransition.tsx]
import { useState } from 'react'
import { useTransition } from 'transition-hooks'

export function BasicUseTransition() {
const [show, setShow] = useState(false)
const { status, shouldMount } = useTransition(show)

return (
<div>
{shouldMount
? (
<div
style={{
transition: 'opacity 0.3s',
opacity: (status === 'entering' || status === 'entered')
? 1
: 0,
}}
>
Hello Word
</div>
)
: null}
<button onClick={() => setShow(!show)}>toggle</button>
<p>{ status }</p>
</div>
)
}
```

:::

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

This hook transforms a boolean state into transition status, and unmount the component after timeout.

import { BasicUseTransition } from "../../components/BasicUseTransition"

:::code-group

<div data-title="Preview">
<BasicUseTransition />
</div>

```tsx [Code]
import { useState } from 'react'
import { useTransition } from 'transition-hooks'

export function BasicUseTransition() {
const [show, setShow] = useState(false) // [!code focus]
const { status, shouldMount } = useTransition(show) // [!code focus]

return (
<div>
{shouldMount // [!code focus]
? (
<div
style={{
transition: 'opacity 0.3s',
opacity: (status === 'entering' || status === 'entered') // [!code focus]
? 1 // [!code focus]
: 0, // [!code focus]
}}
>
Hello Word
</div>
)
: null}
<button onClick={() => setShow(!show)}>toggle</button>
<p>{ status }</p>
</div>
)
}
```
:::

## API

### `useTransition`
```ts
function useTransition(
state: boolean,
transitionOptions?: TransitionOptions
): StatusState

```

### `StatusState`

```ts
type StatusState = {
status: "preEnter" | "entering" | "entered" | "preExit" | "exiting" | "exited" | "unmounted";
shouldMount: boolean;
isEnter: boolean;
isResolved: boolean;
}
```
### `TransitionOptions`
```ts
export interface TransitionOptions {
/**
* Add a 'preEnter' state immediately before 'entering',
* which is necessary to change DOM elements from unmounted
* or display: none with CSS transition (not necessary for CSS animation).
* @default true
*/
preEnter?: boolean
/**
* Add a 'preExit' state immediately before 'exiting'
* @default false
*/
preExit?: boolean
/**
* Set timeout in ms for transitions;
* you can set a single value or different values for enter and exit transitions.
* @default 300
*/
timeout?: number | { enter: number, exit: number }
onStatusChange?: (status: Stage) => void
enter?: boolean
exit?: boolean
}
```
import { useTransition } from "react"
19 changes: 19 additions & 0 deletions docs/pages/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Transition Hooks
showLogo: false
layout: landing
---

import { HomePage } from 'vocs/components'

<HomePage.Root>
<HomePage.Logo />
<HomePage.Tagline>☄️ An extremely light-weight react transition hook which is simpler and easier to use than react-transition-group</HomePage.Tagline>
<HomePage.InstallPackage name="transition-hooks" type="init" />
{/* <HomePage.Description>Vocs is a minimal static documentation generator designed to supercharge your documentation workflow, built with modern web technologies.</HomePage.Description> */}
<HomePage.Buttons>
<HomePage.Button href="/docs" variant="accent">Get started</HomePage.Button>
<HomePage.Button href="https://github.com/Daydreamer-riri/transition-hooks">GitHub</HomePage.Button>
</HomePage.Buttons>
</HomePage.Root>

Binary file added docs/public/status.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 2 additions & 6 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
// @ts-check
import ririd from '@ririd/eslint-config'

export default ririd(
{
ignores: [
],
},
)
export default ririd()
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"version": "0.0.1",
"packageManager": "[email protected]",
"description": "☄️ An extremely light-weight react transition animation hook which is simpler and easier to use than react-transition-group",
"description": "☄️ An extremely light-weight react transition hook which is simpler and easier to use than react-transition-group",
"author": "Riri <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/Daydreamer-riri/transition-hooks#readme",
Expand Down Expand Up @@ -69,6 +69,7 @@
"react": "^18.2.0",
"rimraf": "5.0.5",
"simple-git-hooks": "^2.11.1",
"transition-hooks": "workspace:*",
"typescript": "^5.4.3",
"unbuild": "^2.0.0",
"vite": "^5.2.7",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/hooks/useTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
setAnimationFrameTimeout,
} from '../helpers/setAnimationFrameTimeout'
import { getTimeout } from '../helpers/getTimeout'
import type { StatusState } from '../status'
import { STATUS, getEndStatus, getState } from '../status'
import type { Stage, TransitionOptions } from '../types'
import useMemoizedFn from '../helpers/useMemorizeFn'
Expand Down Expand Up @@ -79,5 +80,5 @@ export function useTransition(state: boolean, transitionOptions?: TransitionOpti
useEffect(() =>
() => clearAnimationFrameTimeout(timer.current), [])

return statusState
return statusState as StatusState
}
16 changes: 14 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ import type { STATUS } from './status'
export type Stage = keyof typeof STATUS

export interface TransitionOptions {
/**
* Add a 'preEnter' state immediately before 'entering', which is necessary to change DOM elements from unmounted or display: none with CSS transition (not necessary for CSS animation).
* @default true
*/
preEnter?: boolean
/**
* Add a 'preExit' state immediately before 'exiting'
* @default false
*/
preExit?: boolean
enter?: boolean
exit?: boolean
/**
* Set timeout in ms for transitions; you can set a single value or different values for enter and exit transitions.
* @default 300
*/
timeout?: number | { enter: number, exit: number }
onStatusChange?: (status: Stage) => void
enter?: boolean
exit?: boolean
}
29 changes: 26 additions & 3 deletions vocs.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
import { defineConfig } from 'vocs'

export default defineConfig({
description: `Build reliable apps & libraries with lightweight,
composable, and type-safe modules that interface with Ethereum.`,
title: 'Viem',
description: `☄️ An extremely light-weight react transition hook which is simpler and easier to use than react-transition-group.`,
title: 'Transition-hooks',
editLink: {
pattern: 'https://github.com/Daydreamer-riri/transition-hooks/edit/main/docs/pages/:path',
text: 'Suggest changes to this page',
},
sidebar: [
{
text: 'Getting Started',
items: [
{
text: 'Introduction',
link: '/docs',
},
],
},
{
text: 'Hooks',
items: [
{
text: 'useTransition',
link: '/docs/useTransition',
},
],
},
],
})

0 comments on commit 124f69a

Please sign in to comment.