@zag-js/core
Advanced tools
| import { MachineSchema, GuardFn, Machine, Transition, Params } from './types.mjs'; | ||
| declare function createGuards<T extends MachineSchema>(): { | ||
| and: (...guards: Array<GuardFn<T> | T["guard"]>) => (params: any) => boolean; | ||
| or: (...guards: Array<GuardFn<T> | T["guard"]>) => (params: any) => boolean; | ||
| not: (guard: GuardFn<T> | T["guard"]) => (params: any) => boolean; | ||
| }; | ||
| declare function createMachine<T extends MachineSchema>(config: Machine<T>): Machine<T>; | ||
| declare function setup<T extends MachineSchema>(): { | ||
| guards: { | ||
| and: (...guards: (T["guard"] | GuardFn<T>)[]) => (params: any) => boolean; | ||
| or: (...guards: (T["guard"] | GuardFn<T>)[]) => (params: any) => boolean; | ||
| not: (guard: T["guard"] | GuardFn<T>) => (params: any) => boolean; | ||
| }; | ||
| createMachine: (config: Machine<T>) => Machine<T>; | ||
| choose: (transitions: Transition<T> | Transition<T>[]) => ({ choose }: Params<T>) => T["action"][] | undefined; | ||
| }; | ||
| export { createGuards, createMachine, setup }; |
| import { MachineSchema, GuardFn, Machine, Transition, Params } from './types.js'; | ||
| declare function createGuards<T extends MachineSchema>(): { | ||
| and: (...guards: Array<GuardFn<T> | T["guard"]>) => (params: any) => boolean; | ||
| or: (...guards: Array<GuardFn<T> | T["guard"]>) => (params: any) => boolean; | ||
| not: (guard: GuardFn<T> | T["guard"]) => (params: any) => boolean; | ||
| }; | ||
| declare function createMachine<T extends MachineSchema>(config: Machine<T>): Machine<T>; | ||
| declare function setup<T extends MachineSchema>(): { | ||
| guards: { | ||
| and: (...guards: (T["guard"] | GuardFn<T>)[]) => (params: any) => boolean; | ||
| or: (...guards: (T["guard"] | GuardFn<T>)[]) => (params: any) => boolean; | ||
| not: (guard: T["guard"] | GuardFn<T>) => (params: any) => boolean; | ||
| }; | ||
| createMachine: (config: Machine<T>) => Machine<T>; | ||
| choose: (transitions: Transition<T> | Transition<T>[]) => ({ choose }: Params<T>) => T["action"][] | undefined; | ||
| }; | ||
| export { createGuards, createMachine, setup }; |
| "use strict"; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // src/create-machine.ts | ||
| var create_machine_exports = {}; | ||
| __export(create_machine_exports, { | ||
| createGuards: () => createGuards, | ||
| createMachine: () => createMachine, | ||
| setup: () => setup | ||
| }); | ||
| module.exports = __toCommonJS(create_machine_exports); | ||
| var import_state = require("./state.cjs"); | ||
| function createGuards() { | ||
| return { | ||
| and: (...guards) => { | ||
| return function andGuard(params) { | ||
| return guards.every((str) => params.guard(str)); | ||
| }; | ||
| }, | ||
| or: (...guards) => { | ||
| return function orGuard(params) { | ||
| return guards.some((str) => params.guard(str)); | ||
| }; | ||
| }, | ||
| not: (guard) => { | ||
| return function notGuard(params) { | ||
| return !params.guard(guard); | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
| function createMachine(config) { | ||
| (0, import_state.ensureStateIndex)(config); | ||
| return config; | ||
| } | ||
| function setup() { | ||
| return { | ||
| guards: createGuards(), | ||
| createMachine: (config) => { | ||
| return createMachine(config); | ||
| }, | ||
| choose: (transitions) => { | ||
| return function chooseFn({ choose }) { | ||
| return choose(transitions)?.actions; | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| createGuards, | ||
| createMachine, | ||
| setup | ||
| }); |
| // src/create-machine.ts | ||
| import { ensureStateIndex } from "./state.mjs"; | ||
| function createGuards() { | ||
| return { | ||
| and: (...guards) => { | ||
| return function andGuard(params) { | ||
| return guards.every((str) => params.guard(str)); | ||
| }; | ||
| }, | ||
| or: (...guards) => { | ||
| return function orGuard(params) { | ||
| return guards.some((str) => params.guard(str)); | ||
| }; | ||
| }, | ||
| not: (guard) => { | ||
| return function notGuard(params) { | ||
| return !params.guard(guard); | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
| function createMachine(config) { | ||
| ensureStateIndex(config); | ||
| return config; | ||
| } | ||
| function setup() { | ||
| return { | ||
| guards: createGuards(), | ||
| createMachine: (config) => { | ||
| return createMachine(config); | ||
| }, | ||
| choose: (transitions) => { | ||
| return function chooseFn({ choose }) { | ||
| return choose(transitions)?.actions; | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
| export { | ||
| createGuards, | ||
| createMachine, | ||
| setup | ||
| }; |
| type NoInfer<T> = [T][T extends any ? 0 : never]; | ||
| declare function memo<TDeps extends any[], TDepArgs, TResult>(getDeps: (depArgs: TDepArgs) => [...TDeps], fn: (args: NoInfer<[...TDeps]>, deps: TDepArgs) => TResult, opts?: { | ||
| onChange?: ((result: TResult) => void) | undefined; | ||
| }): (depArgs: TDepArgs) => TResult; | ||
| export { memo }; |
| type NoInfer<T> = [T][T extends any ? 0 : never]; | ||
| declare function memo<TDeps extends any[], TDepArgs, TResult>(getDeps: (depArgs: TDepArgs) => [...TDeps], fn: (args: NoInfer<[...TDeps]>, deps: TDepArgs) => TResult, opts?: { | ||
| onChange?: ((result: TResult) => void) | undefined; | ||
| }): (depArgs: TDepArgs) => TResult; | ||
| export { memo }; |
+43
| "use strict"; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // src/memo.ts | ||
| var memo_exports = {}; | ||
| __export(memo_exports, { | ||
| memo: () => memo | ||
| }); | ||
| module.exports = __toCommonJS(memo_exports); | ||
| var import_utils = require("@zag-js/utils"); | ||
| function memo(getDeps, fn, opts) { | ||
| let deps = []; | ||
| let result; | ||
| return (depArgs) => { | ||
| const newDeps = getDeps(depArgs); | ||
| const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => !(0, import_utils.isEqual)(deps[index], dep)); | ||
| if (!depsChanged) return result; | ||
| deps = newDeps; | ||
| result = fn(newDeps, depArgs); | ||
| opts?.onChange?.(result); | ||
| return result; | ||
| }; | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| memo | ||
| }); |
| // src/memo.ts | ||
| import { isEqual } from "@zag-js/utils"; | ||
| function memo(getDeps, fn, opts) { | ||
| let deps = []; | ||
| let result; | ||
| return (depArgs) => { | ||
| const newDeps = getDeps(depArgs); | ||
| const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => !isEqual(deps[index], dep)); | ||
| if (!depsChanged) return result; | ||
| deps = newDeps; | ||
| result = fn(newDeps, depArgs); | ||
| opts?.onChange?.(result); | ||
| return result; | ||
| }; | ||
| } | ||
| export { | ||
| memo | ||
| }; |
| interface Props { | ||
| [key: string | symbol]: any; | ||
| } | ||
| type TupleTypes<T extends any[]> = T[number]; | ||
| type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; | ||
| declare function mergeProps<T extends Props>(...args: Array<T | undefined>): UnionToIntersection<TupleTypes<T[]>>; | ||
| export { mergeProps }; |
| interface Props { | ||
| [key: string | symbol]: any; | ||
| } | ||
| type TupleTypes<T extends any[]> = T[number]; | ||
| type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; | ||
| declare function mergeProps<T extends Props>(...args: Array<T | undefined>): UnionToIntersection<TupleTypes<T[]>>; | ||
| export { mergeProps }; |
| "use strict"; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // src/merge-props.ts | ||
| var merge_props_exports = {}; | ||
| __export(merge_props_exports, { | ||
| mergeProps: () => mergeProps | ||
| }); | ||
| module.exports = __toCommonJS(merge_props_exports); | ||
| var import_utils = require("@zag-js/utils"); | ||
| var clsx = (...args) => args.map((str) => str?.trim?.()).filter(Boolean).join(" "); | ||
| var CSS_REGEX = /((?:--)?(?:\w+-?)+)\s*:\s*([^;]*)/g; | ||
| var serialize = (style) => { | ||
| const res = {}; | ||
| let match; | ||
| while (match = CSS_REGEX.exec(style)) { | ||
| res[match[1]] = match[2]; | ||
| } | ||
| return res; | ||
| }; | ||
| var css = (a, b) => { | ||
| if ((0, import_utils.isString)(a)) { | ||
| if ((0, import_utils.isString)(b)) return `${a};${b}`; | ||
| a = serialize(a); | ||
| } else if ((0, import_utils.isString)(b)) { | ||
| b = serialize(b); | ||
| } | ||
| return Object.assign({}, a ?? {}, b ?? {}); | ||
| }; | ||
| function mergeProps(...args) { | ||
| let result = {}; | ||
| for (let props of args) { | ||
| if (!props) continue; | ||
| for (let key in result) { | ||
| if (key.startsWith("on") && typeof result[key] === "function" && typeof props[key] === "function") { | ||
| result[key] = (0, import_utils.callAll)(props[key], result[key]); | ||
| continue; | ||
| } | ||
| if (key === "className" || key === "class") { | ||
| result[key] = clsx(result[key], props[key]); | ||
| continue; | ||
| } | ||
| if (key === "style") { | ||
| result[key] = css(result[key], props[key]); | ||
| continue; | ||
| } | ||
| result[key] = props[key] !== void 0 ? props[key] : result[key]; | ||
| } | ||
| for (let key in props) { | ||
| if (result[key] === void 0) { | ||
| result[key] = props[key]; | ||
| } | ||
| } | ||
| const symbols = Object.getOwnPropertySymbols(props); | ||
| for (let symbol of symbols) { | ||
| result[symbol] = props[symbol]; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| mergeProps | ||
| }); |
| // src/merge-props.ts | ||
| import { callAll, isString } from "@zag-js/utils"; | ||
| var clsx = (...args) => args.map((str) => str?.trim?.()).filter(Boolean).join(" "); | ||
| var CSS_REGEX = /((?:--)?(?:\w+-?)+)\s*:\s*([^;]*)/g; | ||
| var serialize = (style) => { | ||
| const res = {}; | ||
| let match; | ||
| while (match = CSS_REGEX.exec(style)) { | ||
| res[match[1]] = match[2]; | ||
| } | ||
| return res; | ||
| }; | ||
| var css = (a, b) => { | ||
| if (isString(a)) { | ||
| if (isString(b)) return `${a};${b}`; | ||
| a = serialize(a); | ||
| } else if (isString(b)) { | ||
| b = serialize(b); | ||
| } | ||
| return Object.assign({}, a ?? {}, b ?? {}); | ||
| }; | ||
| function mergeProps(...args) { | ||
| let result = {}; | ||
| for (let props of args) { | ||
| if (!props) continue; | ||
| for (let key in result) { | ||
| if (key.startsWith("on") && typeof result[key] === "function" && typeof props[key] === "function") { | ||
| result[key] = callAll(props[key], result[key]); | ||
| continue; | ||
| } | ||
| if (key === "className" || key === "class") { | ||
| result[key] = clsx(result[key], props[key]); | ||
| continue; | ||
| } | ||
| if (key === "style") { | ||
| result[key] = css(result[key], props[key]); | ||
| continue; | ||
| } | ||
| result[key] = props[key] !== void 0 ? props[key] : result[key]; | ||
| } | ||
| for (let key in props) { | ||
| if (result[key] === void 0) { | ||
| result[key] = props[key]; | ||
| } | ||
| } | ||
| const symbols = Object.getOwnPropertySymbols(props); | ||
| for (let symbol of symbols) { | ||
| result[symbol] = props[symbol]; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| export { | ||
| mergeProps | ||
| }; |
| import { isActiveElement } from '@zag-js/dom-query'; | ||
| import { Scope } from './types.mjs'; | ||
| declare function createScope(props: Pick<Scope, "id" | "ids" | "getRootNode">): { | ||
| getRootNode: () => Document | ShadowRoot; | ||
| getDoc: () => Document; | ||
| getWin: () => Window & typeof globalThis; | ||
| getActiveElement: () => HTMLElement | null; | ||
| isActiveElement: typeof isActiveElement; | ||
| getById: <T extends Element = HTMLElement>(id: string) => T | null; | ||
| id?: string | undefined | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| }; | ||
| export { createScope }; |
| import { isActiveElement } from '@zag-js/dom-query'; | ||
| import { Scope } from './types.js'; | ||
| declare function createScope(props: Pick<Scope, "id" | "ids" | "getRootNode">): { | ||
| getRootNode: () => Document | ShadowRoot; | ||
| getDoc: () => Document; | ||
| getWin: () => Window & typeof globalThis; | ||
| getActiveElement: () => HTMLElement | null; | ||
| isActiveElement: typeof isActiveElement; | ||
| getById: <T extends Element = HTMLElement>(id: string) => T | null; | ||
| id?: string | undefined | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| }; | ||
| export { createScope }; |
| "use strict"; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // src/scope.ts | ||
| var scope_exports = {}; | ||
| __export(scope_exports, { | ||
| createScope: () => createScope | ||
| }); | ||
| module.exports = __toCommonJS(scope_exports); | ||
| var import_dom_query = require("@zag-js/dom-query"); | ||
| function createScope(props) { | ||
| const getRootNode = () => props.getRootNode?.() ?? document; | ||
| const getDoc = () => (0, import_dom_query.getDocument)(getRootNode()); | ||
| const getWin = () => getDoc().defaultView ?? window; | ||
| const getActiveElementFn = () => (0, import_dom_query.getActiveElement)(getRootNode()); | ||
| const getById = (id) => getRootNode().getElementById(id); | ||
| return { | ||
| ...props, | ||
| getRootNode, | ||
| getDoc, | ||
| getWin, | ||
| getActiveElement: getActiveElementFn, | ||
| isActiveElement: import_dom_query.isActiveElement, | ||
| getById | ||
| }; | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| createScope | ||
| }); |
| // src/scope.ts | ||
| import { getActiveElement, getDocument, isActiveElement } from "@zag-js/dom-query"; | ||
| function createScope(props) { | ||
| const getRootNode = () => props.getRootNode?.() ?? document; | ||
| const getDoc = () => getDocument(getRootNode()); | ||
| const getWin = () => getDoc().defaultView ?? window; | ||
| const getActiveElementFn = () => getActiveElement(getRootNode()); | ||
| const getById = (id) => getRootNode().getElementById(id); | ||
| return { | ||
| ...props, | ||
| getRootNode, | ||
| getDoc, | ||
| getWin, | ||
| getActiveElement: getActiveElementFn, | ||
| isActiveElement, | ||
| getById | ||
| }; | ||
| } | ||
| export { | ||
| createScope | ||
| }; |
| import { MachineSchema, Machine, MachineState, TransitionMatch } from './types.mjs'; | ||
| declare function ensureStateIndex<T extends MachineSchema>(machine: Machine<T>): Map<string, MachineState<T, string>>; | ||
| type StateChain<T extends MachineSchema> = Array<{ | ||
| path: string; | ||
| state: MachineState<T>; | ||
| }>; | ||
| declare function getStateChain<T extends MachineSchema>(machine: Machine<T>, state: T["state"] | undefined): StateChain<T>; | ||
| declare function resolveStateValue<T extends MachineSchema>(machine: Machine<T>, value: T["state"] | string, source?: string): T["state"]; | ||
| declare function getStateDefinition<T extends MachineSchema>(machine: Machine<T>, state: T["state"]): MachineState<T, string>; | ||
| declare function findTransition<T extends MachineSchema>(machine: Machine<T>, state: T["state"], eventType: string): TransitionMatch<T>; | ||
| declare function getExitEnterStates<T extends MachineSchema>(machine: Machine<T>, prevState: T["state"] | undefined, nextState: T["state"], reenter?: boolean): { | ||
| exiting: { | ||
| path: string; | ||
| state: MachineState<T, string>; | ||
| }[]; | ||
| entering: { | ||
| path: string; | ||
| state: MachineState<T, string>; | ||
| }[]; | ||
| }; | ||
| declare function matchesState(current: string | undefined, value: string): boolean; | ||
| declare function hasTag<T extends MachineSchema>(machine: Machine<T>, state: T["state"], tag: T["tag"]): boolean; | ||
| export { ensureStateIndex, findTransition, getExitEnterStates, getStateChain, getStateDefinition, hasTag, matchesState, resolveStateValue }; |
| import { MachineSchema, Machine, MachineState, TransitionMatch } from './types.js'; | ||
| declare function ensureStateIndex<T extends MachineSchema>(machine: Machine<T>): Map<string, MachineState<T, string>>; | ||
| type StateChain<T extends MachineSchema> = Array<{ | ||
| path: string; | ||
| state: MachineState<T>; | ||
| }>; | ||
| declare function getStateChain<T extends MachineSchema>(machine: Machine<T>, state: T["state"] | undefined): StateChain<T>; | ||
| declare function resolveStateValue<T extends MachineSchema>(machine: Machine<T>, value: T["state"] | string, source?: string): T["state"]; | ||
| declare function getStateDefinition<T extends MachineSchema>(machine: Machine<T>, state: T["state"]): MachineState<T, string>; | ||
| declare function findTransition<T extends MachineSchema>(machine: Machine<T>, state: T["state"], eventType: string): TransitionMatch<T>; | ||
| declare function getExitEnterStates<T extends MachineSchema>(machine: Machine<T>, prevState: T["state"] | undefined, nextState: T["state"], reenter?: boolean): { | ||
| exiting: { | ||
| path: string; | ||
| state: MachineState<T, string>; | ||
| }[]; | ||
| entering: { | ||
| path: string; | ||
| state: MachineState<T, string>; | ||
| }[]; | ||
| }; | ||
| declare function matchesState(current: string | undefined, value: string): boolean; | ||
| declare function hasTag<T extends MachineSchema>(machine: Machine<T>, state: T["state"], tag: T["tag"]): boolean; | ||
| export { ensureStateIndex, findTransition, getExitEnterStates, getStateChain, getStateDefinition, hasTag, matchesState, resolveStateValue }; |
+172
| "use strict"; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // src/state.ts | ||
| var state_exports = {}; | ||
| __export(state_exports, { | ||
| ensureStateIndex: () => ensureStateIndex, | ||
| findTransition: () => findTransition, | ||
| getExitEnterStates: () => getExitEnterStates, | ||
| getStateChain: () => getStateChain, | ||
| getStateDefinition: () => getStateDefinition, | ||
| hasTag: () => hasTag, | ||
| matchesState: () => matchesState, | ||
| resolveStateValue: () => resolveStateValue | ||
| }); | ||
| module.exports = __toCommonJS(state_exports); | ||
| var STATE_DELIMITER = "."; | ||
| var stateIndexCache = /* @__PURE__ */ new WeakMap(); | ||
| function joinStatePath(parts) { | ||
| return parts.join(STATE_DELIMITER); | ||
| } | ||
| function isAbsoluteStatePath(value) { | ||
| return value.includes(STATE_DELIMITER); | ||
| } | ||
| function appendStatePath(base, segment) { | ||
| return base ? `${base}${STATE_DELIMITER}${segment}` : segment; | ||
| } | ||
| function buildStateIndex(machine) { | ||
| const index = /* @__PURE__ */ new Map(); | ||
| const visit = (basePath, state) => { | ||
| index.set(basePath, state); | ||
| const childStates = state.states; | ||
| if (!childStates) return; | ||
| for (const [childKey, childState] of Object.entries(childStates)) { | ||
| if (!childState) continue; | ||
| const childPath = appendStatePath(basePath, childKey); | ||
| visit(childPath, childState); | ||
| } | ||
| }; | ||
| for (const [topKey, topState] of Object.entries(machine.states)) { | ||
| if (!topState) continue; | ||
| visit(topKey, topState); | ||
| } | ||
| return index; | ||
| } | ||
| function ensureStateIndex(machine) { | ||
| const cached = stateIndexCache.get(machine); | ||
| if (cached) return cached; | ||
| const index = buildStateIndex(machine); | ||
| stateIndexCache.set(machine, index); | ||
| return index; | ||
| } | ||
| function toSegments(value) { | ||
| if (!value) return []; | ||
| return String(value).split(STATE_DELIMITER).filter(Boolean); | ||
| } | ||
| function getStateChain(machine, state) { | ||
| if (!state) return []; | ||
| const stateIndex = ensureStateIndex(machine); | ||
| const segments = toSegments(state); | ||
| const chain = []; | ||
| const statePath = []; | ||
| for (const segment of segments) { | ||
| statePath.push(segment); | ||
| const path = joinStatePath(statePath); | ||
| const current = stateIndex.get(path); | ||
| if (!current) break; | ||
| chain.push({ path, state: current }); | ||
| } | ||
| return chain; | ||
| } | ||
| function resolveAbsoluteStateValue(machine, value) { | ||
| const stateIndex = ensureStateIndex(machine); | ||
| const segments = toSegments(value); | ||
| if (!segments.length) return value; | ||
| const resolved = []; | ||
| for (const segment of segments) { | ||
| resolved.push(segment); | ||
| const path = joinStatePath(resolved); | ||
| if (!stateIndex.has(path)) return value; | ||
| } | ||
| let resolvedPath = joinStatePath(resolved); | ||
| let current = stateIndex.get(resolvedPath); | ||
| while (current?.initial) { | ||
| const nextPath = `${resolvedPath}${STATE_DELIMITER}${current.initial}`; | ||
| const nextState = stateIndex.get(nextPath); | ||
| if (!nextState) break; | ||
| resolvedPath = nextPath; | ||
| current = nextState; | ||
| } | ||
| return resolvedPath; | ||
| } | ||
| function hasStatePath(machine, value) { | ||
| const stateIndex = ensureStateIndex(machine); | ||
| return stateIndex.has(value); | ||
| } | ||
| function resolveStateValue(machine, value, source) { | ||
| const stateValue = String(value); | ||
| if (!isAbsoluteStatePath(stateValue) && source) { | ||
| const sourceSegments = toSegments(source); | ||
| for (let index = sourceSegments.length; index >= 1; index--) { | ||
| const base = sourceSegments.slice(0, index).join(STATE_DELIMITER); | ||
| const candidate = appendStatePath(base, stateValue); | ||
| if (hasStatePath(machine, candidate)) return resolveAbsoluteStateValue(machine, candidate); | ||
| } | ||
| } | ||
| return resolveAbsoluteStateValue(machine, stateValue); | ||
| } | ||
| function getStateDefinition(machine, state) { | ||
| const chain = getStateChain(machine, state); | ||
| return chain[chain.length - 1]?.state; | ||
| } | ||
| function findTransition(machine, state, eventType) { | ||
| const chain = getStateChain(machine, state); | ||
| for (let index = chain.length - 1; index >= 0; index--) { | ||
| const transitionMap = chain[index]?.state.on; | ||
| const transition = transitionMap?.[eventType]; | ||
| if (transition) return { transitions: transition, source: chain[index]?.path }; | ||
| } | ||
| const rootTransitionMap = machine.on; | ||
| return { transitions: rootTransitionMap?.[eventType], source: void 0 }; | ||
| } | ||
| function getExitEnterStates(machine, prevState, nextState, reenter) { | ||
| const prevChain = prevState ? getStateChain(machine, prevState) : []; | ||
| const nextChain = getStateChain(machine, nextState); | ||
| let commonIndex = 0; | ||
| while (commonIndex < prevChain.length && commonIndex < nextChain.length && prevChain[commonIndex]?.path === nextChain[commonIndex]?.path) { | ||
| commonIndex += 1; | ||
| } | ||
| let exiting = prevChain.slice(commonIndex).reverse(); | ||
| let entering = nextChain.slice(commonIndex); | ||
| const sameLeaf = prevChain.at(-1)?.path === nextChain.at(-1)?.path; | ||
| if (reenter && sameLeaf) { | ||
| exiting = prevChain.slice().reverse(); | ||
| entering = nextChain; | ||
| } | ||
| return { exiting, entering }; | ||
| } | ||
| function matchesState(current, value) { | ||
| if (!current) return false; | ||
| return current === value || current.startsWith(`${value}${STATE_DELIMITER}`); | ||
| } | ||
| function hasTag(machine, state, tag) { | ||
| return getStateChain(machine, state).some((item) => item.state.tags?.includes(tag)); | ||
| } | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| ensureStateIndex, | ||
| findTransition, | ||
| getExitEnterStates, | ||
| getStateChain, | ||
| getStateDefinition, | ||
| hasTag, | ||
| matchesState, | ||
| resolveStateValue | ||
| }); |
+140
| // src/state.ts | ||
| var STATE_DELIMITER = "."; | ||
| var stateIndexCache = /* @__PURE__ */ new WeakMap(); | ||
| function joinStatePath(parts) { | ||
| return parts.join(STATE_DELIMITER); | ||
| } | ||
| function isAbsoluteStatePath(value) { | ||
| return value.includes(STATE_DELIMITER); | ||
| } | ||
| function appendStatePath(base, segment) { | ||
| return base ? `${base}${STATE_DELIMITER}${segment}` : segment; | ||
| } | ||
| function buildStateIndex(machine) { | ||
| const index = /* @__PURE__ */ new Map(); | ||
| const visit = (basePath, state) => { | ||
| index.set(basePath, state); | ||
| const childStates = state.states; | ||
| if (!childStates) return; | ||
| for (const [childKey, childState] of Object.entries(childStates)) { | ||
| if (!childState) continue; | ||
| const childPath = appendStatePath(basePath, childKey); | ||
| visit(childPath, childState); | ||
| } | ||
| }; | ||
| for (const [topKey, topState] of Object.entries(machine.states)) { | ||
| if (!topState) continue; | ||
| visit(topKey, topState); | ||
| } | ||
| return index; | ||
| } | ||
| function ensureStateIndex(machine) { | ||
| const cached = stateIndexCache.get(machine); | ||
| if (cached) return cached; | ||
| const index = buildStateIndex(machine); | ||
| stateIndexCache.set(machine, index); | ||
| return index; | ||
| } | ||
| function toSegments(value) { | ||
| if (!value) return []; | ||
| return String(value).split(STATE_DELIMITER).filter(Boolean); | ||
| } | ||
| function getStateChain(machine, state) { | ||
| if (!state) return []; | ||
| const stateIndex = ensureStateIndex(machine); | ||
| const segments = toSegments(state); | ||
| const chain = []; | ||
| const statePath = []; | ||
| for (const segment of segments) { | ||
| statePath.push(segment); | ||
| const path = joinStatePath(statePath); | ||
| const current = stateIndex.get(path); | ||
| if (!current) break; | ||
| chain.push({ path, state: current }); | ||
| } | ||
| return chain; | ||
| } | ||
| function resolveAbsoluteStateValue(machine, value) { | ||
| const stateIndex = ensureStateIndex(machine); | ||
| const segments = toSegments(value); | ||
| if (!segments.length) return value; | ||
| const resolved = []; | ||
| for (const segment of segments) { | ||
| resolved.push(segment); | ||
| const path = joinStatePath(resolved); | ||
| if (!stateIndex.has(path)) return value; | ||
| } | ||
| let resolvedPath = joinStatePath(resolved); | ||
| let current = stateIndex.get(resolvedPath); | ||
| while (current?.initial) { | ||
| const nextPath = `${resolvedPath}${STATE_DELIMITER}${current.initial}`; | ||
| const nextState = stateIndex.get(nextPath); | ||
| if (!nextState) break; | ||
| resolvedPath = nextPath; | ||
| current = nextState; | ||
| } | ||
| return resolvedPath; | ||
| } | ||
| function hasStatePath(machine, value) { | ||
| const stateIndex = ensureStateIndex(machine); | ||
| return stateIndex.has(value); | ||
| } | ||
| function resolveStateValue(machine, value, source) { | ||
| const stateValue = String(value); | ||
| if (!isAbsoluteStatePath(stateValue) && source) { | ||
| const sourceSegments = toSegments(source); | ||
| for (let index = sourceSegments.length; index >= 1; index--) { | ||
| const base = sourceSegments.slice(0, index).join(STATE_DELIMITER); | ||
| const candidate = appendStatePath(base, stateValue); | ||
| if (hasStatePath(machine, candidate)) return resolveAbsoluteStateValue(machine, candidate); | ||
| } | ||
| } | ||
| return resolveAbsoluteStateValue(machine, stateValue); | ||
| } | ||
| function getStateDefinition(machine, state) { | ||
| const chain = getStateChain(machine, state); | ||
| return chain[chain.length - 1]?.state; | ||
| } | ||
| function findTransition(machine, state, eventType) { | ||
| const chain = getStateChain(machine, state); | ||
| for (let index = chain.length - 1; index >= 0; index--) { | ||
| const transitionMap = chain[index]?.state.on; | ||
| const transition = transitionMap?.[eventType]; | ||
| if (transition) return { transitions: transition, source: chain[index]?.path }; | ||
| } | ||
| const rootTransitionMap = machine.on; | ||
| return { transitions: rootTransitionMap?.[eventType], source: void 0 }; | ||
| } | ||
| function getExitEnterStates(machine, prevState, nextState, reenter) { | ||
| const prevChain = prevState ? getStateChain(machine, prevState) : []; | ||
| const nextChain = getStateChain(machine, nextState); | ||
| let commonIndex = 0; | ||
| while (commonIndex < prevChain.length && commonIndex < nextChain.length && prevChain[commonIndex]?.path === nextChain[commonIndex]?.path) { | ||
| commonIndex += 1; | ||
| } | ||
| let exiting = prevChain.slice(commonIndex).reverse(); | ||
| let entering = nextChain.slice(commonIndex); | ||
| const sameLeaf = prevChain.at(-1)?.path === nextChain.at(-1)?.path; | ||
| if (reenter && sameLeaf) { | ||
| exiting = prevChain.slice().reverse(); | ||
| entering = nextChain; | ||
| } | ||
| return { exiting, entering }; | ||
| } | ||
| function matchesState(current, value) { | ||
| if (!current) return false; | ||
| return current === value || current.startsWith(`${value}${STATE_DELIMITER}`); | ||
| } | ||
| function hasTag(machine, state, tag) { | ||
| return getStateChain(machine, state).some((item) => item.state.tags?.includes(tag)); | ||
| } | ||
| export { | ||
| ensureStateIndex, | ||
| findTransition, | ||
| getExitEnterStates, | ||
| getStateChain, | ||
| getStateDefinition, | ||
| hasTag, | ||
| matchesState, | ||
| resolveStateValue | ||
| }; |
+232
| type Dict = Record<string, any>; | ||
| interface ComputedParams<T extends Dict> { | ||
| context: BindableContext<T>; | ||
| event: EventType<T["event"]>; | ||
| prop: PropFn<T>; | ||
| refs: BindableRefs<T>; | ||
| scope: Scope; | ||
| computed: ComputedFn<T>; | ||
| } | ||
| interface ContextParams<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| bindable: BindableFn; | ||
| scope: Scope; | ||
| getContext: () => BindableContext<T>; | ||
| getComputed: () => ComputedFn<T>; | ||
| getRefs: () => BindableRefs<T>; | ||
| getEvent: () => EventType<T["event"]>; | ||
| flush: (fn: VoidFunction) => void; | ||
| } | ||
| interface PropFn<T extends Dict> { | ||
| <K extends keyof T["props"]>(key: K): T["props"][K]; | ||
| } | ||
| interface ComputedFn<T extends Dict> { | ||
| <K extends keyof T["computed"]>(key: K): T["computed"][K]; | ||
| } | ||
| type AnyFunction = () => string | number | boolean | null | undefined; | ||
| type TrackFn = (deps: AnyFunction[], fn: VoidFunction) => void; | ||
| interface BindableParams<T> { | ||
| defaultValue?: T | undefined; | ||
| value?: T | undefined; | ||
| hash?: ((a: T) => string) | undefined; | ||
| isEqual?: ((a: T, b: T | undefined) => boolean) | undefined; | ||
| onChange?: ((value: T, prev: T | undefined) => void) | undefined; | ||
| debug?: string | undefined; | ||
| sync?: boolean | undefined; | ||
| } | ||
| type ValueOrFn<T> = T | ((prev: T) => T); | ||
| interface Bindable<T> { | ||
| initial: T | undefined; | ||
| ref: any; | ||
| get: () => T; | ||
| set(value: ValueOrFn<T>): void; | ||
| invoke(nextValue: T, prevValue: T): void; | ||
| hash(value: T): string; | ||
| } | ||
| interface BindableRefs<T extends Dict> { | ||
| set<K extends keyof T["refs"]>(key: K, value: T["refs"][K]): void; | ||
| get<K extends keyof T["refs"]>(key: K): T["refs"][K]; | ||
| } | ||
| interface BindableContext<T extends Dict> { | ||
| set<K extends keyof T["context"]>(key: K, value: ValueOrFn<T["context"][K]>): void; | ||
| get<K extends keyof T["context"]>(key: K): T["context"][K]; | ||
| initial<K extends keyof T["context"]>(key: K): T["context"][K]; | ||
| hash<K extends keyof T["context"]>(key: K): string; | ||
| } | ||
| interface BindableRef<T> { | ||
| get: () => T; | ||
| set: (next: T) => void; | ||
| } | ||
| interface BindableFn { | ||
| <K>(params: () => BindableParams<K>): Bindable<K>; | ||
| cleanup: (fn: VoidFunction) => void; | ||
| ref: <T>(defaultValue: T) => BindableRef<T>; | ||
| } | ||
| interface Scope { | ||
| id?: string | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| getRootNode: () => ShadowRoot | Document | Node; | ||
| getById: <T extends Element = HTMLElement>(id: string) => T | null; | ||
| getActiveElement: () => HTMLElement | null; | ||
| isActiveElement: (elem: HTMLElement | null) => boolean; | ||
| getDoc: () => typeof document; | ||
| getWin: () => typeof window; | ||
| } | ||
| type EventType<T = any> = T & { | ||
| previousEvent?: (T & { | ||
| [key: string]: any; | ||
| }) | undefined; | ||
| src?: string | undefined; | ||
| [key: string]: any; | ||
| }; | ||
| type EventObject = EventType<{ | ||
| type: string; | ||
| }>; | ||
| interface Params<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| action: (action: T["action"][]) => void; | ||
| context: BindableContext<T>; | ||
| refs: BindableRefs<T>; | ||
| track: TrackFn; | ||
| flush: (fn: VoidFunction) => void; | ||
| event: EventType<T["event"]> & { | ||
| current: () => EventType<T["event"]>; | ||
| previous: () => EventType<T["event"]>; | ||
| }; | ||
| send: (event: EventType<T["event"]>) => void; | ||
| computed: ComputedFn<T>; | ||
| scope: Scope; | ||
| state: Bindable<T["state"]> & { | ||
| matches: (...values: T["state"][]) => boolean; | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| }; | ||
| choose: ChooseFn<T>; | ||
| guard: (key: T["guard"] | GuardFn<T>) => boolean | undefined; | ||
| } | ||
| type GuardFn<T extends Dict> = (params: Params<T>) => boolean; | ||
| type TopLevelState<S extends string> = S extends `${infer Top}.${string}` ? Top : S; | ||
| type ChildStateKey<S extends string, Parent extends string> = S extends `${Parent}.${infer Rest}` ? Rest extends `${infer Child}.${string}` ? Child : Rest : never; | ||
| type ParentPath<S extends string> = S extends `${infer Parent}.${string}` ? Parent : never; | ||
| type AncestorPaths<S extends string> = S | (ParentPath<S> extends never ? never : AncestorPaths<ParentPath<S>>); | ||
| type RelativeStateTarget<S extends string, Source extends string> = ChildStateKey<S, AncestorPaths<Source>>; | ||
| interface Transition<T extends Dict, Source extends string | undefined = string | undefined> { | ||
| target?: T["state"] | (Source extends string ? RelativeStateTarget<T["state"], Source> : never) | undefined; | ||
| actions?: T["action"][] | undefined; | ||
| guard?: T["guard"] | GuardFn<T> | undefined; | ||
| reenter?: boolean | undefined; | ||
| } | ||
| type TransitionSet<T extends Dict> = Transition<T> | Transition<T>[] | undefined; | ||
| type TransitionMap<T extends Dict> = Record<string, TransitionSet<T>>; | ||
| type TransitionMatch<T extends Dict> = { | ||
| transitions: TransitionSet<T>; | ||
| source: string | undefined; | ||
| }; | ||
| type MaybeArray<T> = T | T[]; | ||
| type ChooseFn<T extends Dict> = (transitions: MaybeArray<Omit<Transition<T, string>, "target">> | null | undefined) => Transition<T> | undefined; | ||
| interface PropsParams<T extends Dict> { | ||
| props: Partial<T["props"]>; | ||
| scope: Scope; | ||
| } | ||
| interface RefsParams<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| context: BindableContext<T>; | ||
| } | ||
| type ActionsOrFn<T extends Dict> = T["action"][] | ((params: Params<T>) => T["action"][] | undefined); | ||
| type EffectsOrFn<T extends Dict> = T["effect"][] | ((params: Params<T>) => T["effect"][] | undefined); | ||
| interface MachineState<T extends Dict, Parent extends string = string> { | ||
| tags?: T["tag"][] | undefined; | ||
| entry?: ActionsOrFn<T> | undefined; | ||
| exit?: ActionsOrFn<T> | undefined; | ||
| effects?: EffectsOrFn<T> | undefined; | ||
| initial?: ChildStateKey<T["state"], Parent> | undefined; | ||
| states?: { | ||
| [K in ChildStateKey<T["state"], Parent>]?: MachineState<T, `${Parent}.${K}`>; | ||
| } | undefined; | ||
| on?: { | ||
| [E in T["event"]["type"]]?: Transition<T, Parent> | Array<Transition<T, Parent>>; | ||
| } | undefined; | ||
| } | ||
| interface Machine<T extends Dict> { | ||
| debug?: boolean | undefined; | ||
| props?: ((params: PropsParams<T>) => T["props"]) | undefined; | ||
| context?: ((params: ContextParams<T>) => { | ||
| [K in keyof T["context"]]: Bindable<T["context"][K]>; | ||
| }) | undefined; | ||
| computed?: { | ||
| [K in keyof T["computed"]]: (params: ComputedParams<T>) => T["computed"][K]; | ||
| } | undefined; | ||
| initialState: (params: { | ||
| prop: PropFn<T>; | ||
| }) => T["state"]; | ||
| entry?: ActionsOrFn<T> | undefined; | ||
| exit?: ActionsOrFn<T> | undefined; | ||
| effects?: EffectsOrFn<T> | undefined; | ||
| refs?: ((params: RefsParams<T>) => T["refs"]) | undefined; | ||
| watch?: ((params: Params<T>) => void) | undefined; | ||
| on?: { | ||
| [E in T["event"]["type"]]?: Transition<T, undefined> | Array<Transition<T, undefined>>; | ||
| } | undefined; | ||
| states: { | ||
| [K in TopLevelState<T["state"]>]: MachineState<T, K>; | ||
| }; | ||
| implementations?: { | ||
| guards?: { | ||
| [K in T["guard"]]: (params: Params<T>) => boolean; | ||
| } | undefined; | ||
| actions?: { | ||
| [K in T["action"]]: (params: Params<T>) => void; | ||
| } | undefined; | ||
| effects?: { | ||
| [K in T["effect"]]: (params: Params<T>) => void | VoidFunction; | ||
| } | undefined; | ||
| } | undefined; | ||
| } | ||
| interface MachineBaseProps { | ||
| id?: string | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| getRootNode?: (() => ShadowRoot | Document | Node) | undefined; | ||
| [key: string]: any; | ||
| } | ||
| interface MachineSchema { | ||
| props?: MachineBaseProps | undefined; | ||
| context?: Record<string, any> | undefined; | ||
| refs?: Record<string, any> | undefined; | ||
| computed?: Record<string, any> | undefined; | ||
| state?: string | undefined; | ||
| tag?: string | undefined; | ||
| guard?: string | undefined; | ||
| action?: string | undefined; | ||
| effect?: string | undefined; | ||
| event?: ({ | ||
| type: string; | ||
| } & Dict) | undefined; | ||
| } | ||
| type State<T extends MachineSchema> = Bindable<T["state"]> & { | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| matches: (...values: T["state"][]) => boolean; | ||
| }; | ||
| type Service<T extends MachineSchema> = { | ||
| getStatus: () => MachineStatus; | ||
| state: State<T> & { | ||
| matches: (...values: T["state"][]) => boolean; | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| }; | ||
| context: BindableContext<T>; | ||
| send: (event: EventType<T["event"]>) => void; | ||
| prop: PropFn<T>; | ||
| scope: Scope; | ||
| computed: ComputedFn<T>; | ||
| refs: BindableRefs<T>; | ||
| event: EventType<T["event"]> & { | ||
| current: () => EventType<T["event"]>; | ||
| previous: () => EventType<T["event"]>; | ||
| }; | ||
| }; | ||
| declare enum MachineStatus { | ||
| NotStarted = "Not Started", | ||
| Started = "Started", | ||
| Stopped = "Stopped" | ||
| } | ||
| declare const INIT_STATE = "__init__"; | ||
| export { type ActionsOrFn, type Bindable, type BindableContext, type BindableFn, type BindableParams, type BindableRefs, type ChooseFn, type ComputedFn, type EffectsOrFn, type EventObject, type GuardFn, INIT_STATE, type Machine, type MachineSchema, type MachineState, MachineStatus, type Params, type PropFn, type Scope, type Service, type Transition, type TransitionMap, type TransitionMatch, type TransitionSet, type ValueOrFn }; |
+232
| type Dict = Record<string, any>; | ||
| interface ComputedParams<T extends Dict> { | ||
| context: BindableContext<T>; | ||
| event: EventType<T["event"]>; | ||
| prop: PropFn<T>; | ||
| refs: BindableRefs<T>; | ||
| scope: Scope; | ||
| computed: ComputedFn<T>; | ||
| } | ||
| interface ContextParams<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| bindable: BindableFn; | ||
| scope: Scope; | ||
| getContext: () => BindableContext<T>; | ||
| getComputed: () => ComputedFn<T>; | ||
| getRefs: () => BindableRefs<T>; | ||
| getEvent: () => EventType<T["event"]>; | ||
| flush: (fn: VoidFunction) => void; | ||
| } | ||
| interface PropFn<T extends Dict> { | ||
| <K extends keyof T["props"]>(key: K): T["props"][K]; | ||
| } | ||
| interface ComputedFn<T extends Dict> { | ||
| <K extends keyof T["computed"]>(key: K): T["computed"][K]; | ||
| } | ||
| type AnyFunction = () => string | number | boolean | null | undefined; | ||
| type TrackFn = (deps: AnyFunction[], fn: VoidFunction) => void; | ||
| interface BindableParams<T> { | ||
| defaultValue?: T | undefined; | ||
| value?: T | undefined; | ||
| hash?: ((a: T) => string) | undefined; | ||
| isEqual?: ((a: T, b: T | undefined) => boolean) | undefined; | ||
| onChange?: ((value: T, prev: T | undefined) => void) | undefined; | ||
| debug?: string | undefined; | ||
| sync?: boolean | undefined; | ||
| } | ||
| type ValueOrFn<T> = T | ((prev: T) => T); | ||
| interface Bindable<T> { | ||
| initial: T | undefined; | ||
| ref: any; | ||
| get: () => T; | ||
| set(value: ValueOrFn<T>): void; | ||
| invoke(nextValue: T, prevValue: T): void; | ||
| hash(value: T): string; | ||
| } | ||
| interface BindableRefs<T extends Dict> { | ||
| set<K extends keyof T["refs"]>(key: K, value: T["refs"][K]): void; | ||
| get<K extends keyof T["refs"]>(key: K): T["refs"][K]; | ||
| } | ||
| interface BindableContext<T extends Dict> { | ||
| set<K extends keyof T["context"]>(key: K, value: ValueOrFn<T["context"][K]>): void; | ||
| get<K extends keyof T["context"]>(key: K): T["context"][K]; | ||
| initial<K extends keyof T["context"]>(key: K): T["context"][K]; | ||
| hash<K extends keyof T["context"]>(key: K): string; | ||
| } | ||
| interface BindableRef<T> { | ||
| get: () => T; | ||
| set: (next: T) => void; | ||
| } | ||
| interface BindableFn { | ||
| <K>(params: () => BindableParams<K>): Bindable<K>; | ||
| cleanup: (fn: VoidFunction) => void; | ||
| ref: <T>(defaultValue: T) => BindableRef<T>; | ||
| } | ||
| interface Scope { | ||
| id?: string | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| getRootNode: () => ShadowRoot | Document | Node; | ||
| getById: <T extends Element = HTMLElement>(id: string) => T | null; | ||
| getActiveElement: () => HTMLElement | null; | ||
| isActiveElement: (elem: HTMLElement | null) => boolean; | ||
| getDoc: () => typeof document; | ||
| getWin: () => typeof window; | ||
| } | ||
| type EventType<T = any> = T & { | ||
| previousEvent?: (T & { | ||
| [key: string]: any; | ||
| }) | undefined; | ||
| src?: string | undefined; | ||
| [key: string]: any; | ||
| }; | ||
| type EventObject = EventType<{ | ||
| type: string; | ||
| }>; | ||
| interface Params<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| action: (action: T["action"][]) => void; | ||
| context: BindableContext<T>; | ||
| refs: BindableRefs<T>; | ||
| track: TrackFn; | ||
| flush: (fn: VoidFunction) => void; | ||
| event: EventType<T["event"]> & { | ||
| current: () => EventType<T["event"]>; | ||
| previous: () => EventType<T["event"]>; | ||
| }; | ||
| send: (event: EventType<T["event"]>) => void; | ||
| computed: ComputedFn<T>; | ||
| scope: Scope; | ||
| state: Bindable<T["state"]> & { | ||
| matches: (...values: T["state"][]) => boolean; | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| }; | ||
| choose: ChooseFn<T>; | ||
| guard: (key: T["guard"] | GuardFn<T>) => boolean | undefined; | ||
| } | ||
| type GuardFn<T extends Dict> = (params: Params<T>) => boolean; | ||
| type TopLevelState<S extends string> = S extends `${infer Top}.${string}` ? Top : S; | ||
| type ChildStateKey<S extends string, Parent extends string> = S extends `${Parent}.${infer Rest}` ? Rest extends `${infer Child}.${string}` ? Child : Rest : never; | ||
| type ParentPath<S extends string> = S extends `${infer Parent}.${string}` ? Parent : never; | ||
| type AncestorPaths<S extends string> = S | (ParentPath<S> extends never ? never : AncestorPaths<ParentPath<S>>); | ||
| type RelativeStateTarget<S extends string, Source extends string> = ChildStateKey<S, AncestorPaths<Source>>; | ||
| interface Transition<T extends Dict, Source extends string | undefined = string | undefined> { | ||
| target?: T["state"] | (Source extends string ? RelativeStateTarget<T["state"], Source> : never) | undefined; | ||
| actions?: T["action"][] | undefined; | ||
| guard?: T["guard"] | GuardFn<T> | undefined; | ||
| reenter?: boolean | undefined; | ||
| } | ||
| type TransitionSet<T extends Dict> = Transition<T> | Transition<T>[] | undefined; | ||
| type TransitionMap<T extends Dict> = Record<string, TransitionSet<T>>; | ||
| type TransitionMatch<T extends Dict> = { | ||
| transitions: TransitionSet<T>; | ||
| source: string | undefined; | ||
| }; | ||
| type MaybeArray<T> = T | T[]; | ||
| type ChooseFn<T extends Dict> = (transitions: MaybeArray<Omit<Transition<T, string>, "target">> | null | undefined) => Transition<T> | undefined; | ||
| interface PropsParams<T extends Dict> { | ||
| props: Partial<T["props"]>; | ||
| scope: Scope; | ||
| } | ||
| interface RefsParams<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| context: BindableContext<T>; | ||
| } | ||
| type ActionsOrFn<T extends Dict> = T["action"][] | ((params: Params<T>) => T["action"][] | undefined); | ||
| type EffectsOrFn<T extends Dict> = T["effect"][] | ((params: Params<T>) => T["effect"][] | undefined); | ||
| interface MachineState<T extends Dict, Parent extends string = string> { | ||
| tags?: T["tag"][] | undefined; | ||
| entry?: ActionsOrFn<T> | undefined; | ||
| exit?: ActionsOrFn<T> | undefined; | ||
| effects?: EffectsOrFn<T> | undefined; | ||
| initial?: ChildStateKey<T["state"], Parent> | undefined; | ||
| states?: { | ||
| [K in ChildStateKey<T["state"], Parent>]?: MachineState<T, `${Parent}.${K}`>; | ||
| } | undefined; | ||
| on?: { | ||
| [E in T["event"]["type"]]?: Transition<T, Parent> | Array<Transition<T, Parent>>; | ||
| } | undefined; | ||
| } | ||
| interface Machine<T extends Dict> { | ||
| debug?: boolean | undefined; | ||
| props?: ((params: PropsParams<T>) => T["props"]) | undefined; | ||
| context?: ((params: ContextParams<T>) => { | ||
| [K in keyof T["context"]]: Bindable<T["context"][K]>; | ||
| }) | undefined; | ||
| computed?: { | ||
| [K in keyof T["computed"]]: (params: ComputedParams<T>) => T["computed"][K]; | ||
| } | undefined; | ||
| initialState: (params: { | ||
| prop: PropFn<T>; | ||
| }) => T["state"]; | ||
| entry?: ActionsOrFn<T> | undefined; | ||
| exit?: ActionsOrFn<T> | undefined; | ||
| effects?: EffectsOrFn<T> | undefined; | ||
| refs?: ((params: RefsParams<T>) => T["refs"]) | undefined; | ||
| watch?: ((params: Params<T>) => void) | undefined; | ||
| on?: { | ||
| [E in T["event"]["type"]]?: Transition<T, undefined> | Array<Transition<T, undefined>>; | ||
| } | undefined; | ||
| states: { | ||
| [K in TopLevelState<T["state"]>]: MachineState<T, K>; | ||
| }; | ||
| implementations?: { | ||
| guards?: { | ||
| [K in T["guard"]]: (params: Params<T>) => boolean; | ||
| } | undefined; | ||
| actions?: { | ||
| [K in T["action"]]: (params: Params<T>) => void; | ||
| } | undefined; | ||
| effects?: { | ||
| [K in T["effect"]]: (params: Params<T>) => void | VoidFunction; | ||
| } | undefined; | ||
| } | undefined; | ||
| } | ||
| interface MachineBaseProps { | ||
| id?: string | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| getRootNode?: (() => ShadowRoot | Document | Node) | undefined; | ||
| [key: string]: any; | ||
| } | ||
| interface MachineSchema { | ||
| props?: MachineBaseProps | undefined; | ||
| context?: Record<string, any> | undefined; | ||
| refs?: Record<string, any> | undefined; | ||
| computed?: Record<string, any> | undefined; | ||
| state?: string | undefined; | ||
| tag?: string | undefined; | ||
| guard?: string | undefined; | ||
| action?: string | undefined; | ||
| effect?: string | undefined; | ||
| event?: ({ | ||
| type: string; | ||
| } & Dict) | undefined; | ||
| } | ||
| type State<T extends MachineSchema> = Bindable<T["state"]> & { | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| matches: (...values: T["state"][]) => boolean; | ||
| }; | ||
| type Service<T extends MachineSchema> = { | ||
| getStatus: () => MachineStatus; | ||
| state: State<T> & { | ||
| matches: (...values: T["state"][]) => boolean; | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| }; | ||
| context: BindableContext<T>; | ||
| send: (event: EventType<T["event"]>) => void; | ||
| prop: PropFn<T>; | ||
| scope: Scope; | ||
| computed: ComputedFn<T>; | ||
| refs: BindableRefs<T>; | ||
| event: EventType<T["event"]> & { | ||
| current: () => EventType<T["event"]>; | ||
| previous: () => EventType<T["event"]>; | ||
| }; | ||
| }; | ||
| declare enum MachineStatus { | ||
| NotStarted = "Not Started", | ||
| Started = "Started", | ||
| Stopped = "Stopped" | ||
| } | ||
| declare const INIT_STATE = "__init__"; | ||
| export { type ActionsOrFn, type Bindable, type BindableContext, type BindableFn, type BindableParams, type BindableRefs, type ChooseFn, type ComputedFn, type EffectsOrFn, type EventObject, type GuardFn, INIT_STATE, type Machine, type MachineSchema, type MachineState, MachineStatus, type Params, type PropFn, type Scope, type Service, type Transition, type TransitionMap, type TransitionMatch, type TransitionSet, type ValueOrFn }; |
| "use strict"; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __export = (target, all) => { | ||
| for (var name in all) | ||
| __defProp(target, name, { get: all[name], enumerable: true }); | ||
| }; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return to; | ||
| }; | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // src/types.ts | ||
| var types_exports = {}; | ||
| __export(types_exports, { | ||
| INIT_STATE: () => INIT_STATE, | ||
| MachineStatus: () => MachineStatus | ||
| }); | ||
| module.exports = __toCommonJS(types_exports); | ||
| var MachineStatus = /* @__PURE__ */ ((MachineStatus2) => { | ||
| MachineStatus2["NotStarted"] = "Not Started"; | ||
| MachineStatus2["Started"] = "Started"; | ||
| MachineStatus2["Stopped"] = "Stopped"; | ||
| return MachineStatus2; | ||
| })(MachineStatus || {}); | ||
| var INIT_STATE = "__init__"; | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| INIT_STATE, | ||
| MachineStatus | ||
| }); |
| // src/types.ts | ||
| var MachineStatus = /* @__PURE__ */ ((MachineStatus2) => { | ||
| MachineStatus2["NotStarted"] = "Not Started"; | ||
| MachineStatus2["Started"] = "Started"; | ||
| MachineStatus2["Stopped"] = "Stopped"; | ||
| return MachineStatus2; | ||
| })(MachineStatus || {}); | ||
| var INIT_STATE = "__init__"; | ||
| export { | ||
| INIT_STATE, | ||
| MachineStatus | ||
| }; |
+7
-257
@@ -1,257 +0,7 @@ | ||
| import { isActiveElement } from '@zag-js/dom-query'; | ||
| interface Props { | ||
| [key: string | symbol]: any; | ||
| } | ||
| type TupleTypes<T extends any[]> = T[number]; | ||
| type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; | ||
| declare function mergeProps<T extends Props>(...args: Array<T | undefined>): UnionToIntersection<TupleTypes<T[]>>; | ||
| type NoInfer<T> = [T][T extends any ? 0 : never]; | ||
| declare function memo<TDeps extends any[], TDepArgs, TResult>(getDeps: (depArgs: TDepArgs) => [...TDeps], fn: (args: NoInfer<[...TDeps]>, deps: TDepArgs) => TResult, opts?: { | ||
| onChange?: ((result: TResult) => void) | undefined; | ||
| }): (depArgs: TDepArgs) => TResult; | ||
| type Dict = Record<string, any>; | ||
| interface ComputedParams<T extends Dict> { | ||
| context: BindableContext<T>; | ||
| event: EventType<T["event"]>; | ||
| prop: PropFn<T>; | ||
| refs: BindableRefs<T>; | ||
| scope: Scope; | ||
| computed: ComputedFn<T>; | ||
| } | ||
| interface ContextParams<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| bindable: BindableFn; | ||
| scope: Scope; | ||
| getContext: () => BindableContext<T>; | ||
| getComputed: () => ComputedFn<T>; | ||
| getRefs: () => BindableRefs<T>; | ||
| getEvent: () => EventType<T["event"]>; | ||
| flush: (fn: VoidFunction) => void; | ||
| } | ||
| interface PropFn<T extends Dict> { | ||
| <K extends keyof T["props"]>(key: K): T["props"][K]; | ||
| } | ||
| interface ComputedFn<T extends Dict> { | ||
| <K extends keyof T["computed"]>(key: K): T["computed"][K]; | ||
| } | ||
| type AnyFunction = () => string | number | boolean | null | undefined; | ||
| type TrackFn = (deps: AnyFunction[], fn: VoidFunction) => void; | ||
| interface BindableParams<T> { | ||
| defaultValue?: T | undefined; | ||
| value?: T | undefined; | ||
| hash?: ((a: T) => string) | undefined; | ||
| isEqual?: ((a: T, b: T | undefined) => boolean) | undefined; | ||
| onChange?: ((value: T, prev: T | undefined) => void) | undefined; | ||
| debug?: string | undefined; | ||
| sync?: boolean | undefined; | ||
| } | ||
| type ValueOrFn<T> = T | ((prev: T) => T); | ||
| interface Bindable<T> { | ||
| initial: T | undefined; | ||
| ref: any; | ||
| get: () => T; | ||
| set(value: ValueOrFn<T>): void; | ||
| invoke(nextValue: T, prevValue: T): void; | ||
| hash(value: T): string; | ||
| } | ||
| interface BindableRefs<T extends Dict> { | ||
| set<K extends keyof T["refs"]>(key: K, value: T["refs"][K]): void; | ||
| get<K extends keyof T["refs"]>(key: K): T["refs"][K]; | ||
| } | ||
| interface BindableContext<T extends Dict> { | ||
| set<K extends keyof T["context"]>(key: K, value: ValueOrFn<T["context"][K]>): void; | ||
| get<K extends keyof T["context"]>(key: K): T["context"][K]; | ||
| initial<K extends keyof T["context"]>(key: K): T["context"][K]; | ||
| hash<K extends keyof T["context"]>(key: K): string; | ||
| } | ||
| interface BindableRef<T> { | ||
| get: () => T; | ||
| set: (next: T) => void; | ||
| } | ||
| interface BindableFn { | ||
| <K>(params: () => BindableParams<K>): Bindable<K>; | ||
| cleanup: (fn: VoidFunction) => void; | ||
| ref: <T>(defaultValue: T) => BindableRef<T>; | ||
| } | ||
| interface Scope { | ||
| id?: string | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| getRootNode: () => ShadowRoot | Document | Node; | ||
| getById: <T extends Element = HTMLElement>(id: string) => T | null; | ||
| getActiveElement: () => HTMLElement | null; | ||
| isActiveElement: (elem: HTMLElement | null) => boolean; | ||
| getDoc: () => typeof document; | ||
| getWin: () => typeof window; | ||
| } | ||
| type EventType<T = any> = T & { | ||
| previousEvent?: (T & { | ||
| [key: string]: any; | ||
| }) | undefined; | ||
| src?: string | undefined; | ||
| [key: string]: any; | ||
| }; | ||
| type EventObject = EventType<{ | ||
| type: string; | ||
| }>; | ||
| interface Params<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| action: (action: T["action"][]) => void; | ||
| context: BindableContext<T>; | ||
| refs: BindableRefs<T>; | ||
| track: TrackFn; | ||
| flush: (fn: VoidFunction) => void; | ||
| event: EventType<T["event"]> & { | ||
| current: () => EventType<T["event"]>; | ||
| previous: () => EventType<T["event"]>; | ||
| }; | ||
| send: (event: EventType<T["event"]>) => void; | ||
| computed: ComputedFn<T>; | ||
| scope: Scope; | ||
| state: Bindable<T["state"]> & { | ||
| matches: (...values: T["state"][]) => boolean; | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| }; | ||
| choose: ChooseFn<T>; | ||
| guard: (key: T["guard"] | GuardFn<T>) => boolean | undefined; | ||
| } | ||
| type GuardFn<T extends Dict> = (params: Params<T>) => boolean; | ||
| interface Transition<T extends Dict> { | ||
| target?: T["state"] | undefined; | ||
| actions?: T["action"][] | undefined; | ||
| guard?: T["guard"] | GuardFn<T> | undefined; | ||
| reenter?: boolean | undefined; | ||
| } | ||
| type MaybeArray<T> = T | T[]; | ||
| type ChooseFn<T extends Dict> = (transitions: MaybeArray<Omit<Transition<T>, "target">>) => Transition<T> | undefined; | ||
| interface PropsParams<T extends Dict> { | ||
| props: Partial<T["props"]>; | ||
| scope: Scope; | ||
| } | ||
| interface RefsParams<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| context: BindableContext<T>; | ||
| } | ||
| type ActionsOrFn<T extends Dict> = T["action"][] | ((params: Params<T>) => T["action"][] | undefined); | ||
| type EffectsOrFn<T extends Dict> = T["effect"][] | ((params: Params<T>) => T["effect"][] | undefined); | ||
| interface Machine<T extends Dict> { | ||
| debug?: boolean | undefined; | ||
| props?: ((params: PropsParams<T>) => T["props"]) | undefined; | ||
| context?: ((params: ContextParams<T>) => { | ||
| [K in keyof T["context"]]: Bindable<T["context"][K]>; | ||
| }) | undefined; | ||
| computed?: { | ||
| [K in keyof T["computed"]]: (params: ComputedParams<T>) => T["computed"][K]; | ||
| } | undefined; | ||
| initialState: (params: { | ||
| prop: PropFn<T>; | ||
| }) => T["state"]; | ||
| entry?: ActionsOrFn<T> | undefined; | ||
| exit?: ActionsOrFn<T> | undefined; | ||
| effects?: EffectsOrFn<T> | undefined; | ||
| refs?: ((params: RefsParams<T>) => T["refs"]) | undefined; | ||
| watch?: ((params: Params<T>) => void) | undefined; | ||
| on?: { | ||
| [E in T["event"]["type"]]?: Transition<T> | Array<Transition<T>>; | ||
| } | undefined; | ||
| states: { | ||
| [K in T["state"]]: { | ||
| tags?: T["tag"][] | undefined; | ||
| entry?: ActionsOrFn<T> | undefined; | ||
| exit?: ActionsOrFn<T> | undefined; | ||
| effects?: EffectsOrFn<T> | undefined; | ||
| on?: { | ||
| [E in T["event"]["type"]]?: Transition<T> | Array<Transition<T>>; | ||
| } | undefined; | ||
| }; | ||
| }; | ||
| implementations?: { | ||
| guards?: { | ||
| [K in T["guard"]]: (params: Params<T>) => boolean; | ||
| } | undefined; | ||
| actions?: { | ||
| [K in T["action"]]: (params: Params<T>) => void; | ||
| } | undefined; | ||
| effects?: { | ||
| [K in T["effect"]]: (params: Params<T>) => void | VoidFunction; | ||
| } | undefined; | ||
| } | undefined; | ||
| } | ||
| interface MachineBaseProps { | ||
| id?: string | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| getRootNode?: (() => ShadowRoot | Document | Node) | undefined; | ||
| [key: string]: any; | ||
| } | ||
| interface MachineSchema { | ||
| props?: MachineBaseProps | undefined; | ||
| context?: Record<string, any> | undefined; | ||
| refs?: Record<string, any> | undefined; | ||
| computed?: Record<string, any> | undefined; | ||
| state?: string | undefined; | ||
| tag?: string | undefined; | ||
| guard?: string | undefined; | ||
| action?: string | undefined; | ||
| effect?: string | undefined; | ||
| event?: ({ | ||
| type: string; | ||
| } & Dict) | undefined; | ||
| } | ||
| type State<T extends MachineSchema> = Bindable<T["state"]> & { | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| matches: (...values: T["state"][]) => boolean; | ||
| }; | ||
| type Service<T extends MachineSchema> = { | ||
| getStatus: () => MachineStatus; | ||
| state: State<T> & { | ||
| matches: (...values: T["state"][]) => boolean; | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| }; | ||
| context: BindableContext<T>; | ||
| send: (event: EventType<T["event"]>) => void; | ||
| prop: PropFn<T>; | ||
| scope: Scope; | ||
| computed: ComputedFn<T>; | ||
| refs: BindableRefs<T>; | ||
| event: EventType<T["event"]> & { | ||
| current: () => EventType<T["event"]>; | ||
| previous: () => EventType<T["event"]>; | ||
| }; | ||
| }; | ||
| declare enum MachineStatus { | ||
| NotStarted = "Not Started", | ||
| Started = "Started", | ||
| Stopped = "Stopped" | ||
| } | ||
| declare const INIT_STATE = "__init__"; | ||
| declare function createGuards<T extends MachineSchema>(): { | ||
| and: (...guards: Array<GuardFn<T> | T["guard"]>) => (params: any) => boolean; | ||
| or: (...guards: Array<GuardFn<T> | T["guard"]>) => (params: any) => boolean; | ||
| not: (guard: GuardFn<T> | T["guard"]) => (params: any) => boolean; | ||
| }; | ||
| declare function createMachine<T extends MachineSchema>(config: Machine<T>): Machine<T>; | ||
| declare function setup<T extends MachineSchema>(): { | ||
| guards: { | ||
| and: (...guards: (T["guard"] | GuardFn<T>)[]) => (params: any) => boolean; | ||
| or: (...guards: (T["guard"] | GuardFn<T>)[]) => (params: any) => boolean; | ||
| not: (guard: T["guard"] | GuardFn<T>) => (params: any) => boolean; | ||
| }; | ||
| createMachine: (config: Machine<T>) => Machine<T>; | ||
| choose: (transitions: Transition<T> | Transition<T>[]) => ({ choose }: Params<T>) => T["action"][] | undefined; | ||
| }; | ||
| declare function createScope(props: Pick<Scope, "id" | "ids" | "getRootNode">): { | ||
| getRootNode: () => Document | ShadowRoot; | ||
| getDoc: () => Document; | ||
| getWin: () => Window & typeof globalThis; | ||
| getActiveElement: () => HTMLElement | null; | ||
| isActiveElement: typeof isActiveElement; | ||
| getById: <T extends Element = HTMLElement>(id: string) => T | null; | ||
| id?: string | undefined | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| }; | ||
| export { type ActionsOrFn, type Bindable, type BindableContext, type BindableFn, type BindableParams, type BindableRefs, type ChooseFn, type ComputedFn, type EffectsOrFn, type EventObject, type GuardFn, INIT_STATE, type Machine, type MachineSchema, MachineStatus, type Params, type PropFn, type Scope, type Service, type Transition, type ValueOrFn, createGuards, createMachine, createScope, memo, mergeProps, setup }; | ||
| export { mergeProps } from './merge-props.mjs'; | ||
| export { memo } from './memo.mjs'; | ||
| export { createGuards, createMachine, setup } from './create-machine.mjs'; | ||
| export { ensureStateIndex, findTransition, getExitEnterStates, getStateChain, getStateDefinition, hasTag, matchesState, resolveStateValue } from './state.mjs'; | ||
| export { ActionsOrFn, Bindable, BindableContext, BindableFn, BindableParams, BindableRefs, ChooseFn, ComputedFn, EffectsOrFn, EventObject, GuardFn, INIT_STATE, Machine, MachineSchema, MachineState, MachineStatus, Params, PropFn, Scope, Service, Transition, TransitionMap, TransitionMatch, TransitionSet, ValueOrFn } from './types.mjs'; | ||
| export { createScope } from './scope.mjs'; | ||
| import '@zag-js/dom-query'; |
+7
-257
@@ -1,257 +0,7 @@ | ||
| import { isActiveElement } from '@zag-js/dom-query'; | ||
| interface Props { | ||
| [key: string | symbol]: any; | ||
| } | ||
| type TupleTypes<T extends any[]> = T[number]; | ||
| type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never; | ||
| declare function mergeProps<T extends Props>(...args: Array<T | undefined>): UnionToIntersection<TupleTypes<T[]>>; | ||
| type NoInfer<T> = [T][T extends any ? 0 : never]; | ||
| declare function memo<TDeps extends any[], TDepArgs, TResult>(getDeps: (depArgs: TDepArgs) => [...TDeps], fn: (args: NoInfer<[...TDeps]>, deps: TDepArgs) => TResult, opts?: { | ||
| onChange?: ((result: TResult) => void) | undefined; | ||
| }): (depArgs: TDepArgs) => TResult; | ||
| type Dict = Record<string, any>; | ||
| interface ComputedParams<T extends Dict> { | ||
| context: BindableContext<T>; | ||
| event: EventType<T["event"]>; | ||
| prop: PropFn<T>; | ||
| refs: BindableRefs<T>; | ||
| scope: Scope; | ||
| computed: ComputedFn<T>; | ||
| } | ||
| interface ContextParams<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| bindable: BindableFn; | ||
| scope: Scope; | ||
| getContext: () => BindableContext<T>; | ||
| getComputed: () => ComputedFn<T>; | ||
| getRefs: () => BindableRefs<T>; | ||
| getEvent: () => EventType<T["event"]>; | ||
| flush: (fn: VoidFunction) => void; | ||
| } | ||
| interface PropFn<T extends Dict> { | ||
| <K extends keyof T["props"]>(key: K): T["props"][K]; | ||
| } | ||
| interface ComputedFn<T extends Dict> { | ||
| <K extends keyof T["computed"]>(key: K): T["computed"][K]; | ||
| } | ||
| type AnyFunction = () => string | number | boolean | null | undefined; | ||
| type TrackFn = (deps: AnyFunction[], fn: VoidFunction) => void; | ||
| interface BindableParams<T> { | ||
| defaultValue?: T | undefined; | ||
| value?: T | undefined; | ||
| hash?: ((a: T) => string) | undefined; | ||
| isEqual?: ((a: T, b: T | undefined) => boolean) | undefined; | ||
| onChange?: ((value: T, prev: T | undefined) => void) | undefined; | ||
| debug?: string | undefined; | ||
| sync?: boolean | undefined; | ||
| } | ||
| type ValueOrFn<T> = T | ((prev: T) => T); | ||
| interface Bindable<T> { | ||
| initial: T | undefined; | ||
| ref: any; | ||
| get: () => T; | ||
| set(value: ValueOrFn<T>): void; | ||
| invoke(nextValue: T, prevValue: T): void; | ||
| hash(value: T): string; | ||
| } | ||
| interface BindableRefs<T extends Dict> { | ||
| set<K extends keyof T["refs"]>(key: K, value: T["refs"][K]): void; | ||
| get<K extends keyof T["refs"]>(key: K): T["refs"][K]; | ||
| } | ||
| interface BindableContext<T extends Dict> { | ||
| set<K extends keyof T["context"]>(key: K, value: ValueOrFn<T["context"][K]>): void; | ||
| get<K extends keyof T["context"]>(key: K): T["context"][K]; | ||
| initial<K extends keyof T["context"]>(key: K): T["context"][K]; | ||
| hash<K extends keyof T["context"]>(key: K): string; | ||
| } | ||
| interface BindableRef<T> { | ||
| get: () => T; | ||
| set: (next: T) => void; | ||
| } | ||
| interface BindableFn { | ||
| <K>(params: () => BindableParams<K>): Bindable<K>; | ||
| cleanup: (fn: VoidFunction) => void; | ||
| ref: <T>(defaultValue: T) => BindableRef<T>; | ||
| } | ||
| interface Scope { | ||
| id?: string | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| getRootNode: () => ShadowRoot | Document | Node; | ||
| getById: <T extends Element = HTMLElement>(id: string) => T | null; | ||
| getActiveElement: () => HTMLElement | null; | ||
| isActiveElement: (elem: HTMLElement | null) => boolean; | ||
| getDoc: () => typeof document; | ||
| getWin: () => typeof window; | ||
| } | ||
| type EventType<T = any> = T & { | ||
| previousEvent?: (T & { | ||
| [key: string]: any; | ||
| }) | undefined; | ||
| src?: string | undefined; | ||
| [key: string]: any; | ||
| }; | ||
| type EventObject = EventType<{ | ||
| type: string; | ||
| }>; | ||
| interface Params<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| action: (action: T["action"][]) => void; | ||
| context: BindableContext<T>; | ||
| refs: BindableRefs<T>; | ||
| track: TrackFn; | ||
| flush: (fn: VoidFunction) => void; | ||
| event: EventType<T["event"]> & { | ||
| current: () => EventType<T["event"]>; | ||
| previous: () => EventType<T["event"]>; | ||
| }; | ||
| send: (event: EventType<T["event"]>) => void; | ||
| computed: ComputedFn<T>; | ||
| scope: Scope; | ||
| state: Bindable<T["state"]> & { | ||
| matches: (...values: T["state"][]) => boolean; | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| }; | ||
| choose: ChooseFn<T>; | ||
| guard: (key: T["guard"] | GuardFn<T>) => boolean | undefined; | ||
| } | ||
| type GuardFn<T extends Dict> = (params: Params<T>) => boolean; | ||
| interface Transition<T extends Dict> { | ||
| target?: T["state"] | undefined; | ||
| actions?: T["action"][] | undefined; | ||
| guard?: T["guard"] | GuardFn<T> | undefined; | ||
| reenter?: boolean | undefined; | ||
| } | ||
| type MaybeArray<T> = T | T[]; | ||
| type ChooseFn<T extends Dict> = (transitions: MaybeArray<Omit<Transition<T>, "target">>) => Transition<T> | undefined; | ||
| interface PropsParams<T extends Dict> { | ||
| props: Partial<T["props"]>; | ||
| scope: Scope; | ||
| } | ||
| interface RefsParams<T extends Dict> { | ||
| prop: PropFn<T>; | ||
| context: BindableContext<T>; | ||
| } | ||
| type ActionsOrFn<T extends Dict> = T["action"][] | ((params: Params<T>) => T["action"][] | undefined); | ||
| type EffectsOrFn<T extends Dict> = T["effect"][] | ((params: Params<T>) => T["effect"][] | undefined); | ||
| interface Machine<T extends Dict> { | ||
| debug?: boolean | undefined; | ||
| props?: ((params: PropsParams<T>) => T["props"]) | undefined; | ||
| context?: ((params: ContextParams<T>) => { | ||
| [K in keyof T["context"]]: Bindable<T["context"][K]>; | ||
| }) | undefined; | ||
| computed?: { | ||
| [K in keyof T["computed"]]: (params: ComputedParams<T>) => T["computed"][K]; | ||
| } | undefined; | ||
| initialState: (params: { | ||
| prop: PropFn<T>; | ||
| }) => T["state"]; | ||
| entry?: ActionsOrFn<T> | undefined; | ||
| exit?: ActionsOrFn<T> | undefined; | ||
| effects?: EffectsOrFn<T> | undefined; | ||
| refs?: ((params: RefsParams<T>) => T["refs"]) | undefined; | ||
| watch?: ((params: Params<T>) => void) | undefined; | ||
| on?: { | ||
| [E in T["event"]["type"]]?: Transition<T> | Array<Transition<T>>; | ||
| } | undefined; | ||
| states: { | ||
| [K in T["state"]]: { | ||
| tags?: T["tag"][] | undefined; | ||
| entry?: ActionsOrFn<T> | undefined; | ||
| exit?: ActionsOrFn<T> | undefined; | ||
| effects?: EffectsOrFn<T> | undefined; | ||
| on?: { | ||
| [E in T["event"]["type"]]?: Transition<T> | Array<Transition<T>>; | ||
| } | undefined; | ||
| }; | ||
| }; | ||
| implementations?: { | ||
| guards?: { | ||
| [K in T["guard"]]: (params: Params<T>) => boolean; | ||
| } | undefined; | ||
| actions?: { | ||
| [K in T["action"]]: (params: Params<T>) => void; | ||
| } | undefined; | ||
| effects?: { | ||
| [K in T["effect"]]: (params: Params<T>) => void | VoidFunction; | ||
| } | undefined; | ||
| } | undefined; | ||
| } | ||
| interface MachineBaseProps { | ||
| id?: string | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| getRootNode?: (() => ShadowRoot | Document | Node) | undefined; | ||
| [key: string]: any; | ||
| } | ||
| interface MachineSchema { | ||
| props?: MachineBaseProps | undefined; | ||
| context?: Record<string, any> | undefined; | ||
| refs?: Record<string, any> | undefined; | ||
| computed?: Record<string, any> | undefined; | ||
| state?: string | undefined; | ||
| tag?: string | undefined; | ||
| guard?: string | undefined; | ||
| action?: string | undefined; | ||
| effect?: string | undefined; | ||
| event?: ({ | ||
| type: string; | ||
| } & Dict) | undefined; | ||
| } | ||
| type State<T extends MachineSchema> = Bindable<T["state"]> & { | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| matches: (...values: T["state"][]) => boolean; | ||
| }; | ||
| type Service<T extends MachineSchema> = { | ||
| getStatus: () => MachineStatus; | ||
| state: State<T> & { | ||
| matches: (...values: T["state"][]) => boolean; | ||
| hasTag: (tag: T["tag"]) => boolean; | ||
| }; | ||
| context: BindableContext<T>; | ||
| send: (event: EventType<T["event"]>) => void; | ||
| prop: PropFn<T>; | ||
| scope: Scope; | ||
| computed: ComputedFn<T>; | ||
| refs: BindableRefs<T>; | ||
| event: EventType<T["event"]> & { | ||
| current: () => EventType<T["event"]>; | ||
| previous: () => EventType<T["event"]>; | ||
| }; | ||
| }; | ||
| declare enum MachineStatus { | ||
| NotStarted = "Not Started", | ||
| Started = "Started", | ||
| Stopped = "Stopped" | ||
| } | ||
| declare const INIT_STATE = "__init__"; | ||
| declare function createGuards<T extends MachineSchema>(): { | ||
| and: (...guards: Array<GuardFn<T> | T["guard"]>) => (params: any) => boolean; | ||
| or: (...guards: Array<GuardFn<T> | T["guard"]>) => (params: any) => boolean; | ||
| not: (guard: GuardFn<T> | T["guard"]) => (params: any) => boolean; | ||
| }; | ||
| declare function createMachine<T extends MachineSchema>(config: Machine<T>): Machine<T>; | ||
| declare function setup<T extends MachineSchema>(): { | ||
| guards: { | ||
| and: (...guards: (T["guard"] | GuardFn<T>)[]) => (params: any) => boolean; | ||
| or: (...guards: (T["guard"] | GuardFn<T>)[]) => (params: any) => boolean; | ||
| not: (guard: T["guard"] | GuardFn<T>) => (params: any) => boolean; | ||
| }; | ||
| createMachine: (config: Machine<T>) => Machine<T>; | ||
| choose: (transitions: Transition<T> | Transition<T>[]) => ({ choose }: Params<T>) => T["action"][] | undefined; | ||
| }; | ||
| declare function createScope(props: Pick<Scope, "id" | "ids" | "getRootNode">): { | ||
| getRootNode: () => Document | ShadowRoot; | ||
| getDoc: () => Document; | ||
| getWin: () => Window & typeof globalThis; | ||
| getActiveElement: () => HTMLElement | null; | ||
| isActiveElement: typeof isActiveElement; | ||
| getById: <T extends Element = HTMLElement>(id: string) => T | null; | ||
| id?: string | undefined | undefined; | ||
| ids?: Record<string, any> | undefined; | ||
| }; | ||
| export { type ActionsOrFn, type Bindable, type BindableContext, type BindableFn, type BindableParams, type BindableRefs, type ChooseFn, type ComputedFn, type EffectsOrFn, type EventObject, type GuardFn, INIT_STATE, type Machine, type MachineSchema, MachineStatus, type Params, type PropFn, type Scope, type Service, type Transition, type ValueOrFn, createGuards, createMachine, createScope, memo, mergeProps, setup }; | ||
| export { mergeProps } from './merge-props.js'; | ||
| export { memo } from './memo.js'; | ||
| export { createGuards, createMachine, setup } from './create-machine.js'; | ||
| export { ensureStateIndex, findTransition, getExitEnterStates, getStateChain, getStateDefinition, hasTag, matchesState, resolveStateValue } from './state.js'; | ||
| export { ActionsOrFn, Bindable, BindableContext, BindableFn, BindableParams, BindableRefs, ChooseFn, ComputedFn, EffectsOrFn, EventObject, GuardFn, INIT_STATE, Machine, MachineSchema, MachineState, MachineStatus, Params, PropFn, Scope, Service, Transition, TransitionMap, TransitionMatch, TransitionSet, ValueOrFn } from './types.js'; | ||
| export { createScope } from './scope.js'; | ||
| import '@zag-js/dom-query'; |
+31
-137
@@ -1,140 +0,34 @@ | ||
| 'use strict'; | ||
| var utils = require('@zag-js/utils'); | ||
| var domQuery = require('@zag-js/dom-query'); | ||
| // src/merge-props.ts | ||
| var clsx = (...args) => args.map((str) => str?.trim?.()).filter(Boolean).join(" "); | ||
| var CSS_REGEX = /((?:--)?(?:\w+-?)+)\s*:\s*([^;]*)/g; | ||
| var serialize = (style) => { | ||
| const res = {}; | ||
| let match; | ||
| while (match = CSS_REGEX.exec(style)) { | ||
| res[match[1]] = match[2]; | ||
| "use strict"; | ||
| var __defProp = Object.defineProperty; | ||
| var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
| var __getOwnPropNames = Object.getOwnPropertyNames; | ||
| var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
| var __copyProps = (to, from, except, desc) => { | ||
| if (from && typeof from === "object" || typeof from === "function") { | ||
| for (let key of __getOwnPropNames(from)) | ||
| if (!__hasOwnProp.call(to, key) && key !== except) | ||
| __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); | ||
| } | ||
| return res; | ||
| return to; | ||
| }; | ||
| var css = (a, b) => { | ||
| if (utils.isString(a)) { | ||
| if (utils.isString(b)) return `${a};${b}`; | ||
| a = serialize(a); | ||
| } else if (utils.isString(b)) { | ||
| b = serialize(b); | ||
| } | ||
| return Object.assign({}, a ?? {}, b ?? {}); | ||
| }; | ||
| function mergeProps(...args) { | ||
| let result = {}; | ||
| for (let props of args) { | ||
| if (!props) continue; | ||
| for (let key in result) { | ||
| if (key.startsWith("on") && typeof result[key] === "function" && typeof props[key] === "function") { | ||
| result[key] = utils.callAll(props[key], result[key]); | ||
| continue; | ||
| } | ||
| if (key === "className" || key === "class") { | ||
| result[key] = clsx(result[key], props[key]); | ||
| continue; | ||
| } | ||
| if (key === "style") { | ||
| result[key] = css(result[key], props[key]); | ||
| continue; | ||
| } | ||
| result[key] = props[key] !== void 0 ? props[key] : result[key]; | ||
| } | ||
| for (let key in props) { | ||
| if (result[key] === void 0) { | ||
| result[key] = props[key]; | ||
| } | ||
| } | ||
| const symbols = Object.getOwnPropertySymbols(props); | ||
| for (let symbol of symbols) { | ||
| result[symbol] = props[symbol]; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| function memo(getDeps, fn, opts) { | ||
| let deps = []; | ||
| let result; | ||
| return (depArgs) => { | ||
| const newDeps = getDeps(depArgs); | ||
| const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => !utils.isEqual(deps[index], dep)); | ||
| if (!depsChanged) return result; | ||
| deps = newDeps; | ||
| result = fn(newDeps, depArgs); | ||
| opts?.onChange?.(result); | ||
| return result; | ||
| }; | ||
| } | ||
| var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default")); | ||
| var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); | ||
| // src/create-machine.ts | ||
| function createGuards() { | ||
| return { | ||
| and: (...guards) => { | ||
| return function andGuard(params) { | ||
| return guards.every((str) => params.guard(str)); | ||
| }; | ||
| }, | ||
| or: (...guards) => { | ||
| return function orGuard(params) { | ||
| return guards.some((str) => params.guard(str)); | ||
| }; | ||
| }, | ||
| not: (guard) => { | ||
| return function notGuard(params) { | ||
| return !params.guard(guard); | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
| function createMachine(config) { | ||
| return config; | ||
| } | ||
| function setup() { | ||
| return { | ||
| guards: createGuards(), | ||
| createMachine: (config) => { | ||
| return createMachine(config); | ||
| }, | ||
| choose: (transitions) => { | ||
| return function chooseFn({ choose }) { | ||
| return choose(transitions)?.actions; | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
| // src/types.ts | ||
| var MachineStatus = /* @__PURE__ */ ((MachineStatus2) => { | ||
| MachineStatus2["NotStarted"] = "Not Started"; | ||
| MachineStatus2["Started"] = "Started"; | ||
| MachineStatus2["Stopped"] = "Stopped"; | ||
| return MachineStatus2; | ||
| })(MachineStatus || {}); | ||
| var INIT_STATE = "__init__"; | ||
| function createScope(props) { | ||
| const getRootNode = () => props.getRootNode?.() ?? document; | ||
| const getDoc = () => domQuery.getDocument(getRootNode()); | ||
| const getWin = () => getDoc().defaultView ?? window; | ||
| const getActiveElementFn = () => domQuery.getActiveElement(getRootNode()); | ||
| const getById = (id) => getRootNode().getElementById(id); | ||
| return { | ||
| ...props, | ||
| getRootNode, | ||
| getDoc, | ||
| getWin, | ||
| getActiveElement: getActiveElementFn, | ||
| isActiveElement: domQuery.isActiveElement, | ||
| getById | ||
| }; | ||
| } | ||
| exports.INIT_STATE = INIT_STATE; | ||
| exports.MachineStatus = MachineStatus; | ||
| exports.createGuards = createGuards; | ||
| exports.createMachine = createMachine; | ||
| exports.createScope = createScope; | ||
| exports.memo = memo; | ||
| exports.mergeProps = mergeProps; | ||
| exports.setup = setup; | ||
| // src/index.ts | ||
| var index_exports = {}; | ||
| module.exports = __toCommonJS(index_exports); | ||
| __reExport(index_exports, require("./merge-props.cjs"), module.exports); | ||
| __reExport(index_exports, require("./memo.cjs"), module.exports); | ||
| __reExport(index_exports, require("./create-machine.cjs"), module.exports); | ||
| __reExport(index_exports, require("./state.cjs"), module.exports); | ||
| __reExport(index_exports, require("./types.cjs"), module.exports); | ||
| __reExport(index_exports, require("./scope.cjs"), module.exports); | ||
| // Annotate the CommonJS export names for ESM import in node: | ||
| 0 && (module.exports = { | ||
| ...require("./merge-props.cjs"), | ||
| ...require("./memo.cjs"), | ||
| ...require("./create-machine.cjs"), | ||
| ...require("./state.cjs"), | ||
| ...require("./types.cjs"), | ||
| ...require("./scope.cjs") | ||
| }); |
+7
-131
@@ -1,131 +0,7 @@ | ||
| import { callAll, isEqual, isString } from '@zag-js/utils'; | ||
| import { isActiveElement, getDocument, getActiveElement } from '@zag-js/dom-query'; | ||
| // src/merge-props.ts | ||
| var clsx = (...args) => args.map((str) => str?.trim?.()).filter(Boolean).join(" "); | ||
| var CSS_REGEX = /((?:--)?(?:\w+-?)+)\s*:\s*([^;]*)/g; | ||
| var serialize = (style) => { | ||
| const res = {}; | ||
| let match; | ||
| while (match = CSS_REGEX.exec(style)) { | ||
| res[match[1]] = match[2]; | ||
| } | ||
| return res; | ||
| }; | ||
| var css = (a, b) => { | ||
| if (isString(a)) { | ||
| if (isString(b)) return `${a};${b}`; | ||
| a = serialize(a); | ||
| } else if (isString(b)) { | ||
| b = serialize(b); | ||
| } | ||
| return Object.assign({}, a ?? {}, b ?? {}); | ||
| }; | ||
| function mergeProps(...args) { | ||
| let result = {}; | ||
| for (let props of args) { | ||
| if (!props) continue; | ||
| for (let key in result) { | ||
| if (key.startsWith("on") && typeof result[key] === "function" && typeof props[key] === "function") { | ||
| result[key] = callAll(props[key], result[key]); | ||
| continue; | ||
| } | ||
| if (key === "className" || key === "class") { | ||
| result[key] = clsx(result[key], props[key]); | ||
| continue; | ||
| } | ||
| if (key === "style") { | ||
| result[key] = css(result[key], props[key]); | ||
| continue; | ||
| } | ||
| result[key] = props[key] !== void 0 ? props[key] : result[key]; | ||
| } | ||
| for (let key in props) { | ||
| if (result[key] === void 0) { | ||
| result[key] = props[key]; | ||
| } | ||
| } | ||
| const symbols = Object.getOwnPropertySymbols(props); | ||
| for (let symbol of symbols) { | ||
| result[symbol] = props[symbol]; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| function memo(getDeps, fn, opts) { | ||
| let deps = []; | ||
| let result; | ||
| return (depArgs) => { | ||
| const newDeps = getDeps(depArgs); | ||
| const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => !isEqual(deps[index], dep)); | ||
| if (!depsChanged) return result; | ||
| deps = newDeps; | ||
| result = fn(newDeps, depArgs); | ||
| opts?.onChange?.(result); | ||
| return result; | ||
| }; | ||
| } | ||
| // src/create-machine.ts | ||
| function createGuards() { | ||
| return { | ||
| and: (...guards) => { | ||
| return function andGuard(params) { | ||
| return guards.every((str) => params.guard(str)); | ||
| }; | ||
| }, | ||
| or: (...guards) => { | ||
| return function orGuard(params) { | ||
| return guards.some((str) => params.guard(str)); | ||
| }; | ||
| }, | ||
| not: (guard) => { | ||
| return function notGuard(params) { | ||
| return !params.guard(guard); | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
| function createMachine(config) { | ||
| return config; | ||
| } | ||
| function setup() { | ||
| return { | ||
| guards: createGuards(), | ||
| createMachine: (config) => { | ||
| return createMachine(config); | ||
| }, | ||
| choose: (transitions) => { | ||
| return function chooseFn({ choose }) { | ||
| return choose(transitions)?.actions; | ||
| }; | ||
| } | ||
| }; | ||
| } | ||
| // src/types.ts | ||
| var MachineStatus = /* @__PURE__ */ ((MachineStatus2) => { | ||
| MachineStatus2["NotStarted"] = "Not Started"; | ||
| MachineStatus2["Started"] = "Started"; | ||
| MachineStatus2["Stopped"] = "Stopped"; | ||
| return MachineStatus2; | ||
| })(MachineStatus || {}); | ||
| var INIT_STATE = "__init__"; | ||
| function createScope(props) { | ||
| const getRootNode = () => props.getRootNode?.() ?? document; | ||
| const getDoc = () => getDocument(getRootNode()); | ||
| const getWin = () => getDoc().defaultView ?? window; | ||
| const getActiveElementFn = () => getActiveElement(getRootNode()); | ||
| const getById = (id) => getRootNode().getElementById(id); | ||
| return { | ||
| ...props, | ||
| getRootNode, | ||
| getDoc, | ||
| getWin, | ||
| getActiveElement: getActiveElementFn, | ||
| isActiveElement, | ||
| getById | ||
| }; | ||
| } | ||
| export { INIT_STATE, MachineStatus, createGuards, createMachine, createScope, memo, mergeProps, setup }; | ||
| // src/index.ts | ||
| export * from "./merge-props.mjs"; | ||
| export * from "./memo.mjs"; | ||
| export * from "./create-machine.mjs"; | ||
| export * from "./state.mjs"; | ||
| export * from "./types.mjs"; | ||
| export * from "./scope.mjs"; |
+3
-3
| { | ||
| "name": "@zag-js/core", | ||
| "version": "1.34.1", | ||
| "version": "1.35.0", | ||
| "description": "A minimal implementation of xstate fsm for UI machines", | ||
@@ -28,4 +28,4 @@ "keywords": [ | ||
| "dependencies": { | ||
| "@zag-js/utils": "1.34.1", | ||
| "@zag-js/dom-query": "1.34.1" | ||
| "@zag-js/utils": "1.35.0", | ||
| "@zag-js/dom-query": "1.35.0" | ||
| }, | ||
@@ -32,0 +32,0 @@ "devDependencies": { |
+58
-49
@@ -8,3 +8,3 @@ # @zag-js/core | ||
| - Finite states (non-nested) | ||
| - Finite states with optional nested (hierarchical) states | ||
| - Initial state | ||
@@ -15,9 +15,9 @@ - Transitions (object or strings) | ||
| - Exit actions | ||
| - Delayed timeout actions (basically `setTimeout`) | ||
| - Delayed interval actions (basically `setInterval`) | ||
| - Transition actions | ||
| - Effects | ||
| - Boolean guard helpers (`and`, `or`, `not`) | ||
| - Basic spawn helpers | ||
| - Activities (for state nodes) | ||
| > Note: The core package is intentionally minimal. It doesn't include delayed transitions, interval scheduling, spawn | ||
| > helpers, or activities. Use actions/effects to perform side effects. | ||
| > To better understand the state machines, we strongly recommend going though the | ||
@@ -51,28 +51,56 @@ > [xstate docs](https://xstate.js.org/docs/) and videos. It'll give you the foundations you need. | ||
| }) | ||
| ``` | ||
| toggleMachine.start() | ||
| console.log(toggleMachine.state.value) // => "inactive" | ||
| **Usage (service via adapter):** | ||
| toggleMachine.send("TOGGLE") | ||
| console.log(toggleMachine.state.value) // => "active" | ||
| ```js | ||
| import { createMachine } from "@zag-js/core" | ||
| import { VanillaMachine } from "@zag-js/vanilla" | ||
| toggleMachine.send("TOGGLE") | ||
| console.log(toggleMachine.state.value) // => "inactive" | ||
| const machine = createMachine({ | ||
| initialState() { | ||
| return "inactive" | ||
| }, | ||
| states: { | ||
| inactive: { on: { TOGGLE: "active" } }, | ||
| active: { on: { TOGGLE: "inactive" } }, | ||
| }, | ||
| }) | ||
| const service = new VanillaMachine(machine) | ||
| service.start() | ||
| service.subscribe(() => { | ||
| console.log(service.state.get()) | ||
| }) | ||
| service.send({ type: "TOGGLE" }) | ||
| service.send({ type: "TOGGLE" }) | ||
| service.stop() | ||
| ``` | ||
| **Usage (service):** | ||
| ### Nested states (dot notation) | ||
| ```js | ||
| ```ts | ||
| import { createMachine } from "@zag-js/core" | ||
| const toggleMachine = createMachine({...}) | ||
| toggleMachine.start() | ||
| toggleService.subscribe((state) => { | ||
| console.log(state.value) | ||
| const machine = createMachine({ | ||
| initialState() { | ||
| return "dialog" | ||
| }, | ||
| states: { | ||
| dialog: { | ||
| tags: ["overlay"], | ||
| initial: "closed", | ||
| states: { | ||
| closed: { on: { OPEN: { target: "dialog.open" } } }, | ||
| open: { on: { CLOSE: { target: "dialog.closed" } } }, | ||
| }, | ||
| // parent-level transitions still work | ||
| on: { RESET: { target: "dialog.closed" } }, | ||
| }, | ||
| }, | ||
| }) | ||
| toggleService.send("TOGGLE") | ||
| toggleService.send("TOGGLE") | ||
| toggleService.stop() | ||
| // service.state.matches("dialog.open") === true when nested state is active | ||
| ``` | ||
@@ -93,24 +121,5 @@ | ||
| A `Machine`, which provides: | ||
| A machine definition object consumed by framework adapters (React, Solid, Svelte, Vue, Preact, Vanilla) to create a | ||
| runtime service. | ||
| - `machine.initialState`: the machine's resolved initial state | ||
| - `machine.start()`: the function to start the machine in the specified initial state. | ||
| - `machine.stop()`: the function to stop the machine completely. It also cleans up all scheduled actions and timeouts. | ||
| - `machine.transition(state, event)`: a transition function that returns the next state given the current `state` and | ||
| `event`. It also performs any delayed, entry or exit side-effects. | ||
| - `machine.send(event)`: a transition function instructs the machine to execute a transition based on the event. | ||
| - `machine.onTransition(fn)`: a function that gets called when the machine transition function instructs the machine to | ||
| execute a transition based on the event. | ||
| - `machine.onChange(fn)`: a function that gets called when the machine's context value changes. | ||
| - `machine.state`: the state object that describes the machine at a specific point in time. It contains the following | ||
| properties: | ||
| - `value`: the current state value | ||
| - `previousValue`: the previous state value | ||
| - `event`: the event that triggered the transition to the current state | ||
| - `nextEvents`: the list of events the machine can respond to at its current state | ||
| - `tags`: the tags associated with this state | ||
| - `done`: whether the machine that reached its final state | ||
| - `context`: the current context value | ||
| - `matches(...)`: a function used to test whether the current state matches one or more state values | ||
| The machine config has this schema: | ||
@@ -120,9 +129,9 @@ | ||
| - `id` (string) - an identifier for the type of machine this is. Useful for debugging. | ||
| - `context` (object) - the extended state data that represents quantitative data (string, number, objects, etc) that can | ||
| be modified in the machine. | ||
| - `initial` (string) - the key of the initial state. | ||
| - `states` (object) - an object mapping state names (keys) to states | ||
| - `on` (object) - an global mapping of event types to transitions. If specified, this event will called if the state | ||
| node doesn't handle the emitted event. | ||
| - `initialState` (function) - returns the machine's initial state value. | ||
| - `states` (object) - mapping of state names to state configs. Supports nested `states` and `initial` for child states. | ||
| - `on` (object) - optional global transitions available from any state. | ||
| - `context` (function) - returns bindable context fields. | ||
| - `computed` (object) - derived values based on context, props, refs. | ||
| - `entry` / `exit` / `effects` - root-level actions/effects. | ||
| - `implementations` (object) - lookup tables for `actions`, `guards`, `effects`. | ||
@@ -129,0 +138,0 @@ ### State config |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
59724
72.25%31
342.86%1077
109.53%163
5.84%1
Infinity%+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
Updated
Updated