Skip to content

Commit

Permalink
fix: the inaccuracy in height retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
Zerone committed May 27, 2024
1 parent 9a2776a commit 0711dc9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
32 changes: 30 additions & 2 deletions src/components/picture.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts" setup>
import { computed, type VNodeRef, nextTick, ref } from 'vue';
import { addUnit, getPrefix } from '../utils';
import { addUnit, getPrefix, INSTANCE_KEY } from '../utils';
import type { El } from '../types';
export interface PictureProps {
icon?: string;
Expand All @@ -22,12 +23,39 @@
iconShowText: true,
});
const isElement = (node: El) => {
const ELEMENT_NODE_TYPE = 1;
return (
node.tagName !== 'HTML' &&
node.tagName !== 'BODY' &&
node.nodeType === ELEMENT_NODE_TYPE
);
};
const containerPositionReg = /relative|absolute|fixed/i;
const getContainer = (el: El) => {
let node = el;
while (node && isElement(node)) {
const { position } = getComputedStyle(node);
if (
node[INSTANCE_KEY] ||
(containerPositionReg.test(position) && node !== el.parentNode)
) {
return node;
}
node = node.parentNode as El;
}
};
const formatShowIcon = ref(false);
const root: VNodeRef = async (el) => {
if (!el) return;
await nextTick();
// 修复高度获取不准问题
const container = getContainer(el as El);
if (!container) return;
formatShowIcon.value =
parseInt(getComputedStyle(el as Element).height, 10) > 100;
parseInt(getComputedStyle(container).height, 10) > 120;
};
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)"/>`;
Expand Down
12 changes: 2 additions & 10 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,12 @@ import {
toRaw,
} from 'vue';
import VueDefaultPage, { type Props } from './components/index.vue';
import type { DirectiveOptions, Name, Value, CamelName } from './types';
import type { DirectiveOptions, Name, Value, CamelName, El } from './types';
import './style/index.less';
import { getPrefix } from './utils';

const INSTANCE_KEY = Symbol(getPrefix());
import { getPrefix, INSTANCE_KEY } from './utils';

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

interface El extends HTMLElement {
[INSTANCE_KEY]?: {
props: Props;
unmount(): void;
};
}
interface InstanceOptions<T extends Name> {
name: T;
options?: DirectiveOptions<T>;
Expand Down
9 changes: 9 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { INSTANCE_KEY } from 'src/utils';

import type {
EmptyProps,
ErrorProps,
Expand Down Expand Up @@ -46,3 +48,10 @@ export type DirectiveOptions<T extends Name> = (T extends 'loading'
export type Options = {
[N in CamelName]?: (DirectiveOptions<N> & { enable?: boolean }) | boolean;
} & PublicOptions;

export interface El extends HTMLElement {
[INSTANCE_KEY]?: {
props: Props;
unmount(): void;
};
}
2 changes: 2 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ export function getPrefix(string = '', chars = '-') {
.filter((item) => item)
.join(chars);
}

export const INSTANCE_KEY = Symbol(getPrefix());

0 comments on commit 0711dc9

Please sign in to comment.