11<template >
2- <div class =" color-input user" ref =" root" :style =" cssVars" >
32 <div
4- :class =" ['box', { active, disabled } ]"
3+ :class =" [$attrs.class, bem( 'box', { disabled, active }) ]"
54 @click.stop =" pickStart"
6- ref =" box "
5+ ref =" root "
76 >
8- <div class =" inner transparent" >
9- <div class =" color" :style =" boxColorStyles" ></div >
7+ <div
8+ :class =" bem('box-inner', { active })"
9+ :style =" transparentPatternBg"
10+ >
11+ <div :class =" bem('box-color')" :style =" boxColorStyles" ></div >
1012 </div >
1113 </div >
1214
13- <Teleport : to =" parent " v-if = " parent " >
15+ <Teleport to =" body " >
1416 <transition :name =" transition" >
1517 <color-picker
16- class =" picker-popup user "
17- :color =" this. color"
18+ : class =" $attrs.class "
19+ :color =" color"
1820 :position =" processedPosition"
1921 :disable-alpha =" processedDisableAlpha"
2022 :boxRect =" boxRect"
3739 />
3840 </transition >
3941 </Teleport >
40- </div >
4142</template >
4243
4344<script >
4445import { defineComponent } from " vue" ;
4546import ColorPicker from " ./components/color-picker.vue" ;
4647
4748import tinycolor from " tinycolor2" ;
49+ import { bem } from ' ./bem'
4850
4951import transparentPattern from " ./assets/transparent-pattern.svg" ;
5052
@@ -56,8 +58,9 @@ const isSameNodeRecursive = (elA, elB) => {
5658 return false ;
5759};
5860
59- export default /* #__PURE__ */ defineComponent ({
61+ export default defineComponent ({
6062 name: " ColorInput" ,
63+ expose: [' pickStart' , ' pickEnd' , ' color' , ' active' ],
6164 props: {
6265 modelValue: [String , Object ],
6366 position: {
@@ -66,7 +69,7 @@ export default /*#__PURE__*/ defineComponent({
6669 },
6770 transition: {
6871 type: String ,
69- default: " picker-popup " ,
72+ default: ' color-input__popup- ' ,
7073 },
7174 disableAlpha: {
7275 type: Boolean ,
@@ -81,7 +84,6 @@ export default /*#__PURE__*/ defineComponent({
8184 default: false ,
8285 },
8386 format: String ,
84- appendTo: [String , HTMLElement ],
8587 },
8688 emits: [
8789 " mounted" ,
@@ -108,7 +110,6 @@ export default /*#__PURE__*/ defineComponent({
108110 active: false ,
109111 ready: false ,
110112 hidePicker: false ,
111- parent: null ,
112113 boxRect: {},
113114 innerBoxRect: {},
114115 textInputsFormat: " rgb" ,
@@ -117,6 +118,9 @@ export default /*#__PURE__*/ defineComponent({
117118 };
118119 },
119120 computed: {
121+ transparentPatternBg () {
122+ return { backgroundImage: ` url(${ transparentPattern} )` }
123+ },
120124 boxColorStyles () {
121125 return {
122126 background: this .color .toRgbString (),
@@ -152,6 +156,13 @@ export default /*#__PURE__*/ defineComponent({
152156 position = position .split (" " );
153157 position[1 ] = position[1 ] || " center" ;
154158
159+ // reorder [Y, X]
160+ if (
161+ [' top' ,' bottom' ].includes (position[0 ]) ||
162+ [' right' , ' left' ].includes (position[1 ])
163+ )
164+ return position; // already correct order
165+ position .reverse ();
155166 return position;
156167 },
157168 processedFormat () {
@@ -212,6 +223,7 @@ export default /*#__PURE__*/ defineComponent({
212223 },
213224 },
214225 methods: {
226+ bem,
215227 pickStart (e ) {
216228 if (this .active || this .disabled ) return ;
217229 this .getBoxRect ();
@@ -222,7 +234,8 @@ export default /*#__PURE__*/ defineComponent({
222234 this .hidePicker = true ;
223235 this .$refs .picker .init ();
224236
225- document .body .addEventListener (" pointerdown" , this .pickEnd );
237+ document .addEventListener (" pointerdown" , this .pickEnd );
238+ window .addEventListener (' resize' , this .getBoxRect );
226239 this .$emit (" pickStart" );
227240 },
228241 pickEnd (e ) {
@@ -231,7 +244,8 @@ export default /*#__PURE__*/ defineComponent({
231244 (e && isSameNodeRecursive (e .target , this .$refs .picker .$refs .pickerRoot ))
232245 )
233246 return ;
234- document .body .removeEventListener (" pointerdown" , this .pickEnd );
247+ document .removeEventListener (" pointerdown" , this .pickEnd );
248+ window .removeEventListener (' resize' , this .getBoxRect )
235249 this .active = false ;
236250 this .$emit (" pickEnd" );
237251 },
@@ -280,30 +294,14 @@ export default /*#__PURE__*/ defineComponent({
280294 }
281295 this .$emit (" update:modelValue" , this .output );
282296 },
283- getParent () {
284- let parent;
285- if (this .appendTo ) {
286- if (typeof this .appendTo === " string" ) {
287- parent = document .querySelector (this .appendTo );
288- } else {
289- parent = this .appendTo ;
290- }
291- }
292-
293- this .parent = parent || this .$refs .root ;
294- },
295297 getBoxRect () {
296- this .boxRect = this .parent .getBoundingClientRect ();
298+ this .boxRect = this .$refs . root .getBoundingClientRect ();
297299 },
298300 },
299301 created () {
300302 this .init ();
301- this .cssVars = {
302- " --transparent-pattern" : " url(" + transparentPattern + " )" ,
303- };
304303 },
305304 mounted () {
306- this .getParent ();
307305 this .$emit (" mounted" );
308306 },
309307 beforeUnmount () {
@@ -363,58 +361,35 @@ export default /*#__PURE__*/ defineComponent({
363361 position : relative ;
364362 display : inline-block ;
365363
366- .box {
364+ & __box {
367365 width : 40px ;
368366 height : 40px ;
369367 cursor : pointer ;
370368 border-radius : 20% ;
371369 overflow : hidden ;
372- transition : all 0.2 s , background-color 0.05s 0.15s ;
373- . inner {
370+ transition : background-color 0.05s 0.15s ;
371+ & - inner {
374372 border-radius : inherit ;
375373 overflow : hidden ;
376- transition : inherit ;
377- }
378- .transparent {
374+ transition : transform .2s ;
379375 @extend %fill-100 ;
380- background-image : var (--transparent-pattern );
381376 background-color : #fff ;
382377 background-size : 100% ;
378+ & --active {
379+ transform : scale (0.9 );
380+ }
383381 }
384- . color {
382+ & - color {
385383 @extend %fill-100 ;
386384 }
387- & . active {
385+ & -- active {
388386 background : #fbfbfb ;
389387 transition : all 0.2s , background-color 0.05s ;
390- .inner {
391- transform : scale (0.9 );
392- }
393388 }
394- & . disabled {
389+ & -- disabled {
395390 cursor : not-allowed ;
396391 }
397392 }
398393}
399- .picker-popup {
400- position : absolute ;
401- z-index : 9999 ;
402- width : auto ;
403- min-width : 280px ;
404- background-color : #fbfbfb ;
405- box-shadow : 0px 5px 10px rgba (15 , 15 , 15 , 0.4 );
406- margin : 10px ;
407- user-select : none ;
408- color : #0f0f0f ;
409- }
410394
411- .picker-popup-enter-from ,
412- .picker-popup-leave-to {
413- transform : translateY (-10px );
414- opacity : 0 ;
415- }
416- .picker-popup-enter-active ,
417- .picker-popup-leave-active {
418- transition : transform 0.3s , opacity 0.3s ;
419- }
420395 </style >
0 commit comments