Skip to content

Commit

Permalink
feat(utils): move atomWithHash to peer deps (#1579)
Browse files Browse the repository at this point in the history
* feat(utils): move atomWithHash to peer deps

* fix workflow for older ts

* fix the workflow file

* fix merge mistake

* revert src/utils

* add depreacated jsdoc

* remove the peer dep

* fix typo

* fix the workflow file again
  • Loading branch information
dai-shi authored Nov 17, 2022
1 parent 14065f6 commit 822832f
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 58 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-old-typescript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@ jobs:
yarn add -D typescript@${{ matrix.typescript }}
rm -r node_modules/@types/jsdom node_modules/parse5
sed -i~ "s/\">=4.2\": {/\">=4.1\": {/" node_modules/rxjs/package.json
sed -i~ "s/^import type /import /" node_modules/jotai-redux/dist/src/atomWithStore.d.ts node_modules/jotai-immer/dist/src/*.d.ts
sed -i~ "s/^import type /import /" node_modules/jotai-redux/dist/src/*.d.ts node_modules/jotai-immer/dist/src/*.d.ts
yarn tsc --noEmit
165 changes: 165 additions & 0 deletions docs/integrations/location.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
---
title: Location
description: This doc describes window.location integration.
nav: 4.09
---

To deal with `window.location`, we have some functions to create atoms.

## Install

You have to install `jotai-location` to use this feature.

```
yarn add jotai-location
```

## atomWithLocation

```js
atomWithLocation(options): PrimitiveAtom
```

`atomWithLocation` creates a new atom that links to `window.location`.

### Parameters

**options** (optional): an object of options to customize the behavior of the atom

### Options

**preloaded** (optional): preloaded location value to avoid getting location at initialization.

**replace** (optional): a boolean to indicate to use `replaceState` instead of `pushState`.

**getLocation** (optional): a custom function to get location value.

**applyLocation** (optional): a custom function to set location value.

**subscribe** (optional): a custom function to subscribe to location change.

### Examples

```jsx
import { useAtom } from 'jotai'
import { atomWithLocation } from 'jotai-location'

const locationAtom = atomWithLocation()

const App = () => {
const [loc, setLoc] = useAtom(locationAtom)
return (
<ul>
<li>
<button
style={{
fontWeight: loc.pathname === '/' ? 'bold' : 'normal',
}}
onClick={() => setLoc((prev) => ({ ...prev, pathname: '/' }))}>
Home
</button>
</li>
<li>
<button
style={{
fontWeight:
loc.pathname === '/foo' && !loc.searchParams?.get('bar')
? 'bold'
: 'normal',
}}
onClick={() =>
setLoc((prev) => ({
...prev,
pathname: '/foo',
searchParams: new URLSearchParams(),
}))
}>
Foo
</button>
</li>
<li>
<button
style={{
fontWeight:
loc.pathname === '/foo' && loc.searchParams?.get('bar') === '1'
? 'bold'
: 'normal',
}}
onClick={() =>
setLoc((prev) => ({
...prev,
pathname: '/foo',
searchParams: new URLSearchParams([['bar', '1']]),
}))
}>
Foo?bar=1
</button>
</li>
</ul>
)
}
```
### Codesandbox
<CodeSandbox id="kism55" />
## atomWithHash
```js
atomWithHash(key, initialValue, options): PrimitiveAtom
```
This creates a new atom that is connected with URL hash.
The hash must be in the URLSearchParams format.
It's a two-way binding: changing the atom value will change the hash and
changing the hash will change the atom value.
This function works only with DOM.
### Parameters
**key** (required): a unique string used as the key when syncing state with localStorage, sessionStorage, or AsyncStorage
**initialValue** (required): the initial value of the atom
**options** (optional): an object of options to customize the behavior of the atom
### Options
**serialize** (optional): a custom function to serialize the atom value to the hash. Defaults to `JSON.stringify`.
**deserialize** (optional): a custom function to deserialize the hash to the atom value. Defaults to `JSON.parse`.
**delayInit** (optional): delay initialization of the atom to when `onMount` is called. See [#739](/~https://github.com/pmndrs/jotai/issues/739#issuecomment-929260696). Defaults to `false`.
**replaceState** (optional): when the atom value is changed, replace the current history entry instead of adding a new one. See [#660](/~https://github.com/pmndrs/jotai/issues/660). Defaults to `false`.
**subscribe** (optional): custom hash change subscribe function
### Examples
```jsx
import { useAtom } from 'jotai'
import { atomWithHash } from 'jotai-location'

const countAtom = atomWithHash('count', 1)

const Counter = () => {
const [count, setCount] = useAtom(countAtom)
return (
<div>
<div>count: {count}</div>
<button onClick={() => setCount((c) => c + 1)}>+1</button>
</div>
)
}
```
### Codesandbox
<CodeSandbox id="2mocd1" />
### Deleting Item
Please refer [atomWithStorage](../utils/atom-with-storage.mdx)
for the usage about deleting items.
58 changes: 1 addition & 57 deletions docs/utils/atom-with-hash.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,4 @@
title: atomWithHash
---

## Usage

```js
atomWithHash(key, initialValue, options): PrimitiveAtom
```

This creates a new atom that is connected with URL hash.
The hash must be in the URLSearchParams format.
It’s a two-way binding: changing the atom value will change the hash and
changing the hash will change the atom value.
This function works only with DOM.

## Parameters

**key** (required): a unique string used as the key when syncing state with localStorage, sessionStorage, or AsyncStorage

**initialValue** (required): the initial value of the atom

**options** (optional): an object of options to customize the behavior of the atom

## Options

**serialize** (optional): a custom function to serialize the atom value to the hash. Defaults to `JSON.stringify`.

**deserialize** (optional): a custom function to deserialize the hash to the atom value. Defaults to `JSON.parse`.

**delayInit** (optional): delay initialization of the atom to when `onMount` is called. See [#739](/~https://github.com/pmndrs/jotai/issues/739#issuecomment-929260696). Defaults to `false`.

**replaceState** (optional): when the atom value is changed, replace the current history entry instead of adding a new one. See [#660](/~https://github.com/pmndrs/jotai/issues/660). Defaults to `false`.

**subscribe** (optional): custom hash change subscribe function

## Examples

```jsx
import { useAtom } from 'jotai'
import { atomWithHash } from 'jotai/utils'
const countAtom = atomWithHash('count', 1)
const Counter = () => {
const [count, setCount] = useAtom(countAtom)
return (
<>
<div>count: {count}</div>
<button onClick={() => setCount((c) => c + 1)}>button</button>
</>
)
}
```

## Codesandbox

<CodeSandbox id="d5kn6" />

## Deleting Item

Please refer [atomWithStorage](../utils/atom-with-storage.mdx)
for the usage about deleting items.
Moved to [Location / atomWithHash](../integrations/location.mdx#atom-with-hash).
3 changes: 3 additions & 0 deletions src/utils/atomWithStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ export function atomWithStorage<Value>(

// atomWithHash is implemented with atomWithStorage

/**
* @deprecated Please `import { atomWithHash } from 'jotai-location'`
*/
export function atomWithHash<Value>(
key: string,
initialValue: Value,
Expand Down

1 comment on commit 822832f

@vercel
Copy link

@vercel vercel bot commented on 822832f Nov 17, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.