Skip to content

Commit 9818cd0

Browse files
committed
Fix Button to render a <button> tag if the 'type' attribute exists. Issue #299
1 parent 788acd9 commit 9818cd0

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

src/components/ui/Button.astro

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,35 @@ import type { CallToAction } from '~/types';
66
const {
77
variant = 'secondary',
88
target,
9-
text = Astro.slots.render("default"),
9+
text = Astro.slots.render('default'),
1010
icon = '',
1111
class: className = '',
12+
type,
1213
...rest
1314
} = Astro.props as CallToAction;
1415
1516
const variants = {
16-
primary: 'btn-primary' ,
17+
primary: 'btn-primary',
1718
secondary: 'btn-secondary',
1819
tertiary: 'btn btn-tertiary',
1920
link: 'cursor-pointer hover:text-primary',
2021
};
2122
---
2223

23-
<a class={twMerge(variants[variant] || '', className)} {...target ? { target: target, rel: 'noopener noreferrer' } : {}} {...rest}>
24-
<Fragment set:html={text} />
25-
{icon && <Icon name={icon} class="w-5 h-5 ml-1 -mr-1.5 rtl:mr-1 rtl:-ml-1.5 inline-block" />}
26-
</a>
24+
{
25+
type === 'button' || type === 'submit' || type === 'reset' ? (
26+
<button type={type} class={twMerge(variants[variant] || '', className)} {...rest}>
27+
<Fragment set:html={text} />
28+
{icon && <Icon name={icon} class="w-5 h-5 ml-1 -mr-1.5 rtl:mr-1 rtl:-ml-1.5 inline-block" />}
29+
</button>
30+
) : (
31+
<a
32+
class={twMerge(variants[variant] || '', className)}
33+
{...(target ? { target: target, rel: 'noopener noreferrer' } : {})}
34+
{...rest}
35+
>
36+
<Fragment set:html={text} />
37+
{icon && <Icon name={icon} class="w-5 h-5 ml-1 -mr-1.5 rtl:mr-1 rtl:-ml-1.5 inline-block" />}
38+
</a>
39+
)
40+
}

src/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export interface CallToAction extends HTMLAttributes<a> {
180180
text?: string;
181181
icon?: string;
182182
classes?: Record<string, string>;
183+
type?: 'button' | 'submit' | 'reset';
183184
}
184185

185186
export interface ItemGrid {

0 commit comments

Comments
 (0)