|
| 1 | +<script lang="ts" generics="T"> |
| 2 | + import { cn } from '$lib/utils' |
| 3 | + import type { HTMLButtonAttributes } from 'svelte/elements' |
| 4 | +
|
| 5 | + interface IButtonProps extends HTMLButtonAttributes { |
| 6 | + variant?: 'solid' | 'soft' | 'danger' | 'danger-soft' |
| 7 | + isLoading?: boolean |
| 8 | + cb?: () => Promise<void> |
| 9 | + blockingClick?: boolean |
| 10 | + type?: 'button' | 'submit' | 'reset' |
| 11 | + } |
| 12 | +
|
| 13 | + let { |
| 14 | + variant = 'solid', |
| 15 | + isLoading, |
| 16 | + cb, |
| 17 | + blockingClick, |
| 18 | + type = 'button', |
| 19 | + children = undefined, |
| 20 | + ...restProps |
| 21 | + }: IButtonProps = $props() |
| 22 | +
|
| 23 | + let isSubmitting = $state(false) |
| 24 | + let disabled = $derived(restProps.disabled || isLoading || isSubmitting) |
| 25 | +
|
| 26 | + const handleClick = async () => { |
| 27 | + if (typeof cb !== 'function') return |
| 28 | +
|
| 29 | + if (blockingClick) isSubmitting = true |
| 30 | + try { |
| 31 | + await cb() |
| 32 | + } catch (error) { |
| 33 | + console.error('Error in button callback:', error) |
| 34 | + } finally { |
| 35 | + isSubmitting = false |
| 36 | + } |
| 37 | + } |
| 38 | +
|
| 39 | + const variantClasses = { |
| 40 | + solid: { background: 'bg-primary-900', text: 'text-white' }, |
| 41 | + soft: { background: 'bg-primary-100', text: 'text-primary-900' }, |
| 42 | + danger: { background: 'bg-danger-500', text: 'text-white' }, |
| 43 | + 'danger-soft': { background: 'bg-danger-300', text: 'text-danger-500' }, |
| 44 | + } |
| 45 | +
|
| 46 | + const disabledVariantClasses = { |
| 47 | + solid: { background: 'bg-primary-500', text: 'text-white' }, |
| 48 | + 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' }, |
| 51 | + } |
| 52 | +
|
| 53 | + 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', |
| 56 | + background: disabled |
| 57 | + ? disabledVariantClasses[variant].background || |
| 58 | + variantClasses[variant].background |
| 59 | + : variantClasses[variant].background, |
| 60 | + text: disabled |
| 61 | + ? disabledVariantClasses[variant].text || variantClasses[variant].text |
| 62 | + : variantClasses[variant].text, |
| 63 | + disabled: 'cursor-not-allowed', |
| 64 | + }) |
| 65 | +</script> |
| 66 | + |
| 67 | +<button |
| 68 | + {...restProps} |
| 69 | + class={cn( |
| 70 | + [ |
| 71 | + classes.common, |
| 72 | + classes.background, |
| 73 | + classes.text, |
| 74 | + disabled && classes.disabled, |
| 75 | + restProps.class, |
| 76 | + ].join(' ') |
| 77 | + )} |
| 78 | + {disabled} |
| 79 | + onclick={handleClick} |
| 80 | + {type} |
| 81 | +> |
| 82 | + <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 | + <div |
| 87 | + class="flex items-center justify-center duration-100" |
| 88 | + class:translate-x-4={isLoading || isSubmitting} |
| 89 | + > |
| 90 | + {@render children?.()} |
| 91 | + </div> |
| 92 | + </div> |
| 93 | +</button> |
| 94 | + |
| 95 | +<!-- |
| 96 | +@component |
| 97 | +export default ButtonAction |
| 98 | +@description |
| 99 | +This component is a button with a loading spinner that can be used to indicate that an action is being performed. |
| 100 | +
|
| 101 | +@props |
| 102 | +- variant: The variant of the button. Default is `solid`. |
| 103 | +- 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. |
| 105 | +- blockingClick: A boolean to indicate if the button should block the click event while the callback function is being executed. |
| 106 | +- icon: A slot for an icon to be displayed inside the button. |
| 107 | +- ...restProps: Any other props that can be passed to a button element. |
| 108 | +
|
| 109 | +@usage |
| 110 | +```html |
| 111 | +<script lang="ts"> |
| 112 | + import * as Button from '$lib/ui/Button' |
| 113 | +</script> |
| 114 | +
|
| 115 | +<Button.Action variant="solid" cb={() => console.log('clicked')}> |
| 116 | + Click me |
| 117 | +</Button.Action> |
| 118 | +``` |
| 119 | + --> |
0 commit comments