index.astro
<ButtonGroup size="xl">
<Button pill color="purple">Profile</Button>
<Button pill color="purple">Settings</Button>
<Button pill color="purple">Messages</Button>
</ButtonGroup>
ButtonGroup.astro
---
import type { HTMLAttributes } from "astro/types";
import { twMerge } from "tailwind-merge";
import type { SizeType } from "../types";
export interface Props extends HTMLAttributes<"div"> {
size?: SizeType;
divClass?: string;
}
const {
size = "xs",
divClass = "inline-flex rounded-lg shadow-sm",
...attrs
} = Astro.props;
---
<div {...attrs} class={twMerge(divClass, attrs.class)} role="group">
<slot />
</div>
Button.astro
---
import type { HTMLAttributes } from "astro/types";
import { twMerge } from "tailwind-merge";
import type { ButtonType, SizeType } from "../types";
type ButtonColor = keyof typeof colorClasses;
export interface Props extends HTMLAttributes<"button" & "a"> {
group?: SizeType | undefined;
pill?: boolean;
outline?: boolean;
size?: "xs" | "sm" | "md" | "lg" | "xl";
href?: string | undefined;
type?: ButtonType;
color?: ButtonColor;
shadow?: boolean;
}
const {
group = undefined,
pill = false,
outline = false,
size = group ? "sm" : "md",
href = undefined,
type = "button",
color = group ? (outline ? "dark" : "alternative") : "primary",
shadow = false,
...attrs
} = Astro.props;
....
---
<Element
type={href ? undefined : type}
{href}
class={buttonClass}
role={href ? "link" : "button"}
{...attrs}
>
<slot />
</Element>