3
3
import type { HTMLButtonAttributes } from ' svelte/elements'
4
4
5
5
interface IButtonProps extends HTMLButtonAttributes {
6
- variant? : ' solid' | ' soft' | ' danger' | ' danger-soft'
6
+ variant? : ' solid' | ' soft' | ' danger' | ' danger-soft' | ' white '
7
7
isLoading? : boolean
8
- cb ? : () => Promise <void >
8
+ callback ? : () => Promise <void >
9
9
blockingClick? : boolean
10
10
type? : ' button' | ' submit' | ' reset'
11
+ size? : ' sm' | ' md'
11
12
}
12
13
13
14
let {
14
15
variant = ' solid' ,
15
16
isLoading,
16
- cb ,
17
+ callback ,
17
18
blockingClick,
18
19
type = ' button' ,
20
+ size = ' md' ,
19
21
children = undefined ,
20
22
... restProps
21
23
}: IButtonProps = $props ()
24
26
let disabled = $derived (restProps .disabled || isLoading || isSubmitting )
25
27
26
28
const handleClick = async () => {
27
- if (typeof cb !== ' function' ) return
29
+ if (typeof callback !== ' function' ) return
28
30
29
31
if (blockingClick ) isSubmitting = true
30
32
try {
31
- await cb ()
33
+ await callback ()
32
34
} catch (error ) {
33
35
console .error (' Error in button callback:' , error )
34
36
} finally {
40
42
solid: { background: ' bg-primary-900' , text: ' text-white' },
41
43
soft: { background: ' bg-primary-100' , text: ' text-primary-900' },
42
44
danger: { background: ' bg-danger-500' , text: ' text-white' },
43
- ' danger-soft' : { background: ' bg-danger-300' , text: ' text-danger-500' },
45
+ ' danger-soft' : { background: ' bg-danger-100' , text: ' text-danger-500' },
46
+ white: { background: ' bg-white' , text: ' text-black' },
44
47
}
45
48
46
49
const disabledVariantClasses = {
47
50
solid: { background: ' bg-primary-500' , text: ' text-white' },
48
51
soft: { background: ' bg-primary-100' , text: ' text-primary-500' },
49
- danger: { background: ' bg-danger-500' , text: ' text-white' },
50
- ' danger-soft' : { background: ' bg-danger-300' , text: ' text-danger-500' },
52
+ danger: { background: ' bg-danger-300' , text: ' text-white' },
53
+ ' danger-soft' : { background: ' bg-danger-100' , text: ' text-danger-300' },
54
+ white: { background: ' bg-black-100' , text: ' text-black-700' },
55
+ }
56
+
57
+ const sizeVariant = {
58
+ sm: ' px-4 py-1.5 text-base h-11' ,
59
+ md: ' px-8 py-2.5 text-xl h-14' ,
51
60
}
52
61
53
62
let classes = $derived ({
54
- common:
55
- ' cursor-pointer flex items-center justify-center px-8 py-2.5 rounded-full text-xl font-semibold h-[56px] duration-100' ,
63
+ common: cn (
64
+ ' cursor-pointer w-min flex items-center justify-center rounded-full font-semibold duration-100' ,
65
+ sizeVariant [size ]
66
+ ),
56
67
background: disabled
57
68
? disabledVariantClasses [variant ].background ||
58
69
variantClasses [variant ].background
80
91
{type }
81
92
>
82
93
<div class =" relative flex items-center justify-center" >
83
- {#if isLoading || isSubmitting }
84
- <div class =" loading loading-spinner loading-md absolute -left-4" ></div >
85
- {/if }
86
94
<div
87
95
class =" flex items-center justify-center duration-100"
88
- class:translate-x- 4 ={isLoading || isSubmitting }
96
+ class:blur-xs ={isLoading || isSubmitting }
89
97
>
90
98
{@render children ?.()}
91
99
</div >
100
+ {#if isLoading || isSubmitting }
101
+ <div class =" loading loading-spinner absolute loading-xl text-white" ></div >
102
+ {/if }
92
103
</div >
93
104
</button >
94
105
@@ -100,8 +111,9 @@ This component is a button with a loading spinner that can be used to indicate t
100
111
101
112
@props
102
113
- variant: The variant of the button. Default is `solid`.
114
+ - size: The size of the button. Default is `md`.
103
115
- isLoading: A boolean to indicate if the button is in a loading state.
104
- - cb : A callback function that will be called when the button is clicked.
116
+ - callback : A callback function that will be called when the button is clicked.
105
117
- blockingClick: A boolean to indicate if the button should block the click event while the callback function is being executed.
106
118
- icon: A slot for an icon to be displayed inside the button.
107
119
- ...restProps: Any other props that can be passed to a button element.
@@ -112,7 +124,7 @@ This component is a button with a loading spinner that can be used to indicate t
112
124
import * as Button from '$lib/ui/Button'
113
125
</script>
114
126
115
- <Button.Action variant="solid" cb ={() => console.log('clicked')}>
127
+ <Button.Action variant="solid" callback ={() => console.log('clicked')}>
116
128
Click me
117
129
</Button.Action>
118
130
```
0 commit comments