@vuesax-alpha/nightly
Version:
A Component Library for Vue 3
138 lines (135 loc) • 4.37 kB
JavaScript
import { computed, unref, ref, shallowRef, onMounted, watch, getCurrentScope, onScopeDispose, shallowReadonly } from 'vue';
import { useEventListener } from '@vueuse/core';
import { computePosition } from '../dom/index.mjs';
import { getDPR } from './utils/get-dpr.mjs';
import { roundByDPR } from './utils/round-by-dpr.mjs';
import { unwrapElement } from './utils/unwrap-element.mjs';
function useFloating(reference, floating, options = {}) {
const whileElementsMountedOption = options.whileElementsMounted;
const openOption = computed(() => {
var _a;
return (_a = unref(options.open)) != null ? _a : true;
});
const fitOption = computed(() => {
var _a;
return (_a = unref(options.fit)) != null ? _a : false;
});
const middlewareOption = computed(() => unref(options.middleware));
const placementOption = computed(() => {
var _a;
return (_a = unref(options.placement)) != null ? _a : "bottom";
});
const strategyOption = computed(() => {
var _a;
return (_a = unref(options.strategy)) != null ? _a : "absolute";
});
const transformOption = computed(() => {
var _a;
return (_a = unref(options.transform)) != null ? _a : true;
});
const referenceElement = computed(() => unwrapElement(reference.value));
const floatingElement = computed(() => unwrapElement(floating.value));
const x = ref(0);
const y = ref(0);
const strategy = ref(strategyOption.value);
const placement = ref(placementOption.value);
const middlewareData = shallowRef({});
const isPositioned = ref(false);
const floatingStyles = computed(() => {
const initialStyles = {
position: strategy.value,
left: "0",
top: "0"
};
if (!floatingElement.value) {
return initialStyles;
}
const xVal = roundByDPR(floatingElement.value, x.value);
const yVal = roundByDPR(floatingElement.value, y.value);
if (transformOption.value) {
return {
...initialStyles,
transform: `translate(${xVal}px, ${yVal}px)`,
...getDPR(floatingElement.value) >= 1.5 && {
willChange: "transform"
}
};
}
return {
position: strategy.value,
left: `${xVal}px`,
top: `${yVal}px`
};
});
let whileElementsMountedCleanup;
function update() {
if (referenceElement.value == null || floatingElement.value == null) {
return;
}
if (fitOption.value) {
floatingElement.value.style.width = `${referenceElement.value.getBoundingClientRect().width}px`;
}
computePosition(referenceElement.value, floatingElement.value, {
middleware: middlewareOption.value,
placement: placementOption.value,
strategy: strategyOption.value
}).then((position) => {
x.value = position.x;
y.value = position.y;
strategy.value = position.strategy;
placement.value = position.placement;
middlewareData.value = position.middlewareData;
isPositioned.value = true;
});
}
function cleanup() {
if (typeof whileElementsMountedCleanup === "function") {
whileElementsMountedCleanup();
whileElementsMountedCleanup = void 0;
}
}
function attach() {
cleanup();
if (whileElementsMountedOption === void 0) {
update();
return;
}
if (referenceElement.value != null && floatingElement.value != null) {
whileElementsMountedCleanup = whileElementsMountedOption(
referenceElement.value,
floatingElement.value,
update
);
return;
}
}
function reset() {
if (!openOption.value) {
isPositioned.value = false;
}
}
onMounted(() => {
useEventListener("resize", attach, true);
useEventListener("scroll", attach, true);
watch([middlewareOption, placementOption, strategyOption], update, {
flush: "sync"
});
watch([referenceElement, floatingElement], attach, { flush: "sync" });
watch(openOption, reset, { flush: "sync" });
});
if (getCurrentScope()) {
onScopeDispose(cleanup);
}
return {
x: shallowReadonly(x),
y: shallowReadonly(y),
strategy: shallowReadonly(strategy),
placement: shallowReadonly(placement),
middlewareData: shallowReadonly(middlewareData),
isPositioned: shallowReadonly(isPositioned),
floatingStyles,
update
};
}
export { useFloating };
//# sourceMappingURL=use-floating.mjs.map