UNPKG

@aplus-frontend/antdv

Version:

Vue basic component library maintained based on ant-design-vue

108 lines 4.39 kB
import { onBeforeUnmount, nextTick, watch, defineComponent, computed, ref, createVNode as _createVNode } from 'vue'; import { anyType } from '../../_util/type'; const calcThumbStyle = targetElement => targetElement ? { left: targetElement.offsetLeft, right: targetElement.parentElement.clientWidth - targetElement.clientWidth - targetElement.offsetLeft, width: targetElement.clientWidth } : null; const toPX = value => value !== undefined ? `${value}px` : undefined; const MotionThumb = defineComponent({ props: { value: anyType(), getValueIndex: anyType(), prefixCls: anyType(), motionName: anyType(), onMotionStart: anyType(), onMotionEnd: anyType(), direction: anyType(), containerRef: anyType() }, emits: ['motionStart', 'motionEnd'], setup(props, _ref) { let { emit } = _ref; const thumbRef = ref(); // =========================== Effect =========================== const findValueElement = val => { var _a; const index = props.getValueIndex(val); const ele = (_a = props.containerRef.value) === null || _a === void 0 ? void 0 : _a.querySelectorAll(`.${props.prefixCls}-item`)[index]; return (ele === null || ele === void 0 ? void 0 : ele.offsetParent) && ele; }; const prevStyle = ref(null); const nextStyle = ref(null); const thumbStart = computed(() => { var _a, _b; return props.direction === 'rtl' ? toPX(-((_a = prevStyle.value) === null || _a === void 0 ? void 0 : _a.right)) : toPX((_b = prevStyle.value) === null || _b === void 0 ? void 0 : _b.left); }); const thumbActive = computed(() => { var _a, _b; return props.direction === 'rtl' ? toPX(-((_a = nextStyle.value) === null || _a === void 0 ? void 0 : _a.right)) : toPX((_b = nextStyle.value) === null || _b === void 0 ? void 0 : _b.left); }); const mergedStyle = computed(() => { var _a, _b; return { '--thumb-start-left': thumbStart.value, '--thumb-start-width': toPX((_a = prevStyle.value) === null || _a === void 0 ? void 0 : _a.width), '--thumb-active-left': thumbActive.value, '--thumb-active-width': toPX((_b = nextStyle.value) === null || _b === void 0 ? void 0 : _b.width) }; }); // 监听过渡结束 const onTransitionEnd = e => { if (e.propertyName !== 'transform' && e.propertyName !== 'width') return; if (thumbRef.value) { thumbRef.value.removeEventListener('transitionend', onTransitionEnd); } prevStyle.value = null; nextStyle.value = null; emit('motionEnd'); }; watch(() => props.value, (value, prevValue) => { const prev = findValueElement(prevValue); const next = findValueElement(value); const calcPrevStyle = calcThumbStyle(prev); const calcNextStyle = calcThumbStyle(next); prevStyle.value = calcPrevStyle; nextStyle.value = calcNextStyle; nextTick(() => { if (prev && next) { if (thumbRef.value) { // 使用 CSS 变量设置初始位置 thumbRef.value.style.transform = `translateX(var(--thumb-start-left))`; thumbRef.value.style.width = `var(--thumb-start-width)`; // 强制重排 thumbRef.value.offsetHeight; thumbRef.value.style.transform = `translateX(var(--thumb-active-left))`; thumbRef.value.style.width = `var(--thumb-active-width)`; emit('motionStart'); thumbRef.value.addEventListener('transitionend', onTransitionEnd); } } else { emit('motionEnd'); } }); }, { flush: 'post' }); // 清理事件监听 onBeforeUnmount(() => { var _a; (_a = thumbRef.value) === null || _a === void 0 ? void 0 : _a.removeEventListener('transitionend', onTransitionEnd); }); return () => { // It's little ugly which should be refactor when @umi/test update to latest jsdom const motionProps = { ref: thumbRef, style: mergedStyle.value, class: [`${props.prefixCls}-thumb`] }; if (process.env.NODE_ENV === 'test') { motionProps['data-test-style'] = JSON.stringify(mergedStyle.value); } return !prevStyle.value || !nextStyle.value ? null : _createVNode("div", motionProps, null); }; } }); export default MotionThumb;