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

add package's docs #6

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 3 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@

A lightweight state management library that saves and restores state based on history.

## Features
## Packages

TBW

## Installation

TBW

## Usage

TBW
- [@location-state/core](./packages/location-state-core/README.md)
- [@location-state/next](./packages/location-state-next/README.md)
Copy link
Member

Choose a reason for hiding this comment

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

リポジトリを見に来る人が最初に見るのがこのREADMEなのでライブラリの概要はここだけで見れるようにしたいお気持ち
APIリファレンス的なのはそれぞれのパッケージに誘導で十分だけど

124 changes: 124 additions & 0 deletions packages/location-state-core/README.md
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`.
Copy link
Member

Choose a reason for hiding this comment

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

ここでsyncerとか書いても何も伝わらないような?
こことpackage.jsondescriptionを揃えるなら、これ単独でなんのライブラリか分かる説明が必要ですね
npmjs.orgのサーチ結果等で見れるのはこれなので
つまりこのライブラリのことを何も知らない人にこれが何なのかをわかるようにしたい
とすると絶対に伝えたいのは

  • React向けの
  • 状態管理ライブラリで
  • 履歴エントリと同期して
  • Next.js App Routerをサポート

辺りじゃないかなぁと
たとえば上を適当にDeepLすると:

State management library for React that synchronizes with history entries supporting Next.js App Router


## 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.
Copy link
Member

Choose a reason for hiding this comment

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

ここというかトップレベルのREADMEに書きたいことだけど、最初のUsageでいきなりsyncerとか出したくないかな
最小限のデフォルト構成を紹介したい


```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.
Copy link
Member

Choose a reason for hiding this comment

The 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.
Copy link
Member

Choose a reason for hiding this comment

The 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).
Copy link
Member

Choose a reason for hiding this comment

The 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";
Copy link
Member

Choose a reason for hiding this comment

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

このサンプルではStoreName不要では


export function Counter({ storeName }: { storeName: StoreName }) {
Copy link
Member

Choose a reason for hiding this comment

The 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];
```
3 changes: 2 additions & 1 deletion packages/location-state-core/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@location-state/core",
"version": "0.0.0-alpha",
"license": "MIT",
"description": "`@location-state/core` provides the features to save and restore state based on a specified `syncer`.",
"files": [
"dist",
"types",
"src",
"package.json"
],
"exports": {
Expand Down
59 changes: 59 additions & 0 deletions packages/location-state-next/README.md
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;
```
3 changes: 2 additions & 1 deletion packages/location-state-next/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@location-state/next",
"version": "0.0.0-alpha",
"license": "MIT",
"description": "`@location-state/next` provides a syncer that synchronizes with the pages router in next.js.",
"files": [
"dist",
"types",
"src",
"package.json"
],
"exports": {
Expand Down