UNPKG

element-plus

Version:

A Component Library for Vue 3

1 lines 7.01 kB
{"version":3,"file":"item.mjs","sources":["../../../../../../packages/components/carousel/src/item.vue"],"sourcesContent":["<template>\n <div\n v-show=\"data.ready\"\n :class=\"[\n ns.e('item'),\n ns.is('active', data.active),\n ns.is('in-stage', data.inStage),\n ns.is('hover', data.hover),\n ns.is('animating', data.animating),\n { [ns.em('item', 'card')]: type === 'card' },\n ]\"\n :style=\"itemStyle\"\n @click=\"handleItemClick\"\n >\n <div\n v-if=\"type === 'card'\"\n v-show=\"!data.active\"\n :class=\"ns.e('mask')\"\n ></div>\n <slot></slot>\n </div>\n</template>\n<script lang=\"ts\">\nimport {\n defineComponent,\n reactive,\n onMounted,\n inject,\n computed,\n toRefs,\n getCurrentInstance,\n onUnmounted,\n} from 'vue'\nimport { debugWarn } from '@element-plus/utils'\nimport { useNamespace } from '@element-plus/hooks'\nimport type { CSSProperties } from 'vue'\nimport type { InjectCarouselScope, ICarouselItemProps } from './carousel'\n\nconst CARD_SCALE = 0.83\nexport default defineComponent({\n name: 'ElCarouselItem',\n props: {\n name: { type: String, default: '' },\n label: {\n type: [String, Number],\n default: '',\n },\n },\n setup(props: ICarouselItemProps) {\n const ns = useNamespace('carousel')\n // instance\n const instance = getCurrentInstance()\n\n // data\n const data = reactive({\n hover: false,\n translate: 0,\n scale: 1,\n active: false,\n ready: false,\n inStage: false,\n animating: false,\n })\n\n // inject\n const injectCarouselScope: InjectCarouselScope = inject(\n 'injectCarouselScope'\n )\n\n // computed\n const parentDirection = computed(() => {\n return injectCarouselScope.direction\n })\n\n const itemStyle = computed(() => {\n const translateType =\n parentDirection.value === 'vertical' ? 'translateY' : 'translateX'\n const value = `${translateType}(${data.translate}px) scale(${data.scale})`\n const style: CSSProperties = {\n transform: value,\n }\n return style\n })\n\n // methods\n\n function processIndex(index, activeIndex, length) {\n if (activeIndex === 0 && index === length - 1) {\n return -1\n } else if (activeIndex === length - 1 && index === 0) {\n return length\n } else if (index < activeIndex - 1 && activeIndex - index >= length / 2) {\n return length + 1\n } else if (index > activeIndex + 1 && index - activeIndex >= length / 2) {\n return -2\n }\n return index\n }\n\n function calcCardTranslate(index, activeIndex) {\n const parentWidth = injectCarouselScope.root.value?.offsetWidth || 0\n if (data.inStage) {\n return (\n (parentWidth * ((2 - CARD_SCALE) * (index - activeIndex) + 1)) / 4\n )\n } else if (index < activeIndex) {\n return (-(1 + CARD_SCALE) * parentWidth) / 4\n } else {\n return ((3 + CARD_SCALE) * parentWidth) / 4\n }\n }\n\n function calcTranslate(index, activeIndex, isVertical) {\n const distance =\n (isVertical\n ? injectCarouselScope.root.value?.offsetHeight\n : injectCarouselScope.root.value?.offsetWidth) || 0\n return distance * (index - activeIndex)\n }\n\n const translateItem = (\n index: number,\n activeIndex: number,\n oldIndex: number\n ) => {\n const parentType = injectCarouselScope.type\n const length = injectCarouselScope.items.value.length\n if (parentType !== 'card' && oldIndex !== undefined) {\n data.animating = index === activeIndex || index === oldIndex\n }\n if (index !== activeIndex && length > 2 && injectCarouselScope.loop) {\n index = processIndex(index, activeIndex, length)\n }\n if (parentType === 'card') {\n if (parentDirection.value === 'vertical') {\n debugWarn(\n 'Carousel',\n 'vertical direction is not supported in card mode'\n )\n }\n data.inStage = Math.round(Math.abs(index - activeIndex)) <= 1\n data.active = index === activeIndex\n data.translate = calcCardTranslate(index, activeIndex)\n data.scale = data.active ? 1 : CARD_SCALE\n } else {\n data.active = index === activeIndex\n const isVertical = parentDirection.value === 'vertical'\n data.translate = calcTranslate(index, activeIndex, isVertical)\n }\n data.ready = true\n }\n\n function handleItemClick() {\n if (injectCarouselScope && injectCarouselScope.type === 'card') {\n const index = injectCarouselScope.items.value\n .map((d) => d.uid)\n .indexOf(instance.uid)\n injectCarouselScope.setActiveItem(index)\n }\n }\n\n // lifecycle\n onMounted(() => {\n if (injectCarouselScope.addItem) {\n injectCarouselScope.addItem({\n uid: instance.uid,\n ...props,\n ...toRefs(data),\n translateItem,\n })\n }\n })\n\n onUnmounted(() => {\n if (injectCarouselScope.removeItem) {\n injectCarouselScope.removeItem(instance.uid)\n }\n })\n\n return {\n data,\n itemStyle,\n translateItem,\n type: injectCarouselScope.type,\n handleItemClick,\n ns,\n }\n },\n})\n</script>\n"],"names":[],"mappings":";;;;;;;AAsCA,MAAM,aAAa;AACnB,MAAK,YAAa,gBAAa;AAAA,EAC7B,MAAM;AAAA,EACN,OAAO;AAAA,IACL,MAAM,EAAE,MAAM,QAAQ,SAAS;AAAA,IAC/B,OAAO;AAAA,MACL,MAAM,CAAC,QAAQ;AAAA,MACf,SAAS;AAAA;AAAA;AAAA,EAGb,MAAM,OAA2B;AAC/B,UAAM,KAAK,aAAa;AAExB,UAAM,WAAW;AAGjB,UAAM,OAAO,SAAS;AAAA,MACpB,OAAO;AAAA,MACP,WAAW;AAAA,MACX,OAAO;AAAA,MACP,QAAQ;AAAA,MACR,OAAO;AAAA,MACP,SAAS;AAAA,MACT,WAAW;AAAA;AAIb,UAAM,sBAA2C,OAC/C;AAIF,UAAM,kBAAkB,SAAS,MAAM;AACrC,aAAO,oBAAoB;AAAA;AAG7B,UAAM,YAAY,SAAS,MAAM;AAC/B,YAAM,gBACJ,gBAAgB,UAAU,aAAa,eAAe;AACxD,YAAM,QAAQ,GAAG,iBAAiB,KAAK,sBAAsB,KAAK;AAClE,YAAM,QAAuB;AAAA,QAC3B,WAAW;AAAA;AAEb,aAAO;AAAA;AAKT,0BAAsB,OAAO,aAAa,QAAQ;AAChD,UAAI,gBAAgB,KAAK,UAAU,SAAS,GAAG;AAC7C,eAAO;AAAA,iBACE,gBAAgB,SAAS,KAAK,UAAU,GAAG;AACpD,eAAO;AAAA,iBACE,QAAQ,cAAc,KAAK,cAAc,SAAS,SAAS,GAAG;AACvE,eAAO,SAAS;AAAA,iBACP,QAAQ,cAAc,KAAK,QAAQ,eAAe,SAAS,GAAG;AACvE,eAAO;AAAA;AAET,aAAO;AAAA;AAGT,+BAA2B,OAAO,aAAa;AAC7C,YAAM;AACN;AAC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