-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
39 changed files
with
4,210 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule _common
updated
31 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import isUndefined from 'lodash/isUndefined'; | ||
|
||
export const canUseDom = !!( | ||
!isUndefined(window) && | ||
!isUndefined(document) && | ||
window.document && | ||
window.document.createElement | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Direction>(''); | ||
|
||
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { Ref, watch, onMounted, onActivated, onBeforeUnmount, onDeactivated, nextTick } from 'vue'; | ||
import { useTouch } from '../_util/useTouch'; | ||
import getScrollParent from '../_util/getScrollParent'; | ||
import { supportsPassive } from '../_util/supportsPassive'; | ||
|
||
let totalLockCount = 0; | ||
let mounted: boolean = null; | ||
|
||
// 移植自vant:https://github.com/youzan/vant/blob/HEAD/src/composables/use-lock-scroll.ts | ||
export function useLockScroll(rootRef: Ref<HTMLElement | undefined>, shouldLock: () => boolean, componentName: string) { | ||
const touch = useTouch(); | ||
const BODY_LOCK_CLASS = `${componentName}--lock`; | ||
|
||
const onTouchMove = (event: TouchEvent) => { | ||
touch.move(event); | ||
|
||
const direction = touch.deltaY.value > 0 ? '10' : '01'; | ||
const el = getScrollParent(event.target as Element, rootRef.value) as HTMLElement; | ||
if (!el) return; | ||
const { scrollHeight, offsetHeight, scrollTop } = el; | ||
let status = '11'; | ||
|
||
if (scrollTop === 0) { | ||
status = offsetHeight >= scrollHeight ? '00' : '01'; | ||
} else if (scrollTop + offsetHeight >= scrollHeight) { | ||
status = '10'; | ||
} | ||
|
||
if (status !== '11' && touch.isVertical() && !(parseInt(status, 2) & parseInt(direction, 2))) { | ||
if (event.cancelable) { | ||
event.preventDefault(); | ||
} | ||
} | ||
}; | ||
|
||
const lock = () => { | ||
document.addEventListener('touchstart', touch.start); | ||
document.addEventListener('touchmove', onTouchMove, supportsPassive.value ? { passive: false } : false); | ||
|
||
if (!totalLockCount) { | ||
document.body.classList.add(BODY_LOCK_CLASS); | ||
} | ||
|
||
totalLockCount += 1; | ||
}; | ||
|
||
const unlock = () => { | ||
if (totalLockCount) { | ||
document.removeEventListener('touchstart', touch.start); | ||
document.removeEventListener('touchmove', onTouchMove); | ||
|
||
totalLockCount -= 1; | ||
|
||
if (!totalLockCount) { | ||
document.body.classList.remove(BODY_LOCK_CLASS); | ||
} | ||
} | ||
}; | ||
|
||
const init = () => shouldLock() && lock(); | ||
|
||
const destroy = () => shouldLock() && unlock(); | ||
|
||
onMounted(() => { | ||
init(); | ||
nextTick(() => { | ||
mounted = true; | ||
}); | ||
}); | ||
|
||
onActivated(() => { | ||
if (mounted) { | ||
init(); | ||
} | ||
}); | ||
|
||
onDeactivated(destroy); | ||
onBeforeUnmount(destroy); | ||
|
||
watch(shouldLock, (value) => { | ||
value ? lock() : unlock(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { computed, Ref, onMounted, ref, watch } from 'vue'; | ||
import isFunction from 'lodash/isFunction'; | ||
import { getSSRAttach, getAttach } from '../shared/dom'; | ||
import { AttachNode } from '../common'; | ||
|
||
/** | ||
* @description 返回挂载的节点, 用于teleport | ||
* @param attach 既可以是一个函数, 也可以是一个ref | ||
* @param triggerNode 既可以是一个函数, 也可以是一个ref | ||
*/ | ||
const useTeleport = ( | ||
attach: (() => AttachNode) | Ref<AttachNode>, | ||
triggerNode?: (() => any) | Ref<any>, | ||
): Ref<string | Element> => { | ||
// 如果是函数, 则使用computed包裹 否则直接使用ref | ||
const to = isFunction(attach) ? computed(attach) : ref(attach); | ||
const innerTriggerNode = isFunction(triggerNode) ? computed(triggerNode) : ref(triggerNode); | ||
|
||
const element = ref<string | Element>(); | ||
|
||
const getElement = () => { | ||
element.value = getSSRAttach() || getAttach(to.value, innerTriggerNode.value); | ||
}; | ||
|
||
onMounted(() => getElement()); | ||
|
||
watch([to, innerTriggerNode], () => getElement()); | ||
|
||
return element; | ||
}; | ||
|
||
export default useTeleport; |
26 changes: 26 additions & 0 deletions
26
src/image-viewer/__test__/__snapshots__/demo.test.jsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.