-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix:
Drawer.Close
asChild behavior (#22)
- Loading branch information
Showing
4 changed files
with
93 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"vaul-svelte": patch | ||
--- | ||
|
||
fix: close asChild behavior |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters