From 84f2262fbaaee8818f1f823ef702db055ab2e52b Mon Sep 17 00:00:00 2001 From: "Y." Date: Thu, 29 Aug 2024 20:49:57 +0800 Subject: [PATCH] feat(Toast): support attach props for ToastPlugin (#1555) * feat(Toast): support attach props for ToastPlugin * test: update snapshots * fix(DropdownMenu): resolved menuOpened and menuClosed events are invalid * chore: update _common * test: update snapshots * chore(Toast): strip ToastPlugin from index file to plugin file * fix: fix lint --- .eslintrc | 1 + src/_common | 2 +- src/_util/canUseDom.ts | 8 + src/_util/getScrollParent.ts | 20 + src/_util/supportsPassive.ts | 19 + src/_util/useTouch.ts | 72 + src/cascader/demos/lazy.vue | 2 +- src/dropdown-menu/dropdown-menu.tsx | 6 +- src/hooks/useLockScroll.ts | 83 + src/hooks/useTeleport.ts | 32 + .../__test__/__snapshots__/demo.test.jsx.snap | 26 + src/image-viewer/__test__/demo.test.jsx | 2 + .../__test__/__snapshots__/demo.test.jsx.snap | 6 +- src/layout/__test__/demo.test.jsx | 4 +- .../__test__/__snapshots__/demo.test.jsx.snap | 112 + src/loading/__test__/demo.test.jsx | 6 + src/plugins.ts | 2 +- .../__test__/__snapshots__/demo.test.jsx.snap | 351 ++ src/rate/__test__/demo.test.jsx | 2 + src/shared/dom.ts | 16 +- src/swipe-cell/demos/double.vue | 2 +- src/swipe-cell/demos/event.vue | 3 +- src/swipe-cell/demos/icon.vue | 2 +- src/swipe-cell/demos/left.vue | 3 +- src/swipe-cell/demos/right.vue | 2 +- .../__test__/__snapshots__/demo.test.jsx.snap | 107 + src/swiper/__test__/demo.test.jsx | 2 + .../__test__/__snapshots__/demo.test.jsx.snap | 2 + src/toast/__test__/index.test.jsx | 7 +- src/toast/demos/base.vue | 6 +- src/toast/index.ts | 114 +- src/toast/plugin.ts | 118 + src/toast/props.ts | 5 - src/toast/toast.en-US.md | 10 +- src/toast/toast.md | 10 +- src/toast/toast.tsx | 26 +- src/toast/type.ts | 24 +- .../__test__/__snapshots__/demo.test.jsx.snap | 3131 +++++++++++++++++ src/tree-select/__test__/demo.test.jsx | 25 + 39 files changed, 4210 insertions(+), 161 deletions(-) create mode 100644 src/_util/canUseDom.ts create mode 100644 src/_util/getScrollParent.ts create mode 100644 src/_util/supportsPassive.ts create mode 100644 src/_util/useTouch.ts create mode 100644 src/hooks/useLockScroll.ts create mode 100644 src/hooks/useTeleport.ts create mode 100644 src/toast/plugin.ts create mode 100644 src/tree-select/__test__/__snapshots__/demo.test.jsx.snap create mode 100644 src/tree-select/__test__/demo.test.jsx diff --git a/.eslintrc b/.eslintrc index 2632f2c4f..c7f919349 100644 --- a/.eslintrc +++ b/.eslintrc @@ -38,6 +38,7 @@ "no-unused-expressions": "off", "no-return-assign": "off", "no-use-before-define": "off", + "no-bitwise": "off", "func-names": "off", "guard-for-in": "off", "consistent-return": "off", diff --git a/src/_common b/src/_common index f1adcf087..889f7026c 160000 --- a/src/_common +++ b/src/_common @@ -1 +1 @@ -Subproject commit f1adcf087ce845e27c6cedbdeb044a57130b7e8e +Subproject commit 889f7026c7e4b0dda2534c3a4c51ea8cf7f2ac6a diff --git a/src/_util/canUseDom.ts b/src/_util/canUseDom.ts new file mode 100644 index 000000000..496eaebd6 --- /dev/null +++ b/src/_util/canUseDom.ts @@ -0,0 +1,8 @@ +import isUndefined from 'lodash/isUndefined'; + +export const canUseDom = !!( + !isUndefined(window) && + !isUndefined(document) && + window.document && + window.document.createElement +); diff --git a/src/_util/getScrollParent.ts b/src/_util/getScrollParent.ts new file mode 100644 index 000000000..b4ee89a52 --- /dev/null +++ b/src/_util/getScrollParent.ts @@ -0,0 +1,20 @@ +type ScrollElement = HTMLElement | Window; + +const overflowScrollReg = /scroll|auto|overlay/i; + +export default function getScrollParent( + el: Element | null | undefined, + root: ScrollElement | null | undefined = window, +): Window | Element | null | undefined { + let node = el; + + while (node && node !== root && node.nodeType === 1) { + const { overflowY } = window.getComputedStyle(node); + if (overflowScrollReg.test(overflowY)) { + return node; + } + node = node.parentNode as Element; + } + + return root; +} diff --git a/src/_util/supportsPassive.ts b/src/_util/supportsPassive.ts new file mode 100644 index 000000000..a4b32a667 --- /dev/null +++ b/src/_util/supportsPassive.ts @@ -0,0 +1,19 @@ +import { ref } from 'vue'; +import { canUseDom } from './canUseDom'; + +export const supportsPassive = ref(false); + +if (canUseDom) { + try { + const opts = {}; + Object.defineProperty(opts, 'passive', { + get() { + supportsPassive.value = true; + return true; // 添加返回值 + }, + }); + window.addEventListener('test-passive', null as any, opts); + } catch (e) { + // + } +} diff --git a/src/_util/useTouch.ts b/src/_util/useTouch.ts new file mode 100644 index 000000000..5757415f2 --- /dev/null +++ b/src/_util/useTouch.ts @@ -0,0 +1,72 @@ +/* eslint-disable no-undef */ + +import { ref } from 'vue'; + +const MIN_DISTANCE = 10; + +type Direction = '' | 'vertical' | 'horizontal'; + +function getDirection(x: number, y: number) { + if (x > y && x > MIN_DISTANCE) { + return 'horizontal'; + } + if (y > x && y > MIN_DISTANCE) { + return 'vertical'; + } + return ''; +} + +export function useTouch() { + const startX = ref(0); + const startY = ref(0); + const deltaX = ref(0); + const deltaY = ref(0); + const offsetX = ref(0); + const offsetY = ref(0); + const direction = ref(''); + + const isVertical = () => direction.value === 'vertical'; + const isHorizontal = () => direction.value === 'horizontal'; + + const reset = () => { + deltaX.value = 0; + deltaY.value = 0; + offsetX.value = 0; + offsetY.value = 0; + direction.value = ''; + }; + + const start = ((event: TouchEvent) => { + reset(); + startX.value = event.touches[0].clientX; + startY.value = event.touches[0].clientY; + }) as EventListener; + + const move = ((event: TouchEvent) => { + const touch = event.touches[0]; + // Fix: Safari back will set clientX to negative number + deltaX.value = touch.clientX < 0 ? 0 : touch.clientX - startX.value; + deltaY.value = touch.clientY - startY.value; + offsetX.value = Math.abs(deltaX.value); + offsetY.value = Math.abs(deltaY.value); + + if (!direction.value) { + direction.value = getDirection(offsetX.value, offsetY.value); + } + }) as EventListener; + + return { + move, + start, + reset, + startX, + startY, + deltaX, + deltaY, + offsetX, + offsetY, + direction, + isVertical, + isHorizontal, + }; +} diff --git a/src/cascader/demos/lazy.vue b/src/cascader/demos/lazy.vue index 0eb41e28a..f81a00aed 100644 --- a/src/cascader/demos/lazy.vue +++ b/src/cascader/demos/lazy.vue @@ -12,7 +12,7 @@