budibase/packages/standard-components/src/button.svelte

130 lines
2.0 KiB
Svelte
Raw Normal View History

2019-08-19 22:18:23 +02:00
<script>
2019-10-07 07:03:41 +02:00
import cssVars from "./cssVars";
import {buildStyle} from "./buildStyle";
2019-08-27 08:32:56 +02:00
export let className = "default";
2019-08-19 22:18:23 +02:00
export let disabled = false;
2019-08-27 08:32:56 +02:00
export let contentText;
2019-10-10 07:18:02 +02:00
export let onClick;
2019-10-07 07:03:41 +02:00
export let background;
export let color;
export let border;
export let padding;
export let hoverColor;
export let hoverBackground;
export let hoverBorder;
2020-01-18 10:00:18 +01:00
export let _children;
2019-08-27 08:32:56 +02:00
export let _bb;
2020-01-18 10:00:18 +01:00
let theButton;
2019-10-07 07:03:41 +02:00
let cssVariables;
let buttonStyles;
let customHoverColorClass;
let customHoverBorderClass;
let customHoverBackClass;
2019-09-03 11:42:19 +02:00
2019-10-10 11:41:31 +02:00
let customClasses = "";
const createClasses = (classes) => {
let all = "";
for(let cls in classes) {
if(classes[cls]) {
all = all + " " + cls;
}
}
return all;
}
2019-09-03 11:42:19 +02:00
$:{
2020-01-18 10:00:18 +01:00
if(_bb && theButton && _children && _children.length)
_bb.hydrateChildren(_children, theButton);
}
2019-10-07 07:03:41 +02:00
2020-01-18 10:00:18 +01:00
$:{
2019-10-07 07:03:41 +02:00
cssVariables = {
hoverColor, hoverBorder,
2019-10-10 11:41:31 +02:00
hoverBackground,
background, color, border,
2019-10-07 07:03:41 +02:00
};
buttonStyles = buildStyle({
2019-10-10 11:41:31 +02:00
padding
});
customClasses = createClasses({
hoverColor, hoverBorder, hoverBackground,
background, border, color
});
2019-09-03 11:42:19 +02:00
}
2019-09-22 06:02:33 +02:00
2019-10-07 07:03:41 +02:00
2019-09-22 06:02:33 +02:00
const clickHandler = () => {
2019-10-07 07:03:41 +02:00
_bb.call(onClick);
2019-09-22 06:02:33 +02:00
}
2019-08-19 22:18:23 +02:00
</script>
2020-01-18 10:00:18 +01:00
<button bind:this={theButton}
use:cssVars={cssVariables}
2019-10-10 11:41:31 +02:00
class="{className} {customClasses}"
2019-10-10 07:18:02 +02:00
disabled={disabled || false}
2019-10-07 07:03:41 +02:00
on:click={clickHandler}
style={buttonStyles}>
2020-01-18 10:00:18 +01:00
{#if !_children || _children.length === 0}
2019-08-19 22:18:23 +02:00
{contentText}
{/if}
2019-08-27 08:32:56 +02:00
</button>
<style>
.default {
font-family: inherit;
font-size: inherit;
padding: 0.4em;
margin: 0 0 0.5em 0;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 2px;
color: #333;
background-color: #f4f4f4;
outline: none;
}
.default:active {
background-color: #ddd;
}
.default:focus {
border-color: #666;
}
2019-10-10 11:41:31 +02:00
.border {
border: var(--border);
}
.color {
color: var(--color);
}
.background {
background: var(--background);
}
.hoverBorder:hover {
2019-10-07 07:03:41 +02:00
border: var(--hoverBorder);
}
2019-10-10 11:41:31 +02:00
.hoverColor:hover {
2019-10-07 07:03:41 +02:00
color: var(--hoverColor);
}
2019-10-10 11:41:31 +02:00
.hoverBack:hover {
2019-10-07 07:03:41 +02:00
background: var(--hoverBackground);
}
2019-10-10 11:41:31 +02:00
2019-08-27 08:32:56 +02:00
</style>