@yamada-ui/utils
Advanced tools
Comparing version 1.5.2 to 1.5.3-dev-20241006000212
@@ -14,2 +14,3 @@ import { Dict } from './index.types.js'; | ||
declare function isEmpty(value: any): boolean; | ||
declare function isEmptyObject(value: any): boolean; | ||
declare function isFunction<T extends Function = Function>(value: any): value is T; | ||
@@ -19,2 +20,2 @@ declare function isUnit(value: any): boolean; | ||
export { cast, is, isArray, isBoolean, isEmpty, isFunction, isNotNumber, isNull, isNumber, isNumeric, isObject, isString, isUndefined, isUnit }; | ||
export { cast, is, isArray, isBoolean, isEmpty, isEmptyObject, isFunction, isNotNumber, isNull, isNumber, isNumeric, isObject, isString, isUndefined, isUnit }; |
@@ -28,2 +28,3 @@ "use strict"; | ||
isEmpty: () => isEmpty, | ||
isEmptyObject: () => isEmptyObject, | ||
isFunction: () => isFunction, | ||
@@ -59,3 +60,3 @@ isNotNumber: () => isNotNumber, | ||
function isUndefined(value) { | ||
return typeof value === "undefined" && value === void 0; | ||
return typeof value === "undefined"; | ||
} | ||
@@ -74,2 +75,5 @@ function isNull(value) { | ||
} | ||
function isEmptyObject(value) { | ||
return isObject(value) && !Object.keys(value).length; | ||
} | ||
function isFunction(value) { | ||
@@ -93,2 +97,3 @@ return typeof value === "function"; | ||
isEmpty, | ||
isEmptyObject, | ||
isFunction, | ||
@@ -95,0 +100,0 @@ isNotNumber, |
@@ -1,2 +0,2 @@ | ||
type Operand = string | number; | ||
type Operand = number | string; | ||
declare function add(...args: Operand[]): string; | ||
@@ -9,6 +9,6 @@ declare function subtract(...args: Operand[]): string; | ||
add: (...args: Operand[]) => Calc; | ||
subtract: (...args: Operand[]) => Calc; | ||
divide: (...args: Operand[]) => Calc; | ||
multiply: (...args: Operand[]) => Calc; | ||
divide: (...args: Operand[]) => Calc; | ||
negate: () => Calc; | ||
subtract: (...args: Operand[]) => Calc; | ||
toString: () => string; | ||
@@ -18,8 +18,8 @@ } | ||
add: typeof add; | ||
subtract: typeof subtract; | ||
divide: typeof divide; | ||
multiply: typeof multiply; | ||
divide: typeof divide; | ||
negate: typeof negate; | ||
subtract: typeof subtract; | ||
}; | ||
export { type Operand, calc }; |
@@ -42,3 +42,3 @@ "use strict"; | ||
function negate(value) { | ||
if (value != null && !isNaN(parseFloat(value.toString()))) | ||
if (!isNaN(parseFloat(value.toString()))) | ||
return String(value).startsWith("-") ? String(value).slice(1) : `-${value}`; | ||
@@ -50,6 +50,6 @@ return multiply(value, -1); | ||
add: (...args) => calc(add(x, ...args)), | ||
subtract: (...args) => calc(subtract(x, ...args)), | ||
divide: (...args) => calc(divide(x, ...args)), | ||
multiply: (...args) => calc(multiply(x, ...args)), | ||
divide: (...args) => calc(divide(x, ...args)), | ||
negate: () => calc(negate(x)), | ||
subtract: (...args) => calc(subtract(x, ...args)), | ||
toString: () => x.toString() | ||
@@ -59,6 +59,6 @@ }), | ||
add, | ||
subtract, | ||
divide, | ||
multiply, | ||
divide, | ||
negate | ||
negate, | ||
subtract | ||
} | ||
@@ -65,0 +65,0 @@ ); |
import { StringLiteral, Dict } from './index.types.js'; | ||
type Breakpoint = "base" | StringLiteral; | ||
type ColorMode = "light" | "dark"; | ||
type ColorFormat = "hex" | "hexa" | "rgba" | "rgb" | "hsl" | "hsla"; | ||
type ColorMode = "dark" | "light"; | ||
type ColorFormat = "hex" | "hexa" | "hsl" | "hsla" | "rgb" | "rgba"; | ||
declare const SEMANTIC_COLOR_SCHEMES: readonly ["mono", "primary", "secondary", "info", "success", "warning", "danger", "link"]; | ||
@@ -19,7 +19,7 @@ declare const COLOR_SCHEMES: readonly ["gray", "neutral", "red", "rose", "pink", "flashy", "orange", "amber", "yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue", "indigo", "violet", "purple", "fuchsia"]; | ||
declare function transparentizeColor(color: string, alpha: number): (theme?: Dict, colorMode?: ColorMode, breakpoint?: Breakpoint) => string; | ||
declare function randomColor({ string, colors, }?: { | ||
declare function randomColor({ colors, string, }?: { | ||
colors?: string[]; | ||
string?: string; | ||
colors?: string[]; | ||
}): string; | ||
declare function isTone(color: string): (theme?: Dict, colorMode?: ColorMode, breakpoint?: Breakpoint) => "light" | "dark"; | ||
declare function isTone(color: string): (theme?: Dict, colorMode?: ColorMode, breakpoint?: Breakpoint) => "dark" | "light"; | ||
declare function isLight(color: string): (theme?: Dict, colorMode?: ColorMode) => boolean; | ||
@@ -26,0 +26,0 @@ declare function isDark(color: string): (theme?: Dict, colorMode?: ColorMode) => boolean; |
@@ -71,3 +71,3 @@ "use strict"; | ||
function isUndefined(value) { | ||
return typeof value === "undefined" && value === void 0; | ||
return typeof value === "undefined"; | ||
} | ||
@@ -83,6 +83,7 @@ function isObject(value) { | ||
function getObject(obj, path, fallback, i) { | ||
const k = isString(path) ? path.split(/\[(.*?)\]|\./).filter(Boolean) : [path]; | ||
for (i = 0; i < k.length; i += 1) { | ||
const keys = isString(path) ? path.split(/\[(.*?)\]|\./).filter(Boolean) : [path]; | ||
for (i = 0; i < keys.length; i += 1) { | ||
if (!obj) break; | ||
obj = obj[k[i]]; | ||
const key = keys[i]; | ||
obj = key ? obj[key] : void 0; | ||
} | ||
@@ -226,9 +227,10 @@ return obj === void 0 ? fallback : obj; | ||
function randomColor({ | ||
string, | ||
colors | ||
colors, | ||
string | ||
} = {}) { | ||
var _a, _b; | ||
const fallback = randomHex(); | ||
if (string && colors) return randomColorFromList(string, colors); | ||
if (string && colors) return (_a = randomColorFromList(string, colors)) != null ? _a : fallback; | ||
if (string && !colors) return randomColorFromString(string); | ||
if (colors && !string) return randomFromList(colors); | ||
if (colors && !string) return (_b = randomFromList(colors)) != null ? _b : fallback; | ||
return fallback; | ||
@@ -235,0 +237,0 @@ } |
@@ -17,5 +17,5 @@ import React__default from 'react'; | ||
declare function isContains(parent: HTMLElement | null, child: HTMLElement | null): boolean | undefined; | ||
declare function getPx(value: string | number | undefined): number; | ||
declare function getPx(value: number | string | undefined): number; | ||
declare function getEventRelatedTarget(ev: React__default.FocusEvent | React__default.MouseEvent): HTMLElement | null; | ||
type Booleanish = boolean | "true" | "false"; | ||
type Booleanish = "false" | "true" | boolean; | ||
declare function dataAttr(condition: boolean | undefined): Booleanish; | ||
@@ -31,7 +31,7 @@ declare function ariaAttr(condition: boolean | undefined): boolean | undefined; | ||
declare function isTouchDevice(): boolean; | ||
declare function getOwnerWindow(node?: Element | null): Window & typeof globalThis; | ||
declare function getOwnerWindow(node?: Element | null): typeof globalThis & Window; | ||
declare function getOwnerDocument(el?: Element | null): Document; | ||
declare function getActiveElement(el?: HTMLElement): HTMLElement; | ||
declare function getActiveElement(el?: HTMLElement): HTMLElement | null; | ||
declare function isActiveElement(el: HTMLElement): boolean; | ||
export { type FocusableElement, ariaAttr, createdDom, dataAttr, getActiveElement, getAllFocusable, getEventRelatedTarget, getOwnerDocument, getOwnerWindow, getPlatform, getPx, hasNegativeTabIndex, hasTabIndex, isActiveElement, isApple, isContains, isContentEditable, isDisabled, isElement, isFocusable, isHTMLElement, isHidden, isMac, isSafari, isTabbable, isTouchDevice, platform, vendor }; |
@@ -58,3 +58,3 @@ "use strict"; | ||
function isUndefined(value) { | ||
return typeof value === "undefined" && value === void 0; | ||
return typeof value === "undefined"; | ||
} | ||
@@ -64,3 +64,5 @@ | ||
function createdDom() { | ||
return !!(typeof window !== "undefined" && window.document && window.document.createElement); | ||
var _a; | ||
return !!(typeof window !== "undefined" && // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
((_a = window.document) == null ? void 0 : _a.createElement)); | ||
} | ||
@@ -100,3 +102,3 @@ function getPlatform() { | ||
function isDisabled(el) { | ||
return Boolean(el.getAttribute("disabled")) === true || Boolean(el.getAttribute("data-disabled")) === true || Boolean(el.getAttribute("aria-disabled")) === true; | ||
return Boolean(el.getAttribute("disabled")) || Boolean(el.getAttribute("data-disabled")) || Boolean(el.getAttribute("aria-disabled")); | ||
} | ||
@@ -169,3 +171,3 @@ function isVisible(el) { | ||
const focusableTags = ["input", "select", "textarea", "button"]; | ||
if (focusableTags.indexOf(localName) >= 0) return true; | ||
if (focusableTags.includes(localName)) return true; | ||
const others = { | ||
@@ -190,4 +192,4 @@ a: () => el.hasAttribute("href"), | ||
function getOwnerWindow(node) { | ||
var _a, _b; | ||
return (_b = (_a = getOwnerDocument(node)) == null ? void 0 : _a.defaultView) != null ? _b : window; | ||
var _a; | ||
return (_a = getOwnerDocument(node).defaultView) != null ? _a : window; | ||
} | ||
@@ -194,0 +196,0 @@ function getOwnerDocument(el) { |
@@ -1,3 +0,3 @@ | ||
type AnyPointerEvent = MouseEvent | TouchEvent | PointerEvent; | ||
type PointType = "page" | "client"; | ||
type AnyPointerEvent = MouseEvent | PointerEvent | TouchEvent; | ||
type PointType = "client" | "page"; | ||
interface Point { | ||
@@ -4,0 +4,0 @@ x: number; |
@@ -51,4 +51,5 @@ "use strict"; | ||
function pointFromTouch(e, type = "page") { | ||
var _a, _b; | ||
const point = e.touches[0] || e.changedTouches[0]; | ||
return { x: point[`${type}X`], y: point[`${type}Y`] }; | ||
return { x: (_a = point == null ? void 0 : point[`${type}X`]) != null ? _a : 0, y: (_b = point == null ? void 0 : point[`${type}Y`]) != null ? _b : 0 }; | ||
} | ||
@@ -73,3 +74,3 @@ function pointFromMouse(point, type = "page") { | ||
const isMouse = isMouseEvent(ev); | ||
if (!isMouse || isMouse && ev.button === 0) cb(ev); | ||
if (!isMouse || ev.button === 0) cb(ev); | ||
}; | ||
@@ -76,0 +77,0 @@ } |
declare const noop: () => void; | ||
declare function runIfFunc<T, U extends any[]>(valOrFunc: T | ((...funcArgs: U) => T), ...args: U): T; | ||
declare function runIfFunc<T, U extends any[]>(valOrFunc: ((...funcArgs: U) => T) | T, ...args: U): T; | ||
declare function handlerAll<T extends (event: any, ...args: any[]) => void>(...funcs: (T | undefined)[]): (event: T extends (event: infer R, ...args: any[]) => any ? R : never, ...args: T extends (event: any, ...args: infer R) => any ? R : never) => void; | ||
@@ -4,0 +4,0 @@ declare function funcAll<T extends (...args: any[]) => any>(...funcs: (T | undefined)[]): (...args: T extends (...args: infer R) => any ? R : never) => void; |
@@ -0,14 +1,14 @@ | ||
export { filterEmpty } from './array.js'; | ||
export { cast, is, isArray, isBoolean, isEmpty, isEmptyObject, isFunction, isNotNumber, isNull, isNumber, isNumeric, isObject, isString, isUndefined, isUnit } from './assertion.js'; | ||
export { Operand, calc } from './calc.js'; | ||
export { COLOR_SCHEMES, ColorFormat, ColorScheme, SEMANTIC_COLOR_SCHEMES, SemanticColorScheme, TONES, alphaToHex, calcFormat, convertColor, darkenColor, getAlpha, getColor, hslaTo, hsvTo, isAccessible, isDark, isGray, isLight, isTone, lightenColor, parseToHsla, parseToHsv, parseToRgba, randomColor, rgbaTo, sameColor, shadeColor, tintColor, transparentizeColor } from './color.js'; | ||
export { FocusableElement, ariaAttr, createdDom, dataAttr, getActiveElement, getAllFocusable, getEventRelatedTarget, getOwnerDocument, getOwnerWindow, getPlatform, getPx, hasNegativeTabIndex, hasTabIndex, isActiveElement, isApple, isContains, isContentEditable, isDisabled, isElement, isFocusable, isHTMLElement, isHidden, isMac, isSafari, isTabbable, isTouchDevice, platform, vendor } from './dom.js'; | ||
export { AnyPointerEvent, MixedEventListener, Point, PointType, PointerEventInfo, addDomEvent, addPointerEvent, getEventPoint, getEventWindow, isMouseEvent, isMultiTouchEvent, isTouchEvent, pointFromMouse, pointFromTouch } from './event.js'; | ||
export { funcAll, handlerAll, noop, runIfFunc } from './function.js'; | ||
export { Dict, Length, Merge, MergeIfDefined, ObjectLiteral, Path, Primitive, Replace, StringLiteral, Union } from './index.types.js'; | ||
export { cast, is, isArray, isBoolean, isEmpty, isFunction, isNotNumber, isNull, isNumber, isNumeric, isObject, isString, isUndefined, isUnit } from './assertion.js'; | ||
export { interopDefault } from './module.js'; | ||
export { clampNumber, countDecimal, percentToValue, roundNumberToStep, toNumber, toPrecision, valueToPercent } from './number.js'; | ||
export { FlattenObjectOptions, assignAfter, filterObject, filterUndefined, flattenObject, getMemoizedObject, getObject, keysFormObject, memoizeObject, merge, objectFromEntries, omitObject, pickObject, replaceObject, splitObject } from './object.js'; | ||
export { funcAll, handlerAll, noop, runIfFunc } from './function.js'; | ||
export { AsyncFnReturn, AsyncState, AsyncStateRetry, FunctionReturningPromise, MaybeRenderProp, PromiseType, UseIsMountedProps, UseIsMountedReturn, assignRef, createContext, createId, cx, findChild, findChildren, getValidChildren, includesChildren, isRefObject, isSomeElement, isValidElement, mergeRefs, omitChildren, pickChildren, useAsync, useAsyncFunc, useAsyncRetry, useCallbackRef, useIsMounted, useMergeRefs, useSafeLayoutEffect, useUnmountEffect, useUpdateEffect } from './react.js'; | ||
export { FocusableElement, ariaAttr, createdDom, dataAttr, getActiveElement, getAllFocusable, getEventRelatedTarget, getOwnerDocument, getOwnerWindow, getPlatform, getPx, hasNegativeTabIndex, hasTabIndex, isActiveElement, isApple, isContains, isContentEditable, isDisabled, isElement, isFocusable, isHTMLElement, isHidden, isMac, isSafari, isTabbable, isTouchDevice, platform, vendor } from './dom.js'; | ||
export { antonym, escape, toCamelCase, toKebabCase, toTitleCase } from './string.js'; | ||
export { Operand, calc } from './calc.js'; | ||
export { COLOR_SCHEMES, ColorFormat, ColorScheme, SEMANTIC_COLOR_SCHEMES, SemanticColorScheme, TONES, alphaToHex, calcFormat, convertColor, darkenColor, getAlpha, getColor, hslaTo, hsvTo, isAccessible, isDark, isGray, isLight, isTone, lightenColor, parseToHsla, parseToHsv, parseToRgba, randomColor, rgbaTo, sameColor, shadeColor, tintColor, transparentizeColor } from './color.js'; | ||
export { filterEmpty } from './array.js'; | ||
export { clampNumber, countDecimal, percentToValue, roundNumberToStep, toNumber, toPrecision, valueToPercent } from './number.js'; | ||
export { AnyPointerEvent, MixedEventListener, Point, PointType, PointerEventInfo, addDomEvent, addPointerEvent, getEventPoint, getEventWindow, isMouseEvent, isMultiTouchEvent, isTouchEvent, pointFromMouse, pointFromTouch } from './event.js'; | ||
export { interopDefault } from './module.js'; | ||
import 'react'; |
1041
dist/index.js
@@ -96,2 +96,3 @@ "use strict"; | ||
isEmpty: () => isEmpty, | ||
isEmptyObject: () => isEmptyObject, | ||
isFocusable: () => isFocusable, | ||
@@ -169,2 +170,7 @@ isFunction: () => isFunction, | ||
// src/array.ts | ||
function filterEmpty(array) { | ||
return array.filter((value) => value != null); | ||
} | ||
// src/assertion.ts | ||
@@ -190,3 +196,3 @@ function is(x, y) { | ||
function isUndefined(value) { | ||
return typeof value === "undefined" && value === void 0; | ||
return typeof value === "undefined"; | ||
} | ||
@@ -205,2 +211,5 @@ function isNull(value) { | ||
} | ||
function isEmptyObject(value) { | ||
return isObject(value) && !Object.keys(value).length; | ||
} | ||
function isFunction(value) { | ||
@@ -218,2 +227,44 @@ return typeof value === "function"; | ||
// src/calc.ts | ||
function toExpression(operator, ...args) { | ||
return args.join(` ${operator} `).replace(/calc/g, ""); | ||
} | ||
function add(...args) { | ||
return `calc(${toExpression("+", ...args)})`; | ||
} | ||
function subtract(...args) { | ||
return `calc(${toExpression("-", ...args)})`; | ||
} | ||
function multiply(...args) { | ||
return `calc(${toExpression("*", ...args)})`; | ||
} | ||
function divide(...args) { | ||
return `calc(${toExpression("/", ...args)})`; | ||
} | ||
function negate(value) { | ||
if (!isNaN(parseFloat(value.toString()))) | ||
return String(value).startsWith("-") ? String(value).slice(1) : `-${value}`; | ||
return multiply(value, -1); | ||
} | ||
var calc = Object.assign( | ||
(x) => ({ | ||
add: (...args) => calc(add(x, ...args)), | ||
divide: (...args) => calc(divide(x, ...args)), | ||
multiply: (...args) => calc(multiply(x, ...args)), | ||
negate: () => calc(negate(x)), | ||
subtract: (...args) => calc(subtract(x, ...args)), | ||
toString: () => x.toString() | ||
}), | ||
{ | ||
add, | ||
divide, | ||
multiply, | ||
negate, | ||
subtract | ||
} | ||
); | ||
// src/color.ts | ||
var c = __toESM(require("color2k")); | ||
// src/object.ts | ||
@@ -223,7 +274,7 @@ function omitObjectHelper(obj, path) { | ||
const [primaryKey, ...restKeys] = path; | ||
if (restKeys.length === 0 && primaryKey in obj) { | ||
if (restKeys.length === 0 && primaryKey && primaryKey in obj) { | ||
const { [primaryKey]: _, ...rest } = obj; | ||
return rest; | ||
} | ||
if (obj[primaryKey] && isObject(obj[primaryKey])) { | ||
if (primaryKey && obj[primaryKey] && isObject(obj[primaryKey])) { | ||
return { | ||
@@ -307,3 +358,3 @@ ...obj, | ||
return Object.entries(obj).reduce((result, [key, value]) => { | ||
if (isObject(value) && !Object.keys(value).some((key2) => omitKeys == null ? void 0 : omitKeys.includes(key2)) && (!shouldProcess || shouldProcess(value))) { | ||
if (isObject(value) && !Object.keys(value).some((key2) => omitKeys.includes(key2)) && (!shouldProcess || shouldProcess(value))) { | ||
Object.entries( | ||
@@ -347,6 +398,7 @@ flattenObject(value, { | ||
function getObject(obj, path, fallback, i) { | ||
const k = isString(path) ? path.split(/\[(.*?)\]|\./).filter(Boolean) : [path]; | ||
for (i = 0; i < k.length; i += 1) { | ||
const keys = isString(path) ? path.split(/\[(.*?)\]|\./).filter(Boolean) : [path]; | ||
for (i = 0; i < keys.length; i += 1) { | ||
if (!obj) break; | ||
obj = obj[k[i]]; | ||
const key = keys[i]; | ||
obj = key ? obj[key] : void 0; | ||
} | ||
@@ -370,4 +422,2 @@ return obj === void 0 ? fallback : obj; | ||
function assignAfter(target, ...sources) { | ||
if (target == null) | ||
throw new TypeError("Cannot convert undefined or null to object"); | ||
const result = { ...target }; | ||
@@ -385,475 +435,3 @@ for (const nextSource of sources) { | ||
// src/function.ts | ||
var noop = () => { | ||
}; | ||
function runIfFunc(valOrFunc, ...args) { | ||
return isFunction(valOrFunc) ? valOrFunc(...args) : valOrFunc; | ||
} | ||
function handlerAll(...funcs) { | ||
return function(event, ...args) { | ||
funcs.some((func) => { | ||
func == null ? void 0 : func(event, ...args); | ||
return event == null ? void 0 : event.defaultPrevented; | ||
}); | ||
}; | ||
} | ||
function funcAll(...funcs) { | ||
return function(...args) { | ||
return funcs.forEach((func) => func == null ? void 0 : func(...args)); | ||
}; | ||
} | ||
// src/react.tsx | ||
var React = __toESM(require("react")); | ||
function createContext2({ | ||
strict = true, | ||
errorMessage = "useContext: `context` is undefined. Seems you forgot to wrap component within the Provider", | ||
name, | ||
defaultValue | ||
} = {}) { | ||
const Context = React.createContext(defaultValue); | ||
Context.displayName = name; | ||
const useContext2 = () => { | ||
var _a; | ||
const context = React.useContext(Context); | ||
if (!context && strict) { | ||
const error = new Error(errorMessage); | ||
error.name = "ContextError"; | ||
(_a = Error.captureStackTrace) == null ? void 0 : _a.call(Error, error, useContext2); | ||
throw error; | ||
} | ||
return context; | ||
}; | ||
return [ | ||
Context.Provider, | ||
useContext2, | ||
Context | ||
]; | ||
} | ||
var useSafeLayoutEffect = Boolean(globalThis == null ? void 0 : globalThis.document) ? React.useLayoutEffect : React.useEffect; | ||
function useUnmountEffect(callback) { | ||
return React.useEffect(() => () => callback(), []); | ||
} | ||
function useIsMounted({ | ||
rerender = false, | ||
delay = 0 | ||
} = {}) { | ||
const isMountedRef = React.useRef(false); | ||
const [isMounted, setIsMounted] = React.useState(false); | ||
useSafeLayoutEffect(() => { | ||
isMountedRef.current = true; | ||
let timeoutId = null; | ||
if (rerender) { | ||
if (delay > 0) { | ||
timeoutId = setTimeout(() => setIsMounted(true), delay); | ||
} else { | ||
setIsMounted(true); | ||
} | ||
} | ||
return () => { | ||
isMountedRef.current = false; | ||
if (rerender) setIsMounted(false); | ||
if (timeoutId) clearTimeout(timeoutId); | ||
}; | ||
}, [delay, rerender]); | ||
return [React.useCallback(() => isMountedRef.current, []), isMounted]; | ||
} | ||
function getValidChildren(children) { | ||
return React.Children.toArray(children).filter( | ||
(child) => React.isValidElement(child) | ||
); | ||
} | ||
function isValidElement2(child) { | ||
return React.isValidElement(child) || isString(child) || isNumber(child); | ||
} | ||
function isSomeElement(child, type) { | ||
var _a, _b; | ||
if (child.type === type) return true; | ||
if (!!child.__ui__ && !!type.__ui__) { | ||
if (child.__ui__ === type.__ui__) return true; | ||
} | ||
const payload = child.type._payload; | ||
if (!!((_a = payload == null ? void 0 : payload.value) == null ? void 0 : _a.__ui__) && !!type.__ui__) { | ||
if (((_b = payload == null ? void 0 : payload.value) == null ? void 0 : _b.__ui__) === type.__ui__) return true; | ||
} | ||
return false; | ||
} | ||
function findChild(children, ...types) { | ||
const child = children.find( | ||
(child2) => types.some((type) => isSomeElement(child2, type)) | ||
); | ||
return child; | ||
} | ||
function findChildren(children, ...types) { | ||
const child = children.find( | ||
(child2) => types.some((type) => isSomeElement(child2, type)) | ||
); | ||
if (child) { | ||
return children.sort((a, b) => { | ||
if (types.some((type) => isSomeElement(a, type))) { | ||
return -1; | ||
} else if (types.some((type) => isSomeElement(b, type))) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
} else { | ||
return [void 0, ...children]; | ||
} | ||
} | ||
function includesChildren(children, ...types) { | ||
return children.some((child) => { | ||
if (types.some((type) => isSomeElement(child, type))) return true; | ||
const children2 = getValidChildren(child.props.children); | ||
return children2.length ? includesChildren(children2, ...types) : false; | ||
}); | ||
} | ||
function omitChildren(children, ...types) { | ||
return children.filter( | ||
(child) => types.every((type) => !isSomeElement(child, type)) | ||
); | ||
} | ||
function pickChildren(children, ...types) { | ||
return children.filter( | ||
(child) => types.every((type) => isSomeElement(child, type)) | ||
); | ||
} | ||
function cx(...classNames) { | ||
return classNames.filter(Boolean).join(" "); | ||
} | ||
function isRefObject(val) { | ||
return isObject(val) && "current" in val; | ||
} | ||
function assignRef(ref, value) { | ||
if (ref == null) return; | ||
if (typeof ref === "function") { | ||
ref(value); | ||
return; | ||
} | ||
try { | ||
ref.current = value; | ||
} catch { | ||
throw new Error(`Cannot assign value '${value}' to ref '${ref}'`); | ||
} | ||
} | ||
function mergeRefs(...refs) { | ||
return function(node) { | ||
return refs.forEach((ref) => { | ||
assignRef(ref, node); | ||
}); | ||
}; | ||
} | ||
function useMergeRefs(...refs) { | ||
return React.useMemo(() => mergeRefs(...refs), [refs]); | ||
} | ||
function useCallbackRef(callback, deps = []) { | ||
const callbackRef = React.useRef(callback); | ||
React.useEffect(() => { | ||
callbackRef.current = callback; | ||
}); | ||
return React.useCallback( | ||
(...args) => { | ||
var _a; | ||
return (_a = callbackRef.current) == null ? void 0 : _a.call(callbackRef, ...args); | ||
}, | ||
deps | ||
); | ||
} | ||
function useUpdateEffect(callback, deps) { | ||
const renderCycleRef = React.useRef(false); | ||
const effectCycleRef = React.useRef(false); | ||
React.useEffect(() => { | ||
const mounted = renderCycleRef.current; | ||
const run = mounted && effectCycleRef.current; | ||
if (run) return callback(); | ||
effectCycleRef.current = true; | ||
}, deps); | ||
React.useEffect(() => { | ||
renderCycleRef.current = true; | ||
return () => { | ||
renderCycleRef.current = false; | ||
}; | ||
}, []); | ||
} | ||
function useAsync(func, deps = []) { | ||
const [state, callback] = useAsyncFunc(func, deps, { loading: true }); | ||
React.useEffect(() => { | ||
callback(); | ||
}, [callback]); | ||
return state; | ||
} | ||
function useAsyncFunc(func, deps = [], initialState = { loading: false }) { | ||
const lastCallId = React.useRef(0); | ||
const [isMounted] = useIsMounted(); | ||
const [state, setState] = React.useState(initialState); | ||
const callback = React.useCallback( | ||
(...args) => { | ||
const callId = ++lastCallId.current; | ||
if (!state.loading) | ||
setState((prevState) => ({ ...prevState, loading: true })); | ||
return func(...args).then( | ||
(value) => { | ||
if (isMounted() && callId === lastCallId.current) | ||
setState({ value, loading: false }); | ||
return value; | ||
}, | ||
(error) => { | ||
if (isMounted() && callId === lastCallId.current) | ||
setState({ error, loading: false }); | ||
return error; | ||
} | ||
); | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
deps | ||
); | ||
return [state, callback]; | ||
} | ||
function useAsyncRetry(func, deps = []) { | ||
const [attempt, setAttempt] = React.useState(0); | ||
const state = useAsync(func, [...deps, attempt]); | ||
const stateLoading = state.loading; | ||
const retry = React.useCallback(() => { | ||
if (stateLoading) return; | ||
setAttempt((currentAttempt) => currentAttempt + 1); | ||
}, [...deps, stateLoading]); | ||
return { ...state, retry }; | ||
} | ||
var createIdCounter = 0; | ||
function createId(prefix) { | ||
return `${prefix}-${++createIdCounter}-${(/* @__PURE__ */ new Date()).getTime()}`; | ||
} | ||
// src/dom.ts | ||
function createdDom() { | ||
return !!(typeof window !== "undefined" && window.document && window.document.createElement); | ||
} | ||
function getPlatform() { | ||
var _a, _b; | ||
return (_b = (_a = navigator.userAgentData) == null ? void 0 : _a.platform) != null ? _b : navigator.platform; | ||
} | ||
function vendor(v) { | ||
return createdDom() && v.test(navigator.vendor); | ||
} | ||
function platform(v) { | ||
return createdDom() && v.test(getPlatform()); | ||
} | ||
function isMac() { | ||
return platform(/^mac/i); | ||
} | ||
function isApple() { | ||
return platform(/mac|iphone|ipad|ipod/i); | ||
} | ||
function isSafari() { | ||
return isApple() && vendor(/apple/i); | ||
} | ||
function isElement(el) { | ||
return el != null && typeof el == "object" && "nodeType" in el && el.nodeType === Node.ELEMENT_NODE; | ||
} | ||
function isHTMLElement(el) { | ||
var _a; | ||
if (!isElement(el)) return false; | ||
const win = (_a = el.ownerDocument.defaultView) != null ? _a : window; | ||
return el instanceof win.HTMLElement; | ||
} | ||
function isHidden(el) { | ||
if (el.parentElement && isHidden(el.parentElement)) return true; | ||
return el.hidden; | ||
} | ||
function isDisabled(el) { | ||
return Boolean(el.getAttribute("disabled")) === true || Boolean(el.getAttribute("data-disabled")) === true || Boolean(el.getAttribute("aria-disabled")) === true; | ||
} | ||
function isVisible(el) { | ||
return el.offsetWidth > 0 && el.offsetHeight > 0; | ||
} | ||
function hasTabIndex(el) { | ||
return el.hasAttribute("tabindex"); | ||
} | ||
function isContentEditable(el) { | ||
const value = el.getAttribute("contenteditable"); | ||
return value !== "false" && value != null; | ||
} | ||
function isContains(parent, child) { | ||
return parent === child || (parent == null ? void 0 : parent.contains(child)); | ||
} | ||
function getPx(value) { | ||
if (isNumber(value)) return value; | ||
if (isUndefined(value)) return 0; | ||
if (value.includes("px")) return parseFloat(value); | ||
const isBrowser = createdDom(); | ||
let fontSize = 16; | ||
if (isBrowser) { | ||
const style = window.getComputedStyle(document.documentElement); | ||
const computedFontSize = parseFloat(style.fontSize); | ||
if (!isNaN(computedFontSize)) fontSize = computedFontSize; | ||
} | ||
return parseFloat(value) * fontSize; | ||
} | ||
function getEventRelatedTarget(ev) { | ||
var _a; | ||
return (_a = ev.relatedTarget) != null ? _a : ev.currentTarget.ownerDocument.activeElement; | ||
} | ||
function dataAttr(condition) { | ||
return condition ? "" : void 0; | ||
} | ||
function ariaAttr(condition) { | ||
return condition ? true : void 0; | ||
} | ||
var focusableElList = [ | ||
"input:not(:disabled):not([disabled])", | ||
"select:not(:disabled):not([disabled])", | ||
"textarea:not(:disabled):not([disabled])", | ||
"embed", | ||
"iframe", | ||
"object", | ||
"a[href]", | ||
"area[href]", | ||
"button:not(:disabled):not([disabled])", | ||
"[tabindex]", | ||
"audio[controls]", | ||
"video[controls]", | ||
"*[tabindex]:not([aria-disabled])", | ||
"*[contenteditable]" | ||
]; | ||
var focusableElSelector = focusableElList.join(); | ||
function getAllFocusable(container) { | ||
const focusableEls = Array.from( | ||
container.querySelectorAll(focusableElSelector) | ||
); | ||
focusableEls.unshift(container); | ||
return focusableEls.filter((el) => isFocusable(el) && isVisible(el)); | ||
} | ||
function isFocusable(el) { | ||
if (!isHTMLElement(el) || isHidden(el) || isDisabled(el)) { | ||
return false; | ||
} | ||
const { localName } = el; | ||
const focusableTags = ["input", "select", "textarea", "button"]; | ||
if (focusableTags.indexOf(localName) >= 0) return true; | ||
const others = { | ||
a: () => el.hasAttribute("href"), | ||
audio: () => el.hasAttribute("controls"), | ||
video: () => el.hasAttribute("controls") | ||
}; | ||
if (localName in others) return others[localName](); | ||
if (isContentEditable(el)) return true; | ||
return hasTabIndex(el); | ||
} | ||
function hasNegativeTabIndex(el) { | ||
return hasTabIndex(el) && el.tabIndex === -1; | ||
} | ||
function isTabbable(el) { | ||
return el ? isHTMLElement(el) && isFocusable(el) && !hasNegativeTabIndex(el) : false; | ||
} | ||
function isTouchDevice() { | ||
return "ontouchstart" in window; | ||
} | ||
function getOwnerWindow(node) { | ||
var _a, _b; | ||
return (_b = (_a = getOwnerDocument(node)) == null ? void 0 : _a.defaultView) != null ? _b : window; | ||
} | ||
function getOwnerDocument(el) { | ||
return isElement(el) ? el.ownerDocument : document; | ||
} | ||
function getActiveElement(el) { | ||
return getOwnerDocument(el).activeElement; | ||
} | ||
function isActiveElement(el) { | ||
return getActiveElement(el) === el; | ||
} | ||
// src/string.ts | ||
function escape(value, replaceValue = "") { | ||
return value.replace(/\s+/g, replaceValue); | ||
} | ||
function antonym(value) { | ||
switch (value) { | ||
case "top": | ||
return "bottom"; | ||
case "bottom": | ||
return "top"; | ||
case "left": | ||
return "right"; | ||
case "right": | ||
return "left"; | ||
case "start": | ||
return "end"; | ||
case "end": | ||
return "start"; | ||
case "x": | ||
return "y"; | ||
case "y": | ||
return "x"; | ||
case "enter": | ||
return "exit"; | ||
case "exit": | ||
return "enter"; | ||
case "vertical": | ||
return "horizontal"; | ||
case "horizontal": | ||
return "vertical"; | ||
case "up": | ||
return "down"; | ||
case "down": | ||
return "up"; | ||
case "block": | ||
return "inline"; | ||
case "inline": | ||
return "block"; | ||
default: | ||
return value; | ||
} | ||
} | ||
function toCamelCase(value) { | ||
return value.toLowerCase().replace(/[_-](.)/g, (_, val) => val.toUpperCase()).replace(/^(.)/, (_, val) => val.toUpperCase()); | ||
} | ||
function toKebabCase(value) { | ||
return value.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, "$1-$2").toLowerCase().replace(/^-/, ""); | ||
} | ||
function toTitleCase(value) { | ||
return value.replace(/([A-Z])/g, " $1").replace(/[_-](.)/g, (_, val) => ` ${val.toUpperCase()}`).replace(/^./, (str) => str.toUpperCase()).trim(); | ||
} | ||
// src/calc.ts | ||
function toExpression(operator, ...args) { | ||
return args.join(` ${operator} `).replace(/calc/g, ""); | ||
} | ||
function add(...args) { | ||
return `calc(${toExpression("+", ...args)})`; | ||
} | ||
function subtract(...args) { | ||
return `calc(${toExpression("-", ...args)})`; | ||
} | ||
function multiply(...args) { | ||
return `calc(${toExpression("*", ...args)})`; | ||
} | ||
function divide(...args) { | ||
return `calc(${toExpression("/", ...args)})`; | ||
} | ||
function negate(value) { | ||
if (value != null && !isNaN(parseFloat(value.toString()))) | ||
return String(value).startsWith("-") ? String(value).slice(1) : `-${value}`; | ||
return multiply(value, -1); | ||
} | ||
var calc = Object.assign( | ||
(x) => ({ | ||
add: (...args) => calc(add(x, ...args)), | ||
subtract: (...args) => calc(subtract(x, ...args)), | ||
multiply: (...args) => calc(multiply(x, ...args)), | ||
divide: (...args) => calc(divide(x, ...args)), | ||
negate: () => calc(negate(x)), | ||
toString: () => x.toString() | ||
}), | ||
{ | ||
add, | ||
subtract, | ||
multiply, | ||
divide, | ||
negate | ||
} | ||
); | ||
// src/color.ts | ||
var c = __toESM(require("color2k")); | ||
var SEMANTIC_COLOR_SCHEMES = [ | ||
@@ -978,9 +556,10 @@ "mono", | ||
function randomColor({ | ||
string, | ||
colors | ||
colors, | ||
string | ||
} = {}) { | ||
var _a, _b; | ||
const fallback = randomHex(); | ||
if (string && colors) return randomColorFromList(string, colors); | ||
if (string && colors) return (_a = randomColorFromList(string, colors)) != null ? _a : fallback; | ||
if (string && !colors) return randomColorFromString(string); | ||
if (colors && !string) return randomFromList(colors); | ||
if (colors && !string) return (_b = randomFromList(colors)) != null ? _b : fallback; | ||
return fallback; | ||
@@ -1185,42 +764,141 @@ } | ||
// src/array.ts | ||
function filterEmpty(array) { | ||
return array.filter((value) => value != null); | ||
// src/dom.ts | ||
function createdDom() { | ||
var _a; | ||
return !!(typeof window !== "undefined" && // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition | ||
((_a = window.document) == null ? void 0 : _a.createElement)); | ||
} | ||
// src/number.ts | ||
function toNumber(n) { | ||
const num = parseFloat(n); | ||
return typeof num !== "number" || Number.isNaN(num) ? 0 : num; | ||
function getPlatform() { | ||
var _a, _b; | ||
return (_b = (_a = navigator.userAgentData) == null ? void 0 : _a.platform) != null ? _b : navigator.platform; | ||
} | ||
function toPrecision(n, precision) { | ||
n = toNumber(n); | ||
const scale = 10 ** (precision != null ? precision : 10); | ||
n = Math.round(n * scale) / scale; | ||
return precision ? n.toFixed(precision) : n.toString(); | ||
function vendor(v) { | ||
return createdDom() && v.test(navigator.vendor); | ||
} | ||
function countDecimal(n) { | ||
if (!Number.isFinite(n)) return 0; | ||
let e = 1; | ||
let p = 0; | ||
while (Math.round(n * e) / e !== n) { | ||
e *= 10; | ||
p += 1; | ||
function platform(v) { | ||
return createdDom() && v.test(getPlatform()); | ||
} | ||
function isMac() { | ||
return platform(/^mac/i); | ||
} | ||
function isApple() { | ||
return platform(/mac|iphone|ipad|ipod/i); | ||
} | ||
function isSafari() { | ||
return isApple() && vendor(/apple/i); | ||
} | ||
function isElement(el) { | ||
return el != null && typeof el == "object" && "nodeType" in el && el.nodeType === Node.ELEMENT_NODE; | ||
} | ||
function isHTMLElement(el) { | ||
var _a; | ||
if (!isElement(el)) return false; | ||
const win = (_a = el.ownerDocument.defaultView) != null ? _a : window; | ||
return el instanceof win.HTMLElement; | ||
} | ||
function isHidden(el) { | ||
if (el.parentElement && isHidden(el.parentElement)) return true; | ||
return el.hidden; | ||
} | ||
function isDisabled(el) { | ||
return Boolean(el.getAttribute("disabled")) || Boolean(el.getAttribute("data-disabled")) || Boolean(el.getAttribute("aria-disabled")); | ||
} | ||
function isVisible(el) { | ||
return el.offsetWidth > 0 && el.offsetHeight > 0; | ||
} | ||
function hasTabIndex(el) { | ||
return el.hasAttribute("tabindex"); | ||
} | ||
function isContentEditable(el) { | ||
const value = el.getAttribute("contenteditable"); | ||
return value !== "false" && value != null; | ||
} | ||
function isContains(parent, child) { | ||
return parent === child || (parent == null ? void 0 : parent.contains(child)); | ||
} | ||
function getPx(value) { | ||
if (isNumber(value)) return value; | ||
if (isUndefined(value)) return 0; | ||
if (value.includes("px")) return parseFloat(value); | ||
const isBrowser = createdDom(); | ||
let fontSize = 16; | ||
if (isBrowser) { | ||
const style = window.getComputedStyle(document.documentElement); | ||
const computedFontSize = parseFloat(style.fontSize); | ||
if (!isNaN(computedFontSize)) fontSize = computedFontSize; | ||
} | ||
return p; | ||
return parseFloat(value) * fontSize; | ||
} | ||
function roundNumberToStep(n, from, step) { | ||
const nextValue = Math.round((n - from) / step) * step + from; | ||
const precision = countDecimal(step); | ||
return toPrecision(nextValue, precision); | ||
function getEventRelatedTarget(ev) { | ||
var _a; | ||
return (_a = ev.relatedTarget) != null ? _a : ev.currentTarget.ownerDocument.activeElement; | ||
} | ||
function valueToPercent(n, min, max) { | ||
return (n - min) * 100 / (max - min); | ||
function dataAttr(condition) { | ||
return condition ? "" : void 0; | ||
} | ||
function percentToValue(n, min, max) { | ||
return (max - min) * n + min; | ||
function ariaAttr(condition) { | ||
return condition ? true : void 0; | ||
} | ||
function clampNumber(n, min, max) { | ||
return Math.min(Math.max(n, min), max); | ||
var focusableElList = [ | ||
"input:not(:disabled):not([disabled])", | ||
"select:not(:disabled):not([disabled])", | ||
"textarea:not(:disabled):not([disabled])", | ||
"embed", | ||
"iframe", | ||
"object", | ||
"a[href]", | ||
"area[href]", | ||
"button:not(:disabled):not([disabled])", | ||
"[tabindex]", | ||
"audio[controls]", | ||
"video[controls]", | ||
"*[tabindex]:not([aria-disabled])", | ||
"*[contenteditable]" | ||
]; | ||
var focusableElSelector = focusableElList.join(); | ||
function getAllFocusable(container) { | ||
const focusableEls = Array.from( | ||
container.querySelectorAll(focusableElSelector) | ||
); | ||
focusableEls.unshift(container); | ||
return focusableEls.filter((el) => isFocusable(el) && isVisible(el)); | ||
} | ||
function isFocusable(el) { | ||
if (!isHTMLElement(el) || isHidden(el) || isDisabled(el)) { | ||
return false; | ||
} | ||
const { localName } = el; | ||
const focusableTags = ["input", "select", "textarea", "button"]; | ||
if (focusableTags.includes(localName)) return true; | ||
const others = { | ||
a: () => el.hasAttribute("href"), | ||
audio: () => el.hasAttribute("controls"), | ||
video: () => el.hasAttribute("controls") | ||
}; | ||
if (localName in others) return others[localName](); | ||
if (isContentEditable(el)) return true; | ||
return hasTabIndex(el); | ||
} | ||
function hasNegativeTabIndex(el) { | ||
return hasTabIndex(el) && el.tabIndex === -1; | ||
} | ||
function isTabbable(el) { | ||
return el ? isHTMLElement(el) && isFocusable(el) && !hasNegativeTabIndex(el) : false; | ||
} | ||
function isTouchDevice() { | ||
return "ontouchstart" in window; | ||
} | ||
function getOwnerWindow(node) { | ||
var _a; | ||
return (_a = getOwnerDocument(node).defaultView) != null ? _a : window; | ||
} | ||
function getOwnerDocument(el) { | ||
return isElement(el) ? el.ownerDocument : document; | ||
} | ||
function getActiveElement(el) { | ||
return getOwnerDocument(el).activeElement; | ||
} | ||
function isActiveElement(el) { | ||
return getActiveElement(el) === el; | ||
} | ||
@@ -1245,4 +923,5 @@ // src/event.ts | ||
function pointFromTouch(e, type = "page") { | ||
var _a, _b; | ||
const point = e.touches[0] || e.changedTouches[0]; | ||
return { x: point[`${type}X`], y: point[`${type}Y`] }; | ||
return { x: (_a = point == null ? void 0 : point[`${type}X`]) != null ? _a : 0, y: (_b = point == null ? void 0 : point[`${type}Y`]) != null ? _b : 0 }; | ||
} | ||
@@ -1267,3 +946,3 @@ function pointFromMouse(point, type = "page") { | ||
const isMouse = isMouseEvent(ev); | ||
if (!isMouse || isMouse && ev.button === 0) cb(ev); | ||
if (!isMouse || ev.button === 0) cb(ev); | ||
}; | ||
@@ -1282,2 +961,22 @@ } | ||
// src/function.ts | ||
var noop = () => { | ||
}; | ||
function runIfFunc(valOrFunc, ...args) { | ||
return isFunction(valOrFunc) ? valOrFunc(...args) : valOrFunc; | ||
} | ||
function handlerAll(...funcs) { | ||
return function(event, ...args) { | ||
funcs.some((func) => { | ||
func == null ? void 0 : func(event, ...args); | ||
return event == null ? void 0 : event.defaultPrevented; | ||
}); | ||
}; | ||
} | ||
function funcAll(...funcs) { | ||
return function(...args) { | ||
return funcs.forEach((func) => func == null ? void 0 : func(...args)); | ||
}; | ||
} | ||
// src/module.ts | ||
@@ -1287,2 +986,307 @@ function interopDefault(module2) { | ||
} | ||
// src/number.ts | ||
function toNumber(n) { | ||
const num = parseFloat(n); | ||
return typeof num !== "number" || Number.isNaN(num) ? 0 : num; | ||
} | ||
function toPrecision(n, precision) { | ||
n = toNumber(n); | ||
const scale = 10 ** (precision != null ? precision : 10); | ||
n = Math.round(n * scale) / scale; | ||
return precision ? n.toFixed(precision) : n.toString(); | ||
} | ||
function countDecimal(n) { | ||
if (!Number.isFinite(n)) return 0; | ||
let e = 1; | ||
let p = 0; | ||
while (Math.round(n * e) / e !== n) { | ||
e *= 10; | ||
p += 1; | ||
} | ||
return p; | ||
} | ||
function roundNumberToStep(n, from, step) { | ||
const nextValue = Math.round((n - from) / step) * step + from; | ||
const precision = countDecimal(step); | ||
return toPrecision(nextValue, precision); | ||
} | ||
function valueToPercent(n, min, max) { | ||
return (n - min) * 100 / (max - min); | ||
} | ||
function percentToValue(n, min, max) { | ||
return (max - min) * n + min; | ||
} | ||
function clampNumber(n, min, max) { | ||
return Math.min(Math.max(n, min), max); | ||
} | ||
// src/react.tsx | ||
var React = __toESM(require("react")); | ||
function createContext2({ | ||
name, | ||
defaultValue, | ||
errorMessage = "useContext: `context` is undefined. Seems you forgot to wrap component within the Provider", | ||
strict = true | ||
} = {}) { | ||
const Context = React.createContext(defaultValue); | ||
Context.displayName = name; | ||
const useContext2 = () => { | ||
const context = React.useContext(Context); | ||
if (!context && strict) { | ||
const error = new Error(errorMessage); | ||
error.name = "ContextError"; | ||
Error.captureStackTrace(error, useContext2); | ||
throw error; | ||
} | ||
return context; | ||
}; | ||
return [Context.Provider, useContext2, Context]; | ||
} | ||
var useSafeLayoutEffect = Boolean(globalThis.document) ? React.useLayoutEffect : React.useEffect; | ||
function useUnmountEffect(callback) { | ||
return React.useEffect(() => () => callback(), []); | ||
} | ||
function useIsMounted({ | ||
delay = 0, | ||
rerender = false | ||
} = {}) { | ||
const isMountedRef = React.useRef(false); | ||
const [isMounted, setIsMounted] = React.useState(false); | ||
useSafeLayoutEffect(() => { | ||
isMountedRef.current = true; | ||
let timeoutId = null; | ||
if (rerender) { | ||
if (delay > 0) { | ||
timeoutId = setTimeout(() => setIsMounted(true), delay); | ||
} else { | ||
setIsMounted(true); | ||
} | ||
} | ||
return () => { | ||
isMountedRef.current = false; | ||
if (rerender) setIsMounted(false); | ||
if (timeoutId) clearTimeout(timeoutId); | ||
}; | ||
}, [delay, rerender]); | ||
return [React.useCallback(() => isMountedRef.current, []), isMounted]; | ||
} | ||
function getValidChildren(children) { | ||
return React.Children.toArray(children).filter( | ||
(child) => React.isValidElement(child) | ||
); | ||
} | ||
function isValidElement2(child) { | ||
return React.isValidElement(child) || isString(child) || isNumber(child); | ||
} | ||
function isSomeElement(child, type) { | ||
var _a, _b; | ||
if (child.type === type) return true; | ||
if (!!child.__ui__ && !!type.__ui__) { | ||
if (child.__ui__ === type.__ui__) return true; | ||
} | ||
const payload = child.type._payload; | ||
if (!!((_a = payload == null ? void 0 : payload.value) == null ? void 0 : _a.__ui__) && !!type.__ui__) { | ||
if (((_b = payload == null ? void 0 : payload.value) == null ? void 0 : _b.__ui__) === type.__ui__) return true; | ||
} | ||
return false; | ||
} | ||
function findChild(children, ...types) { | ||
const child = children.find( | ||
(child2) => types.some((type) => isSomeElement(child2, type)) | ||
); | ||
return child; | ||
} | ||
function findChildren(children, ...types) { | ||
const child = children.find( | ||
(child2) => types.some((type) => isSomeElement(child2, type)) | ||
); | ||
if (child) { | ||
return children.sort((a, b) => { | ||
if (types.some((type) => isSomeElement(a, type))) { | ||
return -1; | ||
} else if (types.some((type) => isSomeElement(b, type))) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
} else { | ||
return [void 0, ...children]; | ||
} | ||
} | ||
function includesChildren(children, ...types) { | ||
return children.some((child) => { | ||
if (types.some((type) => isSomeElement(child, type))) return true; | ||
const children2 = getValidChildren(child.props.children); | ||
return children2.length ? includesChildren(children2, ...types) : false; | ||
}); | ||
} | ||
function omitChildren(children, ...types) { | ||
return children.filter( | ||
(child) => types.every((type) => !isSomeElement(child, type)) | ||
); | ||
} | ||
function pickChildren(children, ...types) { | ||
return children.filter( | ||
(child) => types.every((type) => isSomeElement(child, type)) | ||
); | ||
} | ||
function cx(...classNames) { | ||
return classNames.filter(Boolean).join(" "); | ||
} | ||
function isRefObject(val) { | ||
return isObject(val) && "current" in val; | ||
} | ||
function assignRef(ref, value) { | ||
if (ref == null) return; | ||
if (typeof ref === "function") { | ||
ref(value); | ||
return; | ||
} | ||
try { | ||
ref.current = value; | ||
} catch { | ||
throw new Error(`Cannot assign value '${value}' to ref '${ref}'`); | ||
} | ||
} | ||
function mergeRefs(...refs) { | ||
return function(node) { | ||
return refs.forEach((ref) => { | ||
assignRef(ref, node); | ||
}); | ||
}; | ||
} | ||
function useMergeRefs(...refs) { | ||
return React.useMemo(() => mergeRefs(...refs), [refs]); | ||
} | ||
function useCallbackRef(callback, deps = []) { | ||
const callbackRef = React.useRef(callback); | ||
React.useEffect(() => { | ||
callbackRef.current = callback; | ||
}); | ||
return React.useCallback( | ||
(...args) => { | ||
var _a; | ||
return (_a = callbackRef.current) == null ? void 0 : _a.call(callbackRef, ...args); | ||
}, | ||
deps | ||
); | ||
} | ||
function useUpdateEffect(callback, deps) { | ||
const renderCycleRef = React.useRef(false); | ||
const effectCycleRef = React.useRef(false); | ||
React.useEffect(() => { | ||
const mounted = renderCycleRef.current; | ||
const run = mounted && effectCycleRef.current; | ||
if (run) return callback(); | ||
effectCycleRef.current = true; | ||
}, deps); | ||
React.useEffect(() => { | ||
renderCycleRef.current = true; | ||
return () => { | ||
renderCycleRef.current = false; | ||
}; | ||
}, []); | ||
} | ||
function useAsync(func, deps = []) { | ||
const [state, callback] = useAsyncFunc(func, deps, { loading: true }); | ||
React.useEffect(() => { | ||
callback(); | ||
}, [callback]); | ||
return state; | ||
} | ||
function useAsyncFunc(func, deps = [], initialState = { loading: false }) { | ||
const lastCallId = React.useRef(0); | ||
const [isMounted] = useIsMounted(); | ||
const [state, setState] = React.useState(initialState); | ||
const callback = React.useCallback( | ||
(...args) => { | ||
const callId = ++lastCallId.current; | ||
if (!state.loading) | ||
setState((prevState) => ({ ...prevState, loading: true })); | ||
return func(...args).then( | ||
(value) => { | ||
if (isMounted() && callId === lastCallId.current) | ||
setState({ loading: false, value }); | ||
return value; | ||
}, | ||
(error) => { | ||
if (isMounted() && callId === lastCallId.current) | ||
setState({ error, loading: false }); | ||
return error; | ||
} | ||
); | ||
}, | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
deps | ||
); | ||
return [state, callback]; | ||
} | ||
function useAsyncRetry(func, deps = []) { | ||
const [attempt, setAttempt] = React.useState(0); | ||
const state = useAsync(func, [...deps, attempt]); | ||
const stateLoading = state.loading; | ||
const retry = React.useCallback(() => { | ||
if (stateLoading) return; | ||
setAttempt((currentAttempt) => currentAttempt + 1); | ||
}, [...deps, stateLoading]); | ||
return { ...state, retry }; | ||
} | ||
var createIdCounter = 0; | ||
function createId(prefix) { | ||
return `${prefix}-${++createIdCounter}-${(/* @__PURE__ */ new Date()).getTime()}`; | ||
} | ||
// src/string.ts | ||
function escape(value, replaceValue = "") { | ||
return value.replace(/\s+/g, replaceValue); | ||
} | ||
function antonym(value) { | ||
switch (value) { | ||
case "top": | ||
return "bottom"; | ||
case "bottom": | ||
return "top"; | ||
case "left": | ||
return "right"; | ||
case "right": | ||
return "left"; | ||
case "start": | ||
return "end"; | ||
case "end": | ||
return "start"; | ||
case "x": | ||
return "y"; | ||
case "y": | ||
return "x"; | ||
case "enter": | ||
return "exit"; | ||
case "exit": | ||
return "enter"; | ||
case "vertical": | ||
return "horizontal"; | ||
case "horizontal": | ||
return "vertical"; | ||
case "up": | ||
return "down"; | ||
case "down": | ||
return "up"; | ||
case "block": | ||
return "inline"; | ||
case "inline": | ||
return "block"; | ||
default: | ||
return value; | ||
} | ||
} | ||
function toCamelCase(value) { | ||
return value.toLowerCase().replace(/[_-](.)/g, (_, val) => val.toUpperCase()).replace(/^(.)/, (_, val) => val.toUpperCase()); | ||
} | ||
function toKebabCase(value) { | ||
return value.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, "$1-$2").toLowerCase().replace(/^-/, ""); | ||
} | ||
function toTitleCase(value) { | ||
return value.replace(/([A-Z])/g, " $1").replace(/[_-](.)/g, (_, val) => ` ${val.toUpperCase()}`).replace(/^./, (str) => str.toUpperCase()).trim(); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -1353,2 +1357,3 @@ 0 && (module.exports = { | ||
isEmpty, | ||
isEmptyObject, | ||
isFocusable, | ||
@@ -1355,0 +1360,0 @@ isFunction, |
@@ -1,3 +0,3 @@ | ||
type Primitive = null | undefined | string | number | boolean | symbol | bigint; | ||
type PathImpl<Y extends string | number | symbol, M> = Y extends string | number ? M extends Primitive ? `${Y}` : `${Y}.${Path<M>}` : ``; | ||
type Primitive = bigint | boolean | null | number | string | symbol | undefined; | ||
type PathImpl<Y extends number | string | symbol, M> = Y extends number | string ? M extends Primitive ? `${Y}` : `${Y}.${Path<M>}` : ``; | ||
type Path<Y> = { | ||
@@ -11,9 +11,9 @@ [M in keyof Y]-?: PathImpl<M, Y[M]>; | ||
} | ||
type StringLiteral = string & {}; | ||
type StringLiteral = {} & string; | ||
type Replace<Y extends string, M extends string, D extends string, H extends string = ""> = Y extends `${infer T}${M}${infer R}` ? Replace<R, M, D, `${H}${T}${D}`> : `${H}${Y}`; | ||
type Union<Y> = Y | StringLiteral; | ||
type Union<Y> = StringLiteral | Y; | ||
type Length<T extends any[]> = T["length"]; | ||
type Merge<Y, M> = Omit<Y, keyof M> & M; | ||
type Merge<Y, M> = M & Omit<Y, keyof M>; | ||
type MergeIfDefined<Y, M> = M extends undefined ? Y : Merge<Y, M>; | ||
export type { Dict, Length, Merge, MergeIfDefined, ObjectLiteral, Path, Primitive, Replace, StringLiteral, Union }; |
@@ -19,5 +19,5 @@ import { Dict } from './index.types.js'; | ||
declare function replaceObject<Y = any>(objOrArray: Y, callBack: (value: any) => any): Y; | ||
declare function getObject(obj: Dict, path: string | number, fallback?: any, i?: number): any; | ||
declare function memoizeObject(func: typeof getObject): <Y>(obj: Dict, path: string | number, fallback?: any, i?: number) => Y; | ||
declare const getMemoizedObject: <Y>(obj: Dict, path: string | number, fallback?: any, i?: number) => Y; | ||
declare function getObject(obj: Dict | undefined, path: number | string, fallback?: any, i?: number): any; | ||
declare function memoizeObject(func: typeof getObject): <Y>(obj: Dict, path: number | string, fallback?: any, i?: number) => Y; | ||
declare const getMemoizedObject: <Y>(obj: Dict, path: number | string, fallback?: any, i?: number) => Y; | ||
declare function assignAfter(target: { | ||
@@ -24,0 +24,0 @@ [key: string]: any; |
@@ -45,3 +45,3 @@ "use strict"; | ||
function isUndefined(value) { | ||
return typeof value === "undefined" && value === void 0; | ||
return typeof value === "undefined"; | ||
} | ||
@@ -62,7 +62,7 @@ function isObject(value) { | ||
const [primaryKey, ...restKeys] = path; | ||
if (restKeys.length === 0 && primaryKey in obj) { | ||
if (restKeys.length === 0 && primaryKey && primaryKey in obj) { | ||
const { [primaryKey]: _, ...rest } = obj; | ||
return rest; | ||
} | ||
if (obj[primaryKey] && isObject(obj[primaryKey])) { | ||
if (primaryKey && obj[primaryKey] && isObject(obj[primaryKey])) { | ||
return { | ||
@@ -146,3 +146,3 @@ ...obj, | ||
return Object.entries(obj).reduce((result, [key, value]) => { | ||
if (isObject(value) && !Object.keys(value).some((key2) => omitKeys == null ? void 0 : omitKeys.includes(key2)) && (!shouldProcess || shouldProcess(value))) { | ||
if (isObject(value) && !Object.keys(value).some((key2) => omitKeys.includes(key2)) && (!shouldProcess || shouldProcess(value))) { | ||
Object.entries( | ||
@@ -186,6 +186,7 @@ flattenObject(value, { | ||
function getObject(obj, path, fallback, i) { | ||
const k = isString(path) ? path.split(/\[(.*?)\]|\./).filter(Boolean) : [path]; | ||
for (i = 0; i < k.length; i += 1) { | ||
const keys = isString(path) ? path.split(/\[(.*?)\]|\./).filter(Boolean) : [path]; | ||
for (i = 0; i < keys.length; i += 1) { | ||
if (!obj) break; | ||
obj = obj[k[i]]; | ||
const key = keys[i]; | ||
obj = key ? obj[key] : void 0; | ||
} | ||
@@ -209,4 +210,2 @@ return obj === void 0 ? fallback : obj; | ||
function assignAfter(target, ...sources) { | ||
if (target == null) | ||
throw new TypeError("Cannot convert undefined or null to object"); | ||
const result = { ...target }; | ||
@@ -213,0 +212,0 @@ for (const nextSource of sources) { |
import * as React from 'react'; | ||
type MaybeRenderProp<Y> = React.ReactNode | ((props: Y) => React.ReactNode); | ||
interface Options<ContextType extends any = any> { | ||
strict?: boolean; | ||
type MaybeRenderProp<Y> = ((props: Y) => React.ReactNode) | React.ReactNode; | ||
type CreateContextReturn<Y> = [React.Provider<Y>, () => Y, React.Context<Y>]; | ||
declare function createContext<Y = any>(options: { | ||
name?: string; | ||
defaultValue?: Y; | ||
errorMessage?: string; | ||
strict?: true; | ||
}): CreateContextReturn<Y>; | ||
declare function createContext<Y = any>(options: { | ||
name?: string; | ||
defaultValue?: ContextType; | ||
} | ||
type CreateContextReturn<T> = [React.Provider<T>, () => T, React.Context<T>]; | ||
declare function createContext<ContextType extends any = any>({ strict, errorMessage, name, defaultValue, }?: Options<ContextType>): CreateContextReturn<ContextType>; | ||
defaultValue?: Y; | ||
errorMessage?: string; | ||
strict?: false; | ||
}): CreateContextReturn<undefined | Y>; | ||
declare const useSafeLayoutEffect: typeof React.useLayoutEffect; | ||
declare function useUnmountEffect(callback: () => void): void; | ||
interface UseIsMountedProps { | ||
delay?: number; | ||
rerender?: boolean; | ||
delay?: number; | ||
} | ||
declare function useIsMounted({ rerender, delay, }?: UseIsMountedProps): [() => boolean, boolean]; | ||
declare function useIsMounted({ delay, rerender, }?: UseIsMountedProps): [() => boolean, boolean]; | ||
type UseIsMountedReturn = ReturnType<typeof useIsMounted>; | ||
@@ -23,15 +28,15 @@ declare function getValidChildren(children: React.ReactNode): React.ReactElement[]; | ||
declare function isSomeElement(child: any, type: any): boolean; | ||
declare function findChild(children: React.ReactElement[], ...types: (string | React.JSXElementConstructor<any>)[]): React.ReactElement | undefined; | ||
declare function findChildren(children: React.ReactElement[], ...types: (string | React.JSXElementConstructor<any>)[]): [React.ReactElement | undefined, ...React.ReactElement[]]; | ||
declare function includesChildren(children: React.ReactElement[], ...types: (string | React.JSXElementConstructor<any>)[]): boolean; | ||
declare function omitChildren(children: React.ReactElement[], ...types: (string | React.JSXElementConstructor<any>)[]): React.ReactElement[]; | ||
declare function pickChildren(children: React.ReactElement[], ...types: (string | React.JSXElementConstructor<any>)[]): React.ReactElement[]; | ||
declare function findChild(children: React.ReactElement[], ...types: (React.JSXElementConstructor<any> | string)[]): React.ReactElement | undefined; | ||
declare function findChildren(children: React.ReactElement[], ...types: (React.JSXElementConstructor<any> | string)[]): [React.ReactElement | undefined, ...React.ReactElement[]]; | ||
declare function includesChildren(children: React.ReactElement[], ...types: (React.JSXElementConstructor<any> | string)[]): boolean; | ||
declare function omitChildren(children: React.ReactElement[], ...types: (React.JSXElementConstructor<any> | string)[]): React.ReactElement[]; | ||
declare function pickChildren(children: React.ReactElement[], ...types: (React.JSXElementConstructor<any> | string)[]): React.ReactElement[]; | ||
declare function cx(...classNames: (string | undefined)[]): string; | ||
type ReactRef<T> = React.Ref<T> | React.MutableRefObject<T> | React.LegacyRef<T>; | ||
type ReactRef<T> = React.LegacyRef<T> | React.MutableRefObject<T> | React.Ref<T>; | ||
declare function isRefObject(val: any): val is { | ||
current: any; | ||
}; | ||
declare function assignRef<T extends any = any>(ref: ReactRef<T> | undefined, value: T): void; | ||
declare function mergeRefs<T extends any = any>(...refs: (ReactRef<T> | null | undefined)[]): (node: T | null) => void; | ||
declare function useMergeRefs<T extends any = any>(...refs: (ReactRef<T> | undefined)[]): (node: T | null) => void; | ||
declare function assignRef<T = any>(ref: ReactRef<T> | undefined, value: T): void; | ||
declare function mergeRefs<T = any>(...refs: (null | ReactRef<T> | undefined)[]): (node: null | T) => void; | ||
declare function useMergeRefs<T = any>(...refs: (ReactRef<T> | undefined)[]): (node: T | null) => void; | ||
declare function useCallbackRef<T extends (...args: any[]) => any>(callback: T | undefined, deps?: React.DependencyList): T; | ||
@@ -52,2 +57,6 @@ /** | ||
type AsyncState<T> = { | ||
error: Error; | ||
loading: false; | ||
value?: undefined; | ||
} | { | ||
loading: boolean; | ||
@@ -57,13 +66,9 @@ error?: undefined; | ||
} | { | ||
loading: false; | ||
value: T; | ||
error?: undefined; | ||
} | { | ||
loading: true; | ||
error?: Error | undefined; | ||
value?: T; | ||
} | { | ||
loading: false; | ||
error: Error; | ||
value?: undefined; | ||
} | { | ||
loading: false; | ||
error?: undefined; | ||
value: T; | ||
}; | ||
@@ -74,7 +79,12 @@ type PromiseType<P extends Promise<any>> = P extends Promise<infer T> ? T : never; | ||
declare function useAsyncFunc<T extends FunctionReturningPromise>(func: T, deps?: React.DependencyList, initialState?: StateFromFunctionReturningPromise<T>): AsyncFnReturn<T>; | ||
type AsyncStateRetry<T> = AsyncState<T> & { | ||
type AsyncStateRetry<T> = { | ||
retry(): void; | ||
}; | ||
} & AsyncState<T>; | ||
declare function useAsyncRetry<T>(func: () => Promise<T>, deps?: React.DependencyList): { | ||
retry: () => void; | ||
error: Error; | ||
loading: false; | ||
value?: undefined; | ||
} | { | ||
retry: () => void; | ||
loading: boolean; | ||
@@ -86,4 +96,4 @@ error?: undefined; | ||
loading: false; | ||
error: Error; | ||
value?: undefined; | ||
value: T; | ||
error?: undefined; | ||
} | { | ||
@@ -94,7 +104,2 @@ retry: () => void; | ||
value?: T | undefined; | ||
} | { | ||
retry: () => void; | ||
loading: false; | ||
error?: undefined; | ||
value: T; | ||
}; | ||
@@ -101,0 +106,0 @@ declare function createId(prefix: string): string; |
@@ -76,6 +76,6 @@ "use strict"; | ||
function createContext2({ | ||
strict = true, | ||
name, | ||
defaultValue, | ||
errorMessage = "useContext: `context` is undefined. Seems you forgot to wrap component within the Provider", | ||
name, | ||
defaultValue | ||
strict = true | ||
} = {}) { | ||
@@ -85,3 +85,2 @@ const Context = React.createContext(defaultValue); | ||
const useContext2 = () => { | ||
var _a; | ||
const context = React.useContext(Context); | ||
@@ -91,3 +90,3 @@ if (!context && strict) { | ||
error.name = "ContextError"; | ||
(_a = Error.captureStackTrace) == null ? void 0 : _a.call(Error, error, useContext2); | ||
Error.captureStackTrace(error, useContext2); | ||
throw error; | ||
@@ -97,9 +96,5 @@ } | ||
}; | ||
return [ | ||
Context.Provider, | ||
useContext2, | ||
Context | ||
]; | ||
return [Context.Provider, useContext2, Context]; | ||
} | ||
var useSafeLayoutEffect = Boolean(globalThis == null ? void 0 : globalThis.document) ? React.useLayoutEffect : React.useEffect; | ||
var useSafeLayoutEffect = Boolean(globalThis.document) ? React.useLayoutEffect : React.useEffect; | ||
function useUnmountEffect(callback) { | ||
@@ -109,4 +104,4 @@ return React.useEffect(() => () => callback(), []); | ||
function useIsMounted({ | ||
rerender = false, | ||
delay = 0 | ||
delay = 0, | ||
rerender = false | ||
} = {}) { | ||
@@ -270,3 +265,3 @@ const isMountedRef = React.useRef(false); | ||
if (isMounted() && callId === lastCallId.current) | ||
setState({ value, loading: false }); | ||
setState({ loading: false, value }); | ||
return value; | ||
@@ -273,0 +268,0 @@ }, |
{ | ||
"name": "@yamada-ui/utils", | ||
"version": "1.5.2", | ||
"version": "1.5.3-dev-20241006000212", | ||
"description": "Yamada UI utils", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
418765
5393
111
1