Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(SwipeCell): new SwipeCell #663

Merged
merged 5 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions src/shared/useClickAway/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { MaybeElementRef, UnRefElementReturn, unrefElement, useEventListener } from '@vueuse/core';
import { isArray } from 'lodash';
import { inBrowser } from '../util';

export interface UseClickAwayOptions {
/**
* The name of the trigger event.
* @default 'touchstart'
*/
eventName?: string;
/**
* Use capturing phase for internal event listener.
* @default true
*/
capture?: boolean;
/**
* List of elements that should not trigger the event.
*/
ignore?: Array<MaybeElementRef>;
/**
* Run handler function if focus moves to an iframe.
* @default false
*/
detectIframe?: boolean;
}

export type UseClickAwayHandler<
T extends { detectIframe: UseClickAwayOptions['detectIframe'] } = { detectIframe: false },
> = (evt: T['detectIframe'] extends true ? PointerEvent | FocusEvent : PointerEvent) => void;

/**
* Listen for clicks outside of a collection of elements.
* @param target
* @param fn
* @param options
*/
export function useClickAway<T extends UseClickAwayOptions>(
target: MaybeElementRef | Array<MaybeElementRef>,
fn: UseClickAwayHandler<{ detectIframe: T['detectIframe'] }>,
options: T = {} as T,
) {
if (!inBrowser) return;

const { eventName = 'touchstart', capture = true, ignore = [], detectIframe = false } = options;

const listenerOptions = {
passive: true,
capture,
};

// el是否包含在event.target
const checkElInEvtTarget = (el: UnRefElementReturn, event: PointerEvent) => {
return el && (event.target === el || event.composedPath().includes(el));
};

const shouldIgnore = (event: PointerEvent) => {
return ignore.some((ignoreTarget) => checkElInEvtTarget(unrefElement(ignoreTarget), event));
};

const eventHandler = (event: PointerEvent) => {
const targets = isArray(target) ? [...target] : [target];

let shouldTrigger = targets.every((targetItem) => !checkElInEvtTarget(unrefElement(targetItem), event));

if (!shouldTrigger) return;

shouldTrigger = !shouldIgnore(event);

if (!shouldTrigger) return;

fn(event);
};

const cleanup = [useEventListener(window, eventName, eventHandler, listenerOptions)];

if (detectIframe) {
cleanup.push(
useEventListener(window, 'blur', (event: PointerEvent) => {
setTimeout(() => {
const targets = isArray(target) ? [...target] : [target];

if (
targets.every((targetItem) => {
const el = unrefElement(targetItem);
return (
window.document.activeElement?.tagName === 'IFRAME' && !el?.contains(window.document.activeElement)
);
})
) {
fn(event);
}
}, 0);
}),
);
}

return () => cleanup.forEach((clean) => clean());
}
Loading