@graphcommerce/next-ui 9.1.0-canary.35 → 9.1.0-canary.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/package.json +8 -8
- package/utils/cookie.ts +30 -13
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Change Log
|
2
2
|
|
3
|
+
## 9.1.0-canary.37
|
4
|
+
|
5
|
+
## 9.1.0-canary.36
|
6
|
+
|
7
|
+
### Patch Changes
|
8
|
+
|
9
|
+
- [`90f8dd8`](https://github.com/graphcommerce-org/graphcommerce/commit/90f8dd89efd62e2b5a13e9c2bf265840d99e2473) - Create useCookies hook and simplify the useCookie query ([@paales](https://github.com/paales))
|
10
|
+
|
3
11
|
## 9.1.0-canary.35
|
4
12
|
|
5
13
|
### Patch Changes
|
package/package.json
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
"name": "@graphcommerce/next-ui",
|
3
3
|
"homepage": "https://www.graphcommerce.org/",
|
4
4
|
"repository": "github:graphcommerce-org/graphcommerce",
|
5
|
-
"version": "9.1.0-canary.
|
5
|
+
"version": "9.1.0-canary.37",
|
6
6
|
"sideEffects": false,
|
7
7
|
"prettier": "@graphcommerce/prettier-config-pwa",
|
8
8
|
"eslintConfig": {
|
@@ -24,13 +24,13 @@
|
|
24
24
|
"@emotion/react": "^11",
|
25
25
|
"@emotion/server": "^11",
|
26
26
|
"@emotion/styled": "^11",
|
27
|
-
"@graphcommerce/eslint-config-pwa": "^9.1.0-canary.
|
28
|
-
"@graphcommerce/framer-next-pages": "^9.1.0-canary.
|
29
|
-
"@graphcommerce/framer-scroller": "^9.1.0-canary.
|
30
|
-
"@graphcommerce/framer-utils": "^9.1.0-canary.
|
31
|
-
"@graphcommerce/image": "^9.1.0-canary.
|
32
|
-
"@graphcommerce/prettier-config-pwa": "^9.1.0-canary.
|
33
|
-
"@graphcommerce/typescript-config-pwa": "^9.1.0-canary.
|
27
|
+
"@graphcommerce/eslint-config-pwa": "^9.1.0-canary.37",
|
28
|
+
"@graphcommerce/framer-next-pages": "^9.1.0-canary.37",
|
29
|
+
"@graphcommerce/framer-scroller": "^9.1.0-canary.37",
|
30
|
+
"@graphcommerce/framer-utils": "^9.1.0-canary.37",
|
31
|
+
"@graphcommerce/image": "^9.1.0-canary.37",
|
32
|
+
"@graphcommerce/prettier-config-pwa": "^9.1.0-canary.37",
|
33
|
+
"@graphcommerce/typescript-config-pwa": "^9.1.0-canary.37",
|
34
34
|
"@lingui/core": "^4.2.1",
|
35
35
|
"@lingui/macro": "^4.2.1",
|
36
36
|
"@lingui/react": "^4.2.1",
|
package/utils/cookie.ts
CHANGED
@@ -1,22 +1,25 @@
|
|
1
|
+
import { useMotionValueValue } from '@graphcommerce/framer-utils'
|
1
2
|
import type { SerializeOptions } from 'cookie'
|
2
3
|
import { parse, serialize } from 'cookie'
|
3
4
|
import { motionValue } from 'framer-motion'
|
4
|
-
import {
|
5
|
+
import { useIsSSR } from '../hooks'
|
5
6
|
|
6
7
|
// We need this motionValue to be synced with the actual cookie store.
|
7
8
|
const cookieNotify = motionValue<number>(0)
|
8
9
|
|
10
|
+
export function cookie(): Record<string, string | undefined>
|
9
11
|
/** Read a cookie */
|
10
|
-
export function cookie(name: string):
|
12
|
+
export function cookie<T extends string>(name: string): T | undefined
|
11
13
|
/** Set a cookie */
|
12
14
|
export function cookie(name: string, value: string, options?: SerializeOptions): void
|
13
15
|
/** Delete a cookie */
|
14
16
|
export function cookie(name: string, value: null): void
|
15
17
|
/** Function to handle the three different cases */
|
16
|
-
export function cookie(name
|
17
|
-
if (typeof window === 'undefined')
|
18
|
-
|
19
|
-
|
18
|
+
export function cookie(name?: string, value?: string | null, options?: SerializeOptions) {
|
19
|
+
if (typeof window === 'undefined') return undefined
|
20
|
+
|
21
|
+
// Read all cookies
|
22
|
+
if (typeof name === 'undefined') return parse(document.cookie)
|
20
23
|
|
21
24
|
// Read a cookie
|
22
25
|
if (typeof value === 'undefined') return parse(document.cookie)[name]
|
@@ -40,17 +43,31 @@ export function cookie(name: string, value?: string | null, options?: SerializeO
|
|
40
43
|
return undefined
|
41
44
|
}
|
42
45
|
|
43
|
-
|
44
|
-
|
46
|
+
/**
|
47
|
+
* This makes sure this hook is rerendered when the cookie changes, we do not actually use the
|
48
|
+
* value.
|
49
|
+
*/
|
50
|
+
export function useCookies(): Record<string, string | undefined> {
|
51
|
+
// Initial render it should always be empty since the server renders cookie-less.
|
52
|
+
// Whenever the server can handle cookies, we can remove this and directly return the cookie value.
|
53
|
+
const isSsr = useIsSSR()
|
45
54
|
|
46
|
-
|
47
|
-
useEffect(() => cookieNotify.on('change', () => setValue(cookie(name))))
|
55
|
+
useMotionValueValue(cookieNotify, (v) => v)
|
48
56
|
|
49
|
-
|
50
|
-
|
57
|
+
return isSsr ? {} : cookie()
|
58
|
+
}
|
59
|
+
|
60
|
+
export function useCookie<T extends string>(
|
61
|
+
name: string,
|
62
|
+
options: SerializeOptions = { sameSite: true },
|
63
|
+
defaultValue?: T | null,
|
64
|
+
): [T | undefined | null, (value: T | null) => void] {
|
65
|
+
const update = (val: T | null) => {
|
66
|
+
if (val) cookie(name, val, options)
|
51
67
|
else cookie(name, null)
|
52
|
-
cookieNotify.set(cookieNotify.get() + 1)
|
53
68
|
}
|
54
69
|
|
70
|
+
const cookies = useCookies()
|
71
|
+
const value = (cookies[name] as T | undefined | null) ?? defaultValue
|
55
72
|
return [value, update] as const
|
56
73
|
}
|