Skip to content

Commit 0711dc9

Browse files
author
Zerone
committed
fix: the inaccuracy in height retrieval
1 parent 9a2776a commit 0711dc9

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

src/components/picture.vue

+30-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts" setup>
22
import { computed, type VNodeRef, nextTick, ref } from 'vue';
3-
import { addUnit, getPrefix } from '../utils';
3+
import { addUnit, getPrefix, INSTANCE_KEY } from '../utils';
4+
import type { El } from '../types';
45
56
export interface PictureProps {
67
icon?: string;
@@ -22,12 +23,39 @@
2223
iconShowText: true,
2324
});
2425
26+
const isElement = (node: El) => {
27+
const ELEMENT_NODE_TYPE = 1;
28+
return (
29+
node.tagName !== 'HTML' &&
30+
node.tagName !== 'BODY' &&
31+
node.nodeType === ELEMENT_NODE_TYPE
32+
);
33+
};
34+
35+
const containerPositionReg = /relative|absolute|fixed/i;
36+
const getContainer = (el: El) => {
37+
let node = el;
38+
while (node && isElement(node)) {
39+
const { position } = getComputedStyle(node);
40+
if (
41+
node[INSTANCE_KEY] ||
42+
(containerPositionReg.test(position) && node !== el.parentNode)
43+
) {
44+
return node;
45+
}
46+
node = node.parentNode as El;
47+
}
48+
};
49+
2550
const formatShowIcon = ref(false);
2651
const root: VNodeRef = async (el) => {
2752
if (!el) return;
2853
await nextTick();
54+
// 修复高度获取不准问题
55+
const container = getContainer(el as El);
56+
if (!container) return;
2957
formatShowIcon.value =
30-
parseInt(getComputedStyle(el as Element).height, 10) > 100;
58+
parseInt(getComputedStyle(container).height, 10) > 120;
3159
};
3260
3361
const svgCommon = `<defs><linearGradient id="be595b27-5838-4fa8-9b74-d90d46f5bb3e" x1="80" y1="-1063.51" x2="80" y2="-1082.94" gradientTransform="matrix(1, 0, 0, -1, 0, -922.94)" gradientUnits="userSpaceOnUse"><stop offset="0" stop-color="#e8e8e8"/><stop offset="0.6" stop-color="#f8f8f8" stop-opacity="0.5"/><stop offset="1" stop-color="#fff" stop-opacity="0"/></linearGradient></defs><path d="M152.29,160c0-5.36-32.38-19.43-72.29-19.43S7.71,154.64,7.71,160Z" fill="url(#be595b27-5838-4fa8-9b74-d90d46f5bb3e)"/>`;

src/core.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,12 @@ import {
1010
toRaw,
1111
} from 'vue';
1212
import VueDefaultPage, { type Props } from './components/index.vue';
13-
import type { DirectiveOptions, Name, Value, CamelName } from './types';
13+
import type { DirectiveOptions, Name, Value, CamelName, El } from './types';
1414
import './style/index.less';
15-
import { getPrefix } from './utils';
16-
17-
const INSTANCE_KEY = Symbol(getPrefix());
15+
import { getPrefix, INSTANCE_KEY } from './utils';
1816

1917
export const publicPropsKeys = ['zIndex', 'background'] as const;
2018

21-
interface El extends HTMLElement {
22-
[INSTANCE_KEY]?: {
23-
props: Props;
24-
unmount(): void;
25-
};
26-
}
2719
interface InstanceOptions<T extends Name> {
2820
name: T;
2921
options?: DirectiveOptions<T>;

src/types/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { INSTANCE_KEY } from 'src/utils';
2+
13
import type {
24
EmptyProps,
35
ErrorProps,
@@ -46,3 +48,10 @@ export type DirectiveOptions<T extends Name> = (T extends 'loading'
4648
export type Options = {
4749
[N in CamelName]?: (DirectiveOptions<N> & { enable?: boolean }) | boolean;
4850
} & PublicOptions;
51+
52+
export interface El extends HTMLElement {
53+
[INSTANCE_KEY]?: {
54+
props: Props;
55+
unmount(): void;
56+
};
57+
}

src/utils/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ export function getPrefix(string = '', chars = '-') {
1515
.filter((item) => item)
1616
.join(chars);
1717
}
18+
19+
export const INSTANCE_KEY = Symbol(getPrefix());

0 commit comments

Comments
 (0)