@cargo-ai/utils 1.0.4 → 1.0.6

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 (96) hide show
  1. package/README.md +52 -0
  2. package/build/tsconfig.tsbuildinfo +1 -1
  3. package/package.json +10 -4
  4. package/build/array.js +0 -52
  5. package/build/collection.js +0 -155
  6. package/build/components.js +0 -56
  7. package/build/cron.js +0 -19
  8. package/build/delay.js +0 -52
  9. package/build/hooks/index.js +0 -35
  10. package/build/hooks/useClickOutside.js +0 -46
  11. package/build/hooks/useCountAnimation.js +0 -34
  12. package/build/hooks/useDeviceType.js +0 -54
  13. package/build/hooks/useEventListener.js +0 -27
  14. package/build/hooks/useHeight.js +0 -26
  15. package/build/hooks/usePictureInPicture.js +0 -126
  16. package/build/hooks/useResizeObserver.js +0 -35
  17. package/build/hooks/useScrollPosition.js +0 -22
  18. package/build/hooks/useStepper.js +0 -138
  19. package/build/hooks/useStorageState.js +0 -110
  20. package/build/hooks/useVideoPlayer/VideoManager.js +0 -78
  21. package/build/hooks/useVideoPlayer/index.js +0 -5
  22. package/build/hooks/useVideoPlayer/useVideoPlayer.js +0 -100
  23. package/build/hooks/useWillUnmountEffect.js +0 -13
  24. package/build/index.js +0 -28
  25. package/build/json.js +0 -81
  26. package/build/random.js +0 -7
  27. package/build/src/components.d.ts +0 -4
  28. package/build/src/components.js +0 -45
  29. package/build/src/date.d.ts +0 -13
  30. package/build/src/date.d.ts.map +0 -1
  31. package/build/src/date.js +0 -14
  32. package/build/src/expression/jsonSchema.d.ts +0 -4
  33. package/build/src/expression/jsonSchema.d.ts.map +0 -1
  34. package/build/src/expression/jsonSchema.js +0 -85
  35. package/build/src/expression/matchIsExpression.d.ts +0 -2
  36. package/build/src/expression/matchIsExpression.d.ts.map +0 -1
  37. package/build/src/expression/matchIsExpression.js +0 -10
  38. package/build/src/expression/matchIsExpressionObject.d.ts +0 -2
  39. package/build/src/expression/matchIsExpressionObject.d.ts.map +0 -1
  40. package/build/src/expression/matchIsExpressionObject.js +0 -10
  41. package/build/src/expression/matchIsExpressionString.d.ts +0 -2
  42. package/build/src/expression/matchIsExpressionString.d.ts.map +0 -1
  43. package/build/src/expression/matchIsExpressionString.js +0 -4
  44. package/build/src/hooks/index.d.ts +0 -12
  45. package/build/src/hooks/index.js +0 -12
  46. package/build/src/hooks/useClickOutside.d.ts +0 -4
  47. package/build/src/hooks/useClickOutside.js +0 -42
  48. package/build/src/hooks/useCountAnimation.d.ts +0 -1
  49. package/build/src/hooks/useCountAnimation.js +0 -30
  50. package/build/src/hooks/useDeviceType.d.ts +0 -2
  51. package/build/src/hooks/useDeviceType.js +0 -50
  52. package/build/src/hooks/useEventListener.d.ts +0 -6
  53. package/build/src/hooks/useEventListener.js +0 -23
  54. package/build/src/hooks/useHeight.d.ts +0 -2
  55. package/build/src/hooks/useHeight.js +0 -20
  56. package/build/src/hooks/usePictureInPicture.d.ts +0 -9
  57. package/build/src/hooks/usePictureInPicture.js +0 -60
  58. package/build/src/hooks/useResizeObserver.d.ts +0 -4
  59. package/build/src/hooks/useResizeObserver.js +0 -31
  60. package/build/src/hooks/useScrollPosition.d.ts +0 -5
  61. package/build/src/hooks/useScrollPosition.js +0 -18
  62. package/build/src/hooks/useStepper.d.ts +0 -26
  63. package/build/src/hooks/useStepper.js +0 -136
  64. package/build/src/hooks/useStorageState.d.ts +0 -12
  65. package/build/src/hooks/useStorageState.js +0 -53
  66. package/build/src/hooks/useVideoPlayer/VideoManager.d.ts +0 -18
  67. package/build/src/hooks/useVideoPlayer/VideoManager.js +0 -72
  68. package/build/src/hooks/useVideoPlayer/index.d.ts +0 -1
  69. package/build/src/hooks/useVideoPlayer/index.js +0 -1
  70. package/build/src/hooks/useVideoPlayer/useVideoPlayer.d.ts +0 -15
  71. package/build/src/hooks/useVideoPlayer/useVideoPlayer.js +0 -96
  72. package/build/src/hooks/useWillUnmountEffect.d.ts +0 -1
  73. package/build/src/hooks/useWillUnmountEffect.js +0 -8
  74. package/build/src/integration.d.ts +0 -14
  75. package/build/src/integration.d.ts.map +0 -1
  76. package/build/src/integration.js +0 -72
  77. package/build/src/json/cleanJson.d.ts +0 -3
  78. package/build/src/json/cleanJson.d.ts.map +0 -1
  79. package/build/src/json/cleanJson.js +0 -18
  80. package/build/src/json.d.ts +0 -13
  81. package/build/src/json.d.ts.map +0 -1
  82. package/build/src/json.js +0 -107
  83. package/build/src/jsonSchema.d.ts +0 -5
  84. package/build/src/jsonSchema.d.ts.map +0 -1
  85. package/build/src/jsonSchema.js +0 -73
  86. package/build/src/type.d.ts +0 -10
  87. package/build/src/type.js +0 -1
  88. package/build/src/vm.d.ts +0 -1
  89. package/build/src/vm.js +0 -32
  90. package/build/src/zod.d.ts +0 -8
  91. package/build/src/zod.d.ts.map +0 -1
  92. package/build/src/zod.js +0 -89
  93. package/build/storage.js +0 -285
  94. package/build/string.js +0 -17
  95. package/build/type.js +0 -2
  96. package/build/user.js +0 -12
package/build/json.js DELETED
@@ -1,81 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isJsonSchemaEmpty = exports.castToJsonType = exports.getJsonType = exports.isJson = exports.jsonTypes = void 0;
4
- var collection_js_1 = require("./collection.js");
5
- exports.jsonTypes = [
6
- "string",
7
- "number",
8
- "object",
9
- "array",
10
- "date",
11
- "boolean",
12
- "any",
13
- ];
14
- var isJson = function (value) {
15
- try {
16
- JSON.parse(value);
17
- return true;
18
- }
19
- catch (_a) {
20
- return false;
21
- }
22
- };
23
- exports.isJson = isJson;
24
- var getJsonType = function (value) {
25
- if (value === null || value === undefined) {
26
- return "any";
27
- }
28
- if (Array.isArray(value)) {
29
- return "array";
30
- }
31
- if (value instanceof Date) {
32
- return "date";
33
- }
34
- if (typeof value === "object") {
35
- return "object";
36
- }
37
- if (value === true || value === false) {
38
- return "boolean";
39
- }
40
- if (typeof value === "number") {
41
- return "number";
42
- }
43
- if (typeof value === "string") {
44
- return "string";
45
- }
46
- return "any";
47
- };
48
- exports.getJsonType = getJsonType;
49
- var castToJsonType = function (value, type) {
50
- try {
51
- if (type === "array") {
52
- return Array.isArray(value) ? value : null;
53
- }
54
- if (type === "string") {
55
- return String(value);
56
- }
57
- if (type === "boolean") {
58
- return Boolean(value);
59
- }
60
- if (type === "number") {
61
- var number = Number(value);
62
- if (isNaN(number)) {
63
- return undefined;
64
- }
65
- return number;
66
- }
67
- if (type === "date") {
68
- return new Date(value);
69
- }
70
- return value;
71
- }
72
- catch (_a) {
73
- return undefined;
74
- }
75
- };
76
- exports.castToJsonType = castToJsonType;
77
- var isJsonSchemaEmpty = function (jsonSchema) {
78
- return jsonSchema.type === "object" &&
79
- (!jsonSchema.properties || (0, collection_js_1.getKeys)(jsonSchema.properties).length === 0);
80
- };
81
- exports.isJsonSchemaEmpty = isJsonSchemaEmpty;
package/build/random.js DELETED
@@ -1,7 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.random = void 0;
4
- var random = function (min, max) {
5
- return Math.floor(Math.random() * (max - min + 1) + min);
6
- };
7
- exports.random = random;
@@ -1,4 +0,0 @@
1
- import React from "react";
2
- export declare const isComponentElement: <Props>(component: React.FC<Props>, element: React.ReactElement) => boolean;
3
- export declare const replaceComponentElements: <Props>(component: React.FC<Props>, replace: (props: Props, index: number, count: number) => React.ReactElement, children?: React.ReactNode) => React.ReactNode;
4
- export declare const filterComponentElements: <Props>(component: React.FC<Props>, children?: React.ReactNode) => React.ReactElement<Props, string | React.JSXElementConstructor<any>>[];
@@ -1,45 +0,0 @@
1
- import React from "react";
2
- export const isComponentElement = (component, element) => {
3
- return component.name === element.type.name;
4
- };
5
- export const replaceComponentElements = (component, replace, children) => {
6
- if (!children) {
7
- return undefined;
8
- }
9
- const count = React.Children.count(children);
10
- return React.Children.map(children, (child, index) => {
11
- if (!React.isValidElement(child)) {
12
- return child;
13
- }
14
- const childElement = child;
15
- if (isComponentElement(component, childElement)) {
16
- return replace(childElement.props, index, count);
17
- }
18
- if (childElement.props.children) {
19
- return {
20
- ...childElement,
21
- props: {
22
- ...childElement.props,
23
- children: replaceComponentElements(component, replace, childElement.props.children),
24
- },
25
- };
26
- }
27
- return child;
28
- });
29
- };
30
- export const filterComponentElements = (component, children) => {
31
- const elements = [];
32
- React.Children.forEach(children, (child) => {
33
- if (!React.isValidElement(child)) {
34
- return;
35
- }
36
- const childElement = child;
37
- if (isComponentElement(component, childElement)) {
38
- elements.push(childElement);
39
- }
40
- else if (childElement.props.children) {
41
- elements.push(...filterComponentElements(component, childElement.props.children));
42
- }
43
- });
44
- return elements;
45
- };
@@ -1,13 +0,0 @@
1
- /**
2
- * Normalizes a Date object to an ISO 8601 date string (`YYYY-MM-DD`),
3
- * compensating for the local timezone offset to ensure consistency in UTC.
4
- *
5
- * @param {Date} date - The date object to normalize.
6
- * @returns {string} The normalized ISO date string (`YYYY-MM-DD`).
7
- *
8
- * @example
9
- * const date = new Date("2024-02-17T23:59:59");
10
- * console.log(getUTCDateString(date)); // "2024-02-17" (adjusted for timezone)
11
- */
12
- export declare function getUTCDateString(date: Date): string | undefined;
13
- //# sourceMappingURL=date.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"date.d.ts","sourceRoot":"","sources":["../../src/date.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,GAAG,SAAS,CAE/D"}
package/build/src/date.js DELETED
@@ -1,14 +0,0 @@
1
- /**
2
- * Normalizes a Date object to an ISO 8601 date string (`YYYY-MM-DD`),
3
- * compensating for the local timezone offset to ensure consistency in UTC.
4
- *
5
- * @param {Date} date - The date object to normalize.
6
- * @returns {string} The normalized ISO date string (`YYYY-MM-DD`).
7
- *
8
- * @example
9
- * const date = new Date("2024-02-17T23:59:59");
10
- * console.log(getUTCDateString(date)); // "2024-02-17" (adjusted for timezone)
11
- */
12
- export function getUTCDateString(date) {
13
- return date.toISOString().split("T")[0];
14
- }
@@ -1,4 +0,0 @@
1
- import type { JsonSchema } from "../jsonSchema.js";
2
- export declare const expressionJsonSchema: JsonSchema;
3
- export declare const jsonSchemaDefinitions: JsonSchema["definitions"];
4
- //# sourceMappingURL=jsonSchema.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"jsonSchema.d.ts","sourceRoot":"","sources":["../../../src/expression/jsonSchema.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD,eAAO,MAAM,oBAAoB,EAAE,UAqDlC,CAAC;AAEF,eAAO,MAAM,qBAAqB,EAAE,UAAU,CAAC,aAAa,CA8B3D,CAAC"}
@@ -1,85 +0,0 @@
1
- export const expressionJsonSchema = {
2
- type: "object",
3
- title: "Expression",
4
- properties: {
5
- kind: {
6
- type: "string",
7
- enum: ["jsExpression", "templateExpression"],
8
- },
9
- expression: {
10
- type: "string",
11
- minLength: 1,
12
- },
13
- instructTo: {
14
- type: ["string"],
15
- enum: ["ai", "none"],
16
- },
17
- fromRecipe: {
18
- type: "boolean",
19
- },
20
- },
21
- allOf: [
22
- {
23
- if: {
24
- properties: {
25
- fromRecipe: {
26
- const: true,
27
- },
28
- },
29
- },
30
- then: {
31
- properties: {
32
- recipe: {
33
- type: "object",
34
- properties: {
35
- uuid: {
36
- type: "string",
37
- },
38
- version: {
39
- type: "number",
40
- },
41
- variables: {
42
- type: "object",
43
- additionalProperties: true,
44
- },
45
- },
46
- required: ["uuid", "version", "variables"],
47
- },
48
- },
49
- required: ["recipe"],
50
- },
51
- },
52
- ],
53
- required: ["kind", "expression", "instructTo", "fromRecipe"],
54
- };
55
- export const jsonSchemaDefinitions = {
56
- expression: expressionJsonSchema,
57
- templateExpression: { type: "string" },
58
- multi: {
59
- anyOf: [
60
- {
61
- type: "array",
62
- items: { type: "string" },
63
- },
64
- { type: "string" },
65
- ],
66
- minItems: 1,
67
- uniqueItems: true,
68
- },
69
- encryption: {
70
- type: "object",
71
- properties: {
72
- type: {
73
- type: "string",
74
- enum: ["encryption"],
75
- },
76
- isEncrypted: {
77
- type: "boolean",
78
- },
79
- value: {
80
- type: "string",
81
- },
82
- },
83
- required: ["type", "isEncrypted", "value"],
84
- },
85
- };
@@ -1,2 +0,0 @@
1
- export declare const matchIsExpression: (value: any) => boolean;
2
- //# sourceMappingURL=matchIsExpression.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"matchIsExpression.d.ts","sourceRoot":"","sources":["../../../src/expression/matchIsExpression.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,iBAAiB,UAAW,GAAG,KAAG,OAQ9C,CAAC"}
@@ -1,10 +0,0 @@
1
- import { validate as jsonSchemaValidate } from "jsonschema";
2
- import { expressionJsonSchema } from "./jsonSchema.js";
3
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
- export const matchIsExpression = (value) => {
5
- if (value === undefined || value === null) {
6
- return false;
7
- }
8
- const { valid: isValid } = jsonSchemaValidate(value, expressionJsonSchema);
9
- return isValid === true;
10
- };
@@ -1,2 +0,0 @@
1
- export declare const matchIsExpressionObject: (value: any) => boolean;
2
- //# sourceMappingURL=matchIsExpressionObject.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"matchIsExpressionObject.d.ts","sourceRoot":"","sources":["../../../src/expression/matchIsExpressionObject.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,uBAAuB,UAAW,GAAG,KAAG,OAQpD,CAAC"}
@@ -1,10 +0,0 @@
1
- import { validate as jsonSchemaValidate } from "jsonschema";
2
- import { expressionJsonSchema } from "../jsonSchema.js";
3
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4
- export const matchIsExpressionObject = (value) => {
5
- if (value === undefined || value === null) {
6
- return false;
7
- }
8
- const { valid: isValid } = jsonSchemaValidate(value, expressionJsonSchema);
9
- return isValid === true;
10
- };
@@ -1,2 +0,0 @@
1
- export declare const matchIsExpressionString: (value: string) => boolean;
2
- //# sourceMappingURL=matchIsExpressionString.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"matchIsExpressionString.d.ts","sourceRoot":"","sources":["../../../src/expression/matchIsExpressionString.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,uBAAuB,UAAW,MAAM,KAAG,OAEvD,CAAC"}
@@ -1,4 +0,0 @@
1
- const EXPRESSION_REGEX = /{{.*}}/;
2
- export const matchIsExpressionString = (value) => {
3
- return EXPRESSION_REGEX.test(value);
4
- };
@@ -1,12 +0,0 @@
1
- export * from "./useClickOutside.js";
2
- export { useCountAnimation } from "./useCountAnimation.js";
3
- export { useDeviceType } from "./useDeviceType.js";
4
- export { useEventListener } from "./useEventListener.js";
5
- export * from "./useHeight.js";
6
- export { usePictureInPicture } from "./usePictureInPicture.js";
7
- export * from "./useResizeObserver.js";
8
- export { useScrollPosition } from "./useScrollPosition.js";
9
- export * from "./useStepper.js";
10
- export * from "./useStorageState.js";
11
- export { useVideoPlayer } from "./useVideoPlayer/index.js";
12
- export * from "./useWillUnmountEffect.js";
@@ -1,12 +0,0 @@
1
- export * from "./useClickOutside.js";
2
- export { useCountAnimation } from "./useCountAnimation.js";
3
- export { useDeviceType } from "./useDeviceType.js";
4
- export { useEventListener } from "./useEventListener.js";
5
- export * from "./useHeight.js";
6
- export { usePictureInPicture } from "./usePictureInPicture.js";
7
- export * from "./useResizeObserver.js";
8
- export { useScrollPosition } from "./useScrollPosition.js";
9
- export * from "./useStepper.js";
10
- export * from "./useStorageState.js";
11
- export { useVideoPlayer } from "./useVideoPlayer/index.js";
12
- export * from "./useWillUnmountEffect.js";
@@ -1,4 +0,0 @@
1
- import React from "react";
2
- type Ref<T extends HTMLElement> = React.RefObject<T | null> | T | null;
3
- export declare const useClickOutside: <T extends HTMLElement>(refOrRefs: Ref<T> | Ref<T>[], handle: (event: MouseEvent) => void) => void;
4
- export {};
@@ -1,42 +0,0 @@
1
- import React from "react";
2
- export const useClickOutside = (refOrRefs, handle) => {
3
- React.useEffect(() => {
4
- if (refOrRefs === null ||
5
- (Array.isArray(refOrRefs) && refOrRefs.length === 0)) {
6
- return;
7
- }
8
- const handleClickOutside = (event) => {
9
- const hasClickedOutsideRef = Array.isArray(refOrRefs)
10
- ? refOrRefs.every((ref) => {
11
- return matchHasClickedOutsideRef(event, ref);
12
- })
13
- : matchHasClickedOutsideRef(event, refOrRefs);
14
- if (hasClickedOutsideRef === true) {
15
- handle(event);
16
- }
17
- };
18
- document.addEventListener("mouseup", handleClickOutside);
19
- return () => {
20
- document.removeEventListener("mouseup", handleClickOutside);
21
- };
22
- }, [refOrRefs, handle]);
23
- };
24
- const matchHasClickedOutsideRef = (event, ref) => {
25
- if (ref === null) {
26
- return false;
27
- }
28
- const element = ref instanceof HTMLElement ? ref : ref.current;
29
- if (element === null) {
30
- return false;
31
- }
32
- if (event.target === null) {
33
- return false;
34
- }
35
- const { clientX: x, clientY: y } = event;
36
- const { height: refHeight, width: refWidth, top: refTop, left: refLeft, } = element.getBoundingClientRect();
37
- const isClickInsideElement = x >= refLeft &&
38
- x <= refLeft + refWidth &&
39
- y >= refTop &&
40
- y <= refTop + refHeight;
41
- return isClickInsideElement === false;
42
- };
@@ -1 +0,0 @@
1
- export declare function useCountAnimation(targetValue: number, triggerKey?: string | number | Date): number;
@@ -1,30 +0,0 @@
1
- import React from "react";
2
- export function useCountAnimation(targetValue, triggerKey) {
3
- const [currentValue, setCurrentValue] = React.useState(0);
4
- const previousKey = React.useRef(triggerKey);
5
- const isFirstMount = React.useRef(true);
6
- React.useEffect(() => {
7
- if (isFirstMount.current === true) {
8
- isFirstMount.current = false;
9
- }
10
- else if (triggerKey === undefined || previousKey.current === triggerKey) {
11
- setCurrentValue(targetValue);
12
- return;
13
- }
14
- previousKey.current = triggerKey;
15
- const start = Date.now();
16
- const duration = 300;
17
- const step = () => {
18
- const progress = (Date.now() - start) / duration;
19
- if (progress < 1) {
20
- setCurrentValue(Math.floor(progress * targetValue));
21
- requestAnimationFrame(step);
22
- }
23
- else {
24
- setCurrentValue(targetValue);
25
- }
26
- };
27
- requestAnimationFrame(step);
28
- }, [targetValue, triggerKey]);
29
- return currentValue;
30
- }
@@ -1,2 +0,0 @@
1
- export type DeviceType = "mobile" | "tablet" | "desktop" | "largeScreen";
2
- export declare function useDeviceType(): DeviceType | null;
@@ -1,50 +0,0 @@
1
- "use client";
2
- import { useEffect, useSyncExternalStore } from "react";
3
- import { useEventListener } from "./useEventListener.js";
4
- const sizes = {
5
- sm: 640,
6
- md: 768,
7
- lg: 1024,
8
- xl: 1280,
9
- "2xl": 1536,
10
- };
11
- function getDeviceType(width, height) {
12
- const isLandscape = width > height;
13
- const landscapeMobileMaxWidth = 850;
14
- if (isLandscape && width <= landscapeMobileMaxWidth)
15
- return "mobile";
16
- if (width < sizes.sm)
17
- return "mobile";
18
- if (width >= sizes.sm && width <= sizes.lg)
19
- return "tablet";
20
- if (width >= sizes.lg && width <= sizes.xl)
21
- return "desktop";
22
- return "largeScreen";
23
- }
24
- function createDeviceTypeStore() {
25
- const listeners = new Set();
26
- let currentDeviceType = null;
27
- const updateDeviceType = () => {
28
- const newDeviceType = getDeviceType(window.innerWidth, window.innerHeight);
29
- if (currentDeviceType !== newDeviceType) {
30
- currentDeviceType = newDeviceType;
31
- listeners.forEach((listener) => listener());
32
- }
33
- };
34
- return {
35
- updateDeviceType,
36
- subscribe: (callback) => {
37
- listeners.add(callback);
38
- return () => listeners.delete(callback);
39
- },
40
- getSnapshot: () => currentDeviceType,
41
- getServerSnapshot: () => currentDeviceType,
42
- };
43
- }
44
- const deviceTypeStore = createDeviceTypeStore();
45
- export function useDeviceType() {
46
- const { subscribe, getSnapshot, getServerSnapshot, updateDeviceType } = deviceTypeStore;
47
- useEffect(() => updateDeviceType(), [updateDeviceType]);
48
- useEventListener("resize", updateDeviceType);
49
- return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
50
- }
@@ -1,6 +0,0 @@
1
- import type { RefObject } from "react";
2
- declare function useEventListener<K extends keyof MediaQueryListEventMap>(eventName: K, handler: (event: MediaQueryListEventMap[K]) => void, element?: RefObject<MediaQueryList>, options?: boolean | AddEventListenerOptions): void;
3
- declare function useEventListener<K extends keyof WindowEventMap>(eventName: K, handler: (event: WindowEventMap[K]) => void, element?: undefined, options?: boolean | AddEventListenerOptions): void;
4
- declare function useEventListener<K extends keyof HTMLElementEventMap, T extends HTMLElement = HTMLDivElement>(eventName: K, handler: (event: HTMLElementEventMap[K]) => void, element?: RefObject<T>, options?: boolean | AddEventListenerOptions): void;
5
- declare function useEventListener<K extends keyof DocumentEventMap>(eventName: K, handler: (event: DocumentEventMap[K]) => void, element?: RefObject<Document>, options?: boolean | AddEventListenerOptions): void;
6
- export { useEventListener };
@@ -1,23 +0,0 @@
1
- "use client";
2
- import { useEffect, useRef } from "react";
3
- function useEventListener(eventName, handler, element, options) {
4
- // Create a ref that stores handler
5
- const savedHandler = useRef(handler);
6
- useEffect(() => {
7
- savedHandler.current = handler;
8
- }, [handler]);
9
- useEffect(() => {
10
- // Define the listening target
11
- const targetElement = element?.current ?? window;
12
- if (!(targetElement && targetElement.addEventListener))
13
- return;
14
- // Create event listener that calls handler function stored in ref
15
- const listener = (event) => savedHandler.current(event);
16
- targetElement.addEventListener(eventName, listener, options);
17
- // Remove event listener on cleanup
18
- return () => {
19
- targetElement.removeEventListener(eventName, listener, options);
20
- };
21
- }, [eventName, element, options]);
22
- }
23
- export { useEventListener };
@@ -1,2 +0,0 @@
1
- import React from "react";
2
- export declare const useHeight: <T extends HTMLElement>() => [React.MutableRefObject<T | null>, number];
@@ -1,20 +0,0 @@
1
- import React from "react";
2
- export const useHeight = () => {
3
- const ref = React.useRef(null);
4
- const [height, setHeight] = React.useState(0);
5
- const heightRef = React.useRef(height);
6
- const [resizeObserver] = React.useState(() => new ResizeObserver(() => {
7
- if (ref.current && heightRef.current !== ref.current.offsetHeight) {
8
- heightRef.current = ref.current.offsetHeight;
9
- setHeight(ref.current.offsetHeight);
10
- }
11
- }));
12
- React.useLayoutEffect(() => {
13
- if (ref.current) {
14
- setHeight(ref.current.offsetHeight);
15
- resizeObserver.observe(ref.current, {});
16
- }
17
- return () => resizeObserver.disconnect();
18
- }, [resizeObserver]);
19
- return [ref, height];
20
- };
@@ -1,9 +0,0 @@
1
- import React from "react";
2
- type UsePictureInPictureValues = {
3
- isPipAvailable: boolean;
4
- isPipActive: boolean;
5
- enterPip: () => Promise<void>;
6
- exitPip: () => Promise<void>;
7
- };
8
- export declare function usePictureInPicture(videoRef: React.MutableRefObject<HTMLVideoElement | null>): UsePictureInPictureValues;
9
- export {};
@@ -1,60 +0,0 @@
1
- import React from "react";
2
- export function usePictureInPicture(videoRef) {
3
- const [isPipAvailable, setIsPipAvailable] = React.useState(false);
4
- const [isPipActive, setIsPipActive] = React.useState(false);
5
- const pipWindowRef = React.useRef(null);
6
- React.useEffect(() => {
7
- setIsPipAvailable(document.pictureInPictureEnabled === true);
8
- }, []);
9
- const enterPip = React.useCallback(async () => {
10
- const videoElement = videoRef.current;
11
- if (videoElement === null || isPipAvailable === false) {
12
- return;
13
- }
14
- try {
15
- pipWindowRef.current = await videoElement.requestPictureInPicture();
16
- setIsPipActive(true);
17
- videoElement.play();
18
- }
19
- catch (error) {
20
- console.error("Failed to enter Picture-in-Picture mode:", { error });
21
- }
22
- }, [isPipAvailable, videoRef]);
23
- const exitPip = React.useCallback(async () => {
24
- if (document.pictureInPictureElement === null) {
25
- return;
26
- }
27
- try {
28
- await document.exitPictureInPicture();
29
- setIsPipActive(false);
30
- }
31
- catch (error) {
32
- console.error("Failed to exit Picture-in-Picture mode:", { error });
33
- }
34
- }, []);
35
- const handlePipChange = React.useCallback(() => {
36
- const isPipActive = document.pictureInPictureElement === videoRef.current;
37
- setIsPipActive(isPipActive);
38
- }, [videoRef]);
39
- React.useEffect(() => {
40
- const videoElement = videoRef.current;
41
- if (videoElement === null) {
42
- return;
43
- }
44
- videoElement.addEventListener("enterpictureinpicture", handlePipChange);
45
- videoElement.addEventListener("leavepictureinpicture", handlePipChange);
46
- return () => {
47
- videoElement.removeEventListener("enterpictureinpicture", handlePipChange);
48
- videoElement.removeEventListener("leavepictureinpicture", handlePipChange);
49
- };
50
- }, [handlePipChange, videoRef]);
51
- const values = React.useMemo(() => {
52
- return {
53
- isPipAvailable,
54
- isPipActive,
55
- enterPip,
56
- exitPip,
57
- };
58
- }, [enterPip, exitPip, isPipActive, isPipAvailable]);
59
- return values;
60
- }
@@ -1,4 +0,0 @@
1
- import React from "react";
2
- type Callback = (entry: DOMRectReadOnly) => void;
3
- export declare function useResizeObserver<T extends Element>(ref: React.MutableRefObject<T | null> | T | null, callback: Callback | undefined): void;
4
- export {};
@@ -1,31 +0,0 @@
1
- import { ResizeObserver } from "@juggle/resize-observer";
2
- import React from "react";
3
- export function useResizeObserver(ref, callback) {
4
- React.useEffect(() => {
5
- const element = ref === null ? null : "current" in ref ? ref.current : ref;
6
- if (element === null || callback === undefined) {
7
- return;
8
- }
9
- const observer = new ResizeObserver((entries) => {
10
- // https://stackoverflow.com/questions/49384120/resizeobserver-loop-limit-exceeded/58701523#58701523
11
- requestAnimationFrame(() => {
12
- if (!Array.isArray(entries) || !entries.length) {
13
- return;
14
- }
15
- // By the time the animation frame fires, the DOM might have be unmounted already
16
- if (element === null || callback === undefined) {
17
- return;
18
- }
19
- const entry = entries[0];
20
- if (entry === undefined) {
21
- return;
22
- }
23
- callback(entry.contentRect);
24
- });
25
- });
26
- observer.observe(element);
27
- return () => {
28
- observer.disconnect();
29
- };
30
- }, [ref, callback]);
31
- }