-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathButton.svelte
104 lines (92 loc) · 2.4 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
<script>
import { onMount } from 'svelte'
import Icon from './Icon.svelte'
import { omit } from '../utils'
/** HTML tag to use for button (either 'a' or 'button')
* @svelte-prop {String} tag=button
* @values <code>button</code>, <code>a</code>
* */
export let tag = 'button'
/** Type (color of control)
* @svelte-prop {String} [type] - Type (color of control)
* @values $$colors$$
* */
export let type = ''
/** Size of button
* @svelte-prop {String} [size]
* @values $$sizes$$
* */
export let size = ''
/** Href to use when <code>tag</code> is 'a'
* @svelte-prop {String} [href]
* */
export let href = ''
/** Native button type
* @svelte-prop {String} [nativeType]=button
* @values Any native button type (button, submit, reset)
* */
export let nativeType = 'button'
export let loading = false
export let inverted = false
export let outlined = false
export let rounded = false
export let iconLeft = null
export let iconRight = null
export let iconPack = null
let iconSize = ''
onMount(() => {
if (!['button', 'a'].includes(tag)) throw new Error(`'${tag}' cannot be used as a tag for a Bulma button`)
})
$: props = {
...omit($$props, 'loading', 'inverted', 'nativeType', 'outlined', 'rounded', 'type'),
class: `button ${type} ${size} ${$$props.class || ''}`,
}
$: {
if (!size || size === 'is-medium') {
iconSize = 'is-small'
} else if (size === 'is-large') {
iconSize = 'is-medium'
} else {
iconSize = size
}
}
</script>
{#if tag === 'button'}
<button
{...props}
type={nativeType}
class:is-inverted={inverted}
class:is-loading={loading}
class:is-outlined={outlined}
class:is-rounded={rounded}
on:click>
{#if iconLeft}
<Icon pack={iconPack} icon={iconLeft} size={iconSize} />
{/if}
<span>
<slot />
</span>
{#if iconRight}
<Icon pack={iconPack} icon={iconRight} size={iconSize} />
{/if}
</button>
{:else if tag === 'a'}
<a
{href}
{...props}
class:is-inverted={inverted}
class:is-loading={loading}
class:is-outlined={outlined}
class:is-rounded={rounded}
on:click>
{#if iconLeft}
<Icon pack={iconPack} icon={iconLeft} size={iconSize} />
{/if}
<span>
<slot />
</span>
{#if iconRight}
<Icon pack={iconPack} icon={iconRight} size={iconSize} />
{/if}
</a>
{/if}