@zag-js/solid
Version:
The solid.js wrapper for zag
84 lines (82 loc) • 2.66 kB
JavaScript
;
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/bindable.ts
var bindable_exports = {};
__export(bindable_exports, {
createBindable: () => createBindable
});
module.exports = __toCommonJS(bindable_exports);
var import_utils = require("@zag-js/utils");
var import_solid_js = require("solid-js");
function createBindable(props) {
const initial = props().value ?? props().defaultValue;
const eq = props().isEqual ?? Object.is;
const [value, setValue] = (0, import_solid_js.createSignal)(initial);
const controlled = (0, import_solid_js.createMemo)(() => props().value != void 0);
const valueRef = { current: value() };
const prevValue = { current: void 0 };
(0, import_solid_js.createEffect)(() => {
const v = controlled() ? props().value : value();
prevValue.current = v;
valueRef.current = v;
});
const set = (v) => {
const prev = prevValue.current;
const next = (0, import_utils.isFunction)(v) ? v(valueRef.current) : v;
if (props().debug) {
console.log(`[bindable > ${props().debug}] setValue`, { next, prev });
}
if (!controlled()) setValue(next);
if (!eq(next, prev)) {
props().onChange?.(next, prev);
}
};
function get() {
const v = controlled() ? props().value : value;
return (0, import_utils.isFunction)(v) ? v() : v;
}
return {
initial,
ref: valueRef,
get,
set,
invoke(nextValue, prevValue2) {
props().onChange?.(nextValue, prevValue2);
},
hash(value2) {
return props().hash?.(value2) ?? String(value2);
}
};
}
createBindable.cleanup = (fn) => {
(0, import_solid_js.onCleanup)(() => fn());
};
createBindable.ref = (defaultValue) => {
let value = defaultValue;
return {
get: () => value,
set: (next) => {
value = next;
}
};
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createBindable
});