-
Notifications
You must be signed in to change notification settings - Fork 1
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
add package's docs #6
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
# @location-state/core | ||
|
||
`@location-state/core` provides the features to save and restore state based on a specified `syncer`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここで
辺りじゃないかなぁと
|
||
|
||
## Installation | ||
|
||
```bash | ||
npm install @location-state/core | ||
# or | ||
yarn add @location-state/core | ||
# or | ||
pnpm add @location-state/core | ||
``` | ||
|
||
## Usage | ||
|
||
Specify the syncer props in Provider that based on the navigation API. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここというかトップレベルのREADMEに書きたいことだけど、最初のUsageでいきなり |
||
|
||
```tsx | ||
import { LocationStateProvider, NavigationSyncer } from "@location-state/core"; | ||
|
||
export function Providers({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<LocationStateProvider syncer={new NavigationSyncer(window.navigation)}> | ||
{children} | ||
</LocationStateProvider> | ||
); | ||
} | ||
``` | ||
|
||
> [!WARNING] | ||
> Please note that the navigation API is not yet supported by some browsers as of 2023. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ここはもっとストレートにChromiumベースのブラウザでしか使えないと明記したいかな |
||
> If you want, you can use `import { unsafeNavigation } from "@location-state/core/unsafe-navigation";` at your own risk. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これは別の見出しを切ってちゃんとしたコード例として載せたい |
||
> However, this is not a perfect complement to the Navigation API. | ||
|
||
If you are using Next.js pages router, use [@location-state/next](../location-state-next/README.md). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これはUsageの冒頭に書きたいかな |
||
|
||
### Session storage persistence | ||
|
||
You can store history-based information in session storage by specifying `store: 'session'`, name, and default value in hooks. | ||
|
||
```tsx | ||
import { useLocationState, StoreName } from "@location-state/core"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. このサンプルでは |
||
|
||
export function Counter({ storeName }: { storeName: StoreName }) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 使ってないのでProps不要ですね |
||
const [counter, setCounter] = useLocationState({ | ||
name: "counter", | ||
defaultValue: 0, | ||
storeName: "session", | ||
}); | ||
|
||
return <>...</>; | ||
} | ||
``` | ||
|
||
### URL persistence | ||
|
||
You can store history-based information in session storage by specifying `store: 'url'`, name, and default value in hooks. | ||
|
||
```tsx | ||
import { useLocationState, StoreName } from "@location-state/core"; | ||
|
||
export function Counter({ storeName }: { storeName: StoreName }) { | ||
const [counter, setCounter] = useLocationState({ | ||
name: "counter", | ||
defaultValue: 0, | ||
storeName: "url", | ||
}); | ||
|
||
return <>...</>; | ||
} | ||
``` | ||
|
||
## API | ||
|
||
### <LocationStateProvider> | ||
|
||
Provider to store the state based on the history. synchronization implementation with the history can be specified in the [syncer](#syncer). | ||
|
||
```ts | ||
function LocationStateProvider(props: { | ||
syncer?: Syncer; | ||
children: ReactNode; | ||
}): ReactNode; | ||
``` | ||
|
||
### Syncer | ||
|
||
Synchronization interface with history. | ||
|
||
```ts | ||
type Syncer = { | ||
key(): string | undefined; | ||
sync(arg: { listener: (key: string) => void; signal: AbortSignal }): void; | ||
updateURL(url: string): void; | ||
}; | ||
``` | ||
|
||
#### NavigationSyncer | ||
|
||
Syncer to synchronize with history using NavigationAPI. | ||
|
||
```ts | ||
class NavigationSyncer implements Syncer { | ||
constructor(navigation: Navigation); | ||
key(): string | undefined; | ||
sync(arg: { listener: (key: string) => void; signal: AbortSignal }): void; | ||
updateURL(url: string): void; | ||
} | ||
``` | ||
|
||
### useLocationState | ||
|
||
State hooks to synchronize with history through Syncer. | ||
|
||
```ts | ||
type StoreName = "session" | "url"; | ||
|
||
const useLocationState = <T>(props: { | ||
name: string; | ||
defaultValue: T; | ||
storeName: StoreName | string; | ||
}): [T, (value: T) => void]; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# @location-state/next | ||
|
||
`@location-state/next` provides a syncer that synchronizes with the pages router in next.js. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install @location-state/core @location-state/next | ||
# or | ||
yarn add @location-state/core @location-state/next | ||
# or | ||
pnpm add @location-state/core @location-state/next | ||
``` | ||
|
||
## Usage | ||
|
||
Specify provider's syncer that can be obtained with `useNextPagesSyncer()`. | ||
|
||
```tsx | ||
// _app.tsx | ||
import { LocationStateProvider } from "@location-state/core"; | ||
import { useNextPagesSyncer } from "@location-state/next"; | ||
import type { AppProps } from "next/app"; | ||
|
||
export default function MyApp({ Component, pageProps }: AppProps) { | ||
const syncer = useNextPagesSyncer(); | ||
return ( | ||
<LocationStateProvider syncer={syncer}> | ||
<Component {...pageProps} /> | ||
</LocationStateProvider> | ||
); | ||
} | ||
``` | ||
|
||
Same as normal usage.Read [@location-state/core](../location-state-core/README.md). | ||
|
||
```tsx | ||
import { useLocationState, StoreName } from "@location-state/core"; | ||
|
||
export function Counter({ storeName }: { storeName: StoreName }) { | ||
const [counter, setCounter] = useLocationState({ | ||
name: "counter", | ||
defaultValue: 0, | ||
storeName: "session", | ||
}); | ||
|
||
return <>...</>; | ||
} | ||
``` | ||
|
||
## API | ||
|
||
### useNextPagesSyncer | ||
|
||
Can get Syncer to sync with Next.js pages router. | ||
|
||
```tsx | ||
function useNextPagesSyncer(): NextPagesSyncer; | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
リポジトリを見に来る人が最初に見るのがこのREADMEなのでライブラリの概要はここだけで見れるようにしたいお気持ち
APIリファレンス的なのはそれぞれのパッケージに誘導で十分だけど