Fix Button to render a <button> tag if the 'type' attribute exists. Issue #299

This commit is contained in:
prototypa
2023-12-02 09:25:34 -05:00
parent 788acd9321
commit 9818cd08b8
2 changed files with 21 additions and 6 deletions

View File

@ -6,9 +6,10 @@ import type { CallToAction } from '~/types';
const {
variant = 'secondary',
target,
text = Astro.slots.render("default"),
text = Astro.slots.render('default'),
icon = '',
class: className = '',
type,
...rest
} = Astro.props as CallToAction;
@ -20,7 +21,20 @@ const variants = {
};
---
<a class={twMerge(variants[variant] || '', className)} {...target ? { target: target, rel: 'noopener noreferrer' } : {}} {...rest}>
{
type === 'button' || type === 'submit' || type === 'reset' ? (
<button type={type} class={twMerge(variants[variant] || '', className)} {...rest}>
<Fragment set:html={text} />
{icon && <Icon name={icon} class="w-5 h-5 ml-1 -mr-1.5 rtl:mr-1 rtl:-ml-1.5 inline-block" />}
</button>
) : (
<a
class={twMerge(variants[variant] || '', className)}
{...(target ? { target: target, rel: 'noopener noreferrer' } : {})}
{...rest}
>
<Fragment set:html={text} />
{icon && <Icon name={icon} class="w-5 h-5 ml-1 -mr-1.5 rtl:mr-1 rtl:-ml-1.5 inline-block" />}
</a>
)
}

1
src/types.d.ts vendored
View File

@ -180,6 +180,7 @@ export interface CallToAction extends HTMLAttributes<a> {
text?: string;
icon?: string;
classes?: Record<string, string>;
type?: 'button' | 'submit' | 'reset';
}
export interface ItemGrid {