@zag-js/checkbox
Advanced tools
Comparing version 0.1.1 to 0.1.2
import type { MachineContext as Ctx } from "./checkbox.types"; | ||
export declare const dom: { | ||
getDoc: (ctx: Ctx) => Document; | ||
getRootNode: (ctx: Ctx) => Document | ShadowRoot; | ||
getRootNode: (ctx: { | ||
getRootNode?: () => Node | Document | ShadowRoot; | ||
}) => Document | ShadowRoot; | ||
getDoc: (ctx: { | ||
getRootNode?: () => Node | Document | ShadowRoot; | ||
}) => Document; | ||
getWin: (ctx: { | ||
getRootNode?: () => Node | Document | ShadowRoot; | ||
}) => Window & typeof globalThis; | ||
getActiveElement: (ctx: { | ||
getRootNode?: () => Node | Document | ShadowRoot; | ||
}) => HTMLElement; | ||
getById: <T_1 = HTMLElement>(ctx: { | ||
getRootNode?: () => Node | Document | ShadowRoot; | ||
}, id: string) => T_1; | ||
} & { | ||
getRootId: (ctx: Ctx) => string; | ||
@@ -6,0 +20,0 @@ getLabelId: (ctx: Ctx) => string; |
import type { MachineContext, MachineState, UserDefinedContext } from "./checkbox.types"; | ||
export declare function machine(ctx?: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>; | ||
export declare function machine(ctx: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>; |
import type { StateMachine as S } from "@zag-js/core"; | ||
import type { Context, DirectionProperty } from "@zag-js/types"; | ||
import type { CommonProperties, Context, DirectionProperty, RequiredBy } from "@zag-js/types"; | ||
declare type ElementIds = Partial<{ | ||
@@ -9,3 +9,3 @@ root: string; | ||
}>; | ||
declare type PublicContext = DirectionProperty & { | ||
declare type PublicContext = DirectionProperty & CommonProperties & { | ||
/** | ||
@@ -70,3 +70,3 @@ * The ids of the elements in the checkbox. Useful for composition. | ||
}; | ||
export declare type UserDefinedContext = Partial<PublicContext>; | ||
export declare type UserDefinedContext = RequiredBy<PublicContext, "id">; | ||
declare type ComputedContext = Readonly<{ | ||
@@ -73,0 +73,0 @@ /** |
@@ -43,5 +43,35 @@ "use strict"; | ||
// ../../utilities/dom/dist/index.mjs | ||
var __defProp2 = Object.defineProperty; | ||
var __getOwnPropSymbols2 = Object.getOwnPropertySymbols; | ||
var __hasOwnProp2 = Object.prototype.hasOwnProperty; | ||
var __propIsEnum2 = Object.prototype.propertyIsEnumerable; | ||
var __defNormalProp2 = (obj, key, value) => key in obj ? __defProp2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __spreadValues2 = (a, b) => { | ||
for (var prop in b || (b = {})) | ||
if (__hasOwnProp2.call(b, prop)) | ||
__defNormalProp2(a, prop, b[prop]); | ||
if (__getOwnPropSymbols2) | ||
for (var prop of __getOwnPropSymbols2(b)) { | ||
if (__propIsEnum2.call(b, prop)) | ||
__defNormalProp2(a, prop, b[prop]); | ||
} | ||
return a; | ||
}; | ||
var dataAttr = (guard) => { | ||
return guard ? "" : void 0; | ||
}; | ||
function isDocument(el) { | ||
return el.nodeType === Node.DOCUMENT_NODE; | ||
} | ||
function isWindow(value) { | ||
return (value == null ? void 0 : value.toString()) === "[object Window]"; | ||
} | ||
function getDocument(el) { | ||
var _a; | ||
if (isWindow(el)) | ||
return el.document; | ||
if (isDocument(el)) | ||
return el; | ||
return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document; | ||
} | ||
function getWindow(el) { | ||
@@ -51,2 +81,18 @@ var _a; | ||
} | ||
function defineDomHelpers(helpers) { | ||
const dom2 = { | ||
getRootNode: (ctx) => { | ||
var _a, _b; | ||
return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document; | ||
}, | ||
getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)), | ||
getWin: (ctx) => { | ||
var _a; | ||
return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window; | ||
}, | ||
getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement, | ||
getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id) | ||
}; | ||
return __spreadValues2(__spreadValues2({}, dom2), helpers); | ||
} | ||
function observeAttributes(node, attributes, fn) { | ||
@@ -100,2 +146,4 @@ if (!node) | ||
var _a; | ||
if (!el) | ||
return; | ||
const win = getWindow(el); | ||
@@ -121,45 +169,24 @@ if (!(el instanceof win.HTMLInputElement)) | ||
}; | ||
function nextTick(fn) { | ||
const set = /* @__PURE__ */ new Set(); | ||
function raf2(fn2) { | ||
const id = globalThis.requestAnimationFrame(fn2); | ||
set.add(() => globalThis.cancelAnimationFrame(id)); | ||
} | ||
raf2(() => raf2(fn)); | ||
return function cleanup() { | ||
set.forEach(function(fn2) { | ||
fn2(); | ||
}); | ||
}; | ||
} | ||
// src/checkbox.dom.ts | ||
var dom = { | ||
getDoc: (ctx) => { | ||
var _a; | ||
return (_a = ctx.doc) != null ? _a : document; | ||
}, | ||
getRootNode: (ctx) => { | ||
var _a; | ||
return (_a = ctx.rootNode) != null ? _a : dom.getDoc(ctx); | ||
}, | ||
var dom = defineDomHelpers({ | ||
getRootId: (ctx) => { | ||
var _a, _b; | ||
return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `checkbox:${ctx.uid}`; | ||
return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `checkbox:${ctx.id}`; | ||
}, | ||
getLabelId: (ctx) => { | ||
var _a, _b; | ||
return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `checkbox:${ctx.uid}:label`; | ||
return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `checkbox:${ctx.id}:label`; | ||
}, | ||
getControlId: (ctx) => { | ||
var _a, _b; | ||
return (_b = (_a = ctx.ids) == null ? void 0 : _a.control) != null ? _b : `checkbox:${ctx.uid}:control`; | ||
return (_b = (_a = ctx.ids) == null ? void 0 : _a.control) != null ? _b : `checkbox:${ctx.id}:control`; | ||
}, | ||
getInputId: (ctx) => { | ||
var _a, _b; | ||
return (_b = (_a = ctx.ids) == null ? void 0 : _a.input) != null ? _b : `checkbox:${ctx.uid}:input`; | ||
return (_b = (_a = ctx.ids) == null ? void 0 : _a.input) != null ? _b : `checkbox:${ctx.id}:input`; | ||
}, | ||
getRootEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getRootId(ctx)), | ||
getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)) | ||
}; | ||
getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)), | ||
getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)) | ||
}); | ||
@@ -308,3 +335,3 @@ // src/checkbox.connect.ts | ||
var { and } = import_core.guards; | ||
function machine(ctx = {}) { | ||
function machine(ctx) { | ||
return (0, import_core.createMachine)({ | ||
@@ -314,3 +341,2 @@ id: "checkbox", | ||
context: __spreadValues({ | ||
uid: "", | ||
active: false, | ||
@@ -360,6 +386,3 @@ hovered: false, | ||
on: { | ||
SETUP: { | ||
target: ctx.defaultChecked ? "checked" : "unchecked", | ||
actions: ["setupDocument"] | ||
} | ||
SETUP: ctx.defaultChecked ? "checked" : "unchecked" | ||
} | ||
@@ -395,32 +418,15 @@ }, | ||
trackFieldsetDisabled(ctx2) { | ||
let cleanup; | ||
nextTick(() => { | ||
cleanup = trackFieldsetDisabled(dom.getRootEl(ctx2), (disabled) => { | ||
if (disabled) { | ||
ctx2.disabled = disabled; | ||
} | ||
}); | ||
return trackFieldsetDisabled(dom.getRootEl(ctx2), (disabled) => { | ||
if (disabled) { | ||
ctx2.disabled = disabled; | ||
} | ||
}); | ||
return () => cleanup == null ? void 0 : cleanup(); | ||
}, | ||
trackFormReset(ctx2, _evt, { send }) { | ||
let cleanup; | ||
nextTick(() => { | ||
cleanup = trackFormReset(dom.getInputEl(ctx2), () => { | ||
if (ctx2.defaultChecked != null) { | ||
send({ type: "SET_STATE", checked: ctx2.defaultChecked }); | ||
} | ||
}); | ||
return trackFormReset(dom.getInputEl(ctx2), () => { | ||
send({ type: "SET_STATE", checked: !!ctx2.defaultChecked }); | ||
}); | ||
return () => cleanup == null ? void 0 : cleanup(); | ||
} | ||
}, | ||
actions: { | ||
setupDocument(ctx2, evt) { | ||
ctx2.uid = evt.id; | ||
if (evt.doc) | ||
ctx2.doc = (0, import_core.ref)(evt.doc); | ||
if (evt.root) | ||
ctx2.rootNode = (0, import_core.ref)(evt.root); | ||
}, | ||
invokeOnChange(ctx2, _evt, { state }) { | ||
@@ -427,0 +433,0 @@ var _a; |
{ | ||
"name": "@zag-js/checkbox", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Core logic for the checkbox widget implemented as a state machine", | ||
@@ -41,9 +41,9 @@ "keywords": [ | ||
"dependencies": { | ||
"@zag-js/core": "0.1.7", | ||
"@zag-js/types": "0.2.1" | ||
"@zag-js/core": "0.1.8", | ||
"@zag-js/types": "0.2.2" | ||
}, | ||
"devDependencies": { | ||
"@zag-js/dom-utils": "0.1.6", | ||
"@zag-js/dom-utils": "0.1.7", | ||
"@zag-js/utils": "0.1.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35275
1042
+ Added@zag-js/core@0.1.8(transitive)
+ Added@zag-js/types@0.2.2(transitive)
- Removed@zag-js/core@0.1.7(transitive)
- Removed@zag-js/types@0.2.1(transitive)
Updated@zag-js/core@0.1.8
Updated@zag-js/types@0.2.2