1
1
<template >
2
- <button ref =" mBtn" :disabled =" handleLoadingState || (disabled as boolean)"
3
- :class = " [ !isCustomColor ? handleColor : 'm-btn' , handleSize, handleShape, handleTransparency, handleTextColor ] "
4
- :style = " isCustomColor ? (handleColor as StyleValue) : '' " >
5
- <div :class =" { 'm-loading': handleLoadingState }" class =" m-btn-content" >
2
+ <button ref =" mBtn" :disabled =" handleLoadingState || (disabled as boolean)" :class = " [ 'm-btn',
3
+ handleCustomColors, handleColor, handleSize, handleShape, handleTransparency, handleTextColor,
4
+ ] " >
5
+ <div :class =" { 'm-loading': handleLoadingState }" class =" m-btn-content" >
6
6
<template v-if =" props .text " > {{ props.text }} </template >
7
7
<slot v-else />
8
8
</div >
14
14
</template >
15
15
16
16
<script lang="ts" setup>
17
- import { ComputedRef , StyleValue , computed , ref } from ' vue' ;
17
+ import { StyleValue , computed , ref } from ' vue' ;
18
18
import { createLighterShades , dynamicSVG } from ' @/utils' ;
19
19
import { btnSizes , btnColors } from ' ./props' ;
20
20
import { shapes , textColors } from ' @/common' ;
21
21
22
22
const mBtn = ref <HTMLButtonElement | null >(null );
23
- const shades = ref <string []>([]);
24
- const mousedown = ref (false );
25
23
26
24
// TODO: Add tab tab-indexing
27
25
const props = defineProps ({
@@ -63,65 +61,38 @@ const props = defineProps({
63
61
});
64
62
65
63
// Prop Handling
66
- const handleTextColor= computed <string >(() => {
64
+ const handleTextColor = computed <string >(() => {
65
+ mBtn .value ?.style .removeProperty (' --m-btn-text-color' );
66
+
67
67
if (! props .textColor )
68
68
return ' ' ;
69
+ if (props .textColor .startsWith (' #' ) || props .textColor .startsWith (' rgb' ))
70
+ return mBtn .value ?.style .setProperty (' --m-btn-text-color' , props .textColor );
69
71
return (textColors as any )[ props .textColor ];
70
72
});
71
73
72
74
const handleColor = computed <string | StyleValue >(() => {
73
- removeEventListeners ();
75
+ mBtn .value ?.style .removeProperty (' --m-btn-bg-color' );
76
+ mBtn .value ?.style .removeProperty (' --m-btn-bg-color-hover' );
77
+ mBtn .value ?.style .removeProperty (' --m-btn-bg-color-active' );
78
+
74
79
if (! props .color )
75
80
return btnColors .default + ' ' + btnSizes .md ;
76
- if (props .color .startsWith (' #' ) || props .color .startsWith (' rgb' ) || props .color .startsWith (' rgba' )) {
77
- shades .value = createLighterShades (props .color );
78
- addEventListeners ();
81
+ if (props .color .startsWith (' #' ) || props .color .startsWith (' rgb' )) {
82
+ const shades = createLighterShades (props .color );
79
83
80
- return { backgroundColor: props .color };
84
+ mBtn .value ?.style .setProperty (' --m-btn-bg-color' , props .color );
85
+ mBtn .value ?.style .setProperty (' --m-btn-bg-color-hover' , shades [ 0 ]);
86
+ mBtn .value ?.style .setProperty (' --m-btn-bg-color-active' , shades [ 1 ]);
87
+ return ;
81
88
}
82
89
return (btnColors as any )[ props .color ];
83
90
});
84
91
85
- function removeEventListeners() {
86
- mBtn .value ?.removeEventListener (' mouseenter' , onMouseEnter );
87
- mBtn .value ?.removeEventListener (' mouseleave' , onMouseLeave );
88
-
89
- mBtn .value ?.removeEventListener (' mousedown' , onMouseDown );
90
- mBtn .value ?.removeEventListener (' mouseup' , onMouseUp );
91
-
92
- mBtn .value ?.removeEventListener (' keydown' , onKeyDown );
93
-
94
- mBtn .value ?.removeEventListener (' focusin' , onFocusIn );
95
- mBtn .value ?.removeEventListener (' blur' , onBlur );
96
- }
97
- function addEventListeners() {
98
- mBtn .value ?.addEventListener (' mouseenter' , onMouseEnter );
99
- mBtn .value ?.addEventListener (' mouseleave' , onMouseLeave );
100
-
101
- mBtn .value ?.addEventListener (' mousedown' , onMouseDown );
102
- mBtn .value ?.addEventListener (' mouseup' , onMouseUp );
103
-
104
- mBtn .value ?.addEventListener (' keydown' , onKeyDown );
105
-
106
- mBtn .value ?.addEventListener (' focusin' , onFocusIn );
107
- mBtn .value ?.addEventListener (' blur' , onBlur );
108
- }
109
- const onMouseEnter = () => mBtn .value ! .style .backgroundColor = shades .value [ 0 ];
110
- const onMouseLeave = () => mBtn .value ! .style .backgroundColor = props .color ;
111
- const onMouseDown = () => { mBtn .value ! .style .backgroundColor = shades .value [ 1 ]; mousedown .value = true ; };
112
- const onMouseUp = () => mBtn .value ! .style .backgroundColor = shades .value [ 0 ];
113
-
114
- const onKeyDown = (e : KeyboardEvent ) => {
115
- if (e .key === ' Shift' )
116
- mBtn .value ! .style .boxShadow = ` 0px 0px 0px white, 0px 0px 0px 1.5px ${props .color } ` ;
117
- }
118
-
119
- const onFocusIn = () => {
120
- if (! mousedown .value )
121
- mBtn .value ! .style .boxShadow = ` 0px 0px 0px white, 0px 0px 0px 1.5px ${props .color } ` ;
122
- mousedown .value = false ;
123
- };
124
- const onBlur = () => { mBtn .value ! .style .boxShadow = ' ' ; mousedown .value = false ; };
92
+ const handleCustomColors = computed <string | void >(() => {
93
+ if (props .color .startsWith (' #' ) || props .color .startsWith (' rgb' ) || props .textColor .startsWith (' #' ) || props .textColor .startsWith (' rgb' ))
94
+ return ' m-btn-custom-colors' ;
95
+ });
125
96
126
97
const handleSize = computed (() => {
127
98
if (! props .size )
@@ -148,13 +119,4 @@ const handleTransparency = computed(() => {
148
119
return ' m-btn-transparent' ;
149
120
});
150
121
151
- const isCustomColor: ComputedRef <boolean > = computed (() => {
152
- if (typeof handleColor .value == ' string' )
153
- return false ;
154
-
155
- return typeof handleColor .value == ' object'
156
- && (handleColor as any ).value ?.backgroundColor ?.startsWith (' #' )
157
- || (handleColor as any ).value ?.backgroundColor ?.startsWith (' rgb' );
158
- })
159
-
160
122
</script >
0 commit comments