@reshaped/headless 3.10.0-canary.10

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 (60) hide show
  1. package/LICENSE.md +21 -0
  2. package/dist/components/Actionable/Actionable.d.ts +4 -0
  3. package/dist/components/Actionable/Actionable.js +72 -0
  4. package/dist/components/Actionable/Actionable.types.d.ts +35 -0
  5. package/dist/components/Actionable/Actionable.types.js +1 -0
  6. package/dist/components/Actionable/index.d.ts +2 -0
  7. package/dist/components/Actionable/index.js +1 -0
  8. package/dist/components/Reshaped/Reshaped.d.ts +4 -0
  9. package/dist/components/Reshaped/Reshaped.js +11 -0
  10. package/dist/components/Reshaped/Reshaped.types.d.ts +5 -0
  11. package/dist/components/Reshaped/Reshaped.types.js +1 -0
  12. package/dist/components/Reshaped/index.d.ts +2 -0
  13. package/dist/components/Reshaped/index.js +1 -0
  14. package/dist/hooks/_internal/useSingletonHotkeys.d.ts +31 -0
  15. package/dist/hooks/_internal/useSingletonHotkeys.js +191 -0
  16. package/dist/hooks/_internal/useSingletonKeyboardMode.d.ts +13 -0
  17. package/dist/hooks/_internal/useSingletonKeyboardMode.js +59 -0
  18. package/dist/hooks/_internal/useSingletonRTL.d.ts +6 -0
  19. package/dist/hooks/_internal/useSingletonRTL.js +40 -0
  20. package/dist/hooks/tests/useHandlerRef.stories.d.ts +14 -0
  21. package/dist/hooks/tests/useHandlerRef.stories.js +40 -0
  22. package/dist/hooks/tests/useHotkeys.stories.d.ts +43 -0
  23. package/dist/hooks/tests/useHotkeys.stories.js +165 -0
  24. package/dist/hooks/tests/useKeyboardArrowNavigation.stories.d.ts +15 -0
  25. package/dist/hooks/tests/useKeyboardArrowNavigation.stories.js +107 -0
  26. package/dist/hooks/tests/useKeyboardMode.stories.d.ts +11 -0
  27. package/dist/hooks/tests/useKeyboardMode.stories.js +36 -0
  28. package/dist/hooks/tests/useOnClickOutside.stories.d.ts +23 -0
  29. package/dist/hooks/tests/useOnClickOutside.stories.js +98 -0
  30. package/dist/hooks/tests/useRTL.stories.d.ts +11 -0
  31. package/dist/hooks/tests/useRTL.stories.js +24 -0
  32. package/dist/hooks/tests/useScrollLock.stories.d.ts +14 -0
  33. package/dist/hooks/tests/useScrollLock.stories.js +75 -0
  34. package/dist/hooks/tests/useToggle.stories.d.ts +13 -0
  35. package/dist/hooks/tests/useToggle.stories.js +50 -0
  36. package/dist/hooks/useHandlerRef.d.ts +8 -0
  37. package/dist/hooks/useHandlerRef.js +16 -0
  38. package/dist/hooks/useHotkeys.d.ts +11 -0
  39. package/dist/hooks/useHotkeys.js +27 -0
  40. package/dist/hooks/useIsomorphicLayoutEffect.d.ts +3 -0
  41. package/dist/hooks/useIsomorphicLayoutEffect.js +4 -0
  42. package/dist/hooks/useKeyboardArrowNavigation.d.ts +9 -0
  43. package/dist/hooks/useKeyboardArrowNavigation.js +62 -0
  44. package/dist/hooks/useKeyboardMode.d.ts +2 -0
  45. package/dist/hooks/useKeyboardMode.js +2 -0
  46. package/dist/hooks/useOnClickOutside.d.ts +5 -0
  47. package/dist/hooks/useOnClickOutside.js +63 -0
  48. package/dist/hooks/useRTL.d.ts +2 -0
  49. package/dist/hooks/useRTL.js +2 -0
  50. package/dist/hooks/useScrollLock.d.ts +10 -0
  51. package/dist/hooks/useScrollLock.js +25 -0
  52. package/dist/hooks/useToggle.d.ts +7 -0
  53. package/dist/hooks/useToggle.js +19 -0
  54. package/dist/index.d.ts +14 -0
  55. package/dist/index.js +15 -0
  56. package/dist/internal.d.ts +8 -0
  57. package/dist/internal.js +8 -0
  58. package/dist/types/global.d.ts +7 -0
  59. package/dist/types/global.js +1 -0
  60. package/package.json +49 -0
@@ -0,0 +1,16 @@
1
+ import React from "react";
2
+ import useIsomorphicLayoutEffect from "./useIsomorphicLayoutEffect.js";
3
+ /**
4
+ * Hook for wrapping event handlers passed as props with a ref
5
+ * This way we can keep the instance of the ref the same and pass this ref to the effects dependency array
6
+ * While also making sure that function implementation stays up-to-date
7
+ */
8
+ const useHandlerRef = (cb) => {
9
+ const ref = React.useRef(cb);
10
+ // Update the callback on every render, keeping the ref instance the same
11
+ useIsomorphicLayoutEffect(() => {
12
+ ref.current = cb;
13
+ }, [cb]);
14
+ return ref;
15
+ };
16
+ export default useHandlerRef;
@@ -0,0 +1,11 @@
1
+ import React from "react";
2
+ import { type Hotkeys } from "./_internal/useSingletonHotkeys";
3
+ declare const useHotkeys: <Element extends HTMLElement>(hotkeys: Hotkeys, deps?: unknown[], options?: {
4
+ ref?: React.RefObject<Element | null>;
5
+ disabled?: boolean;
6
+ preventDefault?: boolean;
7
+ }) => {
8
+ ref: React.RefObject<Element | null>;
9
+ checkHotkeyState: (key: string) => boolean;
10
+ };
11
+ export default useHotkeys;
@@ -0,0 +1,27 @@
1
+ "use client";
2
+ import React from "react";
3
+ import { useSingletonHotkeys } from "./_internal/useSingletonHotkeys.js";
4
+ const useHotkeys = (hotkeys, deps = [], options) => {
5
+ const { addHotkeys, isPressed } = useSingletonHotkeys();
6
+ const generatedRef = React.useRef(null);
7
+ const elementRef = options?.ref || generatedRef;
8
+ React.useEffect(() => {
9
+ if (options?.disabled)
10
+ return;
11
+ const remove = addHotkeys(hotkeys, elementRef, {
12
+ preventDefault: options?.preventDefault,
13
+ });
14
+ return () => remove?.();
15
+ // eslint-disable-next-line react-hooks/exhaustive-deps
16
+ }, [
17
+ addHotkeys,
18
+ // eslint-disable-next-line react-hooks/exhaustive-deps
19
+ Object.keys(hotkeys).join(","),
20
+ options?.disabled,
21
+ options?.preventDefault,
22
+ // eslint-disable-next-line react-hooks/exhaustive-deps
23
+ ...deps,
24
+ ]);
25
+ return { ref: elementRef, checkHotkeyState: isPressed };
26
+ };
27
+ export default useHotkeys;
@@ -0,0 +1,3 @@
1
+ import React from "react";
2
+ declare const useIsomorphicLayoutEffect: typeof React.useEffect;
3
+ export default useIsomorphicLayoutEffect;
@@ -0,0 +1,4 @@
1
+ "use client";
2
+ import React from "react";
3
+ const useIsomorphicLayoutEffect = typeof window !== "undefined" ? React.useLayoutEffect : React.useEffect;
4
+ export default useIsomorphicLayoutEffect;
@@ -0,0 +1,9 @@
1
+ import React from "react";
2
+ type Props = {
3
+ ref: React.RefObject<HTMLElement | null>;
4
+ disabled?: boolean;
5
+ orientation?: "vertical" | "horizontal";
6
+ circular?: boolean;
7
+ };
8
+ declare const useKeyboardArrowNavigation: (props: Props) => void;
9
+ export default useKeyboardArrowNavigation;
@@ -0,0 +1,62 @@
1
+ import { focusFirstElement, focusLastElement, focusNextElement, focusPreviousElement, getFocusableElements, } from "@reshaped/utilities/internal";
2
+ import React, { useEffect } from "react";
3
+ import useHotkeys from "./useHotkeys.js";
4
+ const useKeyboardArrowNavigation = (props) => {
5
+ const { ref, disabled, orientation, circular } = props;
6
+ const backHotkeys = [];
7
+ const forwardHotkeys = [];
8
+ if (!orientation || orientation === "vertical") {
9
+ backHotkeys.push("ArrowUp");
10
+ forwardHotkeys.push("ArrowDown");
11
+ }
12
+ if (!orientation || orientation === "horizontal") {
13
+ backHotkeys.push("ArrowLeft");
14
+ forwardHotkeys.push("ArrowRight");
15
+ }
16
+ const updateTabIndex = React.useCallback((options) => {
17
+ const { el, focusableElements } = options;
18
+ const initialEl = focusableElements.find((el) => el.getAttribute("tabindex") !== "-1");
19
+ const activeEl = el ?? initialEl ?? focusableElements[0];
20
+ focusableElements.forEach((el) => el.setAttribute("tabindex", "-1"));
21
+ activeEl?.setAttribute("tabindex", "0");
22
+ }, []);
23
+ useEffect(() => {
24
+ if (!ref.current)
25
+ return;
26
+ if (disabled)
27
+ return;
28
+ const focusableElements = getFocusableElements(ref.current);
29
+ updateTabIndex({ focusableElements });
30
+ }, [ref, updateTabIndex, disabled]);
31
+ useHotkeys({
32
+ [backHotkeys.join(", ")]: () => {
33
+ if (!ref.current)
34
+ return;
35
+ const data = focusPreviousElement(ref.current, { circular });
36
+ updateTabIndex(data);
37
+ },
38
+ [forwardHotkeys.join(", ")]: () => {
39
+ if (!ref.current)
40
+ return;
41
+ const data = focusNextElement(ref.current, { circular });
42
+ updateTabIndex(data);
43
+ },
44
+ Home: () => {
45
+ if (!ref.current)
46
+ return;
47
+ const data = focusFirstElement(ref.current);
48
+ updateTabIndex(data);
49
+ },
50
+ End: () => {
51
+ if (!ref.current)
52
+ return;
53
+ const data = focusLastElement(ref.current);
54
+ updateTabIndex(data);
55
+ },
56
+ }, [updateTabIndex, circular], {
57
+ ref,
58
+ preventDefault: true,
59
+ disabled,
60
+ });
61
+ };
62
+ export default useKeyboardArrowNavigation;
@@ -0,0 +1,2 @@
1
+ import { useSingletonKeyboardMode } from "./_internal/useSingletonKeyboardMode";
2
+ export default useSingletonKeyboardMode;
@@ -0,0 +1,2 @@
1
+ import { useSingletonKeyboardMode } from "./_internal/useSingletonKeyboardMode.js";
2
+ export default useSingletonKeyboardMode;
@@ -0,0 +1,5 @@
1
+ import React from "react";
2
+ declare const useOnClickOutside: (refs: React.RefObject<HTMLElement | null>[], handler: (event: Event) => void, options?: {
3
+ disabled?: boolean;
4
+ }) => void;
5
+ export default useOnClickOutside;
@@ -0,0 +1,63 @@
1
+ import { keys } from "@reshaped/utilities";
2
+ import React from "react";
3
+ import useHandlerRef from "./useHandlerRef.js";
4
+ const useOnClickOutside = (refs, handler, options) => {
5
+ const { disabled } = options || {};
6
+ const handlerRef = useHandlerRef(handler);
7
+ /**
8
+ * We're checking the element position in the DOM on mousedown to make sure
9
+ * it happens before any other click events that could potentially remove the clicked el
10
+ * before we checked if it's inside the refs
11
+ */
12
+ const isMouseDownInsideRef = React.useRef(false);
13
+ React.useEffect(() => {
14
+ /**
15
+ * Not checking for disabled here since some components can enable the hook
16
+ * after it was clicked
17
+ */
18
+ const handleMouseDown = (event) => {
19
+ isMouseDownInsideRef.current = false;
20
+ const clickedEl = event.composedPath()[0];
21
+ refs.forEach((elRef) => {
22
+ if (!elRef.current)
23
+ return;
24
+ if (elRef.current === clickedEl || elRef.current.contains(clickedEl)) {
25
+ isMouseDownInsideRef.current = true;
26
+ }
27
+ });
28
+ };
29
+ const handleKeyDown = (event) => {
30
+ if (![keys.ENTER, keys.SPACE].includes(event.key))
31
+ return;
32
+ handleMouseDown(event);
33
+ };
34
+ document.addEventListener("mousedown", handleMouseDown, { passive: true });
35
+ document.addEventListener("touchstart", handleMouseDown, { passive: true });
36
+ document.addEventListener("keydown", handleKeyDown, { passive: true });
37
+ return () => {
38
+ document.removeEventListener("mousedown", handleMouseDown);
39
+ document.removeEventListener("touchstart", handleMouseDown);
40
+ document.removeEventListener("keydown", handleKeyDown);
41
+ };
42
+ // eslint-disable-next-line react-hooks/exhaustive-deps
43
+ }, [...refs]);
44
+ React.useEffect(() => {
45
+ if (!handlerRef.current)
46
+ return;
47
+ if (disabled)
48
+ return;
49
+ const handleClick = (event) => {
50
+ if ("button" in event && event.button === 2)
51
+ return;
52
+ if (isMouseDownInsideRef.current)
53
+ return;
54
+ handlerRef.current?.(event);
55
+ };
56
+ document.addEventListener("click", handleClick);
57
+ return () => {
58
+ document.removeEventListener("click", handleClick);
59
+ };
60
+ // eslint-disable-next-line react-hooks/exhaustive-deps
61
+ }, [handlerRef, disabled, ...refs]);
62
+ };
63
+ export default useOnClickOutside;
@@ -0,0 +1,2 @@
1
+ import { useSingletonRTL } from "./_internal/useSingletonRTL";
2
+ export default useSingletonRTL;
@@ -0,0 +1,2 @@
1
+ import { useSingletonRTL } from "./_internal/useSingletonRTL.js";
2
+ export default useSingletonRTL;
@@ -0,0 +1,10 @@
1
+ import React from "react";
2
+ declare const useScrollLock: (options?: {
3
+ containerRef?: React.RefObject<HTMLElement | null>;
4
+ originRef?: React.RefObject<HTMLElement | null>;
5
+ }) => {
6
+ scrollLocked: boolean;
7
+ lockScroll: () => void;
8
+ unlockScroll: () => void;
9
+ };
10
+ export default useScrollLock;
@@ -0,0 +1,25 @@
1
+ "use client";
2
+ import { lockScroll } from "@reshaped/utilities";
3
+ import React from "react";
4
+ const useScrollLock = (options) => {
5
+ const { containerRef, originRef } = options || {};
6
+ const [locked, setLocked] = React.useState(false);
7
+ const unlockScrollRef = React.useRef(null);
8
+ const handleLockScroll = React.useCallback(() => {
9
+ unlockScrollRef.current = lockScroll({
10
+ containerEl: containerRef?.current,
11
+ originEl: originRef?.current,
12
+ callback: () => setLocked(true),
13
+ });
14
+ }, [containerRef, originRef]);
15
+ const handleUnlockScroll = React.useCallback(() => {
16
+ unlockScrollRef.current?.({ callback: () => setLocked(false) });
17
+ unlockScrollRef.current = null;
18
+ }, []);
19
+ return React.useMemo(() => ({
20
+ scrollLocked: locked,
21
+ lockScroll: handleLockScroll,
22
+ unlockScroll: handleUnlockScroll,
23
+ }), [locked, handleLockScroll, handleUnlockScroll]);
24
+ };
25
+ export default useScrollLock;
@@ -0,0 +1,7 @@
1
+ declare const useToggle: (defaultValue?: boolean) => {
2
+ active: boolean;
3
+ activate: () => void;
4
+ deactivate: () => void;
5
+ toggle: (targetValue?: any) => void;
6
+ };
7
+ export default useToggle;
@@ -0,0 +1,19 @@
1
+ "use client";
2
+ import React from "react";
3
+ const useToggle = (defaultValue) => {
4
+ const [active, setActive] = React.useState(defaultValue || false);
5
+ const activate = React.useCallback(() => {
6
+ setActive(true);
7
+ }, []);
8
+ const deactivate = React.useCallback(() => {
9
+ setActive(false);
10
+ }, []);
11
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
12
+ const toggle = React.useCallback((targetValue) => {
13
+ // Checking the targetValue type for backwards compatibility if something like handler events
14
+ // are passed automatically e.g. onClick={toggle}
15
+ setActive(typeof targetValue === "boolean" ? targetValue : (active) => !active);
16
+ }, []);
17
+ return React.useMemo(() => ({ active, activate, deactivate, toggle }), [activate, deactivate, toggle, active]);
18
+ };
19
+ export default useToggle;
@@ -0,0 +1,14 @@
1
+ export { classNames, TrapFocus } from "@reshaped/utilities";
2
+ export { default as Actionable, type ActionableRef, type ActionableProps, } from "./components/Actionable";
3
+ export { default as Reshaped, type ReshapedProps } from "./components/Reshaped";
4
+ export { default as useHandlerRef } from "./hooks/useHandlerRef";
5
+ export { default as useHotkeys } from "./hooks/useHotkeys";
6
+ export { default as useKeyboardArrowNavigation } from "./hooks/useKeyboardArrowNavigation";
7
+ export { default as useKeyboardMode } from "./hooks/useKeyboardMode";
8
+ export { default as useRTL } from "./hooks/useRTL";
9
+ export { default as useIsomorphicLayoutEffect } from "./hooks/useIsomorphicLayoutEffect";
10
+ export { default as useOnClickOutside } from "./hooks/useOnClickOutside";
11
+ export { default as useScrollLock } from "./hooks/useScrollLock";
12
+ export { default as useToggle } from "./hooks/useToggle";
13
+ export type { ClassName } from "@reshaped/utilities";
14
+ export type { Attributes, CSSVariable, StyleAttribute } from "./types/global";
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ // Utilities
2
+ export { classNames, TrapFocus } from "@reshaped/utilities";
3
+ // Components
4
+ export { default as Actionable, } from "./components/Actionable/index.js";
5
+ export { default as Reshaped } from "./components/Reshaped/index.js";
6
+ // Hooks
7
+ export { default as useHandlerRef } from "./hooks/useHandlerRef.js";
8
+ export { default as useHotkeys } from "./hooks/useHotkeys.js";
9
+ export { default as useKeyboardArrowNavigation } from "./hooks/useKeyboardArrowNavigation.js";
10
+ export { default as useKeyboardMode } from "./hooks/useKeyboardMode.js";
11
+ export { default as useRTL } from "./hooks/useRTL.js";
12
+ export { default as useIsomorphicLayoutEffect } from "./hooks/useIsomorphicLayoutEffect.js";
13
+ export { default as useOnClickOutside } from "./hooks/useOnClickOutside.js";
14
+ export { default as useScrollLock } from "./hooks/useScrollLock.js";
15
+ export { default as useToggle } from "./hooks/useToggle.js";
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Internal utilities re-used in other Reshaped components but not meant to be used as a public API
3
+ * Their API is subject to change without a major version bump.
4
+ *
5
+ * If you want to use one of these utilities, open an issue or a PR about moving it to the public API file
6
+ */
7
+ export { Flyout } from "@reshaped/utilities";
8
+ export { disableScroll, enableScroll, rafThrottle, checkKeyboardMode, findParent, getFocusableElements, focusableSelector, type FocusableElement, type TrapMode, type Coordinates, } from "@reshaped/utilities/internal";
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Internal utilities re-used in other Reshaped components but not meant to be used as a public API
3
+ * Their API is subject to change without a major version bump.
4
+ *
5
+ * If you want to use one of these utilities, open an issue or a PR about moving it to the public API file
6
+ */
7
+ export { Flyout } from "@reshaped/utilities";
8
+ export { disableScroll, enableScroll, rafThrottle, checkKeyboardMode, findParent, getFocusableElements, focusableSelector, } from "@reshaped/utilities/internal";
@@ -0,0 +1,7 @@
1
+ export type CSSVariable = `--${string}`;
2
+ export type StyleAttribute = React.CSSProperties | (React.CSSProperties & Record<CSSVariable, string | number | undefined>);
3
+ type DataAttributes = object | Record<`data-${string}`, string | boolean>;
4
+ export type Attributes<TagName extends keyof React.JSX.IntrinsicElements | void = void> = (TagName extends keyof React.JSX.IntrinsicElements ? React.JSX.IntrinsicElements[TagName] : React.HTMLAttributes<HTMLElement>) & DataAttributes & {
5
+ style?: StyleAttribute;
6
+ };
7
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@reshaped/headless",
3
+ "description": "Headless components and utilities for React",
4
+ "version": "3.10.0-canary.10",
5
+ "license": "MIT",
6
+ "homepage": "https://reshaped.so",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/reshaped-ui/reshaped.git",
10
+ "directory": "packages/core"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/reshaped-ui/reshaped/issues"
14
+ },
15
+ "author": {
16
+ "name": "Dmitry Belyaev",
17
+ "email": "blv.dmitry@gmail.com",
18
+ "url": "https://reshaped.so"
19
+ },
20
+ "files": [
21
+ "dist/**/*"
22
+ ],
23
+ "types": "./dist/index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.js",
28
+ "default": "./dist/index.js"
29
+ },
30
+ "./internal": {
31
+ "types": "./dist/internal.d.ts",
32
+ "import": "./dist/internal.js",
33
+ "default": "./dist/internal.js"
34
+ },
35
+ "./package.json": "./package.json"
36
+ },
37
+ "peerDependencies": {
38
+ "react": "^18 || ^19",
39
+ "react-dom": "^18 || ^19"
40
+ },
41
+ "dependencies": {
42
+ "@reshaped/utilities": "3.10.0-canary.10"
43
+ },
44
+ "scripts": {
45
+ "clean": "rm -rf dist",
46
+ "dev": "tsc-watch --onSuccess \"resolve-tspaths\"",
47
+ "build": "tsc && resolve-tspaths"
48
+ }
49
+ }