@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.
Files changed (130) hide show
  1. package/.agents/references/components/ScrolltoTop.md +36 -0
  2. package/.agents/references/components/SwitchCase.md +47 -0
  3. package/.agents/references/components/ViewportPortal.md +34 -0
  4. package/.agents/references/cookie/cookie.md +47 -0
  5. package/.agents/references/datetime/dateTime.client.md +42 -0
  6. package/.agents/references/datetime/dateTime.server.md +42 -0
  7. package/.agents/references/datetime/dateTime.shared.md +86 -0
  8. package/.agents/references/hooks/useAvoidKeyboard.md +29 -0
  9. package/.agents/references/hooks/useCheckInvisible.md +36 -0
  10. package/.agents/references/hooks/useCheckScroll.md +36 -0
  11. package/.agents/references/hooks/useClientDateTime.md +39 -0
  12. package/.agents/references/hooks/useDebounce.md +46 -0
  13. package/.agents/references/hooks/useDebouncedCallback.md +34 -0
  14. package/.agents/references/hooks/useDoubleClick.md +40 -0
  15. package/.agents/references/hooks/useGeolocation.md +37 -0
  16. package/.agents/references/hooks/useHasMounted.md +27 -0
  17. package/.agents/references/hooks/useIntersectionObserver.md +41 -0
  18. package/.agents/references/hooks/useInterval.md +44 -0
  19. package/.agents/references/hooks/useKeyboardHeight.md +33 -0
  20. package/.agents/references/hooks/useKeyboardListNavigation.md +58 -0
  21. package/.agents/references/hooks/useLongPress.md +48 -0
  22. package/.agents/references/hooks/usePreservedCallback.md +37 -0
  23. package/.agents/references/hooks/usePreservedReference.md +34 -0
  24. package/.agents/references/hooks/useRefEffect.md +35 -0
  25. package/.agents/references/hooks/useRelativeDateTime.md +35 -0
  26. package/.agents/references/hooks/useTimeout.md +42 -0
  27. package/.agents/references/hooks/useToggleState.md +39 -0
  28. package/.agents/references/hooks/useViewportHeight.md +26 -0
  29. package/.agents/references/hooks/useViewportMatch.md +32 -0
  30. package/.agents/references/utils/browserStorage.md +41 -0
  31. package/.agents/references/utils/buildContext.md +47 -0
  32. package/.agents/references/utils/clipboardShare.md +59 -0
  33. package/.agents/references/utils/floatingMotion.md +67 -0
  34. package/.agents/references/utils/getDeviceInfo.md +43 -0
  35. package/.agents/references/utils/isEditableKeyboardTarget.md +35 -0
  36. package/.agents/references/utils/mergeRefs.md +29 -0
  37. package/.agents/references/utils/seen.md +37 -0
  38. package/.agents/references/utils/subscribeKeyboardHeight.md +35 -0
  39. package/.github/ISSUE_TEMPLATE/bug_report.md +31 -0
  40. package/.github/ISSUE_TEMPLATE/feature_request.md +22 -0
  41. package/.github/PULL_REQUEST_TEMPLATE.md +22 -0
  42. package/.github/workflows/ci.yml +20 -0
  43. package/.local/state/gh/device-id +1 -0
  44. package/.prettierrc +8 -0
  45. package/AGENTS.md +117 -396
  46. package/CODE_OF_CONDUCT.md +55 -0
  47. package/CONTRIBUTING.md +39 -0
  48. package/README.md +18 -5
  49. package/biome.json +25 -0
  50. package/index.ts +63 -68
  51. package/package.json +46 -13
  52. package/packages/components/ScrolltoTop.tsx +3 -3
  53. package/packages/components/SwitchCase.tsx +7 -7
  54. package/packages/components/ViewportPortal.tsx +22 -23
  55. package/packages/cookie/cookie.shared.ts +80 -110
  56. package/packages/cookie/cookieNames.shared.ts +9 -0
  57. package/packages/datetime/dateTime.client.ts +84 -86
  58. package/packages/datetime/dateTime.server.ts +84 -86
  59. package/packages/datetime/dateTime.shared.ts +249 -287
  60. package/packages/hooks/useAvoidKeyboard.ts +15 -15
  61. package/packages/hooks/useCheckInvisible.ts +16 -16
  62. package/packages/hooks/useCheckScroll.ts +10 -10
  63. package/packages/hooks/useClientDateTime.ts +24 -37
  64. package/packages/hooks/useDebounce.ts +90 -95
  65. package/packages/hooks/useDebouncedCallback.ts +45 -45
  66. package/packages/hooks/useDoubleClick.ts +43 -43
  67. package/packages/hooks/useGeolocation.ts +126 -130
  68. package/packages/hooks/useHasMounted.ts +5 -5
  69. package/packages/hooks/useIntersectionObserver.ts +20 -20
  70. package/packages/hooks/useInterval.ts +45 -45
  71. package/packages/hooks/useKeyboardHeight.ts +9 -9
  72. package/packages/hooks/useKeyboardListNavigation.ts +130 -158
  73. package/packages/hooks/useLongPress.ts +87 -98
  74. package/packages/hooks/usePreservedCallback.ts +12 -12
  75. package/packages/hooks/usePreservedReference.ts +10 -10
  76. package/packages/hooks/useRefEffect.ts +19 -19
  77. package/packages/hooks/useRelativeDateTime.ts +36 -39
  78. package/packages/hooks/useTimeout.ts +41 -41
  79. package/packages/hooks/useToggleState.ts +5 -5
  80. package/packages/hooks/useViewportHeight.ts +12 -13
  81. package/packages/hooks/useViewportMatch.ts +13 -23
  82. package/packages/utils/browserStorage.ts +26 -26
  83. package/packages/utils/buildContext.tsx +10 -11
  84. package/packages/utils/checkDevice.ts +64 -64
  85. package/packages/utils/clipboardShare.tsx +31 -31
  86. package/packages/utils/clipboardShare.types.ts +7 -7
  87. package/packages/utils/floatingMotion.ts +71 -77
  88. package/packages/utils/keyboardTarget.ts +9 -9
  89. package/packages/utils/mergeRefs.ts +9 -9
  90. package/packages/utils/seen.ts +15 -15
  91. package/packages/utils/subscribeKeyboardHeight.ts +49 -46
  92. package/tests/components/ScrolltoTop.test.tsx +15 -0
  93. package/tests/components/SwitchCase.test.tsx +33 -0
  94. package/tests/components/ViewportPortal.test.tsx +46 -0
  95. package/tests/cookie/cookie.test.ts +192 -0
  96. package/tests/datetime/datetime.test.ts +461 -0
  97. package/tests/hooks/useAvoidKeyboard.test.ts +43 -0
  98. package/tests/hooks/useCheckInvisible.test.ts +59 -0
  99. package/tests/hooks/useCheckScroll.test.ts +23 -0
  100. package/tests/hooks/useClientDateTime.test.ts +12 -0
  101. package/tests/hooks/useDebounce.test.ts +96 -0
  102. package/tests/hooks/useDebouncedCallback.test.ts +63 -0
  103. package/tests/hooks/useDoubleClick.test.ts +82 -0
  104. package/tests/hooks/useGeolocation.test.ts +201 -0
  105. package/tests/hooks/useHasMounted.test.ts +10 -0
  106. package/tests/hooks/useIntersectionObserver.test.ts +78 -0
  107. package/tests/hooks/useInterval.test.ts +56 -0
  108. package/tests/hooks/useKeyboardHeight.test.ts +10 -0
  109. package/tests/hooks/useKeyboardListNavigation.test.ts +404 -0
  110. package/tests/hooks/useLongPress.test.ts +214 -0
  111. package/tests/hooks/usePreservedCallback.test.ts +33 -0
  112. package/tests/hooks/usePreservedReference.test.ts +42 -0
  113. package/tests/hooks/useRefEffect.test.ts +67 -0
  114. package/tests/hooks/useRelativeDateTime.test.ts +21 -0
  115. package/tests/hooks/useTimeout.test.ts +72 -0
  116. package/tests/hooks/useToggleState.test.ts +43 -0
  117. package/tests/hooks/useViewportHeight.test.ts +32 -0
  118. package/tests/hooks/useViewportMatch.test.ts +35 -0
  119. package/tests/setup.ts +9 -0
  120. package/tests/utils/browserStorage.test.ts +114 -0
  121. package/tests/utils/buildContext.test.tsx +26 -0
  122. package/tests/utils/checkDevice.test.ts +140 -0
  123. package/tests/utils/clipboardShare.test.ts +152 -0
  124. package/tests/utils/floatingMotion.test.ts +135 -0
  125. package/tests/utils/keyboardTarget.test.ts +85 -0
  126. package/tests/utils/mergeRefs.test.ts +50 -0
  127. package/tests/utils/seen.test.ts +40 -0
  128. package/tests/utils/subscribeKeyboardHeight.test.ts +172 -0
  129. package/tsconfig.json +16 -16
  130. package/vitest.config.ts +22 -0
@@ -0,0 +1,67 @@
1
+ # floatingMotion
2
+
3
+ ## Purpose
4
+
5
+ Provides animation presets and transform utilities for floating UI elements: tooltips, popovers, modals, and mobile sheets. Calculates `initial`, `animate`, and `exit` states for Framer Motion.
6
+
7
+ ## Usage Logic
8
+
9
+ **`getFloatingMotionPreset(mode, placement?)`**: Returns a complete Framer Motion preset object based on the mode and optional placement.
10
+
11
+ **`getFloatingTransformOrigin(placement)`**: Returns the correct `transformOrigin` string for a given placement.
12
+ **`getFloatingHiddenTransform(placement)`**: Returns the hidden-state transform offset for a given placement.
13
+
14
+ ### Modes
15
+ | Mode | Description | Animation |
16
+ |------|-------------|-----------|
17
+ | `anchored` | Tooltips, popovers | Scale + fade from placement origin |
18
+ | `center-selected` | Center-positioned selection | Scale in place |
19
+ | `modal-center` | Modal windows | Scale + fade in center |
20
+ | `mobile-sheet` | Bottom sheets | Slide up from bottom |
21
+
22
+ ## Type Signatures
23
+
24
+ ```ts
25
+ type FloatingMotionMode = "anchored" | "center-selected" | "modal-center" | "mobile-sheet";
26
+ type FloatingPlacement = "top" | "bottom" | "left" | "right" | "top-start" | "top-end" | ...;
27
+ type FloatingMotionPreset = {
28
+ initial: Variants;
29
+ animate: Variants;
30
+ exit: Variants;
31
+ transition: Transition;
32
+ };
33
+
34
+ function getFloatingMotionPreset(
35
+ mode: FloatingMotionMode,
36
+ placement?: FloatingPlacement
37
+ ): FloatingMotionPreset;
38
+
39
+ function getFloatingTransformOrigin(placement: FloatingPlacement): string;
40
+ function getFloatingHiddenTransform(placement: FloatingPlacement): { x?: number; y?: number };
41
+ ```
42
+
43
+ ## Example Code
44
+
45
+ ```tsx
46
+ import { getFloatingMotionPreset } from "@musecat/functionkit";
47
+ import { motion, AnimatePresence } from "framer-motion";
48
+
49
+ function Tooltip({ open, children }: { open: boolean; children: ReactNode }) {
50
+ const preset = getFloatingMotionPreset("anchored", "top");
51
+
52
+ return (
53
+ <AnimatePresence>
54
+ {open && (
55
+ <motion.div
56
+ variants={preset}
57
+ initial="initial"
58
+ animate="animate"
59
+ exit="exit"
60
+ >
61
+ {children}
62
+ </motion.div>
63
+ )}
64
+ </AnimatePresence>
65
+ );
66
+ }
67
+ ```
@@ -0,0 +1,43 @@
1
+ # getDeviceInfo (checkDevice)
2
+
3
+ ## Purpose
4
+
5
+ Parses the User-Agent string to detect the user's device type, operating system, and browser. Callable at module scope — no `window` required.
6
+
7
+ ## Usage Logic
8
+
9
+ Analyzes `navigator.userAgent` with a series of regex checks. Returns a `DeviceInfo` object with boolean flags. SSR-safe — can be called in Server Components, RSC, or module initialization.
10
+
11
+ ## Type Signatures
12
+
13
+ ```ts
14
+ interface DeviceInfo {
15
+ isMobile: boolean;
16
+ isIOS: boolean;
17
+ isAndroid: boolean;
18
+ isSafari: boolean;
19
+ isIOSSafari: boolean;
20
+ isMacSafari: boolean;
21
+ isSamsungBrowser: boolean;
22
+ isTouchDevice: boolean;
23
+ browser: string | null;
24
+ }
25
+
26
+ function getDeviceInfo(): DeviceInfo;
27
+ ```
28
+
29
+ ## Example Code
30
+
31
+ ```tsx
32
+ import { getDeviceInfo } from "@musecat/functionkit";
33
+
34
+ const { isMobile, isIOS, isIOSSafari } = getDeviceInfo();
35
+
36
+ // Call at module scope
37
+ const platform = isIOS ? "ios" : isAndroid ? "android" : "web";
38
+
39
+ export function detectBrowser() {
40
+ const { isSafari, browser } = getDeviceInfo();
41
+ return { isSafari, browser };
42
+ }
43
+ ```
@@ -0,0 +1,35 @@
1
+ # isEditableKeyboardTarget (keyboardTarget)
2
+
3
+ ## Purpose
4
+
5
+ Checks if a keyboard event target is an editable element (`input`, `textarea`, `select`, or `contentEditable`). Used to prevent keyboard navigation hooks from interfering with native input editing.
6
+
7
+ ## Usage Logic
8
+
9
+ Examines the target element's tag name and `isContentEditable` property. Excludes buttons, checkboxes, and radio inputs from the editable set.
10
+
11
+ ## Type Signature
12
+
13
+ ```ts
14
+ function isEditableKeyboardTarget(target: EventTarget | null): boolean;
15
+ ```
16
+
17
+ ## Example Code
18
+
19
+ ```tsx
20
+ import { isEditableKeyboardTarget, useKeyboardListNavigation } from "@musecat/functionkit";
21
+
22
+ function SearchableList({ items }: { items: string[] }) {
23
+ const { handleListKeyDown } = useKeyboardListNavigation({
24
+ itemCount: items.length,
25
+ onSelect: (i) => console.log(items[i]),
26
+ });
27
+
28
+ const onKeyDown = (e: React.KeyboardEvent) => {
29
+ if (isEditableKeyboardTarget(e.target)) return; // Don't steal from inputs
30
+ handleListKeyDown(e);
31
+ };
32
+
33
+ return <ul onKeyDown={onKeyDown}>...</ul>;
34
+ }
35
+ ```
@@ -0,0 +1,29 @@
1
+ # mergeRefs
2
+
3
+ ## Purpose
4
+
5
+ Combines multiple React refs (callback refs and/or object refs) into a single callback ref. Useful when forwarding refs from parent components while keeping internal refs.
6
+
7
+ ## Usage Logic
8
+
9
+ Returns a callback ref that, when called with a DOM node, sets `ref.current` on all object refs and invokes all callback refs with the node. Filters out `undefined` refs.
10
+
11
+ ## Type Signature
12
+
13
+ ```ts
14
+ function mergeRefs<T>(
15
+ ...refs: (React.Ref<T> | undefined)[]
16
+ ): (node: T) => void;
17
+ ```
18
+
19
+ ## Example Code
20
+
21
+ ```tsx
22
+ import { mergeRefs } from "@musecat/functionkit";
23
+ import { forwardRef, useRef } from "react";
24
+
25
+ const Input = forwardRef<HTMLInputElement, Props>((props, ref) => {
26
+ const internalRef = useRef<HTMLInputElement>(null);
27
+ return <input ref={mergeRefs(ref, internalRef)} {...props} />;
28
+ });
29
+ ```
@@ -0,0 +1,37 @@
1
+ # seen
2
+
3
+ ## Purpose
4
+
5
+ Manages a "user has already seen this" state as a JSON string. Useful for marking items as read, dismissing onboarding prompts, or tracking feature discovery without server state.
6
+
7
+ ## Usage Logic
8
+
9
+ Stores an array of string keys serialized as JSON. `parseSeen` deserializes, `hasSeenKey` checks a specific key, and `buildSeenValue` appends a new key and returns the updated JSON string. SSR-safe — pure functions with no side effects.
10
+
11
+ ## Type Signatures
12
+
13
+ ```ts
14
+ const SEEN_STORAGE_KEY = "seen";
15
+
16
+ function parseSeen(seen: string | null): string[];
17
+ function hasSeenKey(seen: string | null, key: string): boolean;
18
+ function buildSeenValue(prev: string | null, key: string): string;
19
+ ```
20
+
21
+ ## Example Code
22
+
23
+ ```tsx
24
+ import { hasSeenKey, buildSeenValue, SEEN_STORAGE_KEY } from "@musecat/functionkit";
25
+ import { getLocalStorage, updateLocalStorage } from "@musecat/functionkit";
26
+
27
+ function useOnboarding() {
28
+ const stored = getLocalStorage(SEEN_STORAGE_KEY);
29
+ const hasSeenWalkthrough = hasSeenKey(stored, "walkthrough-v1");
30
+
31
+ const dismissWalkthrough = () => {
32
+ updateLocalStorage(SEEN_STORAGE_KEY, buildSeenValue(stored, "walkthrough-v1"));
33
+ };
34
+
35
+ return { hasSeenWalkthrough, dismissWalkthrough };
36
+ }
37
+ ```
@@ -0,0 +1,35 @@
1
+ # subscribeKeyboardHeight
2
+
3
+ ## Purpose
4
+
5
+ A low-level utility that subscribes to `visualViewport` resize/scroll events and reports the estimated keyboard height. Used internally by `useKeyboardHeight`.
6
+
7
+ ## Usage Logic
8
+
9
+ Attaches `resize` and `scroll` event listeners to `window.visualViewport`. Computes keyboard height as `window.innerHeight - visualViewport.height`. Throttles callbacks at the specified interval (default 16ms). Returns an unsubscribe function.
10
+
11
+ ## Type Signature
12
+
13
+ ```ts
14
+ function subscribeKeyboardHeight(
15
+ handler: (height: number) => void,
16
+ throttleMs?: number
17
+ ): { unsubscribe: () => void };
18
+ ```
19
+
20
+ ## Example Code
21
+
22
+ ```tsx
23
+ import { subscribeKeyboardHeight } from "@musecat/functionkit";
24
+ import { useEffect } from "react";
25
+
26
+ function useCustomKeyboardHandler() {
27
+ useEffect(() => {
28
+ const { unsubscribe } = subscribeKeyboardHeight(
29
+ (height) => console.log("Keyboard height:", height),
30
+ 100
31
+ );
32
+ return unsubscribe;
33
+ }, []);
34
+ }
35
+ ```
@@ -0,0 +1,31 @@
1
+ ---
2
+ name: Bug report
3
+ about: Report a bug to help us improve
4
+ title: "[BUG] "
5
+ labels: bug
6
+ ---
7
+
8
+ ## Describe the Bug
9
+
10
+ A clear and concise description of what the bug is.
11
+
12
+ ## To Reproduce
13
+
14
+ Steps to reproduce the behavior:
15
+ 1. Import '...'
16
+ 2. Call '...'
17
+ 3. See error
18
+
19
+ ## Expected Behavior
20
+
21
+ A clear and concise description of what you expected to happen.
22
+
23
+ ## Environment
24
+
25
+ - React version:
26
+ - @musecat/functionkit version:
27
+ - Browser / Node version:
28
+
29
+ ## Additional Context
30
+
31
+ Add any other context about the problem here.
@@ -0,0 +1,22 @@
1
+ ---
2
+ name: Feature request
3
+ about: Suggest an idea for this project
4
+ title: "[FEATURE] "
5
+ labels: enhancement
6
+ ---
7
+
8
+ ## Problem Statement
9
+
10
+ Is your feature request related to a problem? Please describe clearly.
11
+
12
+ ## Proposed Solution
13
+
14
+ A clear and concise description of what you want to happen.
15
+
16
+ ## Alternative Solutions
17
+
18
+ A description of any alternative solutions or features you've considered.
19
+
20
+ ## Additional Context
21
+
22
+ Add any other context or examples about the feature request here.
@@ -0,0 +1,22 @@
1
+ ## Description
2
+
3
+ Please include a summary of the change and which issue is fixed.
4
+
5
+ ## Type of Change
6
+
7
+ - [ ] Bug fix
8
+ - [ ] New feature
9
+ - [ ] Breaking change
10
+ - [ ] Documentation update
11
+
12
+ ## How Has This Been Tested?
13
+
14
+ Please describe the tests that you ran to verify your changes.
15
+
16
+ ## Checklist
17
+
18
+ - [ ] My code follows the project's code style
19
+ - [ ] I have added tests that prove my fix/feature works
20
+ - [ ] `npm run lint` passes
21
+ - [ ] `npm test` passes
22
+ - [ ] Barrel exports (`index.ts`) are updated if needed
@@ -0,0 +1,20 @@
1
+ name: CI
2
+
3
+ on:
4
+ push:
5
+ branches: [master]
6
+ pull_request:
7
+ branches: [master]
8
+
9
+ jobs:
10
+ quality:
11
+ runs-on: ubuntu-latest
12
+ steps:
13
+ - uses: actions/checkout@v4
14
+ - uses: actions/setup-node@v4
15
+ with:
16
+ node-version: 22
17
+ cache: npm
18
+ - run: npm ci
19
+ - run: npm run lint
20
+ - run: npm test
@@ -0,0 +1 @@
1
+ f1f55548-1baa-4638-bd0f-f3da5e3262f0
package/.prettierrc ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "useTabs": true,
3
+ "tabWidth": 2,
4
+ "printWidth": 100,
5
+ "singleQuote": false,
6
+ "semi": true,
7
+ "trailingComma": "all"
8
+ }