-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.svelte
139 lines (129 loc) · 4.15 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
<!--
@component
this component is the main button component
@slot `default` - the main content of the component
@prop `isLoading`
-->
<script lang="ts">
import cn from 'classnames'
import _ from 'lodash'
import { writable } from 'svelte/store'
import { pulse } from './actions/pulse'
import { canHover$ } from './helpers/media-queries'
import HoverState from './HoverState.svelte'
import LoadingOverlay from './LoadingOverlay.svelte'
import ToolTip from './ToolTip.svelte'
import { Option } from './types'
/**
* @description use active styles
* @default false
*/
export let active = false
/**
* @description use danger styles
* @default false
*/
export let danger = false
/**
* @description use secondary styles
* @default false
*/
export let secondary = false
/** @default false */
export let isLoading = false
/** @default false */
export let disabled = false
/**
* @description job assigned to on:click, triggers loading status until settled
* @required
*/
export let job: () => Promise<unknown> | unknown
/**
* @description disables the default styling of the component
* @default false
*/
export let overrideStyles = false
/**
* @description a tip to be shown when hovered
* @default undefined
*/
export let tooltip: Option<string | false> = undefined
/** @default false */
export let showTooltipWhenLoading = false
/** @default false */
export let showTooltipWhenEnabled = false
export let className = ''
export let style = ''
let isJobLoading = false
const disabledStore = writable(disabled)
$: disabledStore.set(disabled)
$: shouldShowTooltip =
_.isString(tooltip) &&
tooltip.length &&
(((isLoading || isJobLoading) && showTooltipWhenLoading) ||
(!(isLoading || isJobLoading) && !disabled && showTooltipWhenEnabled) ||
(disabled && !(isLoading || isJobLoading)))
</script>
<HoverState let:hoverState>
<button
use:pulse={{ should$: disabledStore }}
{style}
class={cn(
!overrideStyles && [
'transition-[color,background-color,border-color,text-decoration-color,fill,stroke,transform,box-shadow]',
$canHover$ && !(isLoading || isJobLoading || disabled) && 'hover:scale-105',
'relative',
'border',
'duration-500',
'disabled:bg-gray-800',
'disabled:bg-opacity-30',
'disabled:text-gray-500',
'hover:border-gray-400',
'hover:text-text-hover',
'disabled:cursor-not-allowed',
danger
? 'border-transparent bg-blood hover:bg-blood bg-opacity-70 active:bg-blood text-rose-200'
: active
? 'border-transparent bg-secondary-800 hover:bg-secondary-600 hover:bg-opacity-50 active:bg-secondary-800 text-secondary-400 bg-opacity-50'
: 'border-primary-600 active:bg-primary-600',
(isLoading || isJobLoading) &&
'cursor-wait disabled:cursor-wait text-transparent hover:text-transparent disabled:text-transparent',
secondary
? 'py-1 px-2 rounded-xl text-xs bg-primary-600 leading-none disabled:hover:border-primary-600'
: 'py-2 px-4 rounded-lg disabled:border-gray-500',
],
className,
)}
on:click={async () => {
if (!isLoading && !isJobLoading && !disabled) {
isJobLoading = true
try {
await job()
} catch {
const checkpoint = { danger, active, secondary }
danger = true
// active = false
// secondary = false
setTimeout(() => {
danger = checkpoint.danger
// active = checkpoint.active
// secondary = checkpoint.secondary
}, 500)
}
isJobLoading = false
}
}}
disabled={isLoading || isJobLoading || disabled}>
<div
class={cn(
'contents children:transition-opacity children:duration-500',
(isLoading || isJobLoading) && 'children:opacity-0',
)}>
<slot isLoading={isLoading || isJobLoading} />
</div>
<LoadingOverlay visible={isLoading || isJobLoading} />
{#if hoverState && shouldShowTooltip}
<ToolTip>{tooltip}</ToolTip>
{/if}
</button>
</HoverState>