blakeblackshear.frigate/web/src/components/Button.jsx

77 lines
1.7 KiB
React
Raw Normal View History

2021-01-09 18:26:46 +01:00
import { h } from 'preact';
const noop = () => {};
2021-02-02 05:28:25 +01:00
const ButtonColors = {
blue: {
contained: 'bg-blue-500 focus:bg-blue-400 active:bg-blue-600 ring-blue-300',
outlined: '',
text:
'text-blue-500 hover:bg-blue-500 hover:bg-opacity-20 focus:bg-blue-500 focus:bg-opacity-40 active:bg-blue-500 active:bg-opacity-40',
},
red: {
contained: 'bg-red-500 focus:bg-red-400 active:bg-red-600 ring-red-300',
outlined: '',
text: '',
},
green: {
contained: 'bg-green-500 focus:bg-green-400 active:bg-green-600 ring-green-300',
outlined: '',
text: '',
},
disabled: {
contained: 'bg-gray-400',
outlined: '',
text: '',
},
black: {
contained: '',
outlined: '',
text: 'text-black dark:text-white',
},
};
2021-02-02 05:28:25 +01:00
const ButtonTypes = {
contained: 'text-white shadow focus:shadow-xl hover:shadow-md',
outlined: '',
text: 'transition-opacity',
};
export default function Button({
children,
className = '',
color = 'blue',
disabled = false,
href,
onClick,
size,
type = 'contained',
...attrs
}) {
let classes = `${className} ${
ButtonColors[disabled ? 'disabled' : color][type]
} font-sans inline-flex font-bold uppercase text-xs px-2 py-2 rounded outline-none focus:outline-none ring-opacity-50 transition-shadow transition-colors ${
disabled ? 'cursor-not-allowed' : 'focus:ring-2 cursor-pointer'
}`;
if (disabled) {
classes = classes.replace(/(?:focus|active|hover):[^ ]+/g, '');
}
const Element = href ? 'a' : 'div';
2021-01-09 18:26:46 +01:00
return (
2021-02-02 05:28:25 +01:00
<Element
2021-01-09 18:26:46 +01:00
role="button"
2021-02-02 05:28:25 +01:00
aria-disabled={disabled ? 'true' : 'false'}
2021-01-09 18:26:46 +01:00
tabindex="0"
2021-02-02 05:28:25 +01:00
className={classes}
2021-01-09 18:26:46 +01:00
onClick={onClick || noop}
2021-02-02 05:28:25 +01:00
href={href}
{...attrs}
2021-01-09 18:26:46 +01:00
>
{children}
2021-02-02 05:28:25 +01:00
</Element>
2021-01-09 18:26:46 +01:00
);
}