Skip to content

Commit

Permalink
Fix Button to render a <button> tag if the 'type' attribute exists. I…
Browse files Browse the repository at this point in the history
…ssue onwidget#299
  • Loading branch information
prototypa committed Dec 2, 2023
1 parent 788acd9 commit 9818cd0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/components/ui/Button.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,35 @@ 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;
const variants = {
primary: 'btn-primary' ,
primary: 'btn-primary',
secondary: 'btn-secondary',
tertiary: 'btn btn-tertiary',
link: 'cursor-pointer hover:text-primary',
};
---

<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>
{
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 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 9818cd0

Please sign in to comment.