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

fix: Drawer.Close asChild behavior #22

Merged
merged 2 commits into from
Jan 7, 2024
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/fluffy-dancers-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vaul-svelte": patch
---

fix: close asChild behavior
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = {
"warn",
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^\\$\\$(Props|Events|Slots|Generic)$"
varsIgnorePattern: "^(\\$\\$(Props|Events|Slots|Generic)|_*)$"
}
]
}
Expand Down
45 changes: 45 additions & 0 deletions src/lib/vaul/components/close-wrapper.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<script lang="ts">
// This is kinda weird but we need to be able to pass the builder down somehow
// if someone uses `asChild` to modify it. Since it's only exposed as a slot prop
// we need something in between the `<DialogPrimitive.Trigger>` and the slot

import type { Builder } from "$lib/internal/types.js";
import { getCtx } from "../ctx.js";

export let meltBuilder: Builder;

const {
methods: { closeDrawer }
} = getCtx();

$: ({ _, ...rest } = meltBuilder);

// We're wrapping the melt action so we can set the triggerRef
// even if a user is using `asChild`
const wrappedAction = (node: HTMLElement) => {
const handleKeydown = (e: KeyboardEvent) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
closeDrawer(true);
}
};

const handleClick = () => {
closeDrawer();
};

node.addEventListener("keydown", handleKeydown);
node.addEventListener("click", handleClick);

return () => {
node.removeEventListener("keydown", handleKeydown);
node.removeEventListener("click", handleClick);
};
};

$: Object.assign(rest, {
action: wrappedAction
});
</script>

<slot newBuilder={rest} />
57 changes: 42 additions & 15 deletions src/lib/vaul/components/close.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,56 @@
import { Dialog as DialogPrimitive } from "bits-ui";
import type { CloseProps } from "./types.js";
import { getCtx } from "../ctx.js";
import CloseWrapper from "./close-wrapper.svelte";

type $$Props = CloseProps;

export let el: $$Props["el"] = undefined;
export let asChild = false;

const {
methods: { closeDrawer }
} = getCtx();
</script>

<DialogPrimitive.Close
bind:el
on:click={(e) => {
e.preventDefault();
closeDrawer();
}}
on:keydown={(e) => {
if (e.detail.originalEvent.key === "Enter" || e.detail.originalEvent.key === " ") {
{#if asChild}
<DialogPrimitive.Close
bind:el
on:click={(e) => {
e.preventDefault();
closeDrawer(true);
}
}}
{...$$restProps}
>
<slot />
</DialogPrimitive.Close>
closeDrawer();
}}
on:keydown={(e) => {
if (e.detail.originalEvent.key === "Enter" || e.detail.originalEvent.key === " ") {
e.preventDefault();
closeDrawer(true);
}
}}
{...$$restProps}
{asChild}
let:builder
>
<CloseWrapper meltBuilder={builder} let:newBuilder>
<slot builder={newBuilder} />
</CloseWrapper>
</DialogPrimitive.Close>
{:else}
<DialogPrimitive.Close
bind:el
on:click={(e) => {
e.preventDefault();
closeDrawer();
}}
on:keydown={(e) => {
if (e.detail.originalEvent.key === "Enter" || e.detail.originalEvent.key === " ") {
e.preventDefault();
closeDrawer(true);
}
}}
{...$$restProps}
{asChild}
let:builder
>
<slot {builder} />
</DialogPrimitive.Close>
{/if}