metacubexd/src/components/Button.tsx

47 lines
1.1 KiB
TypeScript
Raw Normal View History

import { JSX, ParentComponent, Show, splitProps } from 'solid-js'
import { twMerge } from 'tailwind-merge'
2023-09-23 14:30:16 +08:00
interface ButtonBaseProps extends JSX.ButtonHTMLAttributes<HTMLButtonElement> {
loading?: boolean
disabled?: boolean
}
interface ButtonWithIconProps extends ButtonBaseProps {
icon: JSX.Element
children?: JSX.Element
}
interface ButtonWithoutIconProps extends ButtonBaseProps {
icon?: JSX.Element
children: JSX.Element
}
export const Button: ParentComponent<
2023-09-23 14:30:16 +08:00
ButtonWithIconProps | ButtonWithoutIconProps
> = (props) => {
2023-09-23 14:30:16 +08:00
const [local, others] = splitProps(props, ['class', 'loading', 'icon'])
return (
<button
2023-09-22 16:05:36 +08:00
class={twMerge(
'btn flex items-center',
local.loading ? 'btn-disabled' : local.class,
)}
{...others}
>
<Show when={local.loading}>
2023-09-22 16:05:36 +08:00
<div class="loading loading-spinner" />
</Show>
<span
class="truncate rounded-none"
classList={{
'flex-1': !local.icon,
}}
>
2023-09-22 16:05:36 +08:00
{props.icon || props.children}
</span>
</button>
)
}