33 import type { HTMLButtonAttributes } from ' svelte/elements'
44
55 interface IButtonProps extends HTMLButtonAttributes {
6- variant? : ' solid' | ' soft' | ' danger' | ' danger-soft'
6+ variant? : ' solid' | ' soft' | ' danger' | ' danger-soft' | ' white '
77 isLoading? : boolean
8- cb ? : () => Promise <void >
8+ callback ? : () => Promise <void >
99 blockingClick? : boolean
1010 type? : ' button' | ' submit' | ' reset'
11+ size? : ' sm' | ' md'
1112 }
1213
1314 let {
1415 variant = ' solid' ,
1516 isLoading,
16- cb ,
17+ callback ,
1718 blockingClick,
1819 type = ' button' ,
20+ size = ' md' ,
1921 children = undefined ,
2022 ... restProps
2123 }: IButtonProps = $props ()
2426 let disabled = $derived (restProps .disabled || isLoading || isSubmitting )
2527
2628 const handleClick = async () => {
27- if (typeof cb !== ' function' ) return
29+ if (typeof callback !== ' function' ) return
2830
2931 if (blockingClick ) isSubmitting = true
3032 try {
31- await cb ()
33+ await callback ()
3234 } catch (error ) {
3335 console .error (' Error in button callback:' , error )
3436 } finally {
4042 solid: { background: ' bg-primary-900' , text: ' text-white' },
4143 soft: { background: ' bg-primary-100' , text: ' text-primary-900' },
4244 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' },
4447 }
4548
4649 const disabledVariantClasses = {
4750 solid: { background: ' bg-primary-500' , text: ' text-white' },
4851 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' ,
5160 }
5261
5362 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+ ),
5667 background: disabled
5768 ? disabledVariantClasses [variant ].background ||
5869 variantClasses [variant ].background
8091 {type }
8192>
8293 <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 }
8694 <div
8795 class =" flex items-center justify-center duration-100"
88- class:translate-x- 4 ={isLoading || isSubmitting }
96+ class:blur-xs ={isLoading || isSubmitting }
8997 >
9098 {@render children ?.()}
9199 </div >
100+ {#if isLoading || isSubmitting }
101+ <div class =" loading loading-spinner absolute loading-xl text-white" ></div >
102+ {/if }
92103 </div >
93104</button >
94105
@@ -100,8 +111,9 @@ This component is a button with a loading spinner that can be used to indicate t
100111
101112@props
102113- variant: The variant of the button. Default is `solid`.
114+ - size: The size of the button. Default is `md`.
103115- 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.
105117- blockingClick: A boolean to indicate if the button should block the click event while the callback function is being executed.
106118- icon: A slot for an icon to be displayed inside the button.
107119- ...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
112124 import * as Button from '$lib/ui/Button'
113125</script>
114126
115- <Button.Action variant="solid" cb ={() => console.log('clicked')}>
127+ <Button.Action variant="solid" callback ={() => console.log('clicked')}>
116128 Click me
117129</Button.Action>
118130```
0 commit comments