budibase/packages/materialdesign-components/src/Button/Button.svelte

67 lines
1.4 KiB
Svelte
Raw Normal View History

<script>
import { setContext, getContext } from "svelte"
import Icon from "../Common/Icon.svelte"
import ripple from "../Common/Ripple.js"
import ClassBuilder from "../ClassBuilder.js"
const cb = new ClassBuilder("button", ["primary", "medium", "text"])
2020-02-22 23:09:26 +01:00
export let onClick
export let variant = "text"
export let colour = "primary"
export let size = "medium"
export let href = ""
export let icon = ""
export let trailingIcon = false
export let fullwidth = false
export let text = ""
export let disabled = false
2020-02-22 23:09:26 +01:00
export let _bb
let modifiers = {}
let customs = { size, colour }
if (!href) modifiers = { variant }
let props = { modifiers, customs }
let blockClasses = cb.build({ props })
const labelClass = cb.elem("label")
2020-02-22 23:09:26 +01:00
const clicked = () => _bb.call(onClick)
$: renderLeadingIcon = !!icon && !trailingIcon
$: renderTrailingIcon = !!icon && trailingIcon
</script>
{#if href}
2020-02-22 23:09:26 +01:00
<a class={blockClasses} {href} on:click={clicked}>
<span class={labelClass}>{text}</span>
</a>
{:else}
<button
use:ripple={{ colour }}
class={blockClasses}
class:fullwidth
{disabled}
2020-02-22 23:09:26 +01:00
on:click={clicked}>
{#if renderLeadingIcon}
2020-03-03 16:23:03 +01:00
<Icon context="button" {icon} />
{/if}
<span class={labelClass}>{text}</span>
{#if renderTrailingIcon}
2020-03-03 16:23:03 +01:00
<Icon context="button" {icon} />
{/if}
</button>
{/if}
<style>
.fullwidth {
width: 100%;
}
</style>