merge: integrate reviewed AlertDialog Drawer with ListBox main
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
import { defineComponent, h, provide, type PropType } from "vue";
|
||||
import {
|
||||
useOverlayState,
|
||||
type OverlayState,
|
||||
} from "../../composables/useOverlayState";
|
||||
import OverlayDialogRoot from "../overlay/OverlayDialogRoot.vue";
|
||||
import { overlayNodes, overlayComponentName } from "../overlay/OverlayAction";
|
||||
import { createAlertDialogContext, alertDialogKey } from "./context";
|
||||
import AlertDialogTrigger from "./AlertDialogTrigger.vue";
|
||||
|
||||
export default defineComponent({
|
||||
name: "AlertDialog",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
open: { type: Boolean, default: undefined },
|
||||
isOpen: { type: Boolean, default: undefined },
|
||||
defaultOpen: Boolean,
|
||||
state: Object as PropType<OverlayState>,
|
||||
},
|
||||
emits: {
|
||||
"update:open": (value: boolean) => typeof value === "boolean",
|
||||
"update:isOpen": (value: boolean) => typeof value === "boolean",
|
||||
openChange: (value: boolean) => typeof value === "boolean",
|
||||
},
|
||||
setup(props, { slots, emit }) {
|
||||
const state = useOverlayState({
|
||||
isOpen: () => props.state?.isOpen.value ?? props.open ?? props.isOpen,
|
||||
defaultOpen: props.defaultOpen,
|
||||
onOpenChange(value) {
|
||||
props.state?.setOpen(value);
|
||||
emit("update:open", value);
|
||||
emit("update:isOpen", value);
|
||||
emit("openChange", value);
|
||||
},
|
||||
});
|
||||
provide(alertDialogKey, createAlertDialogContext(state));
|
||||
return () =>
|
||||
h(
|
||||
OverlayDialogRoot,
|
||||
{ state },
|
||||
{
|
||||
default: () => {
|
||||
let found = false;
|
||||
return overlayNodes(
|
||||
slots.default?.({
|
||||
isOpen: state.isOpen.value,
|
||||
close: state.close,
|
||||
}),
|
||||
).map((node) => {
|
||||
const name = overlayComponentName(node);
|
||||
if (name === "AlertDialogTrigger") found = true;
|
||||
if (
|
||||
!found &&
|
||||
(name === "Button" ||
|
||||
node.type === "button" ||
|
||||
node.type === "a")
|
||||
) {
|
||||
found = true;
|
||||
return h(
|
||||
AlertDialogTrigger,
|
||||
{ asChild: true, key: node.key ?? undefined },
|
||||
{ default: () => node },
|
||||
);
|
||||
}
|
||||
return node;
|
||||
});
|
||||
},
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,64 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h, inject, type PropType } from "vue";
|
||||
import type { AlertDialogVariants } from "@heroui-vue/styles";
|
||||
import type { OverlayState } from "../../composables/useOverlayState";
|
||||
import AlertDialog from "./AlertDialog";
|
||||
import AlertDialogBackdropSurface from "./AlertDialogBackdropSurface.vue";
|
||||
import { alertDialogKey } from "./context";
|
||||
|
||||
/**
|
||||
* Alert dialogs require explicit action by default:
|
||||
* isDismissable=false, isKeyboardDismissDisabled=true.
|
||||
* Both remain independently configurable.
|
||||
*/
|
||||
export default defineComponent({
|
||||
name: "AlertDialogBackdrop",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
open: { type: Boolean, default: undefined },
|
||||
isOpen: { type: Boolean, default: undefined },
|
||||
defaultOpen: Boolean,
|
||||
state: Object as PropType<OverlayState>,
|
||||
variant: String as PropType<AlertDialogVariants["variant"]>,
|
||||
isDismissable: { type: Boolean, default: false },
|
||||
isKeyboardDismissDisabled: { type: Boolean, default: true },
|
||||
to: [String, Object] as PropType<string | HTMLElement>,
|
||||
},
|
||||
emits: {
|
||||
"update:open": (value: boolean) => typeof value === "boolean",
|
||||
"update:isOpen": (value: boolean) => typeof value === "boolean",
|
||||
openChange: (value: boolean) => typeof value === "boolean",
|
||||
},
|
||||
setup(props, { attrs, slots, emit }) {
|
||||
const parent = inject(alertDialogKey, undefined);
|
||||
return () => {
|
||||
const surface = () =>
|
||||
h(
|
||||
AlertDialogBackdropSurface,
|
||||
{
|
||||
...attrs,
|
||||
variant: props.variant,
|
||||
isDismissable: props.isDismissable,
|
||||
isKeyboardDismissDisabled: props.isKeyboardDismissDisabled,
|
||||
to: props.to,
|
||||
},
|
||||
slots,
|
||||
);
|
||||
if (parent) return surface();
|
||||
return h(
|
||||
AlertDialog,
|
||||
{
|
||||
open: props.open,
|
||||
isOpen: props.isOpen,
|
||||
defaultOpen: props.defaultOpen,
|
||||
state: props.state,
|
||||
"onUpdate:open": (v: boolean) => emit("update:open", v),
|
||||
"onUpdate:isOpen": (v: boolean) => emit("update:isOpen", v),
|
||||
onOpenChange: (v: boolean) => emit("openChange", v),
|
||||
},
|
||||
{ default: surface },
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,57 @@
|
||||
<script setup lang="ts">
|
||||
import { useForwardExpose } from "reka-ui";
|
||||
import { computed, provide } from "vue";
|
||||
import {
|
||||
alertDialogVariants,
|
||||
type AlertDialogVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
import OverlayPortal from "../overlay/OverlayPortal.vue";
|
||||
import OverlayBackdrop from "../overlay/OverlayBackdrop.vue";
|
||||
import { alertDialogKey, useAlertDialog } from "./context";
|
||||
import { useOverlayViewport } from "../overlay/viewport";
|
||||
import { useOverlayAnimation } from "../overlay/animation";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
variant?: AlertDialogVariants["variant"];
|
||||
isDismissable?: boolean;
|
||||
isKeyboardDismissDisabled?: boolean;
|
||||
to?: string | HTMLElement;
|
||||
}>(),
|
||||
{
|
||||
variant: "opaque",
|
||||
// Alert dialogs require explicit action by default.
|
||||
isDismissable: false,
|
||||
isKeyboardDismissDisabled: true,
|
||||
},
|
||||
);
|
||||
const ctx = useAlertDialog();
|
||||
const height = useOverlayViewport();
|
||||
const { forwardRef, currentElement } = useForwardExpose();
|
||||
provide(alertDialogKey, {
|
||||
...ctx,
|
||||
variant: computed(() => props.variant),
|
||||
dismissable: computed(() => props.isDismissable),
|
||||
keyboardDisabled: computed(() => props.isKeyboardDismissDisabled),
|
||||
});
|
||||
const { entering, exiting, onAnimationEnd } = useOverlayAnimation(
|
||||
ctx.state.isOpen,
|
||||
currentElement,
|
||||
);
|
||||
</script>
|
||||
<template>
|
||||
<OverlayPortal :to="to">
|
||||
<OverlayBackdrop
|
||||
:ref="forwardRef"
|
||||
v-bind="$attrs"
|
||||
:style="{ '--visual-viewport-height': height }"
|
||||
:class="alertDialogVariants({ variant }).backdrop()"
|
||||
data-slot="alert-dialog-backdrop"
|
||||
:data-entering="entering || undefined"
|
||||
:data-exiting="exiting || undefined"
|
||||
@animationend="onAnimationEnd"
|
||||
>
|
||||
<slot :is-open="ctx.state.isOpen.value" :close="ctx.state.close" />
|
||||
</OverlayBackdrop>
|
||||
</OverlayPortal>
|
||||
</template>
|
||||
@@ -0,0 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
defineOptions({ inheritAttrs: false });
|
||||
</script>
|
||||
<template>
|
||||
<div
|
||||
v-bind="$attrs"
|
||||
class="hv-alert-dialog__body"
|
||||
data-slot="alert-dialog-body"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,29 @@
|
||||
import OverlayAction from "../overlay/OverlayAction";
|
||||
import { useAlertDialog } from "./context";
|
||||
import { defineComponent, h } from "vue";
|
||||
|
||||
/** Closes the alert dialog when activated (replaces RAC slot="close"). */
|
||||
export default defineComponent({
|
||||
name: "AlertDialogClose",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
as: { type: String, default: "button" },
|
||||
asChild: { type: Boolean, default: false },
|
||||
isDisabled: { type: Boolean, default: false },
|
||||
},
|
||||
setup(props, { attrs, slots }) {
|
||||
const ctx = useAlertDialog();
|
||||
return () =>
|
||||
h(
|
||||
OverlayAction,
|
||||
{
|
||||
...attrs,
|
||||
as: props.as,
|
||||
asChild: props.asChild,
|
||||
isDisabled: props.isDisabled,
|
||||
activate: ctx.state.close,
|
||||
},
|
||||
slots,
|
||||
);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { defineComponent, h, mergeProps, type PropType } from "vue";
|
||||
import type { CloseButtonVariants } from "@heroui-vue/styles";
|
||||
import CloseButton from "../close-button/CloseButton.vue";
|
||||
import { useAlertDialog } from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "AlertDialogCloseTrigger",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
variant: String as PropType<CloseButtonVariants["variant"]>,
|
||||
isDisabled: Boolean,
|
||||
disabled: Boolean,
|
||||
},
|
||||
setup(props, { attrs, slots }) {
|
||||
const ctx = useAlertDialog();
|
||||
return () =>
|
||||
h(
|
||||
CloseButton,
|
||||
mergeProps(attrs, props, {
|
||||
class: "hv-alert-dialog__close-trigger",
|
||||
"data-slot": "alert-dialog-close-trigger",
|
||||
onClick: (event: MouseEvent) => {
|
||||
if (!event.defaultPrevented) ctx.state.close();
|
||||
},
|
||||
}),
|
||||
slots,
|
||||
);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,48 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, provide, ref } from "vue";
|
||||
import {
|
||||
alertDialogVariants,
|
||||
type AlertDialogVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
import {
|
||||
alertDialogKey,
|
||||
useAlertDialog,
|
||||
type AlertDialogPlacement,
|
||||
} from "./context";
|
||||
import { useOverlayAnimation } from "../overlay/animation";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
placement?: AlertDialogPlacement;
|
||||
size?: AlertDialogVariants["size"];
|
||||
as?: string;
|
||||
}>(),
|
||||
{ placement: "auto", size: "md", as: "div" },
|
||||
);
|
||||
const ctx = useAlertDialog();
|
||||
provide(alertDialogKey, {
|
||||
...ctx,
|
||||
placement: computed(() => props.placement),
|
||||
size: computed(() => props.size),
|
||||
});
|
||||
const container = ref<HTMLElement>();
|
||||
const { entering, exiting, onAnimationEnd } = useOverlayAnimation(
|
||||
ctx.state.isOpen,
|
||||
container,
|
||||
);
|
||||
</script>
|
||||
<template>
|
||||
<component
|
||||
:is="as"
|
||||
ref="container"
|
||||
v-bind="$attrs"
|
||||
:class="alertDialogVariants({ size }).container()"
|
||||
data-slot="alert-dialog-container"
|
||||
:data-placement="placement"
|
||||
:data-entering="entering || undefined"
|
||||
:data-exiting="exiting || undefined"
|
||||
@animationend="onAnimationEnd"
|
||||
>
|
||||
<slot :close="ctx.state.close" />
|
||||
</component>
|
||||
</template>
|
||||
@@ -0,0 +1,52 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, provide, watchEffect, onUnmounted, useAttrs } from "vue";
|
||||
import { DialogTitle } from "reka-ui";
|
||||
import { alertDialogVariants } from "@heroui-vue/styles";
|
||||
import OverlayDialogContent from "../overlay/OverlayDialogContent.vue";
|
||||
import { surfaceContextKey } from "../surface/context";
|
||||
import { useAlertDialog } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = withDefaults(defineProps<{ as?: string; id?: string }>(), {
|
||||
as: "div",
|
||||
});
|
||||
const ctx = useAlertDialog();
|
||||
const attrs = useAttrs();
|
||||
watchEffect(() => {
|
||||
ctx.contentId.value = props.id ?? ctx.defaultId;
|
||||
});
|
||||
onUnmounted(() => {
|
||||
ctx.contentId.value = ctx.defaultId;
|
||||
});
|
||||
provide(surfaceContextKey, { variant: computed(() => "default" as const) });
|
||||
</script>
|
||||
<template>
|
||||
<OverlayDialogContent
|
||||
v-bind="attrs"
|
||||
:as="as"
|
||||
:id="ctx.contentId.value"
|
||||
:class="alertDialogVariants({ size: ctx.size.value }).dialog()"
|
||||
:is-dismissable="ctx.dismissable.value"
|
||||
:is-keyboard-dismiss-disabled="ctx.keyboardDisabled.value"
|
||||
role="alertdialog"
|
||||
data-slot="alert-dialog-dialog"
|
||||
:data-exiting="!ctx.state.isOpen.value || undefined"
|
||||
:data-placement="ctx.placement.value"
|
||||
:aria-labelledby="
|
||||
'aria-labelledby' in attrs
|
||||
? (attrs['aria-labelledby'] as string)
|
||||
: Array.from(ctx.titles.values()).join(' ') || undefined
|
||||
"
|
||||
:aria-describedby="attrs['aria-describedby'] as string | undefined"
|
||||
>
|
||||
<DialogTitle
|
||||
v-if="
|
||||
('aria-label' in attrs || 'aria-labelledby' in attrs) &&
|
||||
!ctx.titles.size
|
||||
"
|
||||
as="span"
|
||||
class="sr-only"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<slot :close="ctx.state.close" :is-open="ctx.state.isOpen.value" />
|
||||
</OverlayDialogContent>
|
||||
</template>
|
||||
@@ -0,0 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
defineOptions({ inheritAttrs: false });
|
||||
</script>
|
||||
<template>
|
||||
<div
|
||||
v-bind="$attrs"
|
||||
class="hv-alert-dialog__footer"
|
||||
data-slot="alert-dialog-footer"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,12 @@
|
||||
<script setup lang="ts">
|
||||
defineOptions({ inheritAttrs: false });
|
||||
</script>
|
||||
<template>
|
||||
<div
|
||||
v-bind="$attrs"
|
||||
class="hv-alert-dialog__header"
|
||||
data-slot="alert-dialog-header"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,31 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, watchEffect, onUnmounted, useId } from "vue";
|
||||
import { DialogTitle, injectDialogRootContext } from "reka-ui";
|
||||
import { useAlertDialog } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = withDefaults(defineProps<{ as?: string; id?: string }>(), {
|
||||
as: "h2",
|
||||
});
|
||||
const ctx = useAlertDialog();
|
||||
const root = injectDialogRootContext();
|
||||
const suffix = useId();
|
||||
const key = Symbol();
|
||||
const generated = ctx.titles.size ? `${root.titleId}-${suffix}` : root.titleId;
|
||||
const id = computed(() => props.id ?? generated);
|
||||
watchEffect(() => {
|
||||
ctx.titles.set(key, id.value);
|
||||
});
|
||||
onUnmounted(() => ctx.titles.delete(key));
|
||||
</script>
|
||||
<template>
|
||||
<DialogTitle v-if="props.id" as="span" class="sr-only" aria-hidden="true" />
|
||||
<DialogTitle
|
||||
v-bind="$attrs"
|
||||
:as="as"
|
||||
:id="id"
|
||||
class="hv-alert-dialog__heading"
|
||||
data-slot="alert-dialog-heading"
|
||||
>
|
||||
<slot />
|
||||
</DialogTitle>
|
||||
</template>
|
||||
@@ -0,0 +1,68 @@
|
||||
<script lang="ts">
|
||||
import { computed, defineComponent, h, useAttrs, type PropType } from "vue";
|
||||
import {
|
||||
alertDialogVariants,
|
||||
type AlertDialogVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
import {
|
||||
AlertDialogDangerIcon,
|
||||
AlertDialogInfoIcon,
|
||||
AlertDialogSuccessIcon,
|
||||
AlertDialogWarningIcon,
|
||||
} from "./icons";
|
||||
import type { AlertDialogStatus } from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "AlertDialogIcon",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
status: {
|
||||
type: String as PropType<AlertDialogStatus>,
|
||||
default: "danger",
|
||||
},
|
||||
as: {
|
||||
type: String,
|
||||
default: "div",
|
||||
},
|
||||
},
|
||||
setup(props, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const classes = computed(() =>
|
||||
alertDialogVariants({
|
||||
status: props.status as AlertDialogVariants["status"],
|
||||
}).icon(),
|
||||
);
|
||||
function defaultIcon() {
|
||||
switch (props.status) {
|
||||
case "default":
|
||||
case "accent":
|
||||
return h(AlertDialogInfoIcon, {
|
||||
"data-slot": "alert-dialog-default-icon",
|
||||
});
|
||||
case "success":
|
||||
return h(AlertDialogSuccessIcon, {
|
||||
"data-slot": "alert-dialog-default-icon",
|
||||
});
|
||||
case "warning":
|
||||
return h(AlertDialogWarningIcon, {
|
||||
"data-slot": "alert-dialog-default-icon",
|
||||
});
|
||||
default:
|
||||
return h(AlertDialogDangerIcon, {
|
||||
"data-slot": "alert-dialog-default-icon",
|
||||
});
|
||||
}
|
||||
}
|
||||
return () =>
|
||||
h(
|
||||
props.as,
|
||||
{
|
||||
...attrs,
|
||||
class: [classes.value, attrs.class],
|
||||
"data-slot": "alert-dialog-icon",
|
||||
},
|
||||
slots.default?.() ?? [defaultIcon()],
|
||||
);
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,38 @@
|
||||
<script setup lang="ts">
|
||||
import { watch } from "vue";
|
||||
import { injectDialogRootContext, useForwardExpose } from "reka-ui";
|
||||
import OverlayAction from "../overlay/OverlayAction";
|
||||
import { useAlertDialog } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
withDefaults(
|
||||
defineProps<{ as?: string; asChild?: boolean; isDisabled?: boolean }>(),
|
||||
{ as: "div" },
|
||||
);
|
||||
const ctx = useAlertDialog();
|
||||
const dialog = injectDialogRootContext();
|
||||
const { forwardRef, currentElement } = useForwardExpose();
|
||||
watch(
|
||||
currentElement,
|
||||
(el) => {
|
||||
dialog.triggerElement.value = el;
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
</script>
|
||||
<template>
|
||||
<OverlayAction
|
||||
:ref="forwardRef"
|
||||
v-bind="$attrs"
|
||||
:as="as"
|
||||
:as-child="asChild"
|
||||
:is-disabled="isDisabled"
|
||||
:activate="ctx.state.open"
|
||||
class="hv-alert-dialog__trigger"
|
||||
data-slot="alert-dialog-trigger"
|
||||
aria-haspopup="dialog"
|
||||
:aria-expanded="ctx.state.isOpen.value"
|
||||
:aria-controls="ctx.state.isOpen.value ? ctx.contentId.value : undefined"
|
||||
>
|
||||
<slot />
|
||||
</OverlayAction>
|
||||
</template>
|
||||
@@ -0,0 +1,60 @@
|
||||
import {
|
||||
computed,
|
||||
inject,
|
||||
reactive,
|
||||
ref,
|
||||
useId,
|
||||
type ComputedRef,
|
||||
type InjectionKey,
|
||||
type Ref,
|
||||
} from "vue";
|
||||
import type { AlertDialogVariants } from "@heroui-vue/styles";
|
||||
import type { OverlayState } from "../../composables/useOverlayState";
|
||||
|
||||
export type AlertDialogPlacement = "auto" | "top" | "center" | "bottom";
|
||||
export type AlertDialogStatus =
|
||||
"default" | "accent" | "success" | "warning" | "danger";
|
||||
|
||||
export interface AlertDialogContext {
|
||||
state: OverlayState;
|
||||
contentId: Ref<string>;
|
||||
defaultId: string;
|
||||
titles: Map<symbol, string>;
|
||||
placement: ComputedRef<AlertDialogPlacement>;
|
||||
size: ComputedRef<AlertDialogVariants["size"]>;
|
||||
/** Backdrop variant; defaults to opaque. */
|
||||
variant: ComputedRef<AlertDialogVariants["variant"]>;
|
||||
dismissable: ComputedRef<boolean>;
|
||||
keyboardDisabled: ComputedRef<boolean>;
|
||||
}
|
||||
|
||||
export const alertDialogKey: InjectionKey<AlertDialogContext> =
|
||||
Symbol("hv-alert-dialog");
|
||||
|
||||
export function useAlertDialog(required = true) {
|
||||
const ctx = inject(alertDialogKey, undefined);
|
||||
if (!ctx && required) {
|
||||
throw new Error(
|
||||
"AlertDialog parts require AlertDialog or AlertDialogBackdrop.",
|
||||
);
|
||||
}
|
||||
return ctx!;
|
||||
}
|
||||
|
||||
export function createAlertDialogContext(
|
||||
state: OverlayState,
|
||||
): AlertDialogContext {
|
||||
const id = "hv-alert-dialog-" + useId();
|
||||
return {
|
||||
state,
|
||||
contentId: ref(id),
|
||||
defaultId: id,
|
||||
titles: reactive(new Map()),
|
||||
// Alert dialogs require explicit action by default.
|
||||
placement: computed(() => "auto"),
|
||||
size: computed(() => "md"),
|
||||
variant: computed(() => "opaque"),
|
||||
dismissable: computed(() => false),
|
||||
keyboardDisabled: computed(() => true),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/** Baseline icons from HeroUI icons.tsx (Apache-2.0). */
|
||||
import { defineComponent, h } from "vue";
|
||||
|
||||
function icon(name: string, d: string) {
|
||||
return defineComponent({
|
||||
name,
|
||||
inheritAttrs: false,
|
||||
setup(_, { attrs }) {
|
||||
return () =>
|
||||
h(
|
||||
"svg",
|
||||
{
|
||||
...attrs,
|
||||
"aria-hidden": "true",
|
||||
fill: "none",
|
||||
height: "16",
|
||||
role: "presentation",
|
||||
viewBox: "0 0 16 16",
|
||||
width: "16",
|
||||
xmlns: "http://www.w3.org/2000/svg",
|
||||
},
|
||||
[
|
||||
h("path", {
|
||||
"clip-rule": "evenodd",
|
||||
d,
|
||||
fill: "currentColor",
|
||||
"fill-rule": "evenodd",
|
||||
}),
|
||||
],
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export const AlertDialogInfoIcon = icon(
|
||||
"AlertDialogInfoIcon",
|
||||
"M8 13.5a5.5 5.5 0 1 0 0-11a5.5 5.5 0 0 0 0 11M8 15A7 7 0 1 0 8 1a7 7 0 0 0 0 14m1-9.5a1 1 0 1 1-2 0a1 1 0 0 1 2 0m-.25 3a.75.75 0 0 0-1.5 0V11a.75.75 0 0 0 1.5 0z",
|
||||
);
|
||||
|
||||
export const AlertDialogWarningIcon = icon(
|
||||
"AlertDialogWarningIcon",
|
||||
"M7.134 2.994L2.217 11.5a1 1 0 0 0 .866 1.5h9.834a1 1 0 0 0 .866-1.5L8.866 2.993a1 1 0 0 0-1.732 0m3.03-.75c-.962-1.665-3.366-1.665-4.329 0L.918 10.749c-.963 1.666.24 3.751 2.165 3.751h9.834c1.925 0 3.128-2.085 2.164-3.751zM8 5a.75.75 0 0 1 .75.75v2a.75.75 0 0 1-1.5 0v-2A.75.75 0 0 1 8 5m1 5.75a1 1 0 1 1-2 0a1 1 0 0 1 2 0",
|
||||
);
|
||||
|
||||
export const AlertDialogDangerIcon = icon(
|
||||
"AlertDialogDangerIcon",
|
||||
"M8 13.5a5.5 5.5 0 1 0 0-11a5.5 5.5 0 0 0 0 11M8 15A7 7 0 1 0 8 1a7 7 0 0 0 0 14m1-4.5a1 1 0 1 1-2 0a1 1 0 0 1 2 0M8.75 5a.75.75 0 0 0-1.5 0v2.5a.75.75 0 0 0 1.5 0z",
|
||||
);
|
||||
|
||||
export const AlertDialogSuccessIcon = icon(
|
||||
"AlertDialogSuccessIcon",
|
||||
"M13.5 8a5.5 5.5 0 1 1-11 0a5.5 5.5 0 0 1 11 0M15 8A7 7 0 1 1 1 8a7 7 0 0 1 14 0m-3.9-1.55a.75.75 0 1 0-1.2-.9L7.419 8.858L6.03 7.47a.75.75 0 0 0-1.06 1.06l2 2a.75.75 0 0 0 1.13-.08z",
|
||||
);
|
||||
@@ -0,0 +1,71 @@
|
||||
import { defineComponent, h, provide, type PropType } from "vue";
|
||||
import {
|
||||
useOverlayState,
|
||||
type OverlayState,
|
||||
} from "../../composables/useOverlayState";
|
||||
import OverlayDialogRoot from "../overlay/OverlayDialogRoot.vue";
|
||||
import { overlayNodes, overlayComponentName } from "../overlay/OverlayAction";
|
||||
import { createDrawerContext, drawerKey } from "./context";
|
||||
import DrawerTrigger from "./DrawerTrigger.vue";
|
||||
|
||||
export default defineComponent({
|
||||
name: "Drawer",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
open: { type: Boolean, default: undefined },
|
||||
isOpen: { type: Boolean, default: undefined },
|
||||
defaultOpen: Boolean,
|
||||
state: Object as PropType<OverlayState>,
|
||||
},
|
||||
emits: {
|
||||
"update:open": (value: boolean) => typeof value === "boolean",
|
||||
"update:isOpen": (value: boolean) => typeof value === "boolean",
|
||||
openChange: (value: boolean) => typeof value === "boolean",
|
||||
},
|
||||
setup(props, { slots, emit }) {
|
||||
const state = useOverlayState({
|
||||
isOpen: () => props.state?.isOpen.value ?? props.open ?? props.isOpen,
|
||||
defaultOpen: props.defaultOpen,
|
||||
onOpenChange(value) {
|
||||
props.state?.setOpen(value);
|
||||
emit("update:open", value);
|
||||
emit("update:isOpen", value);
|
||||
emit("openChange", value);
|
||||
},
|
||||
});
|
||||
provide(drawerKey, createDrawerContext(state));
|
||||
return () =>
|
||||
h(
|
||||
OverlayDialogRoot,
|
||||
{ state },
|
||||
{
|
||||
default: () => {
|
||||
let found = false;
|
||||
return overlayNodes(
|
||||
slots.default?.({
|
||||
isOpen: state.isOpen.value,
|
||||
close: state.close,
|
||||
}),
|
||||
).map((node) => {
|
||||
const name = overlayComponentName(node);
|
||||
if (name === "DrawerTrigger") found = true;
|
||||
if (
|
||||
!found &&
|
||||
(name === "Button" ||
|
||||
node.type === "button" ||
|
||||
node.type === "a")
|
||||
) {
|
||||
found = true;
|
||||
return h(
|
||||
DrawerTrigger,
|
||||
{ asChild: true, key: node.key ?? undefined },
|
||||
{ default: () => node },
|
||||
);
|
||||
}
|
||||
return node;
|
||||
});
|
||||
},
|
||||
},
|
||||
);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h, inject, type PropType } from "vue";
|
||||
import type { DrawerVariants } from "@heroui-vue/styles";
|
||||
import type { OverlayState } from "../../composables/useOverlayState";
|
||||
import Drawer from "./Drawer";
|
||||
import DrawerBackdropSurface from "./DrawerBackdropSurface.vue";
|
||||
import { drawerKey } from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "DrawerBackdrop",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
open: { type: Boolean, default: undefined },
|
||||
isOpen: { type: Boolean, default: undefined },
|
||||
defaultOpen: Boolean,
|
||||
state: Object as PropType<OverlayState>,
|
||||
variant: String as PropType<DrawerVariants["variant"]>,
|
||||
isDismissable: { type: Boolean, default: true },
|
||||
isKeyboardDismissDisabled: Boolean,
|
||||
to: [String, Object] as PropType<string | HTMLElement>,
|
||||
},
|
||||
emits: {
|
||||
"update:open": (value: boolean) => typeof value === "boolean",
|
||||
"update:isOpen": (value: boolean) => typeof value === "boolean",
|
||||
openChange: (value: boolean) => typeof value === "boolean",
|
||||
},
|
||||
setup(props, { attrs, slots, emit }) {
|
||||
const parent = inject(drawerKey, undefined);
|
||||
return () => {
|
||||
const surface = () =>
|
||||
h(
|
||||
DrawerBackdropSurface,
|
||||
{
|
||||
...attrs,
|
||||
variant: props.variant,
|
||||
isDismissable: props.isDismissable,
|
||||
isKeyboardDismissDisabled: props.isKeyboardDismissDisabled,
|
||||
to: props.to,
|
||||
},
|
||||
slots,
|
||||
);
|
||||
if (parent) return surface();
|
||||
return h(
|
||||
Drawer,
|
||||
{
|
||||
open: props.open,
|
||||
isOpen: props.isOpen,
|
||||
defaultOpen: props.defaultOpen,
|
||||
state: props.state,
|
||||
"onUpdate:open": (v: boolean) => emit("update:open", v),
|
||||
"onUpdate:isOpen": (v: boolean) => emit("update:isOpen", v),
|
||||
onOpenChange: (v: boolean) => emit("openChange", v),
|
||||
},
|
||||
{ default: surface },
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
@@ -0,0 +1,49 @@
|
||||
<script setup lang="ts">
|
||||
import { useForwardExpose } from "reka-ui";
|
||||
import { computed, provide } from "vue";
|
||||
import { drawerVariants, type DrawerVariants } from "@heroui-vue/styles";
|
||||
import OverlayPortal from "../overlay/OverlayPortal.vue";
|
||||
import OverlayBackdrop from "../overlay/OverlayBackdrop.vue";
|
||||
import { drawerKey, useDrawer } from "./context";
|
||||
import { useOverlayViewport } from "../overlay/viewport";
|
||||
import { useOverlayAnimation } from "../overlay/animation";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
variant?: DrawerVariants["variant"];
|
||||
isDismissable?: boolean;
|
||||
isKeyboardDismissDisabled?: boolean;
|
||||
to?: string | HTMLElement;
|
||||
}>(),
|
||||
{ variant: "opaque", isDismissable: true, isKeyboardDismissDisabled: false },
|
||||
);
|
||||
const ctx = useDrawer();
|
||||
const height = useOverlayViewport();
|
||||
const { forwardRef, currentElement } = useForwardExpose();
|
||||
provide(drawerKey, {
|
||||
...ctx,
|
||||
variant: computed(() => props.variant),
|
||||
dismissable: computed(() => props.isDismissable),
|
||||
keyboardDisabled: computed(() => props.isKeyboardDismissDisabled),
|
||||
});
|
||||
const { entering, exiting, onAnimationEnd } = useOverlayAnimation(
|
||||
ctx.state.isOpen,
|
||||
currentElement,
|
||||
);
|
||||
</script>
|
||||
<template>
|
||||
<OverlayPortal :to="to">
|
||||
<OverlayBackdrop
|
||||
:ref="forwardRef"
|
||||
v-bind="$attrs"
|
||||
:style="{ '--visual-viewport-height': height }"
|
||||
:class="drawerVariants({ variant }).backdrop()"
|
||||
data-slot="drawer-backdrop"
|
||||
:data-entering="entering || undefined"
|
||||
:data-exiting="exiting || undefined"
|
||||
@animationend="onAnimationEnd"
|
||||
>
|
||||
<slot :is-open="ctx.state.isOpen.value" :close="ctx.state.close" />
|
||||
</OverlayBackdrop>
|
||||
</OverlayPortal>
|
||||
</template>
|
||||
@@ -0,0 +1,13 @@
|
||||
<script setup lang="ts">
|
||||
defineOptions({ inheritAttrs: false });
|
||||
</script>
|
||||
<template>
|
||||
<div
|
||||
v-bind="$attrs"
|
||||
class="hv-drawer__body"
|
||||
data-slot="drawer-body"
|
||||
style="touch-action: pan-y"
|
||||
>
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,29 @@
|
||||
import OverlayAction from "../overlay/OverlayAction";
|
||||
import { useDrawer } from "./context";
|
||||
import { defineComponent, h } from "vue";
|
||||
|
||||
/** Closes the drawer when activated (replaces RAC slot="close"). */
|
||||
export default defineComponent({
|
||||
name: "DrawerClose",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
as: { type: String, default: "button" },
|
||||
asChild: { type: Boolean, default: false },
|
||||
isDisabled: { type: Boolean, default: false },
|
||||
},
|
||||
setup(props, { attrs, slots }) {
|
||||
const ctx = useDrawer();
|
||||
return () =>
|
||||
h(
|
||||
OverlayAction,
|
||||
{
|
||||
...attrs,
|
||||
as: props.as,
|
||||
asChild: props.asChild,
|
||||
isDisabled: props.isDisabled,
|
||||
activate: ctx.state.close,
|
||||
},
|
||||
slots,
|
||||
);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { defineComponent, h, mergeProps, type PropType } from "vue";
|
||||
import type { CloseButtonVariants } from "@heroui-vue/styles";
|
||||
import CloseButton from "../close-button/CloseButton.vue";
|
||||
import { useDrawer } from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "DrawerCloseTrigger",
|
||||
inheritAttrs: false,
|
||||
props: {
|
||||
variant: String as PropType<CloseButtonVariants["variant"]>,
|
||||
isDisabled: Boolean,
|
||||
disabled: Boolean,
|
||||
},
|
||||
setup(props, { attrs, slots }) {
|
||||
const ctx = useDrawer();
|
||||
return () =>
|
||||
h(
|
||||
CloseButton,
|
||||
mergeProps(attrs, props, {
|
||||
class: "hv-drawer__close-trigger",
|
||||
"data-slot": "drawer-close-trigger",
|
||||
onClick: (event: MouseEvent) => {
|
||||
if (!event.defaultPrevented) ctx.state.close();
|
||||
},
|
||||
}),
|
||||
slots,
|
||||
);
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,31 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, provide, ref } from "vue";
|
||||
import { drawerVariants } from "@heroui-vue/styles";
|
||||
import { drawerKey, useDrawer, type DrawerPlacement } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
placement?: DrawerPlacement;
|
||||
as?: string;
|
||||
}>(),
|
||||
{ placement: "bottom", as: "div" },
|
||||
);
|
||||
const ctx = useDrawer();
|
||||
provide(drawerKey, {
|
||||
...ctx,
|
||||
placement: computed(() => props.placement),
|
||||
});
|
||||
const content = ref<HTMLElement>();
|
||||
</script>
|
||||
<template>
|
||||
<component
|
||||
:is="as"
|
||||
ref="content"
|
||||
v-bind="$attrs"
|
||||
:class="drawerVariants({ placement }).content()"
|
||||
data-slot="drawer-content"
|
||||
:data-placement="placement"
|
||||
>
|
||||
<slot :close="ctx.state.close" />
|
||||
</component>
|
||||
</template>
|
||||
@@ -0,0 +1,87 @@
|
||||
<script setup lang="ts">
|
||||
import {
|
||||
computed,
|
||||
provide,
|
||||
watchEffect,
|
||||
onUnmounted,
|
||||
useAttrs,
|
||||
ref,
|
||||
} from "vue";
|
||||
import { DialogTitle } from "reka-ui";
|
||||
import { drawerVariants } from "@heroui-vue/styles";
|
||||
import OverlayDialogContent from "../overlay/OverlayDialogContent.vue";
|
||||
import { surfaceContextKey } from "../surface/context";
|
||||
import { useOverlayAnimation } from "../overlay/animation";
|
||||
import { useDrawer } from "./context";
|
||||
import { useDrawerDrag } from "./useDrawerDrag";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = withDefaults(defineProps<{ as?: string; id?: string }>(), {
|
||||
as: "div",
|
||||
});
|
||||
const ctx = useDrawer();
|
||||
const attrs = useAttrs();
|
||||
watchEffect(() => {
|
||||
ctx.contentId.value = props.id ?? ctx.defaultId;
|
||||
});
|
||||
onUnmounted(() => {
|
||||
ctx.contentId.value = ctx.defaultId;
|
||||
});
|
||||
provide(surfaceContextKey, { variant: computed(() => "default" as const) });
|
||||
const dialogEl = ref<HTMLElement | null>(null);
|
||||
const { dialogRef } = useDrawerDrag(
|
||||
ctx.placement,
|
||||
ctx.dismissable,
|
||||
ctx.state.isOpen,
|
||||
() => ctx.state.close(),
|
||||
);
|
||||
// Phase markers follow the Dialog element — it is the node that actually
|
||||
// runs the enter/exit animations, so Presence and CSS stay in sync.
|
||||
const { entering, exiting, onAnimationEnd } = useOverlayAnimation(
|
||||
ctx.state.isOpen,
|
||||
dialogRef,
|
||||
);
|
||||
function setDialogRef(el: unknown) {
|
||||
const node = (el as { $el?: HTMLElement } | HTMLElement | null) ?? null;
|
||||
const next = !node
|
||||
? null
|
||||
: node instanceof HTMLElement
|
||||
? node
|
||||
: ((node.$el as HTMLElement) ?? null);
|
||||
dialogEl.value = next;
|
||||
dialogRef.value = next;
|
||||
}
|
||||
</script>
|
||||
<template>
|
||||
<OverlayDialogContent
|
||||
:ref="setDialogRef"
|
||||
v-bind="attrs"
|
||||
:as="as"
|
||||
:id="ctx.contentId.value"
|
||||
:class="drawerVariants({ placement: ctx.placement.value }).dialog()"
|
||||
:is-dismissable="ctx.dismissable.value"
|
||||
:is-keyboard-dismiss-disabled="ctx.keyboardDisabled.value"
|
||||
data-slot="drawer-dialog"
|
||||
:data-entering="entering || undefined"
|
||||
:data-exiting="exiting || undefined"
|
||||
:data-placement="ctx.placement.value"
|
||||
:style="ctx.dismissable.value ? { touchAction: 'none' } : undefined"
|
||||
:aria-labelledby="
|
||||
'aria-labelledby' in attrs
|
||||
? (attrs['aria-labelledby'] as string)
|
||||
: Array.from(ctx.titles.values()).join(' ') || undefined
|
||||
"
|
||||
:aria-describedby="attrs['aria-describedby'] as string | undefined"
|
||||
@animationend="onAnimationEnd"
|
||||
>
|
||||
<DialogTitle
|
||||
v-if="
|
||||
('aria-label' in attrs || 'aria-labelledby' in attrs) &&
|
||||
!ctx.titles.size
|
||||
"
|
||||
as="span"
|
||||
class="sr-only"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<slot :close="ctx.state.close" :is-open="ctx.state.isOpen.value" />
|
||||
</OverlayDialogContent>
|
||||
</template>
|
||||
@@ -0,0 +1,8 @@
|
||||
<script setup lang="ts">
|
||||
defineOptions({ inheritAttrs: false });
|
||||
</script>
|
||||
<template>
|
||||
<div v-bind="$attrs" class="hv-drawer__footer" data-slot="drawer-footer">
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,15 @@
|
||||
<script setup lang="ts">
|
||||
defineOptions({ inheritAttrs: false });
|
||||
</script>
|
||||
<template>
|
||||
<div
|
||||
v-bind="$attrs"
|
||||
class="hv-drawer__handle"
|
||||
data-slot="drawer-handle"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<slot>
|
||||
<div data-slot="drawer-handle-bar" />
|
||||
</slot>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,8 @@
|
||||
<script setup lang="ts">
|
||||
defineOptions({ inheritAttrs: false });
|
||||
</script>
|
||||
<template>
|
||||
<div v-bind="$attrs" class="hv-drawer__header" data-slot="drawer-header">
|
||||
<slot />
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,31 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, watchEffect, onUnmounted, useId } from "vue";
|
||||
import { DialogTitle, injectDialogRootContext } from "reka-ui";
|
||||
import { useDrawer } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
const props = withDefaults(defineProps<{ as?: string; id?: string }>(), {
|
||||
as: "h2",
|
||||
});
|
||||
const ctx = useDrawer();
|
||||
const root = injectDialogRootContext();
|
||||
const suffix = useId();
|
||||
const key = Symbol();
|
||||
const generated = ctx.titles.size ? `${root.titleId}-${suffix}` : root.titleId;
|
||||
const id = computed(() => props.id ?? generated);
|
||||
watchEffect(() => {
|
||||
ctx.titles.set(key, id.value);
|
||||
});
|
||||
onUnmounted(() => ctx.titles.delete(key));
|
||||
</script>
|
||||
<template>
|
||||
<DialogTitle v-if="props.id" as="span" class="sr-only" aria-hidden="true" />
|
||||
<DialogTitle
|
||||
v-bind="$attrs"
|
||||
:as="as"
|
||||
:id="id"
|
||||
class="hv-drawer__heading"
|
||||
data-slot="drawer-heading"
|
||||
>
|
||||
<slot />
|
||||
</DialogTitle>
|
||||
</template>
|
||||
@@ -0,0 +1,38 @@
|
||||
<script setup lang="ts">
|
||||
import { watch } from "vue";
|
||||
import { injectDialogRootContext, useForwardExpose } from "reka-ui";
|
||||
import OverlayAction from "../overlay/OverlayAction";
|
||||
import { useDrawer } from "./context";
|
||||
defineOptions({ inheritAttrs: false });
|
||||
withDefaults(
|
||||
defineProps<{ as?: string; asChild?: boolean; isDisabled?: boolean }>(),
|
||||
{ as: "div" },
|
||||
);
|
||||
const ctx = useDrawer();
|
||||
const dialog = injectDialogRootContext();
|
||||
const { forwardRef, currentElement } = useForwardExpose();
|
||||
watch(
|
||||
currentElement,
|
||||
(el) => {
|
||||
dialog.triggerElement.value = el;
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
</script>
|
||||
<template>
|
||||
<OverlayAction
|
||||
:ref="forwardRef"
|
||||
v-bind="$attrs"
|
||||
:as="as"
|
||||
:as-child="asChild"
|
||||
:is-disabled="isDisabled"
|
||||
:activate="ctx.state.open"
|
||||
class="hv-drawer__trigger"
|
||||
data-slot="drawer-trigger"
|
||||
aria-haspopup="dialog"
|
||||
:aria-expanded="ctx.state.isOpen.value"
|
||||
:aria-controls="ctx.state.isOpen.value ? ctx.contentId.value : undefined"
|
||||
>
|
||||
<slot />
|
||||
</OverlayAction>
|
||||
</template>
|
||||
@@ -0,0 +1,49 @@
|
||||
import {
|
||||
computed,
|
||||
inject,
|
||||
reactive,
|
||||
ref,
|
||||
useId,
|
||||
type ComputedRef,
|
||||
type InjectionKey,
|
||||
type Ref,
|
||||
} from "vue";
|
||||
import type { DrawerVariants } from "@heroui-vue/styles";
|
||||
import type { OverlayState } from "../../composables/useOverlayState";
|
||||
|
||||
export type DrawerPlacement = "top" | "bottom" | "left" | "right";
|
||||
|
||||
export interface DrawerContext {
|
||||
state: OverlayState;
|
||||
contentId: Ref<string>;
|
||||
defaultId: string;
|
||||
titles: Map<symbol, string>;
|
||||
placement: ComputedRef<DrawerPlacement>;
|
||||
variant: ComputedRef<DrawerVariants["variant"]>;
|
||||
dismissable: ComputedRef<boolean>;
|
||||
keyboardDisabled: ComputedRef<boolean>;
|
||||
}
|
||||
|
||||
export const drawerKey: InjectionKey<DrawerContext> = Symbol("hv-drawer");
|
||||
|
||||
export function useDrawer(required = true) {
|
||||
const ctx = inject(drawerKey, undefined);
|
||||
if (!ctx && required) {
|
||||
throw new Error("Drawer parts require Drawer or DrawerBackdrop.");
|
||||
}
|
||||
return ctx!;
|
||||
}
|
||||
|
||||
export function createDrawerContext(state: OverlayState): DrawerContext {
|
||||
const id = "hv-drawer-" + useId();
|
||||
return {
|
||||
state,
|
||||
contentId: ref(id),
|
||||
defaultId: id,
|
||||
titles: reactive(new Map()),
|
||||
placement: computed(() => "bottom"),
|
||||
variant: computed(() => "opaque"),
|
||||
dismissable: computed(() => true),
|
||||
keyboardDisabled: computed(() => false),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
/**
|
||||
* Drawer drag-to-dismiss — thresholds match the fixed HeroUI baseline:
|
||||
* activate after 8px, dismiss at >30% dimension or >0.5px/ms velocity.
|
||||
* Drag originates from handle/header/footer; body is excluded to avoid scroll conflicts.
|
||||
*/
|
||||
import { nextTick, onBeforeUnmount, ref, watch, type Ref } from "vue";
|
||||
import type { DrawerPlacement } from "./context";
|
||||
|
||||
const DRAG_THRESHOLD = 8;
|
||||
const DISMISS_FRACTION = 0.3;
|
||||
const VELOCITY_THRESHOLD = 0.5;
|
||||
const SNAP_BACK_TRANSITION = "transform 300ms cubic-bezier(0.32, 0.72, 0, 1)";
|
||||
|
||||
type GesturePhase = "idle" | "dragging" | "dismissed";
|
||||
|
||||
function isDrawerDragTargetOwnedBy(
|
||||
target: HTMLElement,
|
||||
dialog: HTMLElement,
|
||||
): boolean {
|
||||
return target.closest('[data-slot="drawer-dialog"]') === dialog;
|
||||
}
|
||||
|
||||
export function useDrawerDrag(
|
||||
placement: Ref<DrawerPlacement>,
|
||||
isDismissable: Ref<boolean>,
|
||||
isOpen: Ref<boolean>,
|
||||
onDismiss: () => void,
|
||||
) {
|
||||
const dialogRef = ref<HTMLElement | null>(null);
|
||||
let phase: GesturePhase = "idle";
|
||||
let isActive = false;
|
||||
let activePointerId: number | null = null;
|
||||
let startPos = 0;
|
||||
let currentOffset = 0;
|
||||
let velocity = 0;
|
||||
let lastTime = 0;
|
||||
let lastPos = 0;
|
||||
|
||||
function isVertical() {
|
||||
return placement.value === "top" || placement.value === "bottom";
|
||||
}
|
||||
|
||||
function getPos(event: PointerEvent) {
|
||||
return isVertical() ? event.clientY : event.clientX;
|
||||
}
|
||||
|
||||
function clamp(delta: number) {
|
||||
switch (placement.value) {
|
||||
case "bottom":
|
||||
case "right":
|
||||
return Math.max(0, delta);
|
||||
case "top":
|
||||
case "left":
|
||||
return Math.min(0, delta);
|
||||
default:
|
||||
return delta;
|
||||
}
|
||||
}
|
||||
|
||||
function prefersReducedMotion() {
|
||||
return Boolean(
|
||||
dialogRef.value?.ownerDocument.defaultView?.matchMedia?.(
|
||||
"(prefers-reduced-motion: reduce)",
|
||||
).matches,
|
||||
);
|
||||
}
|
||||
|
||||
function clearStyles(el: HTMLElement | null) {
|
||||
if (!el) return;
|
||||
el.style.transform = "";
|
||||
el.style.transition = "";
|
||||
}
|
||||
|
||||
function snapBack(el: HTMLElement) {
|
||||
el.style.transition = prefersReducedMotion()
|
||||
? "none"
|
||||
: SNAP_BACK_TRANSITION;
|
||||
el.style.transform = "";
|
||||
const cleanup = () => {
|
||||
el.style.transition = "";
|
||||
};
|
||||
el.addEventListener("transitionend", cleanup, { once: true });
|
||||
}
|
||||
|
||||
function releaseCapture(pointerId: number | null, el = dialogRef.value) {
|
||||
if (pointerId === null) return;
|
||||
try {
|
||||
el?.releasePointerCapture(pointerId);
|
||||
} catch {
|
||||
// already released
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel an in-flight gesture. When `preserveOffset` is true the inline
|
||||
* transform is kept so an accepted exit continues from the dragged position.
|
||||
*/
|
||||
function endGesture(
|
||||
options: { preserveOffset?: boolean } = {},
|
||||
el = dialogRef.value,
|
||||
) {
|
||||
const pointerId = activePointerId;
|
||||
phase = "idle";
|
||||
isActive = false;
|
||||
activePointerId = null;
|
||||
currentOffset = 0;
|
||||
velocity = 0;
|
||||
releaseCapture(pointerId, el);
|
||||
if (!options.preserveOffset) clearStyles(el);
|
||||
}
|
||||
|
||||
function onPointerDown(event: Event) {
|
||||
const pointerEvent = event as PointerEvent;
|
||||
if (pointerEvent.defaultPrevented) return;
|
||||
// Do not start gestures on a closing / closed panel.
|
||||
if (!isDismissable.value || !isOpen.value) return;
|
||||
if (pointerEvent.button !== 0) return;
|
||||
if (phase !== "idle" || activePointerId !== null) return;
|
||||
const target = pointerEvent.target as HTMLElement;
|
||||
const currentTarget = pointerEvent.currentTarget as HTMLElement;
|
||||
if (!target || !currentTarget) return;
|
||||
if (!isDrawerDragTargetOwnedBy(target, currentTarget)) return;
|
||||
if (
|
||||
target.closest(
|
||||
"input, textarea, button, [role='button'], select, a, [data-slot='drawer-body']",
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
phase = "dragging";
|
||||
isActive = false;
|
||||
activePointerId = pointerEvent.pointerId;
|
||||
startPos = getPos(pointerEvent);
|
||||
lastPos = startPos;
|
||||
lastTime = Date.now();
|
||||
currentOffset = 0;
|
||||
velocity = 0;
|
||||
}
|
||||
|
||||
function onPointerMove(event: Event) {
|
||||
const pointerEvent = event as PointerEvent;
|
||||
if (phase !== "dragging" || !dialogRef.value) return;
|
||||
if (pointerEvent.pointerId !== activePointerId) return;
|
||||
// A retained exit node must not keep accepting drag updates.
|
||||
if (!isOpen.value) return;
|
||||
const pos = getPos(pointerEvent);
|
||||
const rawDelta = pos - startPos;
|
||||
const delta = clamp(rawDelta);
|
||||
if (!isActive) {
|
||||
if (Math.abs(rawDelta) < DRAG_THRESHOLD) return;
|
||||
isActive = true;
|
||||
dialogRef.value.style.transition = "none";
|
||||
try {
|
||||
dialogRef.value.setPointerCapture(pointerEvent.pointerId);
|
||||
} catch {
|
||||
// jsdom / browsers may already have released capture
|
||||
}
|
||||
}
|
||||
currentOffset = delta;
|
||||
const now = Date.now();
|
||||
const dt = now - lastTime;
|
||||
if (dt > 0) {
|
||||
velocity = (pos - lastPos) / dt;
|
||||
lastTime = now;
|
||||
lastPos = pos;
|
||||
}
|
||||
const axis = isVertical() ? "Y" : "X";
|
||||
dialogRef.value.style.transform = `translate${axis}(${delta}px)`;
|
||||
}
|
||||
|
||||
function onPointerUp(event: Event) {
|
||||
const pointerEvent = event as PointerEvent;
|
||||
if (phase !== "dragging" || pointerEvent.pointerId !== activePointerId) {
|
||||
return;
|
||||
}
|
||||
const el = dialogRef.value;
|
||||
const wasActive = isActive;
|
||||
const pointerId = activePointerId;
|
||||
phase = "idle";
|
||||
isActive = false;
|
||||
activePointerId = null;
|
||||
if (!el || !wasActive) {
|
||||
currentOffset = 0;
|
||||
velocity = 0;
|
||||
releaseCapture(pointerId);
|
||||
return;
|
||||
}
|
||||
releaseCapture(pointerId);
|
||||
const dimension = isVertical() ? el.offsetHeight : el.offsetWidth;
|
||||
const absOffset = Math.abs(currentOffset);
|
||||
const absVelocity = Math.abs(velocity);
|
||||
const shouldDismiss =
|
||||
absOffset > dimension * DISMISS_FRACTION ||
|
||||
absVelocity > VELOCITY_THRESHOLD;
|
||||
if (shouldDismiss) {
|
||||
// Keep the inline transform so exit continues from the dragged position.
|
||||
// Mark dismissed so a later lostpointercapture does not clear the offset.
|
||||
phase = "dismissed";
|
||||
onDismiss();
|
||||
void nextTick(() => {
|
||||
if (isOpen.value && dialogRef.value === el) {
|
||||
// Controlled parent rejected the close request — restore the panel.
|
||||
phase = "idle";
|
||||
snapBack(el);
|
||||
} else {
|
||||
phase = "idle";
|
||||
}
|
||||
});
|
||||
} else {
|
||||
snapBack(el);
|
||||
}
|
||||
currentOffset = 0;
|
||||
velocity = 0;
|
||||
}
|
||||
|
||||
function onPointerCancel(event: Event) {
|
||||
const pointerEvent = event as PointerEvent;
|
||||
// A normal capture release after an accepted dismiss must not jump the
|
||||
// panel back — only cancel gestures that are still in flight.
|
||||
if (phase === "dismissed") return;
|
||||
if (phase !== "dragging") return;
|
||||
if (
|
||||
activePointerId !== null &&
|
||||
pointerEvent.pointerId !== undefined &&
|
||||
pointerEvent.pointerId !== activePointerId
|
||||
) {
|
||||
return;
|
||||
}
|
||||
endGesture();
|
||||
}
|
||||
|
||||
function bind(el: HTMLElement | null) {
|
||||
el?.addEventListener("pointerdown", onPointerDown);
|
||||
el?.addEventListener("pointermove", onPointerMove);
|
||||
el?.addEventListener("pointerup", onPointerUp);
|
||||
el?.addEventListener("pointercancel", onPointerCancel);
|
||||
el?.addEventListener("lostpointercapture", onPointerCancel);
|
||||
}
|
||||
|
||||
function unbind(el: HTMLElement | null) {
|
||||
el?.removeEventListener("pointerdown", onPointerDown);
|
||||
el?.removeEventListener("pointermove", onPointerMove);
|
||||
el?.removeEventListener("pointerup", onPointerUp);
|
||||
el?.removeEventListener("pointercancel", onPointerCancel);
|
||||
el?.removeEventListener("lostpointercapture", onPointerCancel);
|
||||
}
|
||||
|
||||
// Config changes mid-gesture cancel it; an accepted programmatic close
|
||||
// stops further updates but keeps whatever offset is already applied.
|
||||
watch([isDismissable, placement], () => {
|
||||
if (phase === "dragging") endGesture();
|
||||
});
|
||||
|
||||
watch(isOpen, (open) => {
|
||||
if (open) {
|
||||
// Presence may reuse the exiting node on a quick reopen.
|
||||
endGesture();
|
||||
} else if (phase === "dragging") {
|
||||
// Programmatic close during a drag: stop the gesture without jumping.
|
||||
endGesture({ preserveOffset: true });
|
||||
}
|
||||
});
|
||||
|
||||
watch(dialogRef, (el, prev) => {
|
||||
if (prev) {
|
||||
// Clean up the previous node (capture + any in-flight style).
|
||||
endGesture({}, prev);
|
||||
unbind(prev);
|
||||
}
|
||||
bind(el);
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
endGesture();
|
||||
unbind(dialogRef.value);
|
||||
});
|
||||
|
||||
return {
|
||||
dialogRef,
|
||||
};
|
||||
}
|
||||
@@ -658,3 +658,69 @@ export {
|
||||
type ListBoxItemVariants,
|
||||
type ListBoxSectionVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
|
||||
export { default as AlertDialog } from "./components/alert-dialog/AlertDialog";
|
||||
export { default as AlertDialogRoot } from "./components/alert-dialog/AlertDialog";
|
||||
export { default as AlertDialogTrigger } from "./components/alert-dialog/AlertDialogTrigger.vue";
|
||||
export { default as AlertDialogBackdrop } from "./components/alert-dialog/AlertDialogBackdrop.vue";
|
||||
export { default as AlertDialogContainer } from "./components/alert-dialog/AlertDialogContainer.vue";
|
||||
export { default as AlertDialogDialog } from "./components/alert-dialog/AlertDialogDialog.vue";
|
||||
export { default as AlertDialogHeader } from "./components/alert-dialog/AlertDialogHeader.vue";
|
||||
export { default as AlertDialogHeading } from "./components/alert-dialog/AlertDialogHeading.vue";
|
||||
export { default as AlertDialogBody } from "./components/alert-dialog/AlertDialogBody.vue";
|
||||
export { default as AlertDialogFooter } from "./components/alert-dialog/AlertDialogFooter.vue";
|
||||
export { default as AlertDialogIcon } from "./components/alert-dialog/AlertDialogIcon.vue";
|
||||
export { default as AlertDialogCloseTrigger } from "./components/alert-dialog/AlertDialogCloseTrigger";
|
||||
export { default as AlertDialogClose } from "./components/alert-dialog/AlertDialogClose";
|
||||
export {
|
||||
alertDialogVariants,
|
||||
type AlertDialogVariants,
|
||||
} from "@heroui-vue/styles";
|
||||
export type {
|
||||
AlertDialogPlacement,
|
||||
AlertDialogStatus,
|
||||
} from "./components/alert-dialog/context";
|
||||
export type {
|
||||
AlertDialogProps,
|
||||
AlertDialogTriggerProps,
|
||||
AlertDialogBackdropProps,
|
||||
AlertDialogContainerProps,
|
||||
AlertDialogDialogProps,
|
||||
AlertDialogHeaderProps,
|
||||
AlertDialogHeadingProps,
|
||||
AlertDialogBodyProps,
|
||||
AlertDialogFooterProps,
|
||||
AlertDialogIconProps,
|
||||
AlertDialogCloseTriggerProps,
|
||||
AlertDialogCloseProps,
|
||||
} from "./types";
|
||||
|
||||
export { default as Drawer } from "./components/drawer/Drawer";
|
||||
export { default as DrawerRoot } from "./components/drawer/Drawer";
|
||||
export { default as DrawerTrigger } from "./components/drawer/DrawerTrigger.vue";
|
||||
export { default as DrawerBackdrop } from "./components/drawer/DrawerBackdrop.vue";
|
||||
export { default as DrawerContent } from "./components/drawer/DrawerContent.vue";
|
||||
export { default as DrawerDialog } from "./components/drawer/DrawerDialog.vue";
|
||||
export { default as DrawerHeader } from "./components/drawer/DrawerHeader.vue";
|
||||
export { default as DrawerHeading } from "./components/drawer/DrawerHeading.vue";
|
||||
export { default as DrawerBody } from "./components/drawer/DrawerBody.vue";
|
||||
export { default as DrawerFooter } from "./components/drawer/DrawerFooter.vue";
|
||||
export { default as DrawerHandle } from "./components/drawer/DrawerHandle.vue";
|
||||
export { default as DrawerCloseTrigger } from "./components/drawer/DrawerCloseTrigger";
|
||||
export { default as DrawerClose } from "./components/drawer/DrawerClose";
|
||||
export { drawerVariants, type DrawerVariants } from "@heroui-vue/styles";
|
||||
export type { DrawerPlacement } from "./components/drawer/context";
|
||||
export type {
|
||||
DrawerProps,
|
||||
DrawerTriggerProps,
|
||||
DrawerBackdropProps,
|
||||
DrawerContentProps,
|
||||
DrawerDialogProps,
|
||||
DrawerHeaderProps,
|
||||
DrawerHeadingProps,
|
||||
DrawerBodyProps,
|
||||
DrawerFooterProps,
|
||||
DrawerHandleProps,
|
||||
DrawerCloseTriggerProps,
|
||||
DrawerCloseProps,
|
||||
} from "./types";
|
||||
|
||||
@@ -515,3 +515,79 @@ export type SliderOutputProps = InstanceType<
|
||||
export type SliderMarksProps = InstanceType<
|
||||
(typeof import("./components/slider/SliderMarks.vue"))["default"]
|
||||
>["$props"];
|
||||
|
||||
import type AlertDialog from "./components/alert-dialog/AlertDialog";
|
||||
import type AlertDialogTrigger from "./components/alert-dialog/AlertDialogTrigger.vue";
|
||||
import type AlertDialogBackdrop from "./components/alert-dialog/AlertDialogBackdrop.vue";
|
||||
import type AlertDialogContainer from "./components/alert-dialog/AlertDialogContainer.vue";
|
||||
import type AlertDialogDialog from "./components/alert-dialog/AlertDialogDialog.vue";
|
||||
import type AlertDialogHeader from "./components/alert-dialog/AlertDialogHeader.vue";
|
||||
import type AlertDialogHeading from "./components/alert-dialog/AlertDialogHeading.vue";
|
||||
import type AlertDialogBody from "./components/alert-dialog/AlertDialogBody.vue";
|
||||
import type AlertDialogFooter from "./components/alert-dialog/AlertDialogFooter.vue";
|
||||
import type AlertDialogIcon from "./components/alert-dialog/AlertDialogIcon.vue";
|
||||
import type AlertDialogCloseTrigger from "./components/alert-dialog/AlertDialogCloseTrigger";
|
||||
import type AlertDialogClose from "./components/alert-dialog/AlertDialogClose";
|
||||
|
||||
export type AlertDialogProps = InstanceType<typeof AlertDialog>["$props"];
|
||||
export type AlertDialogTriggerProps = InstanceType<
|
||||
typeof AlertDialogTrigger
|
||||
>["$props"];
|
||||
export type AlertDialogBackdropProps = InstanceType<
|
||||
typeof AlertDialogBackdrop
|
||||
>["$props"];
|
||||
export type AlertDialogContainerProps = InstanceType<
|
||||
typeof AlertDialogContainer
|
||||
>["$props"];
|
||||
export type AlertDialogDialogProps = InstanceType<
|
||||
typeof AlertDialogDialog
|
||||
>["$props"];
|
||||
export type AlertDialogHeaderProps = InstanceType<
|
||||
typeof AlertDialogHeader
|
||||
>["$props"];
|
||||
export type AlertDialogHeadingProps = InstanceType<
|
||||
typeof AlertDialogHeading
|
||||
>["$props"];
|
||||
export type AlertDialogBodyProps = InstanceType<
|
||||
typeof AlertDialogBody
|
||||
>["$props"];
|
||||
export type AlertDialogFooterProps = InstanceType<
|
||||
typeof AlertDialogFooter
|
||||
>["$props"];
|
||||
export type AlertDialogIconProps = InstanceType<
|
||||
typeof AlertDialogIcon
|
||||
>["$props"];
|
||||
export type AlertDialogCloseTriggerProps = InstanceType<
|
||||
typeof AlertDialogCloseTrigger
|
||||
>["$props"];
|
||||
export type AlertDialogCloseProps = InstanceType<
|
||||
typeof AlertDialogClose
|
||||
>["$props"];
|
||||
|
||||
import type Drawer from "./components/drawer/Drawer";
|
||||
import type DrawerTrigger from "./components/drawer/DrawerTrigger.vue";
|
||||
import type DrawerBackdrop from "./components/drawer/DrawerBackdrop.vue";
|
||||
import type DrawerContent from "./components/drawer/DrawerContent.vue";
|
||||
import type DrawerDialog from "./components/drawer/DrawerDialog.vue";
|
||||
import type DrawerHeader from "./components/drawer/DrawerHeader.vue";
|
||||
import type DrawerHeading from "./components/drawer/DrawerHeading.vue";
|
||||
import type DrawerBody from "./components/drawer/DrawerBody.vue";
|
||||
import type DrawerFooter from "./components/drawer/DrawerFooter.vue";
|
||||
import type DrawerHandle from "./components/drawer/DrawerHandle.vue";
|
||||
import type DrawerCloseTrigger from "./components/drawer/DrawerCloseTrigger";
|
||||
import type DrawerClose from "./components/drawer/DrawerClose";
|
||||
|
||||
export type DrawerProps = InstanceType<typeof Drawer>["$props"];
|
||||
export type DrawerTriggerProps = InstanceType<typeof DrawerTrigger>["$props"];
|
||||
export type DrawerBackdropProps = InstanceType<typeof DrawerBackdrop>["$props"];
|
||||
export type DrawerContentProps = InstanceType<typeof DrawerContent>["$props"];
|
||||
export type DrawerDialogProps = InstanceType<typeof DrawerDialog>["$props"];
|
||||
export type DrawerHeaderProps = InstanceType<typeof DrawerHeader>["$props"];
|
||||
export type DrawerHeadingProps = InstanceType<typeof DrawerHeading>["$props"];
|
||||
export type DrawerBodyProps = InstanceType<typeof DrawerBody>["$props"];
|
||||
export type DrawerFooterProps = InstanceType<typeof DrawerFooter>["$props"];
|
||||
export type DrawerHandleProps = InstanceType<typeof DrawerHandle>["$props"];
|
||||
export type DrawerCloseTriggerProps = InstanceType<
|
||||
typeof DrawerCloseTrigger
|
||||
>["$props"];
|
||||
export type DrawerCloseProps = InstanceType<typeof DrawerClose>["$props"];
|
||||
|
||||
Reference in New Issue
Block a user