UNPKG

solid-floating-ui

Version:
118 lines (116 loc) 3.4 kB
"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/index.ts var src_exports = {}; __export(src_exports, { useFloating: () => useFloating }); module.exports = __toCommonJS(src_exports); var import_solid_js = require("solid-js"); var import_dom = require("@floating-ui/dom"); function useFloating(reference, floating, options) { const placement = () => { var _a; return (_a = options == null ? void 0 : options.placement) != null ? _a : "bottom"; }; const strategy = () => { var _a; return (_a = options == null ? void 0 : options.strategy) != null ? _a : "absolute"; }; const [data, setData] = (0, import_solid_js.createSignal)({ x: null, y: null, placement: placement(), strategy: strategy(), middlewareData: {} }); const [error, setError] = (0, import_solid_js.createSignal)(); (0, import_solid_js.createEffect)(() => { const currentError = error(); if (currentError) { throw currentError.value; } }); const version = (0, import_solid_js.createMemo)(() => { reference(); floating(); return {}; }); function update() { const currentReference = reference(); const currentFloating = floating(); if (currentReference && currentFloating) { const capturedVersion = version(); (0, import_dom.computePosition)(currentReference, currentFloating, { middleware: options == null ? void 0 : options.middleware, placement: placement(), strategy: strategy() }).then( (currentData) => { if (capturedVersion === version()) { setData(currentData); } }, (err) => { setError(err); } ); } } (0, import_solid_js.createEffect)(() => { const currentReference = reference(); const currentFloating = floating(); options == null ? void 0 : options.middleware; placement(); strategy(); if (currentReference && currentFloating) { if (options == null ? void 0 : options.whileElementsMounted) { const cleanup = options.whileElementsMounted( currentReference, currentFloating, update ); if (cleanup) { (0, import_solid_js.onCleanup)(cleanup); } } else { update(); } } }); return { get x() { return data().x; }, get y() { return data().y; }, get placement() { return data().placement; }, get strategy() { return data().strategy; }, get middlewareData() { return data().middlewareData; }, update }; } //# sourceMappingURL=index.cjs.map