-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathButton.tsx
More file actions
88 lines (81 loc) · 2.09 KB
/
Button.tsx
File metadata and controls
88 lines (81 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import React from 'react'
import type { PropsWithChildren, Ref } from 'react';
import { Button as DesignSystemButton } from '@digdir/designsystemet-react';
import type { ButtonProps as DesignSystemButtonProps } from '@digdir/designsystemet-react';
import { Spinner } from '../Spinner';
export type ButtonVariant = 'primary' | 'secondary' | 'tertiary' | undefined;
export type ButtonColor = 'first' | 'second' | 'success' | 'danger' | undefined;
export type TextAlign = 'left' | 'center' | 'right';
export type ButtonProps = {
variant?: ButtonVariant;
color?: ButtonColor;
isLoading?: boolean;
loadingLabel?: string;
size?: 'sm' | 'md' | 'lg';
fullWidth?: boolean;
textAlign?: TextAlign;
title?: string;
'aria-label'?: string;
ref?: Ref<HTMLButtonElement>;
} & Omit<DesignSystemButtonProps, 'variant' | 'color' | 'size' | 'title' | 'aria-label'>;
type DSButtonColor =
| 'accent'
| 'neutral'
| 'success'
| 'danger'
| 'brand1'
| 'brand2'
| 'brand3'
| undefined;
function mapColorNames(color: ButtonColor): DSButtonColor {
switch (color) {
case 'first':
return 'accent';
case 'second':
return 'neutral';
default:
return color ?? 'accent';
}
}
export function Button({
disabled,
isLoading = false,
variant = 'primary',
color = 'first',
size = 'sm',
children,
fullWidth,
style,
textAlign,
loadingLabel,
ref,
...rest
}: PropsWithChildren<ButtonProps>) {
const expandedStyle = { ...style, justifyContent: textAlign ? textAlign : undefined };
return (
<DesignSystemButton
{...rest}
disabled={disabled || isLoading}
variant={variant}
data-color={mapColorNames(color)}
data-size={size}
data-fullwidth={fullWidth ? true : undefined}
ref={ref}
style={expandedStyle}
>
{isLoading ? (
<>
<Spinner
aria-hidden='true'
aria-label={loadingLabel}
data-color={color}
data-size={size === 'lg' ? 'sm' : 'xs'}
/>
{children}
</>
) : (
children
)}
</DesignSystemButton>
);
}