Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow <Button/> with href to be disabled #1055

Merged
merged 2 commits into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/khaki-drinks-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": patch
---

Allow `<Button/>` with `href` to be disabled.
16 changes: 13 additions & 3 deletions packages/bits-ui/src/lib/bits/button/components/button.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
<script lang="ts">
import type { ButtonRootProps } from "../types.js";

let { href, type, children, ref = $bindable(), ...restProps }: ButtonRootProps = $props();
let {
href,
type,
children,
disabled = false,
ref = $bindable(),
...restProps
}: ButtonRootProps = $props();
</script>

<svelte:element
this={href ? "a" : "button"}
type={href ? undefined : type}
{href}
tabindex="0"
href={href && !disabled ? href : undefined}
disabled={href ? undefined : disabled}
aria-disabled={href ? disabled : undefined}
role={href && disabled ? "link" : undefined}
tabindex={href && disabled ? -1 : 0}
bind:this={ref}
{...restProps}
>
Expand Down
2 changes: 2 additions & 0 deletions packages/bits-ui/src/lib/bits/button/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ type AnchorElement = ButtonRootPropsWithoutHTML &
WithoutChildren<Omit<HTMLAnchorAttributes, "href" | "type">> & {
href: HTMLAnchorAttributes["href"];
type?: never;
disabled?: HTMLButtonAttributes["disabled"];
};

type ButtonElement = ButtonRootPropsWithoutHTML &
WithoutChildren<Omit<HTMLButtonAttributes, "type" | "href">> & {
type?: HTMLButtonAttributes["type"];
href?: never;
disabled?: HTMLButtonAttributes["disabled"];
};

export type ButtonRootProps = AnchorElement | ButtonElement;
11 changes: 10 additions & 1 deletion sites/docs/src/lib/content/api-reference/button.api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { ButtonRootPropsWithoutHTML } from "bits-ui";
import { childrenSnippet, createApiSchema, createPropSchema, refProp } from "./helpers.js";
import * as C from "$lib/content/constants.js";
import type { HTMLButtonAttributes } from "svelte/elements";

export const root = createApiSchema<ButtonRootPropsWithoutHTML & { href: string }>({
export const root = createApiSchema<
ButtonRootPropsWithoutHTML & { href: string; disabled: HTMLButtonAttributes["disabled"] }
>({
title: "Root",
description:
"A component that can switch between a button and an anchor tag based on the `href`/`type` props.",
Expand All @@ -12,6 +15,12 @@ export const root = createApiSchema<ButtonRootPropsWithoutHTML & { href: string
description:
"An optional prop that when passed converts the button into an anchor tag.",
}),
disabled: createPropSchema({
type: C.BOOLEAN,
description:
"Whether or not the button is disabled. When disabled, the button cannot be interacted with.",
default: "false",
}),
ref: refProp({ elType: "HTMLButtonElement" }),
children: childrenSnippet(),
},
Expand Down