/
Button.svelte
149 lines (136 loc) · 8.92 KB
/
Button.svelte
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<script lang="ts">
import { twMerge } from 'tailwind-merge';
import { getContext } from 'svelte';
import type { SizeType } from '$lib/types';
import type { HTMLButtonAttributes } from 'svelte/elements';
type ButtonColor = keyof typeof colorClasses;
const group: SizeType = getContext('group');
export let pill: boolean = false;
export let outline: boolean = false;
export let size: 'xs' | 'sm' | 'md' | 'lg' | 'xl' = group ? 'sm' : 'md';
export let href: string | undefined = undefined;
export let type: HTMLButtonAttributes['type'] = 'button';
export let color: ButtonColor = group ? (outline ? 'dark' : 'alternative') : 'primary';
export let shadow: boolean = false;
export let tag: string = 'button';
export let checked: boolean | undefined = undefined;
// export let disabled: boolean = false;
const colorClasses = {
alternative: 'text-gray-900 bg-white border border-gray-200 hover:bg-gray-100 dark:bg-gray-800 dark:text-gray-400 hover:text-primary-700 focus-within:text-primary-700 dark:focus-within:text-white dark:hover:text-white dark:hover:bg-gray-700',
blue: 'text-white bg-blue-700 hover:bg-blue-800 dark:bg-blue-600 dark:hover:bg-blue-700',
dark: 'text-white bg-gray-800 hover:bg-gray-900 dark:bg-gray-800 dark:hover:bg-gray-700',
green: 'text-white bg-green-700 hover:bg-green-800 dark:bg-green-600 dark:hover:bg-green-700',
light: 'text-gray-900 bg-white border border-gray-300 hover:bg-gray-100 dark:bg-gray-800 dark:text-white dark:border-gray-600 dark:hover:bg-gray-700 dark:hover:border-gray-600',
primary: 'text-white bg-primary-700 hover:bg-primary-800 dark:bg-primary-600 dark:hover:bg-primary-700',
purple: 'text-white bg-purple-700 hover:bg-purple-800 dark:bg-purple-600 dark:hover:bg-purple-700',
red: 'text-white bg-red-700 hover:bg-red-800 dark:bg-red-600 dark:hover:bg-red-700',
yellow: 'text-white bg-yellow-400 hover:bg-yellow-500 ',
none: ''
};
const colorCheckedClasses = {
alternative: 'text-primary-700 border dark:text-primary-500 bg-gray-100 dark:bg-gray-700 border-gray-300 shadow-gray-300 dark:shadow-gray-800 shadow-inner',
blue: 'text-blue-900 bg-blue-400 dark:bg-blue-500 shadow-blue-700 dark:shadow-blue-800 shadow-inner',
dark: 'text-white bg-gray-500 dark:bg-gray-600 shadow-gray-800 dark:shadow-gray-900 shadow-inner',
green: 'text-green-900 bg-green-400 dark:bg-green-500 shadow-green-700 dark:shadow-green-800 shadow-inner',
light: 'text-gray-900 bg-gray-100 border border-gray-300 dark:bg-gray-500 dark:text-gray-900 dark:border-gray-700 shadow-gray-300 dark:shadow-gray-700 shadow-inner',
primary: 'text-primary-900 bg-primary-400 dark:bg-primary-500 shadow-primary-700 dark:shadow-primary-800 shadow-inner',
purple: 'text-purple-900 bg-purple-400 dark:bg-purple-500 shadow-purple-700 dark:shadow-purple-800 shadow-inner',
red: 'text-red-900 bg-red-400 dark:bg-red-500 shadow-red-700 dark:shadow-red-800 shadow-inner',
yellow: 'text-yellow-900 bg-yellow-300 dark:bg-yellow-400 shadow-yellow-500 dark:shadow-yellow-700 shadow-inner',
none: ''
};
const coloredFocusClasses = {
alternative: 'focus-within:ring-gray-200 dark:focus-within:ring-gray-700',
blue: 'focus-within:ring-blue-300 dark:focus-within:ring-blue-800',
dark: 'focus-within:ring-gray-300 dark:focus-within:ring-gray-700',
green: 'focus-within:ring-green-300 dark:focus-within:ring-green-800',
light: 'focus-within:ring-gray-200 dark:focus-within:ring-gray-700',
primary: 'focus-within:ring-primary-300 dark:focus-within:ring-primary-800',
purple: 'focus-within:ring-purple-300 dark:focus-within:ring-purple-900',
red: 'focus-within:ring-red-300 dark:focus-within:ring-red-900',
yellow: 'focus-within:ring-yellow-300 dark:focus-within:ring-yellow-900',
none: ''
};
const coloredShadowClasses = {
alternative: 'shadow-gray-500/50 dark:shadow-gray-800/80',
blue: 'shadow-blue-500/50 dark:shadow-blue-800/80',
dark: 'shadow-gray-500/50 dark:shadow-gray-800/80',
green: 'shadow-green-500/50 dark:shadow-green-800/80',
light: 'shadow-gray-500/50 dark:shadow-gray-800/80',
primary: 'shadow-primary-500/50 dark:shadow-primary-800/80',
purple: 'shadow-purple-500/50 dark:shadow-purple-800/80',
red: 'shadow-red-500/50 dark:shadow-red-800/80 ',
yellow: 'shadow-yellow-500/50 dark:shadow-yellow-800/80 ',
none: ''
};
const outlineClasses = {
alternative: 'text-gray-900 dark:text-gray-400 hover:text-white border border-gray-800 hover:bg-gray-900 focus-within:bg-gray-900 focus-within:text-white focus-within:ring-gray-300 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-600 dark:focus-within:ring-gray-800',
blue: 'text-blue-700 hover:text-white border border-blue-700 hover:bg-blue-800 dark:border-blue-500 dark:text-blue-500 dark:hover:text-white dark:hover:bg-blue-600',
dark: 'text-gray-900 hover:text-white border border-gray-800 hover:bg-gray-900 focus-within:bg-gray-900 focus-within:text-white dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-600',
green: 'text-green-700 hover:text-white border border-green-700 hover:bg-green-800 dark:border-green-500 dark:text-green-500 dark:hover:text-white dark:hover:bg-green-600',
light: 'text-gray-500 hover:text-gray-900 bg-white border border-gray-200 dark:border-gray-600 dark:hover:text-white dark:text-gray-400 hover:bg-gray-50 dark:bg-gray-700 dark:hover:bg-gray-600',
primary: 'text-primary-700 hover:text-white border border-primary-700 hover:bg-primary-700 dark:border-primary-500 dark:text-primary-500 dark:hover:text-white dark:hover:bg-primary-600',
purple: 'text-purple-700 hover:text-white border border-purple-700 hover:bg-purple-800 dark:border-purple-400 dark:text-purple-400 dark:hover:text-white dark:hover:bg-purple-500',
red: 'text-red-700 hover:text-white border border-red-700 hover:bg-red-800 dark:border-red-500 dark:text-red-500 dark:hover:text-white dark:hover:bg-red-600',
yellow: 'text-yellow-400 hover:text-white border border-yellow-400 hover:bg-yellow-500 dark:border-yellow-300 dark:text-yellow-300 dark:hover:text-white dark:hover:bg-yellow-400',
none: ''
};
const sizeClasses = {
xs: 'px-3 py-2 text-xs',
sm: 'px-4 py-2 text-sm',
md: 'px-5 py-2.5 text-sm',
lg: 'px-5 py-3 text-base',
xl: 'px-6 py-3.5 text-base'
};
const hasBorder = () => outline || color === 'alternative' || color === 'light';
let buttonClass: string;
$: buttonClass = twMerge(
'text-center font-medium',
group ? 'focus-within:ring-2' : 'focus-within:ring-4',
group && 'focus-within:z-10',
group || 'focus-within:outline-none',
'inline-flex items-center justify-center ' + sizeClasses[size],
outline && checked && 'border dark:border-gray-900',
outline && checked && colorCheckedClasses[color],
outline && !checked && outlineClasses[color],
!outline && checked && colorCheckedClasses[color],
!outline && !checked && colorClasses[color],
color === 'alternative' && (group && !checked ? 'dark:bg-gray-700 dark:text-white dark:border-gray-700 dark:hover:border-gray-600 dark:hover:bg-gray-600' : 'dark:bg-transparent dark:border-gray-600 dark:hover:border-gray-600'),
outline && color === 'dark' && (group ? (checked ? 'bg-gray-900 border-gray-800 dark:border-white dark:bg-gray-600' : 'dark:text-white border-gray-800 dark:border-white') : 'dark:text-gray-400 dark:border-gray-700'),
coloredFocusClasses[color],
hasBorder() && group && 'border-s-0 first:border-s',
group ? (pill && 'first:rounded-s-full last:rounded-e-full') || 'first:rounded-s-lg last:rounded-e-lg' : (pill && 'rounded-full') || 'rounded-lg',
shadow && 'shadow-lg',
shadow && coloredShadowClasses[color],
$$restProps.disabled && 'cursor-not-allowed opacity-50',
$$props.class
);
</script>
{#if href && !$$restProps.disabled}
<a {href} {...$$restProps} class={buttonClass} role="button" on:click on:change on:keydown on:keyup on:touchstart|passive on:touchend on:touchcancel on:mouseenter on:mouseleave>
<slot />
</a>
{:else if tag === 'button' && !$$restProps.disabled }
<button {type} {...$$restProps} class={buttonClass} on:click on:change on:keydown on:keyup on:touchstart|passive on:touchend on:touchcancel on:mouseenter on:mouseleave>
<slot />
</button>
{:else}
<svelte:element this={tag} class={buttonClass}>
<slot />
</svelte:element>
{/if}
<!--
@component
[Go to docs](https://flowbite-svelte.com/)
## Props
@prop export let pill: boolean = false;
@prop export let outline: boolean = false;
@prop export let size: 'xs' | 'sm' | 'md' | 'lg' | 'xl' = group ? 'sm' : 'md';
@prop export let href: string | undefined = undefined;
@prop export let type: HTMLButtonAttributes['type'] = 'button';
@prop export let color: ButtonColor = group ? (outline ? 'dark' : 'alternative') : 'primary';
@prop export let shadow: boolean = false;
@prop export let tag: string = 'button';
@prop export let checked: boolean | undefined = undefined;
@prop export let disabled: boolean = false;
-->