Skip to content

Commit a503797

Browse files
authored
Feat/improve button component (#60)
* feat: add white variant * feat: add small variant * chore: update doc and story for button * chore: rename cb into callback * update: improve small size * update: modify loading style
1 parent c755720 commit a503797

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

infrastructure/eid-wallet/src/app.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
--color-black-300: #a5a5a5;
3838
--color-black-100: #d2d2d2;
3939

40-
--color-black: #1F1F1F;
40+
--color-black: #1f1f1f;
4141

4242
--color-red-900: #FF5255;
4343
--color-red-700: #FF7B77;
@@ -46,7 +46,8 @@
4646
--color-red-100: #FFDCDD;
4747

4848
--color-danger-500: #ff5255;
49-
--color-danger-300: #ffdcdd;
49+
--color-danger-300: #ff7d7f;
50+
--color-danger-100: #ffdcdd;
5051
}
5152

5253
body {

infrastructure/eid-wallet/src/lib/ui/Button/ButtonAction.stories.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,18 @@ const meta: Meta<ButtonAction> = {
1515
variant: {
1616
control: {
1717
type: 'select',
18-
options: ['solid', 'soft', 'danger', 'danger-soft'],
18+
options: ['solid', 'soft', 'danger', 'danger-soft', 'white'],
19+
},
20+
},
21+
size: {
22+
control: {
23+
type: 'select',
24+
options: ['sm', 'md'],
1925
},
2026
},
2127
isLoading: { control: 'boolean' },
2228
blockingClick: { control: 'boolean' },
23-
cb: { action: 'clicked' },
29+
callback: { action: 'clicked' },
2430
},
2531
}
2632

@@ -51,7 +57,7 @@ export const BlockingClick: Story = {
5157
args: {
5258
blockingClick: true,
5359
children: ButtonText,
54-
cb: async () => {
60+
callback: async () => {
5561
await new Promise((resolve) => setTimeout(resolve, 2000))
5662
},
5763
},

infrastructure/eid-wallet/src/lib/ui/Button/ButtonAction.svelte

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@
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()
@@ -24,11 +26,11 @@
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 {
@@ -40,19 +42,28 @@
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
@@ -80,15 +91,15 @@
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

Comments
 (0)