UNPKG

@extclp/vexip-ui

Version:

A Vue 3 UI library, Highly customizability, full TypeScript, performance pretty good

1 lines 3.32 kB
{"version":3,"file":"color-hue.vue.mjs","sources":["../../../components/color-picker/color-hue.vue"],"sourcesContent":["<template>\n <div\n ref=\"wrapper\"\n :class=\"nh.be('hue')\"\n tabindex=\"-1\"\n role=\"group\"\n >\n <div :class=\"nh.be('hue-handler')\" :style=\"{ left: `${currentLeft}%` }\"></div>\n </div>\n</template>\n\n<script lang=\"ts\">\nimport { defineComponent, ref, watch } from 'vue'\n\nimport { useNameHelper } from '@vexip-ui/config'\nimport { useModifier, useMoving } from '@vexip-ui/hooks'\nimport { boundRange, toFixed } from '@vexip-ui/utils'\n\nexport default defineComponent({\n name: 'ColorHue',\n props: {\n hue: {\n type: Number,\n default: 0,\n validator: (value: number) => {\n return value >= 0 && value <= 360\n }\n }\n },\n emits: ['edit-start', 'edit-end', 'change'],\n setup(props, { emit }) {\n const currentLeft = ref(props.hue * 100)\n\n let prevLeft = currentLeft.value\n let widthLimit: number\n let leftStartAt: number\n\n const { target: wrapper } = useModifier({\n passive: false,\n onKeyDown: (event, modifier) => {\n if (modifier.left || modifier.right) {\n event.preventDefault()\n\n const step = event.ctrlKey ? 10 : event.altKey ? 0.5 : 2\n const delta = step * (modifier.left ? -1 : 1)\n\n currentLeft.value += delta\n\n verifyPosition()\n prevLeft = currentLeft.value\n handleChange()\n }\n }\n })\n\n const { moving: editing } = useMoving({\n target: wrapper,\n onStart: (state, event) => {\n if (!wrapper.value || event.button > 0) {\n return false\n }\n\n const rect = wrapper.value.getBoundingClientRect()\n const { left, width } = rect\n\n widthLimit = width\n currentLeft.value = ((leftStartAt = state.clientX - left) / width) * 100\n\n verifyPosition()\n emit('edit-start')\n\n if (Math.abs(currentLeft.value - prevLeft) >= 0.01) {\n prevLeft = currentLeft.value\n handleChange()\n }\n },\n onMove: state => {\n currentLeft.value = ((leftStartAt + state.deltaX) / widthLimit) * 100\n\n verifyPosition()\n handleChange()\n },\n onEnd: () => {\n emit('edit-end')\n }\n })\n\n watch(\n () => props.hue,\n value => {\n currentLeft.value = (value / 360) * 100\n verifyPosition()\n },\n { immediate: true }\n )\n\n function verifyPosition() {\n currentLeft.value = toFixed(boundRange(currentLeft.value, 0, 100), 3)\n }\n\n function handleChange() {\n emit('change', (currentLeft.value / 100) * 360)\n }\n\n return {\n nh: useNameHelper('color-picker'),\n currentLeft,\n editing,\n\n wrapper\n }\n }\n})\n</script>\n"],"names":["_sfc_render","_ctx","_cache","$props","$setup","$data","$options","_openBlock","_createElementBlock","_normalizeClass","_createElementVNode"],"mappings":";;;SAEQA,EAASC,GAAAC,GAAAC,GAAAC,GAAAC,GAAAC,GAAA;AACP,SAAAC,EAAA,GAAOC,EAAA,OAAA;AAAA,IACb,KAAA;AAAA,IACA,OAAKC,EAAOR,EAAA,GAAA,GAAA,KAAA,CAAA;AAAA,IAAA,UAAA;AAAA,IAEZ,MAAA;AAAA,EAAA,GAAA;AAAA,IAPJS,EAAA,OAAA;AAAA,MAAA,OAAAD,EAAAR,EAAA,GAAA,GAAA,aAAA,CAAA;AAAA;;;;;"}