element-plus
Version:
A Component Library for Vue 3
1 lines • 4.58 kB
Source Map (JSON)
{"version":3,"file":"split-bar.mjs","names":[],"sources":["../../../../../../packages/components/splitter/src/split-bar.vue"],"sourcesContent":["<script setup lang=\"ts\">\nimport { computed, ref } from 'vue'\nimport {\n ArrowDown,\n ArrowLeft,\n ArrowRight,\n ArrowUp,\n} from '@element-plus/icons-vue'\nimport { useNamespace } from '@element-plus/hooks'\n\nconst ns = useNamespace('splitter-bar')\n\ndefineOptions({\n name: 'ElSplitterBar',\n})\n\nconst props = defineProps({\n index: {\n type: Number,\n required: true,\n },\n layout: {\n type: String,\n values: ['horizontal', 'vertical'] as const,\n default: 'horizontal',\n },\n resizable: {\n type: Boolean,\n default: true,\n },\n lazy: Boolean,\n startCollapsible: Boolean,\n endCollapsible: Boolean,\n})\n\nconst emit = defineEmits(['moveStart', 'moving', 'moveEnd', 'collapse'])\n\nconst isHorizontal = computed(() => props.layout === 'horizontal')\n\nconst barWrapStyles = computed(() => {\n if (isHorizontal.value) {\n return { width: 0 }\n }\n return { height: 0 }\n})\n\nconst draggerStyles = computed(() => {\n return {\n width: isHorizontal.value ? '16px' : '100%',\n height: isHorizontal.value ? '100%' : '16px',\n cursor: !props.resizable\n ? 'auto'\n : isHorizontal.value\n ? 'ew-resize'\n : 'ns-resize',\n touchAction: 'none',\n }\n})\n\nconst draggerPseudoClass = computed(() => {\n const prefix = ns.e('dragger')\n return {\n [`${prefix}-horizontal`]: isHorizontal.value,\n [`${prefix}-vertical`]: !isHorizontal.value,\n [`${prefix}-active`]: !!startPos.value,\n }\n})\n\nconst startPos = ref<[x: number, y: number] | null>(null)\n\n// Start dragging\nconst onMousedown = (e: MouseEvent) => {\n if (!props.resizable) return\n startPos.value = [e.pageX, e.pageY]\n emit('moveStart', props.index)\n window.addEventListener('mouseup', onMouseUp)\n window.addEventListener('mousemove', onMouseMove)\n}\n\nconst onTouchStart = (e: TouchEvent) => {\n if (props.resizable && e.touches.length === 1) {\n e.preventDefault()\n const touch = e.touches[0]\n startPos.value = [touch.pageX, touch.pageY]\n emit('moveStart', props.index)\n window.addEventListener('touchend', onTouchEnd)\n window.addEventListener('touchmove', onTouchMove)\n }\n}\n\n// During dragging\nconst onMouseMove = (e: MouseEvent) => {\n const { pageX, pageY } = e\n const offsetX = pageX - startPos.value![0]\n const offsetY = pageY - startPos.value![1]\n const offset = isHorizontal.value ? offsetX : offsetY\n emit('moving', props.index, offset)\n}\n\nconst onTouchMove = (e: TouchEvent) => {\n if (e.touches.length === 1) {\n e.preventDefault()\n const touch = e.touches[0]\n const offsetX = touch.pageX - startPos.value![0]\n const offsetY = touch.pageY - startPos.value![1]\n const offset = isHorizontal.value ? offsetX : offsetY\n emit('moving', props.index, offset)\n }\n}\n\n// End dragging\nconst onMouseUp = () => {\n startPos.value = null\n window.removeEventListener('mouseup', onMouseUp)\n window.removeEventListener('mousemove', onMouseMove)\n emit('moveEnd', props.index)\n}\n\nconst onTouchEnd = () => {\n startPos.value = null\n window.removeEventListener('touchend', onTouchEnd)\n window.removeEventListener('touchmove', onTouchMove)\n emit('moveEnd', props.index)\n}\n\nconst StartIcon = computed(() => (isHorizontal.value ? ArrowLeft : ArrowUp))\nconst EndIcon = computed(() => (isHorizontal.value ? ArrowRight : ArrowDown))\n</script>\n\n<template>\n <div :class=\"[ns.b()]\" :style=\"barWrapStyles\">\n <div\n v-if=\"startCollapsible\"\n :class=\"[ns.e('collapse-icon'), ns.e(`${layout}-collapse-icon-start`)]\"\n @click=\"emit('collapse', index, 'start')\"\n >\n <slot name=\"start-collapsible\">\n <component :is=\"StartIcon\" style=\"width: 12px; height: 12px\" />\n </slot>\n </div>\n\n <div\n :class=\"[\n ns.e('dragger'),\n draggerPseudoClass,\n ns.is('disabled', !resizable),\n ns.is('lazy', resizable && lazy),\n ]\"\n :style=\"draggerStyles\"\n @mousedown=\"onMousedown\"\n @touchstart=\"onTouchStart\"\n />\n <div\n v-if=\"endCollapsible\"\n :class=\"[ns.e('collapse-icon'), ns.e(`${layout}-collapse-icon-end`)]\"\n @click=\"emit('collapse', index, 'end')\"\n >\n <slot name=\"end-collapsible\">\n <component :is=\"EndIcon\" style=\"width: 12px; height: 12px\" />\n </slot>\n </div>\n </div>\n</template>\n"],"mappings":""}