@musecat/functionkit 1.0.1 → 1.0.2
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 +37 -0
- package/.agents/references/hooks/useInterval.md +40 -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 +33 -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/AGENTS.md +103 -396
- package/README.md +18 -5
- package/index.ts +63 -68
- package/package.json +27 -13
- package/packages/components/ScrolltoTop.tsx +3 -3
- package/packages/components/SwitchCase.tsx +7 -7
- package/packages/components/ViewportPortal.tsx +23 -23
- package/packages/cookie/cookie.shared.ts +97 -101
- package/packages/datetime/dateTime.client.ts +86 -86
- package/packages/datetime/dateTime.server.ts +86 -86
- package/packages/datetime/dateTime.shared.ts +281 -281
- 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 +33 -33
- package/packages/hooks/useDebounce.ts +95 -95
- package/packages/hooks/useDebouncedCallback.ts +45 -45
- package/packages/hooks/useDoubleClick.ts +43 -43
- package/packages/hooks/useGeolocation.ts +130 -130
- package/packages/hooks/useHasMounted.ts +5 -5
- package/packages/hooks/useIntersectionObserver.ts +20 -20
- package/packages/hooks/useInterval.ts +46 -45
- package/packages/hooks/useKeyboardHeight.ts +9 -9
- package/packages/hooks/useKeyboardListNavigation.ts +156 -156
- package/packages/hooks/useLongPress.ts +98 -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 +37 -37
- package/packages/hooks/useTimeout.ts +41 -41
- package/packages/hooks/useToggleState.ts +5 -5
- package/packages/hooks/useViewportHeight.ts +13 -13
- package/packages/hooks/useViewportMatch.ts +16 -22
- package/packages/utils/browserStorage.ts +26 -26
- package/packages/utils/buildContext.tsx +10 -10
- 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 -71
- 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 +46 -46
- package/tests/components/ScrolltoTop.test.tsx +15 -0
- package/tests/components/SwitchCase.test.tsx +34 -0
- package/tests/components/ViewportPortal.test.tsx +42 -0
- package/tests/cookie/cookie.test.ts +156 -0
- package/tests/datetime/datetime.test.ts +250 -0
- package/tests/hooks/useAvoidKeyboard.test.ts +12 -0
- package/tests/hooks/useCheckInvisible.test.ts +28 -0
- package/tests/hooks/useCheckScroll.test.ts +23 -0
- package/tests/hooks/useClientDateTime.test.ts +14 -0
- package/tests/hooks/useDebounce.test.ts +71 -0
- package/tests/hooks/useDebouncedCallback.test.ts +34 -0
- package/tests/hooks/useDoubleClick.test.ts +31 -0
- package/tests/hooks/useGeolocation.test.ts +50 -0
- package/tests/hooks/useHasMounted.test.ts +10 -0
- package/tests/hooks/useIntersectionObserver.test.ts +13 -0
- package/tests/hooks/useInterval.test.ts +31 -0
- package/tests/hooks/useKeyboardHeight.test.ts +10 -0
- package/tests/hooks/useKeyboardListNavigation.test.ts +82 -0
- package/tests/hooks/useLongPress.test.ts +52 -0
- package/tests/hooks/usePreservedCallback.test.ts +35 -0
- package/tests/hooks/usePreservedReference.test.ts +35 -0
- package/tests/hooks/useRefEffect.test.ts +30 -0
- package/tests/hooks/useRelativeDateTime.test.ts +19 -0
- package/tests/hooks/useTimeout.test.ts +34 -0
- package/tests/hooks/useToggleState.test.ts +35 -0
- package/tests/hooks/useViewportHeight.test.ts +10 -0
- package/tests/hooks/useViewportMatch.test.ts +35 -0
- package/tests/setup.ts +9 -0
- package/tests/utils/browserStorage.test.ts +54 -0
- package/tests/utils/buildContext.test.tsx +27 -0
- package/tests/utils/checkDevice.test.ts +84 -0
- package/tests/utils/clipboardShare.test.ts +78 -0
- package/tests/utils/floatingMotion.test.ts +84 -0
- package/tests/utils/keyboardTarget.test.ts +44 -0
- package/tests/utils/mergeRefs.test.ts +18 -0
- package/tests/utils/seen.test.ts +31 -0
- package/tests/utils/subscribeKeyboardHeight.test.ts +50 -0
- package/tsconfig.json +16 -16
- package/vitest.config.ts +16 -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
|
+
```
|