/
alpha-slider.vue
181 lines (161 loc) Β· 3.99 KB
/
alpha-slider.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<template>
<div class="el-color-alpha-slider" :class="{ 'is-vertical': vertical }">
<div
ref="bar"
class="el-color-alpha-slider__bar"
:style="{
background,
}"
@click="handleClick"
/>
<div
ref="thumb"
class="el-color-alpha-slider__thumb"
:style="{
left: thumbLeft + 'px',
top: thumbTop + 'px',
}"
/>
</div>
</template>
<script lang="ts">
// @ts-nocheck
import {
defineComponent,
getCurrentInstance,
onMounted,
ref,
shallowRef,
watch,
} from 'vue'
import { getClientXY } from '@element-plus/utils'
import draggable from '../draggable'
import type { PropType } from 'vue'
import type { Nullable } from '@element-plus/utils'
import type Color from '../color'
export default defineComponent({
name: 'ElColorAlphaSlider',
props: {
color: {
type: Object as PropType<Color>,
required: true,
},
vertical: {
type: Boolean,
default: false,
},
},
setup(props) {
const instance = getCurrentInstance()
// ref
const thumb = shallowRef<Nullable<HTMLElement>>(null)
const bar = shallowRef<Nullable<HTMLElement>>(null)
// data
const thumbLeft = ref(0)
const thumbTop = ref(0)
const background = ref<Nullable<string>>(null)
watch(
() => props.color.get('alpha'),
() => {
update()
}
)
watch(
() => props.color.value,
() => {
update()
}
)
//methods
function getThumbLeft() {
if (props.vertical) return 0
const el = instance.vnode.el
const alpha = props.color.get('alpha')
if (!el) return 0
return Math.round(
(alpha * (el.offsetWidth - thumb.value.offsetWidth / 2)) / 100
)
}
function getThumbTop() {
const el = instance.vnode.el
if (!props.vertical) return 0
const alpha = props.color.get('alpha')
if (!el) return 0
return Math.round(
(alpha * (el.offsetHeight - thumb.value.offsetHeight / 2)) / 100
)
}
function getBackground() {
if (props.color && props.color.value) {
const { r, g, b } = props.color.toRgb()
return `linear-gradient(to right, rgba(${r}, ${g}, ${b}, 0) 0%, rgba(${r}, ${g}, ${b}, 1) 100%)`
}
return null
}
function handleClick(event: Event) {
const target = event.target
if (target !== thumb.value) {
handleDrag(event)
}
}
function handleDrag(event) {
const el = instance.vnode.el as HTMLElement
const rect = el.getBoundingClientRect()
const { clientX, clientY } = getClientXY(event)
if (!props.vertical) {
let left = clientX - rect.left
left = Math.max(thumb.value.offsetWidth / 2, left)
left = Math.min(left, rect.width - thumb.value.offsetWidth / 2)
props.color.set(
'alpha',
Math.round(
((left - thumb.value.offsetWidth / 2) /
(rect.width - thumb.value.offsetWidth)) *
100
)
)
} else {
let top = clientY - rect.top
top = Math.max(thumb.value.offsetHeight / 2, top)
top = Math.min(top, rect.height - thumb.value.offsetHeight / 2)
props.color.set(
'alpha',
Math.round(
((top - thumb.value.offsetHeight / 2) /
(rect.height - thumb.value.offsetHeight)) *
100
)
)
}
}
function update() {
thumbLeft.value = getThumbLeft()
thumbTop.value = getThumbTop()
background.value = getBackground()
}
// mounded
onMounted(() => {
const dragConfig = {
drag: (event) => {
handleDrag(event)
},
end: (event) => {
handleDrag(event)
},
}
draggable(bar.value, dragConfig)
draggable(thumb.value, dragConfig)
update()
})
return {
thumb,
bar,
thumbLeft,
thumbTop,
background,
handleClick,
update,
}
},
})
</script>