11<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" >
66 <template v-if =" props .text " > {{ props.text }} </template >
77 <slot v-else />
88 </div >
1414</template >
1515
1616<script lang="ts" setup>
17- import { ComputedRef , StyleValue , computed , ref } from ' vue' ;
17+ import { StyleValue , computed , ref } from ' vue' ;
1818import { createLighterShades , dynamicSVG } from ' @/utils' ;
1919import { btnSizes , btnColors } from ' ./props' ;
2020import { shapes , textColors } from ' @/common' ;
2121
2222const mBtn = ref <HTMLButtonElement | null >(null );
23- const shades = ref <string []>([]);
24- const mousedown = ref (false );
2523
2624// TODO: Add tab tab-indexing
2725const props = defineProps ({
@@ -63,65 +61,38 @@ const props = defineProps({
6361});
6462
6563// Prop Handling
66- const handleTextColor= computed <string >(() => {
64+ const handleTextColor = computed <string >(() => {
65+ mBtn .value ?.style .removeProperty (' --m-btn-text-color' );
66+
6767 if (! props .textColor )
6868 return ' ' ;
69+ if (props .textColor .startsWith (' #' ) || props .textColor .startsWith (' rgb' ))
70+ return mBtn .value ?.style .setProperty (' --m-btn-text-color' , props .textColor );
6971 return (textColors as any )[ props .textColor ];
7072});
7173
7274const 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+
7479 if (! props .color )
7580 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 );
7983
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 ;
8188 }
8289 return (btnColors as any )[ props .color ];
8390});
8491
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+ });
12596
12697const handleSize = computed (() => {
12798 if (! props .size )
@@ -148,13 +119,4 @@ const handleTransparency = computed(() => {
148119 return ' m-btn-transparent' ;
149120});
150121
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-
160122 </script >
0 commit comments