perf: trigger

refactor-trigger
tangjinzhou 2021-08-15 21:03:47 +08:00
parent 44acf6139d
commit f623c3fcac
5 changed files with 24 additions and 48 deletions

View File

@ -16,7 +16,7 @@ export const getTransitionProps = (transitionName: string, opt: TransitionProps
const transitionProps: TransitionProps = transitionName const transitionProps: TransitionProps = transitionName
? { ? {
appear: true, appear: true,
type: 'animation', // type: 'animation',
// appearFromClass: `${transitionName}-appear ${transitionName}-appear-prepare`, // appearFromClass: `${transitionName}-appear ${transitionName}-appear-prepare`,
// appearActiveClass: `antdv-base-transtion`, // appearActiveClass: `antdv-base-transtion`,
// appearToClass: `${transitionName}-appear ${transitionName}-appear-active`, // appearToClass: `${transitionName}-appear ${transitionName}-appear-active`,

View File

@ -1,4 +1,4 @@
import type { PropType } from 'vue'; import { nextTick, PropType } from 'vue';
import { defineComponent, ref, computed, onMounted, onUpdated, watch, onUnmounted } from 'vue'; import { defineComponent, ref, computed, onMounted, onUpdated, watch, onUnmounted } from 'vue';
import { alignElement, alignPoint } from 'dom-align'; import { alignElement, alignPoint } from 'dom-align';
import addEventListener from '../vc-util/Dom/addEventListener'; import addEventListener from '../vc-util/Dom/addEventListener';
@ -56,13 +56,6 @@ export default defineComponent({
setup(props, { expose, slots }) { setup(props, { expose, slots }) {
const cacheRef = ref<{ element?: HTMLElement; point?: TargetPoint; align?: AlignType }>({}); const cacheRef = ref<{ element?: HTMLElement; point?: TargetPoint; align?: AlignType }>({});
const nodeRef = ref(); const nodeRef = ref();
const forceAlignPropsRef = computed(() => ({
disabled: props.disabled,
target: props.target,
align: props.align,
onAlign: props.onAlign,
}));
const [forceAlign, cancelForceAlign] = useBuffer( const [forceAlign, cancelForceAlign] = useBuffer(
() => { () => {
const { const {
@ -70,7 +63,7 @@ export default defineComponent({
target: latestTarget, target: latestTarget,
align: latestAlign, align: latestAlign,
onAlign: latestOnAlign, onAlign: latestOnAlign,
} = forceAlignPropsRef.value; } = props;
if (!latestDisabled && latestTarget && nodeRef.value) { if (!latestDisabled && latestTarget && nodeRef.value) {
const source = nodeRef.value; const source = nodeRef.value;
@ -83,29 +76,14 @@ export default defineComponent({
cacheRef.value.align = latestAlign; cacheRef.value.align = latestAlign;
// IE lose focus after element realign // IE lose focus after element realign
// We should record activeElement and restore later // We should record activeElement and restore later
// const { activeElement } = document; const { activeElement } = document;
// console.log(
// '🌹',
// source.style.display,
// source.style.left,
// source.style.top,
// source.className,
// );
// We only align when element is visible // We only align when element is visible
if (element && isVisible(element)) { if (element && isVisible(element)) {
result = alignElement(source, element, latestAlign); result = alignElement(source, element, latestAlign);
} else if (point) { } else if (point) {
result = alignPoint(source, point, latestAlign); result = alignPoint(source, point, latestAlign);
} }
restoreFocus(activeElement, source);
// console.log(
// '😸',
// source.style.display,
// source.style.left,
// source.style.top,
// source.className,
// );
// restoreFocus(activeElement, source);
if (latestOnAlign && result) { if (latestOnAlign && result) {
latestOnAlign(source, result); latestOnAlign(source, result);
@ -157,11 +135,15 @@ export default defineComponent({
}; };
onMounted(() => { onMounted(() => {
goAlign(); nextTick(() => {
goAlign();
});
}); });
onUpdated(() => { onUpdated(() => {
goAlign(); nextTick(() => {
goAlign();
});
}); });
// Listen for disabled change // Listen for disabled change
@ -174,7 +156,7 @@ export default defineComponent({
cancelForceAlign(); cancelForceAlign();
} }
}, },
{ flush: 'post' }, { immediate: true, flush: 'post' },
); );
// Listen for window resize // Listen for window resize
@ -210,7 +192,7 @@ export default defineComponent({
if (child) { if (child) {
return cloneElement(child[0], { ref: nodeRef }, true, true); return cloneElement(child[0], { ref: nodeRef }, true, true);
} }
return child && child[0]; return null;
}; };
}, },
}); });

View File

@ -59,15 +59,14 @@ export default defineComponent({
const onInternalAlign = (popupDomNode: HTMLElement, matchAlign: AlignType) => { const onInternalAlign = (popupDomNode: HTMLElement, matchAlign: AlignType) => {
const nextAlignedClassName = props.getClassNameFromAlign(matchAlign); const nextAlignedClassName = props.getClassNameFromAlign(matchAlign);
const preAlignedClassName = alignedClassName.value;
if (alignedClassName.value !== nextAlignedClassName) { if (alignedClassName.value !== nextAlignedClassName) {
nextTick(() => { alignedClassName.value = nextAlignedClassName;
alignedClassName.value = nextAlignedClassName;
});
} }
if (status.value === 'align') { if (status.value === 'align') {
// Repeat until not more align needed // Repeat until not more align needed
if (alignedClassName.value !== nextAlignedClassName) { if (preAlignedClassName !== nextAlignedClassName) {
Promise.resolve().then(() => { nextTick(() => {
forceAlign(); forceAlign();
}); });
} else { } else {
@ -148,12 +147,7 @@ export default defineComponent({
const mergedClassName = classNames(prefixCls, attrs.class, alignedClassName.value); const mergedClassName = classNames(prefixCls, attrs.class, alignedClassName.value);
const transitionProps = getTransitionProps(motion.value.name, motion.value); const transitionProps = getTransitionProps(motion.value.name, motion.value);
return ( return (
<Transition <Transition ref={elementRef} {...transitionProps} onBeforeEnter={onShowPrepare}>
ref={elementRef}
{...transitionProps}
onBeforeAppear={onShowPrepare}
onBeforeEnter={onShowPrepare}
>
{!destroyPopupOnHide || visible ? ( {!destroyPopupOnHide || visible ? (
<Align <Align
v-show={visible} v-show={visible}

View File

@ -1,4 +1,5 @@
import type { Ref } from 'vue'; import type { Ref } from 'vue';
import { nextTick } from 'vue';
import { onBeforeUnmount } from 'vue'; import { onBeforeUnmount } from 'vue';
import { ref, watch } from 'vue'; import { ref, watch } from 'vue';
import raf from '../../_util/raf'; import raf from '../../_util/raf';
@ -26,7 +27,6 @@ export default (
const status = ref<PopupStatus>(null); const status = ref<PopupStatus>(null);
const rafRef = ref<number>(); const rafRef = ref<number>();
const destroyRef = ref(false); const destroyRef = ref(false);
function setStatus(nextStatus: PopupStatus) { function setStatus(nextStatus: PopupStatus) {
if (!destroyRef.value) { if (!destroyRef.value) {
status.value = nextStatus; status.value = nextStatus;
@ -60,9 +60,9 @@ export default (
watch( watch(
visible, visible,
() => { () => {
status.value = 'measure'; setStatus('measure');
}, },
{ immediate: true }, { immediate: true, flush: 'post' },
); );
// Go next status // Go next status
watch( watch(
@ -76,7 +76,7 @@ export default (
} }
if (status.value) { if (status.value) {
rafRef.value = raf(async () => { nextTick(() => {
const index = StatusQueue.indexOf(status.value); const index = StatusQueue.indexOf(status.value);
const nextStatus = StatusQueue[index + 1]; const nextStatus = StatusQueue[index + 1];
if (nextStatus && index !== -1) { if (nextStatus && index !== -1) {
@ -85,7 +85,7 @@ export default (
}); });
} }
}, },
{ immediate: true }, { immediate: true, flush: 'post' },
); );
onBeforeUnmount(() => { onBeforeUnmount(() => {
destroyRef.value = true; destroyRef.value = true;

View File

@ -455,7 +455,7 @@ export default defineComponent({
maskAnimation, maskAnimation,
maskTransitionName, maskTransitionName,
getContainer, getContainer,
popupClassName, class: popupClassName,
style: popupStyle, style: popupStyle,
onAlign: $attrs.onPopupAlign || noop, onAlign: $attrs.onPopupAlign || noop,
...mouseProps, ...mouseProps,