mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-04-05 05:42:44 +08:00
perf(@vant/use): reduce usePageVisibility event binding (#9835)
* perf(@vant/use): reduce usePageVisibility event binding * chore: update
This commit is contained in:
parent
a9ce9096e4
commit
a0b368f1b4
@ -1,4 +1,7 @@
|
||||
{
|
||||
"root": true,
|
||||
"extends": ["@vant"]
|
||||
"extends": ["@vant"],
|
||||
"rules": {
|
||||
"prefer-object-spread": "off"
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,4 @@
|
||||
import { Ref, unref } from 'vue';
|
||||
import { inBrowser } from '../utils';
|
||||
import { useEventListener } from '../useEventListener';
|
||||
|
||||
export type UseClickAwayOptions = {
|
||||
@ -11,10 +10,6 @@ export function useClickAway(
|
||||
listener: EventListener,
|
||||
options: UseClickAwayOptions = {}
|
||||
) {
|
||||
if (!inBrowser) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { eventName = 'click' } = options;
|
||||
|
||||
const onClick = (event: Event) => {
|
||||
|
@ -1,18 +1,21 @@
|
||||
import { ref } from 'vue';
|
||||
import { ref, Ref } from 'vue';
|
||||
import { inBrowser } from '../utils';
|
||||
import { useEventListener } from '../useEventListener';
|
||||
|
||||
let visibility: Ref<VisibilityState>;
|
||||
|
||||
export function usePageVisibility() {
|
||||
const visibility = ref<VisibilityState>('visible');
|
||||
if (!visibility) {
|
||||
visibility = ref<VisibilityState>('visible');
|
||||
|
||||
const setVisibility = () => {
|
||||
if (inBrowser) {
|
||||
visibility.value = document.hidden ? 'hidden' : 'visible';
|
||||
}
|
||||
};
|
||||
const update = () => {
|
||||
visibility.value = document.hidden ? 'hidden' : 'visible';
|
||||
};
|
||||
|
||||
setVisibility();
|
||||
useEventListener('visibilitychange', setVisibility);
|
||||
update();
|
||||
window.addEventListener('visibilitychange', update);
|
||||
}
|
||||
}
|
||||
|
||||
return visibility;
|
||||
}
|
||||
|
@ -1,19 +1,16 @@
|
||||
import { Ref, unref } from 'vue';
|
||||
|
||||
function isWindow(val: unknown): val is Window {
|
||||
return val === window;
|
||||
}
|
||||
const isWindow = (val: unknown): val is Window => val === window;
|
||||
|
||||
function makeDOMRect(width: number, height: number) {
|
||||
return {
|
||||
const makeDOMRect = (width: number, height: number) =>
|
||||
({
|
||||
top: 0,
|
||||
left: 0,
|
||||
right: width,
|
||||
bottom: height,
|
||||
width,
|
||||
height,
|
||||
} as DOMRect;
|
||||
}
|
||||
} as DOMRect);
|
||||
|
||||
export const useRect = (
|
||||
elementOrRef: Element | Window | Ref<Element | Window | undefined>
|
||||
@ -26,7 +23,7 @@ export const useRect = (
|
||||
return makeDOMRect(width, height);
|
||||
}
|
||||
|
||||
if (element && element.getBoundingClientRect) {
|
||||
if (element?.getBoundingClientRect) {
|
||||
return element.getBoundingClientRect();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user