Skip to content

Commit

Permalink
Add support for Anchor dropdown items (#131)
Browse files Browse the repository at this point in the history
Co-authored-by: Hunter Johnston <[email protected]>
  • Loading branch information
saturnonearth and huntabyte authored Oct 24, 2023
1 parent 62c1cee commit e645ad2
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-spoons-heal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"bits-ui": minor
---

Add support for Anchor dropdown items
1 change: 1 addition & 0 deletions src/components/demos/dropdown-menu-demo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<DropdownMenu.Item>
<span>Support</span>
</DropdownMenu.Item>
<DropdownMenu.Item href="/">Anchor Link</DropdownMenu.Item>
<DropdownMenu.Item>
<span>API</span>
</DropdownMenu.Item>
Expand Down
5 changes: 5 additions & 0 deletions src/content/api-reference/dropdown-menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ export const item: APISchema = {
type: "boolean",
default: "false",
description: "Whether or not the menu item is disabled."
},
{
name: "href",
type: "string",
description: "An optional prop that when passed converts the dropdown item into an anchor tag."
}
],
dataAttributes: [
Expand Down
11 changes: 7 additions & 4 deletions src/lib/bits/dropdown-menu/components/DropdownMenuItem.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script lang="ts">
import { melt } from "@melt-ui/svelte";
import { createDispatcher } from "$lib/internal/events.js";
import { disabledAttrs } from "$lib/internal/helpers.js";
import { melt } from "@melt-ui/svelte";
import { ctx } from "../ctx.js";
import type { ItemEvents, ItemProps } from "../types.js";
import { createDispatcher } from "$lib/internal/events.js";
type $$Props = ItemProps;
type $$Events = ItemEvents;
export let href: $$Props["href"] = undefined;
export let asChild = false;
export let disabled = false;
const {
Expand All @@ -20,7 +21,9 @@
<slot builder={$item} />
{:else}
{@const builder = $item}
<div
<svelte:element
this={href ? "a" : "div"}
{href}
use:melt={builder}
{...$$restProps}
on:m-click={dispatch}
Expand All @@ -33,5 +36,5 @@
{...disabledAttrs(disabled)}
>
<slot {builder} />
</div>
</svelte:element>
{/if}
62 changes: 31 additions & 31 deletions src/lib/bits/dropdown-menu/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import type {
DropdownMenuRadioItemProps,
CreateDropdownSubmenuProps
} from "@melt-ui/svelte";
import type { HTMLButtonAttributes } from "svelte/elements";
import type { HTMLAnchorAttributes, HTMLButtonAttributes } from "svelte/elements";

type Props = Expand<
OmitOpen<CreateDropdownMenuProps> & {
Expand Down Expand Up @@ -61,6 +61,7 @@ type GroupProps = AsChild & HTMLDivAttributes;
type ItemProps = Expand<
{
disabled?: boolean;
href?: HTMLAnchorAttributes["href"];
} & AsChild
> &
HTMLDivAttributes;
Expand Down Expand Up @@ -128,52 +129,51 @@ type SubContentEvents<T extends Element = HTMLDivElement> = {
};

export type {
Props,
ArrowProps,
CheckboxItemEvents,
CheckboxItemIndicatorProps,
CheckboxItemProps,
ContentEvents,
ContentProps,
GroupProps,
ItemProps,
CheckboxItemIndicatorProps,
LabelProps,
RadioGroupProps,
RadioItemProps,
SeparatorProps,
SubProps,
SubContentProps,
SubTriggerProps,
TriggerProps,

//
Props as DropdownMenuProps,
ArrowProps as DropdownMenuArrowProps,
CheckboxItemEvents as DropdownMenuCheckboxItemEvents,
CheckboxItemIndicatorProps as DropdownMenuCheckboxItemIndicatorProps,
CheckboxItemProps as DropdownMenuCheckboxItemProps,
ContentEvents as DropdownMenuContentEvents,
ContentProps as DropdownMenuContentProps,
GroupProps as DropdownMenuGroupProps,
ItemEvents as DropdownMenuItemEvents,
ItemProps as DropdownMenuItemProps,
CheckboxItemIndicatorProps as DropdownMenuCheckboxItemIndicatorProps,
LabelProps as DropdownMenuLabelProps,
//
Props as DropdownMenuProps,
RadioGroupProps as DropdownMenuRadioGroupProps,
RadioItemEvents as DropdownMenuRadioItemEvents,
RadioItemProps as DropdownMenuRadioItemProps,
SeparatorProps as DropdownMenuSeparatorProps,
SubProps as DropdownMenuSubProps,
SubContentEvents as DropdownMenuSubContentEvents,
SubContentProps as DropdownMenuSubContentProps,
SubProps as DropdownMenuSubProps,
SubTriggerEvents as DropdownMenuSubTriggerEvents,
SubTriggerProps as DropdownMenuSubTriggerProps,
//,
TriggerEvents as DropdownMenuTriggerEvents,
TriggerProps as DropdownMenuTriggerProps,
//
TriggerEvents,
CheckboxItemEvents,
ContentEvents,
GroupProps,
ItemEvents,
ItemProps,
LabelProps,
Props,
RadioGroupProps,
RadioItemEvents,
RadioItemProps,
SeparatorProps,
SubContentEvents,
SubContentProps,
SubProps,
SubTriggerEvents,
ItemEvents,
//,
TriggerEvents as DropdownMenuTriggerEvents,
CheckboxItemEvents as DropdownMenuCheckboxItemEvents,
ContentEvents as DropdownMenuContentEvents,
RadioItemEvents as DropdownMenuRadioItemEvents,
SubContentEvents as DropdownMenuSubContentEvents,
SubTriggerEvents as DropdownMenuSubTriggerEvents,
ItemEvents as DropdownMenuItemEvents
SubTriggerProps,
//
TriggerEvents,
TriggerProps
};

0 comments on commit e645ad2

Please sign in to comment.