2020-03-11 15:04:47 +01:00
< script >
2020-03-20 10:57:49 +01:00
import { onMount } from "svelte"
2020-03-11 17:28:09 +01:00
import ripple from "../Common/Ripple.js"
import ClassBuilder from "../ClassBuilder.js"
2020-03-11 15:04:47 +01:00
2020-03-11 17:28:09 +01:00
const cb = new ClassBuilder("icon-button")
2020-03-11 15:04:47 +01:00
2020-03-12 15:07:20 +01:00
let on = false
2020-03-11 17:28:09 +01:00
export let _bb
export let context = ""
export let onClick = () => {}
export let disabled = false
export let href = ""
export let icon = ""
export let onIcon = "" //on state icon for toggle button
export let size = "medium"
2020-03-11 15:04:47 +01:00
2020-03-20 10:57:49 +01:00
onMount(() => {
let ctx = !!_bb ? _bb.getContext("BBMD:icon-button:context") : ""
//It isn't possible to use context within nested components as they do not have their own _bb instance (has to be passed down from parent component). This allows context to be passed as props
if (!context && !!ctx) {
context = ctx
}
})
2020-03-12 15:07:20 +01:00
function onButtonClick() {
open = !open
2020-03-30 12:20:55 +02:00
_bb.call(onClick)
2020-03-12 15:07:20 +01:00
}
2020-03-11 17:28:09 +01:00
$: isToggleButton = !!icon & & !!onIcon
$: useLinkButton = !!href
2020-03-11 15:04:47 +01:00
2020-03-11 17:28:09 +01:00
$: customs = { size }
$: props = { customs , extras : [ "material-icons" , context ] }
$: iconBtnClass = cb.build({ props } )
2020-03-11 15:04:47 +01:00
< / script >
{ #if useLinkButton }
2020-03-12 15:07:20 +01:00
< a
on:click={ onButtonClick }
class={ iconBtnClass }
{ href }
{ disabled }
role="button"
tabindex="0">
2020-03-11 15:04:47 +01:00
{ #if isToggleButton }
2020-03-11 17:28:09 +01:00
< i
use:ripple
class="material-icons mdc-icon-button__icon mdc-icon-button__icon--on">
2020-03-11 15:04:47 +01:00
{ onIcon }
< / i >
2020-03-11 17:28:09 +01:00
< i use:ripple class = "material-icons mdc-icon-button__icon" > { icon } </ i >
2020-03-11 15:04:47 +01:00
{ : else }{ icon }{ /if }
< / a >
{ : else }
2020-03-11 17:28:09 +01:00
< button
2020-03-12 15:07:20 +01:00
on:click={ onButtonClick }
2020-03-11 17:28:09 +01:00
class={ iconBtnClass }
{ disabled }
role="button"
2020-03-12 15:07:20 +01:00
aria-label="Add to favorites"
aria-pressed="false"
2020-03-11 17:28:09 +01:00
tabindex="0">
2020-03-11 15:04:47 +01:00
{ #if isToggleButton }
2020-03-12 15:07:20 +01:00
< i use:ripple class = "material-icons mdc-icon-button__icon" > { icon } </ i >
2020-03-11 17:28:09 +01:00
< i
use:ripple
class="material-icons mdc-icon-button__icon mdc-icon-button__icon--on">
2020-03-11 15:04:47 +01:00
{ onIcon }
< / i >
{ : else }{ icon }{ /if }
< / button >
{ /if }