UNPKG

@mantine/hooks

Version:

A collection of 50+ hooks for state and UI management

70 lines (69 loc) 2.04 kB
"use client"; import { useForceUpdate } from "../use-force-update/use-force-update.mjs"; import { useRef } from "react"; //#region packages/@mantine/hooks/src/use-set/use-set.ts function readonlySetLikeToSet(input) { if (input instanceof Set) return input; const result = /* @__PURE__ */ new Set(); const iterator = input.keys(); let next = iterator.next(); while (!next.done) { result.add(next.value); next = iterator.next(); } return result; } function useSet(values) { const setRef = useRef(new Set(values)); const forceUpdate = useForceUpdate(); setRef.current.add = (...args) => { const res = Set.prototype.add.apply(setRef.current, args); forceUpdate(); return res; }; setRef.current.clear = (...args) => { Set.prototype.clear.apply(setRef.current, args); forceUpdate(); }; setRef.current.delete = (...args) => { const res = Set.prototype.delete.apply(setRef.current, args); forceUpdate(); return res; }; setRef.current.union = (other) => { const result = new Set(setRef.current); readonlySetLikeToSet(other).forEach((item) => result.add(item)); return result; }; setRef.current.intersection = (other) => { const result = /* @__PURE__ */ new Set(); const otherSet = readonlySetLikeToSet(other); setRef.current.forEach((item) => { if (otherSet.has(item)) result.add(item); }); return result; }; setRef.current.difference = (other) => { const result = /* @__PURE__ */ new Set(); const otherSet = readonlySetLikeToSet(other); setRef.current.forEach((item) => { if (!otherSet.has(item)) result.add(item); }); return result; }; setRef.current.symmetricDifference = (other) => { const result = /* @__PURE__ */ new Set(); const otherSet = readonlySetLikeToSet(other); setRef.current.forEach((item) => { if (!otherSet.has(item)) result.add(item); }); otherSet.forEach((item) => { if (!setRef.current.has(item)) result.add(item); }); return result; }; return setRef.current; } //#endregion export { useSet }; //# sourceMappingURL=use-set.mjs.map