@musecat/functionkit 1.0.1 → 1.1.0
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/.agents/references/components/ScrolltoTop.md +36 -0
- package/.agents/references/components/SwitchCase.md +47 -0
- package/.agents/references/components/ViewportPortal.md +34 -0
- package/.agents/references/cookie/cookie.md +47 -0
- package/.agents/references/datetime/dateTime.client.md +42 -0
- package/.agents/references/datetime/dateTime.server.md +42 -0
- package/.agents/references/datetime/dateTime.shared.md +86 -0
- package/.agents/references/hooks/useAvoidKeyboard.md +29 -0
- package/.agents/references/hooks/useCheckInvisible.md +36 -0
- package/.agents/references/hooks/useCheckScroll.md +36 -0
- package/.agents/references/hooks/useClientDateTime.md +39 -0
- package/.agents/references/hooks/useDebounce.md +46 -0
- package/.agents/references/hooks/useDebouncedCallback.md +34 -0
- package/.agents/references/hooks/useDoubleClick.md +40 -0
- package/.agents/references/hooks/useGeolocation.md +37 -0
- package/.agents/references/hooks/useHasMounted.md +27 -0
- package/.agents/references/hooks/useIntersectionObserver.md +41 -0
- package/.agents/references/hooks/useInterval.md +44 -0
- package/.agents/references/hooks/useKeyboardHeight.md +33 -0
- package/.agents/references/hooks/useKeyboardListNavigation.md +58 -0
- package/.agents/references/hooks/useLongPress.md +48 -0
- package/.agents/references/hooks/usePreservedCallback.md +37 -0
- package/.agents/references/hooks/usePreservedReference.md +34 -0
- package/.agents/references/hooks/useRefEffect.md +35 -0
- package/.agents/references/hooks/useRelativeDateTime.md +35 -0
- package/.agents/references/hooks/useTimeout.md +42 -0
- package/.agents/references/hooks/useToggleState.md +39 -0
- package/.agents/references/hooks/useViewportHeight.md +26 -0
- package/.agents/references/hooks/useViewportMatch.md +32 -0
- package/.agents/references/utils/browserStorage.md +41 -0
- package/.agents/references/utils/buildContext.md +47 -0
- package/.agents/references/utils/clipboardShare.md +59 -0
- package/.agents/references/utils/floatingMotion.md +67 -0
- package/.agents/references/utils/getDeviceInfo.md +43 -0
- package/.agents/references/utils/isEditableKeyboardTarget.md +35 -0
- package/.agents/references/utils/mergeRefs.md +29 -0
- package/.agents/references/utils/seen.md +37 -0
- package/.agents/references/utils/subscribeKeyboardHeight.md +35 -0
- package/.github/ISSUE_TEMPLATE/bug_report.md +31 -0
- package/.github/ISSUE_TEMPLATE/feature_request.md +22 -0
- package/.github/PULL_REQUEST_TEMPLATE.md +22 -0
- package/.github/workflows/ci.yml +20 -0
- package/.local/state/gh/device-id +1 -0
- package/.prettierrc +8 -0
- package/AGENTS.md +117 -396
- package/CODE_OF_CONDUCT.md +55 -0
- package/CONTRIBUTING.md +39 -0
- package/README.md +18 -5
- package/biome.json +25 -0
- package/index.ts +63 -68
- package/package.json +46 -13
- package/packages/components/ScrolltoTop.tsx +3 -3
- package/packages/components/SwitchCase.tsx +7 -7
- package/packages/components/ViewportPortal.tsx +22 -23
- package/packages/cookie/cookie.shared.ts +80 -110
- package/packages/cookie/cookieNames.shared.ts +9 -0
- package/packages/datetime/dateTime.client.ts +84 -86
- package/packages/datetime/dateTime.server.ts +84 -86
- package/packages/datetime/dateTime.shared.ts +249 -287
- package/packages/hooks/useAvoidKeyboard.ts +15 -15
- package/packages/hooks/useCheckInvisible.ts +16 -16
- package/packages/hooks/useCheckScroll.ts +10 -10
- package/packages/hooks/useClientDateTime.ts +24 -37
- package/packages/hooks/useDebounce.ts +90 -95
- package/packages/hooks/useDebouncedCallback.ts +45 -45
- package/packages/hooks/useDoubleClick.ts +43 -43
- package/packages/hooks/useGeolocation.ts +126 -130
- package/packages/hooks/useHasMounted.ts +5 -5
- package/packages/hooks/useIntersectionObserver.ts +20 -20
- package/packages/hooks/useInterval.ts +45 -45
- package/packages/hooks/useKeyboardHeight.ts +9 -9
- package/packages/hooks/useKeyboardListNavigation.ts +130 -158
- package/packages/hooks/useLongPress.ts +87 -98
- package/packages/hooks/usePreservedCallback.ts +12 -12
- package/packages/hooks/usePreservedReference.ts +10 -10
- package/packages/hooks/useRefEffect.ts +19 -19
- package/packages/hooks/useRelativeDateTime.ts +36 -39
- package/packages/hooks/useTimeout.ts +41 -41
- package/packages/hooks/useToggleState.ts +5 -5
- package/packages/hooks/useViewportHeight.ts +12 -13
- package/packages/hooks/useViewportMatch.ts +13 -23
- package/packages/utils/browserStorage.ts +26 -26
- package/packages/utils/buildContext.tsx +10 -11
- package/packages/utils/checkDevice.ts +64 -64
- package/packages/utils/clipboardShare.tsx +31 -31
- package/packages/utils/clipboardShare.types.ts +7 -7
- package/packages/utils/floatingMotion.ts +71 -77
- package/packages/utils/keyboardTarget.ts +9 -9
- package/packages/utils/mergeRefs.ts +9 -9
- package/packages/utils/seen.ts +15 -15
- package/packages/utils/subscribeKeyboardHeight.ts +49 -46
- package/tests/components/ScrolltoTop.test.tsx +15 -0
- package/tests/components/SwitchCase.test.tsx +33 -0
- package/tests/components/ViewportPortal.test.tsx +46 -0
- package/tests/cookie/cookie.test.ts +192 -0
- package/tests/datetime/datetime.test.ts +461 -0
- package/tests/hooks/useAvoidKeyboard.test.ts +43 -0
- package/tests/hooks/useCheckInvisible.test.ts +59 -0
- package/tests/hooks/useCheckScroll.test.ts +23 -0
- package/tests/hooks/useClientDateTime.test.ts +12 -0
- package/tests/hooks/useDebounce.test.ts +96 -0
- package/tests/hooks/useDebouncedCallback.test.ts +63 -0
- package/tests/hooks/useDoubleClick.test.ts +82 -0
- package/tests/hooks/useGeolocation.test.ts +201 -0
- package/tests/hooks/useHasMounted.test.ts +10 -0
- package/tests/hooks/useIntersectionObserver.test.ts +78 -0
- package/tests/hooks/useInterval.test.ts +56 -0
- package/tests/hooks/useKeyboardHeight.test.ts +10 -0
- package/tests/hooks/useKeyboardListNavigation.test.ts +404 -0
- package/tests/hooks/useLongPress.test.ts +214 -0
- package/tests/hooks/usePreservedCallback.test.ts +33 -0
- package/tests/hooks/usePreservedReference.test.ts +42 -0
- package/tests/hooks/useRefEffect.test.ts +67 -0
- package/tests/hooks/useRelativeDateTime.test.ts +21 -0
- package/tests/hooks/useTimeout.test.ts +72 -0
- package/tests/hooks/useToggleState.test.ts +43 -0
- package/tests/hooks/useViewportHeight.test.ts +32 -0
- package/tests/hooks/useViewportMatch.test.ts +35 -0
- package/tests/setup.ts +9 -0
- package/tests/utils/browserStorage.test.ts +114 -0
- package/tests/utils/buildContext.test.tsx +26 -0
- package/tests/utils/checkDevice.test.ts +140 -0
- package/tests/utils/clipboardShare.test.ts +152 -0
- package/tests/utils/floatingMotion.test.ts +135 -0
- package/tests/utils/keyboardTarget.test.ts +85 -0
- package/tests/utils/mergeRefs.test.ts +50 -0
- package/tests/utils/seen.test.ts +40 -0
- package/tests/utils/subscribeKeyboardHeight.test.ts +172 -0
- package/tsconfig.json +16 -16
- package/vitest.config.ts +22 -0
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @musecat/functionkit
|
|
2
2
|
|
|
3
|
-
React 19
|
|
3
|
+
A React 19 + TypeScript UI F.E. utility library.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -10,16 +10,29 @@ npm install @musecat/functionkit
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
모든 훅, 컴포넌트, 유틸리티는 배럴 파일을 통해 직접 임포트하여 사용할 수 있습니다.
|
|
14
|
-
|
|
15
13
|
```tsx
|
|
16
14
|
import { useDebounce, formatClientDateTime } from "@musecat/functionkit";
|
|
15
|
+
|
|
16
|
+
export default function Example() {
|
|
17
|
+
const handleSearch = useDebounce((term: string) => fetchResults(term), 300);
|
|
18
|
+
const now = formatClientDateTime(new Date(), {
|
|
19
|
+
datePreset: "long",
|
|
20
|
+
timePreset: "24h-minute",
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return (
|
|
24
|
+
<View column gap={24}>
|
|
25
|
+
<Input onChange={(e) => handleSearch(e.target.value)} />
|
|
26
|
+
<Text type="Caption1">{now}</Text>
|
|
27
|
+
</View>
|
|
28
|
+
);
|
|
29
|
+
}
|
|
17
30
|
```
|
|
18
31
|
|
|
19
32
|
## Acknowledgements
|
|
20
33
|
|
|
21
|
-
|
|
34
|
+
- [toss/react-simplikit](https://github.com/toss/react-simplikit/)
|
|
22
35
|
|
|
23
36
|
## License
|
|
24
37
|
|
|
25
|
-
MIT License © Musecat Team.
|
|
38
|
+
[MIT License](./LICENSE) © Musecat Team.
|
package/biome.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://biomejs.dev/schemas/2.5.4/schema.json",
|
|
3
|
+
"assist": { "actions": { "source": { "organizeImports": "on" } } },
|
|
4
|
+
"linter": {
|
|
5
|
+
"enabled": true,
|
|
6
|
+
"rules": {
|
|
7
|
+
"preset": "recommended"
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
"formatter": {
|
|
11
|
+
"enabled": true,
|
|
12
|
+
"indentStyle": "tab",
|
|
13
|
+
"indentWidth": 2,
|
|
14
|
+
"lineWidth": 100
|
|
15
|
+
},
|
|
16
|
+
"javascript": {
|
|
17
|
+
"formatter": {
|
|
18
|
+
"quoteStyle": "double",
|
|
19
|
+
"semicolons": "always"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": {
|
|
23
|
+
"includes": ["**", "!**/dist", "!**/coverage", "!**/node_modules"]
|
|
24
|
+
}
|
|
25
|
+
}
|
package/index.ts
CHANGED
|
@@ -1,53 +1,49 @@
|
|
|
1
|
-
// 컴포넌트
|
|
2
1
|
export {
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
getViewportPortalRoot,
|
|
3
|
+
ViewportPortal,
|
|
5
4
|
} from "./packages/components/ViewportPortal";
|
|
6
|
-
// 쿠키
|
|
7
5
|
export {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
setClientCookie
|
|
6
|
+
clearAllClientCookies,
|
|
7
|
+
clearClientCookie,
|
|
8
|
+
getClientCookie,
|
|
9
|
+
setClientCookie,
|
|
13
10
|
} from "./packages/cookie/cookie.shared";
|
|
14
|
-
|
|
11
|
+
export { parseClientCookieNames } from "./packages/cookie/cookieNames.shared";
|
|
15
12
|
export {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
13
|
+
formatClientDate,
|
|
14
|
+
formatClientDateTime,
|
|
15
|
+
formatClientRelative,
|
|
16
|
+
formatClientTime,
|
|
20
17
|
} from "./packages/datetime/dateTime.client";
|
|
21
18
|
export {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
19
|
+
formatServerDate,
|
|
20
|
+
formatServerDateTime,
|
|
21
|
+
formatServerRelative,
|
|
22
|
+
formatServerTime,
|
|
26
23
|
} from "./packages/datetime/dateTime.server";
|
|
27
|
-
export {
|
|
28
|
-
addUtcDays,
|
|
29
|
-
format24HourTime,
|
|
30
|
-
formatDotDate,
|
|
31
|
-
formatKoreanTime,
|
|
32
|
-
formatLongDate,
|
|
33
|
-
formatRelativeText,
|
|
34
|
-
formatRemainingText,
|
|
35
|
-
formatTwelveHourTime,
|
|
36
|
-
formatUtcDateKey,
|
|
37
|
-
getUtcWeekdayIndex,
|
|
38
|
-
normalizeAppLocale,
|
|
39
|
-
parseUtcDateInput,
|
|
40
|
-
toDate,
|
|
41
|
-
toIntlLocale,
|
|
42
|
-
toUtcMidnight
|
|
43
|
-
} from "./packages/datetime/dateTime.shared";
|
|
44
24
|
export type {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
25
|
+
AppLocale,
|
|
26
|
+
DateInput,
|
|
27
|
+
DatePreset,
|
|
28
|
+
TimePreset,
|
|
29
|
+
} from "./packages/datetime/dateTime.shared";
|
|
30
|
+
export {
|
|
31
|
+
addUtcDays,
|
|
32
|
+
format24HourTime,
|
|
33
|
+
formatDotDate,
|
|
34
|
+
formatKoreanTime,
|
|
35
|
+
formatLongDate,
|
|
36
|
+
formatRelativeText,
|
|
37
|
+
formatRemainingText,
|
|
38
|
+
formatTwelveHourTime,
|
|
39
|
+
formatUtcDateKey,
|
|
40
|
+
getUtcWeekdayIndex,
|
|
41
|
+
normalizeAppLocale,
|
|
42
|
+
parseUtcDateInput,
|
|
43
|
+
toDate,
|
|
44
|
+
toIntlLocale,
|
|
45
|
+
toUtcMidnight,
|
|
49
46
|
} from "./packages/datetime/dateTime.shared";
|
|
50
|
-
// hooks
|
|
51
47
|
export { useAvoidKeyboard } from "./packages/hooks/useAvoidKeyboard";
|
|
52
48
|
export { useClientDateTime } from "./packages/hooks/useClientDateTime";
|
|
53
49
|
export { useDebounce } from "./packages/hooks/useDebounce";
|
|
@@ -68,44 +64,43 @@ export { useTimeout } from "./packages/hooks/useTimeout";
|
|
|
68
64
|
export { useToggleState } from "./packages/hooks/useToggleState";
|
|
69
65
|
export { useViewportHeight } from "./packages/hooks/useViewportHeight";
|
|
70
66
|
export { useViewportMatch } from "./packages/hooks/useViewportMatch";
|
|
71
|
-
// 유틸리티
|
|
72
67
|
export {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
68
|
+
getLocalStorage,
|
|
69
|
+
getSessionStorage,
|
|
70
|
+
removeLocalStorage,
|
|
71
|
+
removeSessionStorage,
|
|
72
|
+
updateLocalStorage,
|
|
73
|
+
updateSessionStorage,
|
|
79
74
|
} from "./packages/utils/browserStorage";
|
|
80
|
-
// 유틸리티
|
|
81
75
|
export { buildContext } from "./packages/utils/buildContext";
|
|
82
|
-
export { getDeviceInfo } from "./packages/utils/checkDevice";
|
|
83
76
|
export type { DeviceInfo } from "./packages/utils/checkDevice";
|
|
84
|
-
export {
|
|
77
|
+
export { getDeviceInfo } from "./packages/utils/checkDevice";
|
|
78
|
+
export {
|
|
79
|
+
NavigatorClipboard,
|
|
80
|
+
NavigatorShare,
|
|
81
|
+
} from "./packages/utils/clipboardShare";
|
|
85
82
|
export type {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
83
|
+
NavigatorClipboardProps,
|
|
84
|
+
NavigatorClipboardResult,
|
|
85
|
+
NavigatorShareProps,
|
|
86
|
+
NavigatorShareResult,
|
|
90
87
|
} from "./packages/utils/clipboardShare.types";
|
|
91
|
-
export {
|
|
92
|
-
getFloatingHiddenTransform,
|
|
93
|
-
getFloatingMotionPreset,
|
|
94
|
-
getFloatingTransformOrigin
|
|
95
|
-
} from "./packages/utils/floatingMotion";
|
|
96
88
|
export type {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
89
|
+
FloatingMotionMode,
|
|
90
|
+
FloatingMotionPreset,
|
|
91
|
+
FloatingPlacement,
|
|
92
|
+
} from "./packages/utils/floatingMotion";
|
|
93
|
+
export {
|
|
94
|
+
getFloatingHiddenTransform,
|
|
95
|
+
getFloatingMotionPreset,
|
|
96
|
+
getFloatingTransformOrigin,
|
|
100
97
|
} from "./packages/utils/floatingMotion";
|
|
101
|
-
// 유틸리티
|
|
102
98
|
export { isEditableKeyboardTarget } from "./packages/utils/keyboardTarget";
|
|
103
99
|
export { mergeRefs } from "./packages/utils/mergeRefs";
|
|
104
100
|
export {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
101
|
+
buildSeenValue,
|
|
102
|
+
hasSeenKey,
|
|
103
|
+
parseSeen,
|
|
104
|
+
SEEN_STORAGE_KEY,
|
|
109
105
|
} from "./packages/utils/seen";
|
|
110
106
|
export { subscribeKeyboardHeight } from "./packages/utils/subscribeKeyboardHeight";
|
|
111
|
-
|
package/package.json
CHANGED
|
@@ -1,15 +1,48 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
"name": "@musecat/functionkit",
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "React 19 + TypeScript frontend utility library — hooks, components, datetime, cookie, and utils",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"react",
|
|
7
|
+
"hooks",
|
|
8
|
+
"typescript",
|
|
9
|
+
"utility",
|
|
10
|
+
"frontend"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "https://github.com/TheTechclip/FunctionKit.git"
|
|
16
|
+
},
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/TheTechclip/FunctionKit/issues"
|
|
19
|
+
},
|
|
20
|
+
"homepage": "https://musecat.app",
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "./index.ts",
|
|
23
|
+
"types": "./index.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": "./index.ts",
|
|
26
|
+
"./*": "./*"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"lint": "biome check",
|
|
30
|
+
"format": "prettier --write . && biome format --write",
|
|
31
|
+
"test": "vitest run",
|
|
32
|
+
"test:coverage": "vitest run --coverage"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": "^19.0.0",
|
|
36
|
+
"react-dom": "^19.0.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@biomejs/biome": "^2.5.4",
|
|
40
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
41
|
+
"@testing-library/react": "^16.2.0",
|
|
42
|
+
"@testing-library/user-event": "^14.6.1",
|
|
43
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
44
|
+
"jsdom": "^26.0.0",
|
|
45
|
+
"prettier": "^3.9.5",
|
|
46
|
+
"vitest": "^4.1.10"
|
|
47
|
+
}
|
|
15
48
|
}
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
import type { ReactNode } from "react";
|
|
2
2
|
|
|
3
3
|
type SwitchCaseProps<T extends string | number> = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
value: T;
|
|
5
|
+
cases: Partial<Record<T, ReactNode>>;
|
|
6
|
+
otherwise?: ReactNode;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
export function SwitchCase<T extends string | number>({
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
value,
|
|
11
|
+
cases,
|
|
12
|
+
otherwise = null,
|
|
13
13
|
}: SwitchCaseProps<T>) {
|
|
14
|
-
|
|
14
|
+
return (cases[value] ?? otherwise) as ReactNode;
|
|
15
15
|
}
|
|
@@ -6,35 +6,34 @@ import { createPortal } from "react-dom";
|
|
|
6
6
|
const VIEWPORT_PORTAL_ROOT_ID = "viewport-portal-root";
|
|
7
7
|
|
|
8
8
|
export function getViewportPortalRoot() {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return root;
|
|
9
|
+
if (typeof document === "undefined") {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const existingRoot = document.getElementById(VIEWPORT_PORTAL_ROOT_ID);
|
|
14
|
+
if (existingRoot instanceof HTMLDivElement) {
|
|
15
|
+
return existingRoot;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const root = document.createElement("div");
|
|
19
|
+
root.id = VIEWPORT_PORTAL_ROOT_ID;
|
|
20
|
+
root.setAttribute("data-viewport-portal-root", "true");
|
|
21
|
+
root.style.cssText = "position:fixed;inset:0;pointer-events:none;z-index:9999;";
|
|
22
|
+
document.body.appendChild(root);
|
|
23
|
+
return root;
|
|
25
24
|
}
|
|
26
25
|
|
|
27
26
|
type ViewportPortalProps = {
|
|
28
|
-
|
|
27
|
+
children: React.ReactNode;
|
|
29
28
|
};
|
|
30
29
|
|
|
31
30
|
export function ViewportPortal({ children }: ViewportPortalProps) {
|
|
32
|
-
|
|
31
|
+
const [root, setRoot] = useState<HTMLElement | null>(null);
|
|
33
32
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
setRoot(getViewportPortalRoot());
|
|
35
|
+
}, []);
|
|
37
36
|
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
if (!root) return null;
|
|
38
|
+
return createPortal(children, root);
|
|
40
39
|
}
|
|
@@ -3,140 +3,110 @@
|
|
|
3
3
|
const EXPIRED_COOKIE_DATE = "Thu, 01 Jan 1970 00:00:00 GMT";
|
|
4
4
|
|
|
5
5
|
type CookieStoreLike = {
|
|
6
|
-
|
|
6
|
+
delete: (name: string) => Promise<void>;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
9
|
type DocumentCookieRef = {
|
|
10
|
-
|
|
10
|
+
cookie: string;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
13
|
type ClearClientCookieOptions = {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
14
|
+
hostname?: string;
|
|
15
|
+
path?: string;
|
|
16
|
+
documentRef?: DocumentCookieRef;
|
|
17
|
+
cookieStore?: CookieStoreLike;
|
|
18
18
|
};
|
|
19
19
|
|
|
20
20
|
type ClearAllClientCookiesOptions = ClearClientCookieOptions & {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
* Use with caution.
|
|
24
|
-
*/
|
|
25
|
-
includeRoot?: boolean;
|
|
26
|
-
cookieString?: string;
|
|
21
|
+
includeRoot?: boolean;
|
|
22
|
+
cookieString?: string;
|
|
27
23
|
};
|
|
28
24
|
|
|
29
|
-
function findClientCookie(
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
): string | undefined {
|
|
33
|
-
const parsed = parseClientCookie(documentRef.cookie);
|
|
34
|
-
return parsed[name];
|
|
25
|
+
function findClientCookie(name: string, documentRef: DocumentCookieRef): string | undefined {
|
|
26
|
+
const parsed = parseClientCookie(documentRef.cookie);
|
|
27
|
+
return parsed[name];
|
|
35
28
|
}
|
|
36
29
|
|
|
37
30
|
function parseClientCookie(cookieString: string): Record<string, string> {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
31
|
+
return cookieString
|
|
32
|
+
.split(";")
|
|
33
|
+
.map((pair) => pair.trim().split("=") as [string, string])
|
|
34
|
+
.reduce<Record<string, string>>((acc, [key, value]) => {
|
|
35
|
+
if (key) {
|
|
36
|
+
acc[key] = value;
|
|
37
|
+
}
|
|
38
|
+
return acc;
|
|
39
|
+
}, {});
|
|
47
40
|
}
|
|
48
41
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
.split(";")
|
|
52
|
-
.map((entry) => {
|
|
53
|
-
const eqPos = entry.indexOf("=");
|
|
54
|
-
return eqPos > -1 ? entry.slice(0, eqPos).trim() : entry.trim();
|
|
55
|
-
})
|
|
56
|
-
.filter(Boolean);
|
|
57
|
-
}
|
|
42
|
+
import { parseClientCookieNames } from "./cookieNames.shared";
|
|
43
|
+
export { parseClientCookieNames };
|
|
58
44
|
|
|
59
45
|
export function getClientCookie(name: string): string | undefined {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
46
|
+
if (typeof document === "undefined") {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
63
49
|
|
|
64
|
-
|
|
50
|
+
return findClientCookie(name, document);
|
|
65
51
|
}
|
|
66
52
|
|
|
67
53
|
export function setClientCookie(name: string, value: string, days?: number) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
54
|
+
if (typeof document === "undefined") {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let expires = "";
|
|
59
|
+
if (days) {
|
|
60
|
+
const date = new Date();
|
|
61
|
+
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000);
|
|
62
|
+
expires = `; expires=${date.toUTCString()}`;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
document.cookie = `${name}=${encodeURIComponent(value)}${expires}; path=/`;
|
|
80
66
|
}
|
|
81
67
|
|
|
82
|
-
export function clearClientCookie(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const {
|
|
87
|
-
hostname = window.location.hostname,
|
|
88
|
-
path = "/",
|
|
89
|
-
documentRef = document,
|
|
90
|
-
} = options;
|
|
91
|
-
|
|
92
|
-
documentRef.cookie = `${name}=; expires=${EXPIRED_COOKIE_DATE}; path=${path}; domain=${hostname};`;
|
|
68
|
+
export function clearClientCookie(name: string, options: ClearClientCookieOptions = {}): void {
|
|
69
|
+
const { hostname = window.location.hostname, path = "/", documentRef = document } = options;
|
|
70
|
+
|
|
71
|
+
documentRef.cookie = `${name}=; expires=${EXPIRED_COOKIE_DATE}; path=${path}; domain=${hostname};`;
|
|
93
72
|
}
|
|
94
73
|
|
|
95
|
-
export function clearAllClientCookies(
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
for (const path of paths) {
|
|
134
|
-
for (let i = 0; i < hostnameParts.length; i++) {
|
|
135
|
-
const domain = hostnameParts.slice(i).join(".");
|
|
136
|
-
documentRef.cookie = `${name}=; expires=${EXPIRED_COOKIE_DATE}; path=${path}; domain=${domain};`;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return entries;
|
|
74
|
+
export function clearAllClientCookies(options: ClearAllClientCookiesOptions = {}): string[] {
|
|
75
|
+
const { documentRef = document, cookieStore, includeRoot = false, cookieString } = options;
|
|
76
|
+
|
|
77
|
+
const entries = cookieString
|
|
78
|
+
? parseClientCookieNames(cookieString)
|
|
79
|
+
: documentRef.cookie
|
|
80
|
+
.split(/;\s*/)
|
|
81
|
+
.map((entry) => {
|
|
82
|
+
const eqPos = entry.indexOf("=");
|
|
83
|
+
return eqPos > -1 ? entry.slice(0, eqPos).trim() : entry.trim();
|
|
84
|
+
})
|
|
85
|
+
.filter(Boolean);
|
|
86
|
+
|
|
87
|
+
if (cookieStore) {
|
|
88
|
+
for (const name of entries) {
|
|
89
|
+
cookieStore.delete(name);
|
|
90
|
+
}
|
|
91
|
+
return entries;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const hostnameParts = window.location.hostname.split(".");
|
|
95
|
+
const { pathname } = window.location;
|
|
96
|
+
|
|
97
|
+
const paths = [pathname];
|
|
98
|
+
if (includeRoot) {
|
|
99
|
+
paths.unshift("/");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
for (const name of entries) {
|
|
103
|
+
for (const path of paths) {
|
|
104
|
+
for (let i = 0; i < hostnameParts.length; i++) {
|
|
105
|
+
const domain = hostnameParts.slice(i).join(".");
|
|
106
|
+
documentRef.cookie = `${name}=; expires=${EXPIRED_COOKIE_DATE}; path=${path}; domain=${domain};`;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return entries;
|
|
142
112
|
}
|