@octanejs/dnd-kit-utilities 0.0.0-stage → 0.0.1

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 (38) hide show
  1. package/LICENSE +21 -0
  2. package/LICENSE.upstream +21 -0
  3. package/README.md +38 -2
  4. package/UPSTREAM.md +55 -0
  5. package/package.json +49 -3
  6. package/src/adjustment.ts +28 -0
  7. package/src/coordinates/getEventCoordinates.ts +34 -0
  8. package/src/coordinates/index.ts +2 -0
  9. package/src/coordinates/types.ts +4 -0
  10. package/src/css.ts +51 -0
  11. package/src/event/hasViewportRelativeCoordinates.ts +5 -0
  12. package/src/event/index.ts +3 -0
  13. package/src/event/isKeyboardEvent.ts +11 -0
  14. package/src/event/isTouchEvent.ts +11 -0
  15. package/src/execution-context/canUseDOM.ts +5 -0
  16. package/src/execution-context/getOwnerDocument.ts +25 -0
  17. package/src/execution-context/getWindow.ts +18 -0
  18. package/src/execution-context/index.ts +3 -0
  19. package/src/focus/findFirstFocusableNode.ts +10 -0
  20. package/src/focus/index.ts +1 -0
  21. package/src/hooks/index.ts +9 -0
  22. package/src/hooks/useCombinedRefs.ts +11 -0
  23. package/src/hooks/useEvent.ts +15 -0
  24. package/src/hooks/useInterval.ts +18 -0
  25. package/src/hooks/useIsomorphicLayoutEffect.ts +9 -0
  26. package/src/hooks/useLatestValue.ts +16 -0
  27. package/src/hooks/useLazyMemo.ts +16 -0
  28. package/src/hooks/useNodeRef.ts +23 -0
  29. package/src/hooks/usePrevious.ts +11 -0
  30. package/src/hooks/useUniqueId.ts +16 -0
  31. package/src/index.ts +22 -0
  32. package/src/type-guards/index.ts +5 -0
  33. package/src/type-guards/isDocument.ts +7 -0
  34. package/src/type-guards/isHTMLElement.ts +11 -0
  35. package/src/type-guards/isNode.ts +3 -0
  36. package/src/type-guards/isSVGElement.ts +5 -0
  37. package/src/type-guards/isWindow.ts +8 -0
  38. package/src/types.ts +11 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Dominic Gannaway
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021, Claudéric Demers
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
1
+ # `@octanejs/dnd-kit-utilities`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Hooks and DOM helpers for the legacy dnd kit API on Octane, ported from
4
+ `@dnd-kit/utilities` 3.2.2: `CSS` transform serializers, `getWindow`,
5
+ `getOwnerDocument`, coordinate math, event and node type guards, and the
6
+ `useCombinedRefs`, `useEvent`, `useNodeRef`, `useUniqueId` and related hooks.
7
+
8
+ ## Installation
9
+
10
+ ```sh
11
+ npm install @octanejs/dnd-kit-utilities octane
12
+ pnpm add @octanejs/dnd-kit-utilities octane
13
+ ```
14
+
15
+ Replace imports from `@dnd-kit/utilities` with `@octanejs/dnd-kit-utilities`;
16
+ [`@octanejs/codemod`](../codemod) rewrites them for you.
17
+
18
+ ## Usage
19
+
20
+ ```ts
21
+ import { CSS, getOwnerDocument } from '@octanejs/dnd-kit-utilities';
22
+
23
+ CSS.Translate.toString({ x: 10, y: 20, scaleX: 1, scaleY: 1 }); // 'translate3d(10px, 20px, 0)'
24
+ getOwnerDocument(element);
25
+ ```
26
+
27
+ ## About this port
28
+
29
+ This package is part of Octane's port of the legacy dnd kit API, the
30
+ `@dnd-kit/core` 6.x family: `@octanejs/dnd-kit-core`, `@octanejs/dnd-kit-sortable`,
31
+ `@octanejs/dnd-kit-modifiers`, `@octanejs/dnd-kit-utilities`, and
32
+ `@octanejs/dnd-kit-accessibility`, one per upstream package. For the modern
33
+ `@dnd-kit/react` API, use [`@octanejs/dnd-kit`](../dnd-kit).
34
+
35
+ Parity is `recorded-unverified`. Upstream's own unit tests are placeholders, so
36
+ each package runs them pristine and adapted, and a repo-authored differential test
37
+ runs the same fixtures against the published React package and compares the DOM
38
+ after every drag step. See [`UPSTREAM.md`](./UPSTREAM.md) for the pin and the
39
+ evidence.
package/UPSTREAM.md ADDED
@@ -0,0 +1,55 @@
1
+ # `@octanejs/dnd-kit-utilities` upstream ledger
2
+
3
+ `@octanejs/dnd-kit-utilities` ports `@dnd-kit/utilities@3.2.2` from
4
+ `https://github.com/clauderic/dnd-kit.git`, the legacy dnd kit API.
5
+
6
+ ## Pin
7
+
8
+ - Package: `@dnd-kit/utilities@3.2.2`, the latest release.
9
+ - Source commit: `e9215e820798459ae036896fce7fd9a6fe855772`, the `@dnd-kit/core@6.3.1` release tag. It holds all
10
+ five legacy packages at their latest releases. Their trees there are
11
+ byte-identical to their own release tags; this package's tag is
12
+ `@dnd-kit/utilities@3.2.2` at `694dcc2f62e5269541fc941fa6c9af46ccd682ad`.
13
+ - npm integrity: `sha512-+MKAJEOfaBe5SmV6t34p80MMKhjvUz0vRrvVJbPT0WElzaOJ/1xs+D+KDv+tD/NE5ujfrChEcshd4fLn0wpiqg==`
14
+ - npm tarball SHA-256: `048c7f56d17ceae19a29979700582566c0036600ee4d2c38ba13e9e8f906a18e`
15
+ - License: MIT. `LICENSE.upstream` is the upstream package's license, byte for byte.
16
+ - React oracle: `react@19.2.7` and `react-dom@19.2.7` from the default catalog.
17
+
18
+ The legacy packages were published without a `gitHead` or npm provenance, so the
19
+ repository's preflight cannot tie the npm artifact to a commit on its own. The
20
+ source is pinned to the release tag instead, and the parity status stays
21
+ `recorded-unverified`.
22
+
23
+ `upstream/` holds the pinned package directory byte for byte. Each file
24
+ is checked against its git blob SHA in `audit/upstream.lock.json`:
25
+
26
+ ```sh
27
+ pnpm react-port:materialize run --check --package-dir packages/dnd-kit-utilities
28
+ ```
29
+
30
+ ## Source boundary
31
+
32
+ `src/` mirrors `upstream/src/` file for file, formatted with the repository's
33
+ Prettier settings, with `react`/`react-dom` imports pointed at `octane` and
34
+ `@dnd-kit/*` imports pointed at the `@octanejs/dnd-kit-*` packages. Files that
35
+ change beyond that:
36
+
37
+ - Every module is copied from `upstream/src/` with its React import pointed at `octane`; no file has a behavioral change.
38
+
39
+ ## Public surface
40
+
41
+ Every runtime export of `@dnd-kit/utilities@3.2.2` is ported under the same name: hooks, `CSS`, coordinate math, event and node type guards, `canUseDOM`, `getWindow`, `getOwnerDocument`, `findFirstFocusableNode`, and the `Arguments`/`DeepRequired`/`FirstArgument`/`Without`/`Coordinates`/`Transform`/`Transition` types.
42
+ `tests/exports.test.ts` compares the export names and value kinds with the published
43
+ package, and `typetests/public-api.test-d.ts` checks the public types.
44
+
45
+ ## Tests
46
+
47
+ | Upstream | Disposition |
48
+ | --- | --- |
49
+ | `src/test/utilities.test.ts (1 placeholder case)` | Runs unchanged against the pinned source and React (`dnd-kit-utilities-pristine`), and regenerated from the lock against the port (`dnd-kit-utilities-upstream`). |
50
+ | Repository Cypress specs (`cypress/integration/draggable_spec.ts`, `sortable_spec.ts`) | Not ported. They drive the Storybook stories outside the package boundary. The differential below covers the same pointer and keyboard drags against the published package. |
51
+
52
+ Because the upstream unit tests are placeholders, the behavioral contract is the
53
+ repo-authored differential: `tests/differential/helpers.test.ts` (every framework-free helper against the published package) and `tests/differential/parity.test.ts` (every hook through one fixture: values, ref callbacks, layout effects). `audit/react-parity.json` registers all three
54
+ lanes; `node scripts/react-parity/dnd-kit-legacy-manifest.mjs` regenerates it and
55
+ the runtime inventories.
package/package.json CHANGED
@@ -1,6 +1,52 @@
1
1
  {
2
2
  "name": "@octanejs/dnd-kit-utilities",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.0.1",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "engines": {
8
+ "node": ">=22.22.2"
9
+ },
10
+ "description": "dnd kit utilities for the octane renderer — a port of the legacy @dnd-kit/utilities 3.2.2 hooks and DOM helpers (CSS, getWindow, getOwnerDocument, useCombinedRefs, useUniqueId, …).",
11
+ "author": {
12
+ "name": "Dominic Gannaway",
13
+ "email": "dg@domgan.com"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/octanejs/octane.git",
21
+ "directory": "packages/dnd-kit-utilities"
22
+ },
23
+ "main": "src/index.ts",
24
+ "module": "src/index.ts",
25
+ "types": "src/index.ts",
26
+ "files": [
27
+ "src",
28
+ "README.md",
29
+ "UPSTREAM.md",
30
+ "LICENSE",
31
+ "LICENSE.upstream"
32
+ ],
33
+ "exports": {
34
+ ".": "./src/index.ts"
35
+ },
36
+ "peerDependencies": {
37
+ "octane": "^0.16.0"
38
+ },
39
+ "devDependencies": {
40
+ "@dnd-kit/utilities": "3.2.2",
41
+ "@tsrx/react": "^0.4.5",
42
+ "esbuild": "^0.28.1",
43
+ "octane": "0.16.4",
44
+ "react": "19.2.7",
45
+ "react-dom": "19.2.7",
46
+ "vitest": "^4.1.10"
47
+ },
48
+ "scripts": {
49
+ "test": "vitest run --root ../.. --project dnd-kit-utilities",
50
+ "typecheck": "octane-tsc -p tsconfig.json"
51
+ }
6
52
  }
@@ -0,0 +1,28 @@
1
+ function createAdjustmentFn(modifier: number) {
2
+ return <T extends Record<U, number>, U extends string>(
3
+ object: T,
4
+ ...adjustments: Partial<T>[]
5
+ ): T => {
6
+ return adjustments.reduce<T>(
7
+ (accumulator, adjustment) => {
8
+ const entries = Object.entries(adjustment) as [U, number][];
9
+
10
+ for (const [key, valueAdjustment] of entries) {
11
+ const value = accumulator[key];
12
+
13
+ if (value != null) {
14
+ accumulator[key] = (value + modifier * valueAdjustment) as T[U];
15
+ }
16
+ }
17
+
18
+ return accumulator;
19
+ },
20
+ {
21
+ ...object,
22
+ },
23
+ );
24
+ };
25
+ }
26
+
27
+ export const add = createAdjustmentFn(1);
28
+ export const subtract = createAdjustmentFn(-1);
@@ -0,0 +1,34 @@
1
+ import type { Coordinates } from './types';
2
+ import { isTouchEvent, hasViewportRelativeCoordinates } from '../event';
3
+
4
+ /**
5
+ * Returns the normalized x and y coordinates for mouse and touch events.
6
+ */
7
+ export function getEventCoordinates(event: Event): Coordinates | null {
8
+ if (isTouchEvent(event)) {
9
+ if (event.touches && event.touches.length) {
10
+ const { clientX: x, clientY: y } = event.touches[0];
11
+
12
+ return {
13
+ x,
14
+ y,
15
+ };
16
+ } else if (event.changedTouches && event.changedTouches.length) {
17
+ const { clientX: x, clientY: y } = event.changedTouches[0];
18
+
19
+ return {
20
+ x,
21
+ y,
22
+ };
23
+ }
24
+ }
25
+
26
+ if (hasViewportRelativeCoordinates(event)) {
27
+ return {
28
+ x: event.clientX,
29
+ y: event.clientY,
30
+ };
31
+ }
32
+
33
+ return null;
34
+ }
@@ -0,0 +1,2 @@
1
+ export type { Coordinates } from './types';
2
+ export { getEventCoordinates } from './getEventCoordinates';
@@ -0,0 +1,4 @@
1
+ export type Coordinates = {
2
+ x: number;
3
+ y: number;
4
+ };
package/src/css.ts ADDED
@@ -0,0 +1,51 @@
1
+ export type Transform = {
2
+ x: number;
3
+ y: number;
4
+ scaleX: number;
5
+ scaleY: number;
6
+ };
7
+
8
+ export interface Transition {
9
+ property: string;
10
+ easing: string;
11
+ duration: number;
12
+ }
13
+
14
+ export const CSS = Object.freeze({
15
+ Translate: {
16
+ toString(transform: Transform | null) {
17
+ if (!transform) {
18
+ return;
19
+ }
20
+
21
+ const { x, y } = transform;
22
+
23
+ return `translate3d(${x ? Math.round(x) : 0}px, ${y ? Math.round(y) : 0}px, 0)`;
24
+ },
25
+ },
26
+ Scale: {
27
+ toString(transform: Transform | null) {
28
+ if (!transform) {
29
+ return;
30
+ }
31
+
32
+ const { scaleX, scaleY } = transform;
33
+
34
+ return `scaleX(${scaleX}) scaleY(${scaleY})`;
35
+ },
36
+ },
37
+ Transform: {
38
+ toString(transform: Transform | null) {
39
+ if (!transform) {
40
+ return;
41
+ }
42
+
43
+ return [CSS.Translate.toString(transform), CSS.Scale.toString(transform)].join(' ');
44
+ },
45
+ },
46
+ Transition: {
47
+ toString({ property, duration, easing }: Transition) {
48
+ return `${property} ${duration}ms ${easing}`;
49
+ },
50
+ },
51
+ });
@@ -0,0 +1,5 @@
1
+ export function hasViewportRelativeCoordinates(
2
+ event: Event,
3
+ ): event is Event & Pick<PointerEvent, 'clientX' | 'clientY'> {
4
+ return 'clientX' in event && 'clientY' in event;
5
+ }
@@ -0,0 +1,3 @@
1
+ export { hasViewportRelativeCoordinates } from './hasViewportRelativeCoordinates';
2
+ export { isKeyboardEvent } from './isKeyboardEvent';
3
+ export { isTouchEvent } from './isTouchEvent';
@@ -0,0 +1,11 @@
1
+ import { getWindow } from '../execution-context';
2
+
3
+ export function isKeyboardEvent(event: Event | undefined | null): event is KeyboardEvent {
4
+ if (!event) {
5
+ return false;
6
+ }
7
+
8
+ const { KeyboardEvent } = getWindow(event.target);
9
+
10
+ return KeyboardEvent && event instanceof KeyboardEvent;
11
+ }
@@ -0,0 +1,11 @@
1
+ import { getWindow } from '../execution-context';
2
+
3
+ export function isTouchEvent(event: Event | undefined | null): event is TouchEvent {
4
+ if (!event) {
5
+ return false;
6
+ }
7
+
8
+ const { TouchEvent } = getWindow(event.target);
9
+
10
+ return TouchEvent && event instanceof TouchEvent;
11
+ }
@@ -0,0 +1,5 @@
1
+ // https://github.com/facebook/react/blob/master/packages/shared/ExecutionEnvironment.js
2
+ export const canUseDOM =
3
+ typeof window !== 'undefined' &&
4
+ typeof window.document !== 'undefined' &&
5
+ typeof window.document.createElement !== 'undefined';
@@ -0,0 +1,25 @@
1
+ import { isWindow, isHTMLElement, isDocument, isNode, isSVGElement } from '../type-guards';
2
+
3
+ export function getOwnerDocument(target: Event['target']): Document {
4
+ if (!target) {
5
+ return document;
6
+ }
7
+
8
+ if (isWindow(target)) {
9
+ return target.document;
10
+ }
11
+
12
+ if (!isNode(target)) {
13
+ return document;
14
+ }
15
+
16
+ if (isDocument(target)) {
17
+ return target;
18
+ }
19
+
20
+ if (isHTMLElement(target) || isSVGElement(target)) {
21
+ return target.ownerDocument;
22
+ }
23
+
24
+ return document;
25
+ }
@@ -0,0 +1,18 @@
1
+ import { isWindow } from '../type-guards/isWindow';
2
+ import { isNode } from '../type-guards/isNode';
3
+
4
+ export function getWindow(target: Event['target']): typeof window {
5
+ if (!target) {
6
+ return window;
7
+ }
8
+
9
+ if (isWindow(target)) {
10
+ return target;
11
+ }
12
+
13
+ if (!isNode(target)) {
14
+ return window;
15
+ }
16
+
17
+ return target.ownerDocument?.defaultView ?? window;
18
+ }
@@ -0,0 +1,3 @@
1
+ export { canUseDOM } from './canUseDOM';
2
+ export { getOwnerDocument } from './getOwnerDocument';
3
+ export { getWindow } from './getWindow';
@@ -0,0 +1,10 @@
1
+ const SELECTOR =
2
+ 'a,frame,iframe,input:not([type=hidden]):not(:disabled),select:not(:disabled),textarea:not(:disabled),button:not(:disabled),*[tabindex]';
3
+
4
+ export function findFirstFocusableNode(element: HTMLElement): HTMLElement | null {
5
+ if (element.matches(SELECTOR)) {
6
+ return element;
7
+ }
8
+
9
+ return element.querySelector(SELECTOR);
10
+ }
@@ -0,0 +1 @@
1
+ export { findFirstFocusableNode } from './findFirstFocusableNode';
@@ -0,0 +1,9 @@
1
+ export { useCombinedRefs } from './useCombinedRefs';
2
+ export { useEvent } from './useEvent';
3
+ export { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
4
+ export { useInterval } from './useInterval';
5
+ export { useLatestValue } from './useLatestValue';
6
+ export { useLazyMemo } from './useLazyMemo';
7
+ export { useNodeRef } from './useNodeRef';
8
+ export { usePrevious } from './usePrevious';
9
+ export { useUniqueId } from './useUniqueId';
@@ -0,0 +1,11 @@
1
+ import { useMemo } from 'octane';
2
+
3
+ export function useCombinedRefs<T>(...refs: ((node: T) => void)[]): (node: T) => void {
4
+ return useMemo(
5
+ () => (node: T) => {
6
+ refs.forEach((ref) => ref(node));
7
+ },
8
+ // eslint-disable-next-line react-hooks/exhaustive-deps
9
+ refs,
10
+ );
11
+ }
@@ -0,0 +1,15 @@
1
+ import { useCallback, useRef } from 'octane';
2
+
3
+ import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
4
+
5
+ export function useEvent<T extends Function>(handler: T | undefined) {
6
+ const handlerRef = useRef<T | undefined>(handler);
7
+
8
+ useIsomorphicLayoutEffect(() => {
9
+ handlerRef.current = handler;
10
+ });
11
+
12
+ return useCallback(function (...args: any) {
13
+ return handlerRef.current?.(...args);
14
+ }, []);
15
+ }
@@ -0,0 +1,18 @@
1
+ import { useCallback, useRef } from 'octane';
2
+
3
+ export function useInterval() {
4
+ const intervalRef = useRef<number | null>(null);
5
+
6
+ const set = useCallback((listener: Function, duration: number) => {
7
+ intervalRef.current = setInterval(listener, duration);
8
+ }, []);
9
+
10
+ const clear = useCallback(() => {
11
+ if (intervalRef.current !== null) {
12
+ clearInterval(intervalRef.current);
13
+ intervalRef.current = null;
14
+ }
15
+ }, []);
16
+
17
+ return [set, clear] as const;
18
+ }
@@ -0,0 +1,9 @@
1
+ import { useEffect, useLayoutEffect } from 'octane';
2
+
3
+ import { canUseDOM } from '../execution-context';
4
+
5
+ /**
6
+ * A hook that resolves to useEffect on the server and useLayoutEffect on the client
7
+ * @param callback {function} Callback function that is invoked when the dependencies of the hook change
8
+ */
9
+ export const useIsomorphicLayoutEffect = canUseDOM ? useLayoutEffect : useEffect;
@@ -0,0 +1,16 @@
1
+ import { useRef } from 'octane';
2
+ import type { DependencyList } from 'octane';
3
+
4
+ import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect';
5
+
6
+ export function useLatestValue<T extends any>(value: T, dependencies: DependencyList = [value]) {
7
+ const valueRef = useRef<T>(value);
8
+
9
+ useIsomorphicLayoutEffect(() => {
10
+ if (valueRef.current !== value) {
11
+ valueRef.current = value;
12
+ }
13
+ }, dependencies);
14
+
15
+ return valueRef;
16
+ }
@@ -0,0 +1,16 @@
1
+ import { useMemo, useRef } from 'octane';
2
+
3
+ export function useLazyMemo<T>(callback: (prevValue: T | undefined) => T, dependencies: any[]) {
4
+ const valueRef = useRef<T>();
5
+
6
+ return useMemo(
7
+ () => {
8
+ const newValue = callback(valueRef.current);
9
+ valueRef.current = newValue;
10
+
11
+ return newValue;
12
+ },
13
+ // eslint-disable-next-line react-hooks/exhaustive-deps
14
+ [...dependencies],
15
+ );
16
+ }
@@ -0,0 +1,23 @@
1
+ import { useRef, useCallback } from 'octane';
2
+
3
+ import { useEvent } from './useEvent';
4
+
5
+ export function useNodeRef(
6
+ onChange?: (newElement: HTMLElement | null, previousElement: HTMLElement | null) => void,
7
+ ) {
8
+ const onChangeHandler = useEvent(onChange);
9
+ const node = useRef<HTMLElement | null>(null);
10
+ const setNodeRef = useCallback(
11
+ (element: HTMLElement | null) => {
12
+ if (element !== node.current) {
13
+ onChangeHandler?.(element, node.current);
14
+ }
15
+
16
+ node.current = element;
17
+ },
18
+ //eslint-disable-next-line
19
+ [],
20
+ );
21
+
22
+ return [node, setNodeRef] as const;
23
+ }
@@ -0,0 +1,11 @@
1
+ import { useRef, useEffect } from 'octane';
2
+
3
+ export function usePrevious<T>(value: T) {
4
+ const ref = useRef<T>();
5
+
6
+ useEffect(() => {
7
+ ref.current = value;
8
+ }, [value]);
9
+
10
+ return ref.current;
11
+ }
@@ -0,0 +1,16 @@
1
+ import { useMemo } from 'octane';
2
+
3
+ let ids: Record<string, number> = {};
4
+
5
+ export function useUniqueId(prefix: string, value?: string) {
6
+ return useMemo(() => {
7
+ if (value) {
8
+ return value;
9
+ }
10
+
11
+ const id = ids[prefix] == null ? 0 : ids[prefix] + 1;
12
+ ids[prefix] = id;
13
+
14
+ return `${prefix}-${id}`;
15
+ }, [prefix, value]);
16
+ }
package/src/index.ts ADDED
@@ -0,0 +1,22 @@
1
+ export {
2
+ useCombinedRefs,
3
+ useEvent,
4
+ useIsomorphicLayoutEffect,
5
+ useInterval,
6
+ useLatestValue,
7
+ useLazyMemo,
8
+ useNodeRef,
9
+ usePrevious,
10
+ useUniqueId,
11
+ } from './hooks';
12
+
13
+ export { add, subtract } from './adjustment';
14
+ export type { Coordinates } from './coordinates';
15
+ export { getEventCoordinates } from './coordinates';
16
+ export { CSS } from './css';
17
+ export type { Transform, Transition } from './css';
18
+ export { hasViewportRelativeCoordinates, isKeyboardEvent, isTouchEvent } from './event';
19
+ export { canUseDOM, getOwnerDocument, getWindow } from './execution-context';
20
+ export { findFirstFocusableNode } from './focus';
21
+ export { isDocument, isHTMLElement, isNode, isSVGElement, isWindow } from './type-guards';
22
+ export type { Arguments, DeepRequired, FirstArgument, Without } from './types';
@@ -0,0 +1,5 @@
1
+ export { isDocument } from './isDocument';
2
+ export { isHTMLElement } from './isHTMLElement';
3
+ export { isNode } from './isNode';
4
+ export { isSVGElement } from './isSVGElement';
5
+ export { isWindow } from './isWindow';
@@ -0,0 +1,7 @@
1
+ import { getWindow } from '../execution-context/getWindow';
2
+
3
+ export function isDocument(node: Node): node is Document {
4
+ const { Document } = getWindow(node);
5
+
6
+ return node instanceof Document;
7
+ }
@@ -0,0 +1,11 @@
1
+ import { getWindow } from '../execution-context/getWindow';
2
+
3
+ import { isWindow } from './isWindow';
4
+
5
+ export function isHTMLElement(node: Node | Window): node is HTMLElement {
6
+ if (isWindow(node)) {
7
+ return false;
8
+ }
9
+
10
+ return node instanceof getWindow(node).HTMLElement;
11
+ }
@@ -0,0 +1,3 @@
1
+ export function isNode(node: Object): node is Node {
2
+ return 'nodeType' in node;
3
+ }
@@ -0,0 +1,5 @@
1
+ import { getWindow } from '../execution-context/getWindow';
2
+
3
+ export function isSVGElement(node: Node): node is SVGElement {
4
+ return node instanceof getWindow(node).SVGElement;
5
+ }
@@ -0,0 +1,8 @@
1
+ export function isWindow(element: Object): element is typeof window {
2
+ const elementString = Object.prototype.toString.call(element);
3
+ return (
4
+ elementString === '[object Window]' ||
5
+ // In Electron context the Window object serializes to [object global]
6
+ elementString === '[object global]'
7
+ );
8
+ }
package/src/types.ts ADDED
@@ -0,0 +1,11 @@
1
+ export type Arguments<T> = T extends (...args: infer U) => any ? U : never;
2
+
3
+ export type DeepRequired<T> = {
4
+ [K in keyof T]-?: Required<T[K]>;
5
+ };
6
+
7
+ export type FirstArgument<T> = T extends (firstArg: infer U, ...args: Array<any>) => any
8
+ ? U
9
+ : never;
10
+
11
+ export type Without<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;