🌀: fix linter

This commit is contained in:
sight 2022-06-26 23:29:29 +08:00
parent 33258dac6c
commit 49231783f7

View File

@ -19,6 +19,7 @@ import {
} from "vue"; } from "vue";
import { import {
onClickOutside, onClickOutside,
useMouse,
useResizeObserver, useResizeObserver,
useThrottleFn, useThrottleFn,
useWindowSize, useWindowSize,
@ -41,9 +42,11 @@ export interface LayDropdownProps {
blurToClose?: boolean; blurToClose?: boolean;
clickOutsideToClose?: boolean; clickOutsideToClose?: boolean;
contentOffset?: number; contentOffset?: number;
//
mouseEnterDelay?: number; mouseEnterDelay?: number;
mouseLeaveDelay?: number; mouseLeaveDelay?: number;
focusDelay?: number; focusDelay?: number;
alignPoint?: boolean;
} }
const props = withDefaults(defineProps<LayDropdownProps>(), { const props = withDefaults(defineProps<LayDropdownProps>(), {
@ -63,12 +66,14 @@ const props = withDefaults(defineProps<LayDropdownProps>(), {
mouseEnterDelay: 150, mouseEnterDelay: 150,
mouseLeaveDelay: 150, mouseLeaveDelay: 150,
focusDelay: 150, focusDelay: 150,
alignPoint: false,
}); });
const dropdownRef = shallowRef<HTMLElement | undefined>(); const dropdownRef = shallowRef<HTMLElement | undefined>();
const contentRef = shallowRef<HTMLElement | undefined>(); const contentRef = shallowRef<HTMLElement | undefined>();
const contentStyle = ref<CSSProperties>({}); const contentStyle = ref<CSSProperties>({});
const { width: windowWidth, height: windowHeight } = useWindowSize(); const { width: windowWidth, height: windowHeight } = useWindowSize();
const { x: mouseLeft, y: mouseTop } = useMouse();
const openState = ref(false); const openState = ref(false);
const triggerMethods = computed(() => const triggerMethods = computed(() =>
@ -132,9 +137,14 @@ const updateContentStyle = () => {
if (!dropdownRef.value || !contentRef.value) { if (!dropdownRef.value || !contentRef.value) {
return; return;
} }
const triggerRect = dropdownRef.value.getBoundingClientRect(); const triggerRect = dropdownRef.value!.getBoundingClientRect();
const contentRect = contentRef.value.getBoundingClientRect(); const contentRect = contentRef.value.getBoundingClientRect();
const { style } = getContentStyle(props.placement, triggerRect, contentRect); const { style } = getContentStyle(
props.placement,
triggerRect,
contentRect,
props.alignPoint
);
if (props.autoFitMinWidth) { if (props.autoFitMinWidth) {
style.minWidth = `${triggerRect.width}px`; style.minWidth = `${triggerRect.width}px`;
@ -171,13 +181,19 @@ const getContentStyle = (
placement: DropdownPlacement, placement: DropdownPlacement,
triggerRect: DOMRect, triggerRect: DOMRect,
contentRect: DOMRect, contentRect: DOMRect,
isAlignPoint?: boolean,
{ {
customStyle = {}, customStyle = {},
}: { }: {
customStyle?: CSSProperties; customStyle?: CSSProperties;
} = {} } = {}
) => { ) => {
let { top, left } = getContentOffset(placement, triggerRect, contentRect); let { top, left } = getContentOffset(
placement,
triggerRect,
contentRect,
isAlignPoint
);
const style = { const style = {
top: `${top}px`, top: `${top}px`,
left: `${left}px`, left: `${left}px`,
@ -259,8 +275,15 @@ const getFitPlacement = (
const getContentOffset = ( const getContentOffset = (
placement: DropdownPlacement, placement: DropdownPlacement,
triggerRect: DOMRect, triggerRect: DOMRect,
contentRect: DOMRect contentRect: DOMRect,
isAlignPoint?: boolean
) => { ) => {
if (isAlignPoint) {
return {
top: mouseTop.value - triggerRect.top,
left: mouseLeft.value - triggerRect.left,
};
}
switch (placement) { switch (placement) {
case "top": case "top":
return { return {
@ -458,6 +481,21 @@ watch(
{ immediate: true } { immediate: true }
); );
const getTriggerRect = (isAlignPoint: boolean) => {
return isAlignPoint
? ({
top: mouseTop.value,
bottom: mouseTop.value,
left: mouseLeft.value,
right: mouseLeft.value,
width: 0,
height: 0,
x: mouseLeft.value,
y: mouseTop.value,
} as DOMRect)
: dropdownRef.value!.getBoundingClientRect();
};
provide("openState", openState); provide("openState", openState);
defineExpose({ open, hide, toggle }); defineExpose({ open, hide, toggle });