fix: retain pending Toast and Table fidelity corrections
This commit is contained in:
@@ -1,24 +1,187 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h, inject, useAttrs } from "vue";
|
||||
import { tableContextKey } from "./context";
|
||||
import {
|
||||
Comment,
|
||||
Fragment,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
isVNode,
|
||||
provide,
|
||||
onMounted,
|
||||
onUpdated,
|
||||
onBeforeUnmount,
|
||||
ref,
|
||||
useAttrs,
|
||||
type PropType,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import {
|
||||
tableBodyContextKey,
|
||||
tableContextKey,
|
||||
type TableBodyContextValue,
|
||||
} from "./context";
|
||||
|
||||
function flattenChildren(
|
||||
nodes: VNodeChild[],
|
||||
out: VNodeChild[] = [],
|
||||
): VNodeChild[] {
|
||||
for (const child of nodes) {
|
||||
if (Array.isArray(child)) {
|
||||
flattenChildren(child, out);
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
isVNode(child) &&
|
||||
child.type === Fragment &&
|
||||
Array.isArray(child.children)
|
||||
) {
|
||||
flattenChildren(child.children as VNodeChild[], out);
|
||||
continue;
|
||||
}
|
||||
out.push(child);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
function componentTypeName(node: VNodeChild): string | undefined {
|
||||
if (!isVNode(node)) return undefined;
|
||||
const type = node.type;
|
||||
if (typeof type === "string") return type;
|
||||
if (typeof type === "object" && type !== null && "name" in type) {
|
||||
return (type as { name?: string }).name;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Vnode-level populated check that does not execute child setup/render.
|
||||
* Covers TableRow/native tr and TableCollection via its `items` prop so a
|
||||
* populated collection is not swallowed by the empty slot.
|
||||
*/
|
||||
function slotLooksPopulated(children: VNodeChild[]): boolean {
|
||||
for (const node of flattenChildren(children)) {
|
||||
if (!isVNode(node)) {
|
||||
if (typeof node === "string" && node.trim() !== "") return true;
|
||||
if (typeof node === "number") return true;
|
||||
continue;
|
||||
}
|
||||
if (node.type === Comment) continue;
|
||||
const name = componentTypeName(node);
|
||||
if (name === "TableRow" || name === "tr") return true;
|
||||
if (name === "TableCollection") {
|
||||
const items = (node.props as { items?: unknown[] } | null)?.items;
|
||||
if (Array.isArray(items) && items.length > 0) return true;
|
||||
continue;
|
||||
}
|
||||
// Unknown components stay mounted below; their actual rows are observed
|
||||
// after rendering instead of treating the component itself as a row.
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: "TableBody",
|
||||
inheritAttrs: false,
|
||||
props: { as: { type: String, default: "tbody" } },
|
||||
props: {
|
||||
as: { type: String, default: "tbody" },
|
||||
/**
|
||||
* Force the empty state even if default slot content is present.
|
||||
* Omit to auto-detect: empty when the default slot looks unpopulated
|
||||
* (TableRow/native tr, TableCollection.items) and no rows registered.
|
||||
*/
|
||||
empty: { type: Boolean, default: undefined },
|
||||
/** Colspan of the empty-state cell. */
|
||||
colspan: {
|
||||
type: [Number, String] as PropType<number | string>,
|
||||
default: 1,
|
||||
},
|
||||
},
|
||||
setup(props, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(tableContextKey, null);
|
||||
return () =>
|
||||
h(
|
||||
const rowRefCount = ref(0);
|
||||
const bodyElement = ref<HTMLElement | null>(null);
|
||||
const hasRenderedRows = ref(false);
|
||||
let observer: MutationObserver | undefined;
|
||||
function syncRenderedRows() {
|
||||
hasRenderedRows.value = Array.from(
|
||||
bodyElement.value?.children ?? [],
|
||||
).some(
|
||||
(node) =>
|
||||
node.tagName === "TR" &&
|
||||
node.getAttribute("data-slot") !== "table-empty-row",
|
||||
);
|
||||
}
|
||||
onMounted(() => {
|
||||
syncRenderedRows();
|
||||
if (typeof MutationObserver !== "undefined" && bodyElement.value) {
|
||||
observer = new MutationObserver(syncRenderedRows);
|
||||
observer.observe(bodyElement.value, { childList: true });
|
||||
}
|
||||
});
|
||||
onUpdated(syncRenderedRows);
|
||||
onBeforeUnmount(() => observer?.disconnect());
|
||||
|
||||
provide(tableBodyContextKey, {
|
||||
registerRow() {
|
||||
rowRefCount.value += 1;
|
||||
return () => {
|
||||
rowRefCount.value = Math.max(0, rowRefCount.value - 1);
|
||||
};
|
||||
},
|
||||
} satisfies TableBodyContextValue);
|
||||
|
||||
function renderEmptyRow() {
|
||||
const content = slots.empty?.();
|
||||
if (!content) return null;
|
||||
return h(
|
||||
"tr",
|
||||
{
|
||||
"data-slot": "table-empty-row",
|
||||
role: "presentation",
|
||||
style: "pointer-events: auto",
|
||||
},
|
||||
[
|
||||
h(
|
||||
"td",
|
||||
{
|
||||
class: "hv-table__empty-cell",
|
||||
"data-slot": "table-empty-cell",
|
||||
colspan: props.colspan,
|
||||
role: "presentation",
|
||||
},
|
||||
content,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
return () => {
|
||||
const children = flattenChildren(slots.default?.() ?? []);
|
||||
const looksPopulated = slotLooksPopulated(children);
|
||||
const hasRows = rowRefCount.value > 0 || hasRenderedRows.value;
|
||||
const isEmpty =
|
||||
props.empty === true ||
|
||||
(props.empty !== false && !!slots.empty && !hasRows && !looksPopulated);
|
||||
|
||||
return h(
|
||||
props.as,
|
||||
{
|
||||
...attrs,
|
||||
ref: bodyElement,
|
||||
class: [ctx?.slots.value.body(), attrs.class],
|
||||
"data-slot": "table-body",
|
||||
"data-empty": isEmpty || undefined,
|
||||
},
|
||||
slots.default?.(),
|
||||
// Automatic detection keeps wrappers mounted so later data can appear.
|
||||
// Explicit empty suppresses data rows, their selection and navigation.
|
||||
props.empty === true
|
||||
? [renderEmptyRow()]
|
||||
: isEmpty
|
||||
? [...children, renderEmptyRow()]
|
||||
: children,
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -51,6 +51,8 @@ export default defineComponent({
|
||||
tabindex: attrs.tabindex ?? -1,
|
||||
class: [ctx?.slots.value.cell(), attrs.class],
|
||||
"data-slot": "table-cell",
|
||||
"data-tree-column": isTreeColumn.value || undefined,
|
||||
"data-level": row?.level,
|
||||
colspan: props.colspan,
|
||||
},
|
||||
slots.default?.({
|
||||
|
||||
@@ -69,8 +69,15 @@ export default defineComponent({
|
||||
width,
|
||||
setWidth,
|
||||
});
|
||||
const cssWidth = (value: string | number | undefined) =>
|
||||
typeof value === "number" ? `${value}px` : value;
|
||||
const cssWidth = (value: string | number | undefined) => {
|
||||
if (value === undefined) return undefined;
|
||||
if (typeof value === "number") return `${value}px`;
|
||||
// CSS Grid fr units are not valid table widths. Treat Nfr as a flexible
|
||||
// column (native width:auto sharing remaining space). This does not
|
||||
// implement RAC fr weight proportions across columns.
|
||||
if (/^\d+(\.\d+)?fr$/.test(value)) return undefined;
|
||||
return value;
|
||||
};
|
||||
const sortDirection = computed<TableSortDirection | undefined>(() => {
|
||||
if (!props.allowsSorting || !props.id) return undefined;
|
||||
const d = ctx?.sortDescriptor.value;
|
||||
@@ -99,6 +106,10 @@ export default defineComponent({
|
||||
maxWidth: cssWidth(props.maxWidth),
|
||||
},
|
||||
"data-column-id": props.id,
|
||||
"data-default-width":
|
||||
typeof (props.width ?? width.value) === "string"
|
||||
? String(props.width ?? width.value)
|
||||
: undefined,
|
||||
"data-row-header": props.isRowHeader || undefined,
|
||||
"data-slot": "table-column",
|
||||
"data-allows-sorting": props.allowsSorting || undefined,
|
||||
|
||||
@@ -13,7 +13,16 @@ import {
|
||||
onUpdated,
|
||||
type PropType,
|
||||
} from "vue";
|
||||
import { tableContextKey, tableRowContextKey, type TableKey } from "./context";
|
||||
import {
|
||||
tableBodyContextKey,
|
||||
tableContextKey,
|
||||
tableRowContextKey,
|
||||
type TableKey,
|
||||
} from "./context";
|
||||
|
||||
/** Nested controls own their own activation; row click must not also toggle selection. */
|
||||
const INTERACTIVE_SELECTOR =
|
||||
"button, a, input, select, textarea, label, [role='button'], [role='link'], [contenteditable='true'], [data-choice-input], [data-slot='checkbox-content'], [data-slot='checkbox-control'], [data-slot='checkbox-indicator'], [data-slot='radio-content'], [data-slot='radio-control'], [data-slot='radio-indicator']";
|
||||
|
||||
export default defineComponent({
|
||||
name: "TableRow",
|
||||
@@ -28,17 +37,26 @@ export default defineComponent({
|
||||
setup(props, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(tableContextKey, null);
|
||||
const bodyCtx = inject(tableBodyContextKey, null);
|
||||
const element = ref<HTMLElement | null>(null);
|
||||
let unregisterBodyRow: (() => void) | undefined;
|
||||
watch(
|
||||
[() => props.rowKey, element],
|
||||
([key, node], previous) => {
|
||||
if (previous) ctx?.navigation?.register(previous[0], null);
|
||||
if (!node?.closest("thead"))
|
||||
if (previous?.[0] != null) ctx?.navigation?.register(previous[0], null);
|
||||
if (!node?.closest("thead")) {
|
||||
ctx?.navigation?.register(key, node, () => props.isDisabled);
|
||||
if (!unregisterBodyRow && bodyCtx && node)
|
||||
unregisterBodyRow = bodyCtx.registerRow();
|
||||
}
|
||||
},
|
||||
{ flush: "sync" },
|
||||
{ flush: "sync", immediate: true },
|
||||
);
|
||||
onBeforeUnmount(() => ctx?.navigation?.register(props.rowKey, null));
|
||||
onBeforeUnmount(() => {
|
||||
ctx?.navigation?.register(props.rowKey, null);
|
||||
unregisterBodyRow?.();
|
||||
unregisterBodyRow = undefined;
|
||||
});
|
||||
onUpdated(() => ctx?.navigation?.refresh());
|
||||
const isSelected = computed(
|
||||
() => ctx?.isRowSelected(props.rowKey) ?? false,
|
||||
@@ -61,6 +79,9 @@ export default defineComponent({
|
||||
get hasChildItems() {
|
||||
return props.hasChildItems;
|
||||
},
|
||||
get level() {
|
||||
return props.level;
|
||||
},
|
||||
});
|
||||
return () => {
|
||||
// Vue DOM merge: user onClick is first in the array, so
|
||||
@@ -75,8 +96,17 @@ export default defineComponent({
|
||||
"data-selected": isSelected.value || undefined,
|
||||
"data-disabled": isDisabled.value || undefined,
|
||||
"data-expanded": isExpanded.value || undefined,
|
||||
"data-level": props.level,
|
||||
"aria-expanded": props.hasChildItems ? isExpanded.value : undefined,
|
||||
"aria-level": props.level,
|
||||
// 1-based level for CSS indent (matches upstream --table-row-level).
|
||||
style:
|
||||
props.level != null
|
||||
? ({ "--table-row-level": String(props.level) } as Record<
|
||||
string,
|
||||
string
|
||||
>)
|
||||
: undefined,
|
||||
"aria-selected":
|
||||
ctx && ctx.selectionMode.value !== "none"
|
||||
? isSelected.value
|
||||
@@ -95,11 +125,7 @@ export default defineComponent({
|
||||
if (event.defaultPrevented) return;
|
||||
if (isDisabled.value) return;
|
||||
const target = event.target as HTMLElement | null;
|
||||
if (
|
||||
target?.closest(
|
||||
"button, a, input, select, textarea, [role='button'], [role='link']",
|
||||
)
|
||||
) {
|
||||
if (target?.closest(INTERACTIVE_SELECTOR)) {
|
||||
return;
|
||||
}
|
||||
ctx?.toggleSelect(props.rowKey, event);
|
||||
@@ -108,11 +134,7 @@ export default defineComponent({
|
||||
if (event.defaultPrevented || isDisabled.value) return;
|
||||
if (event.key !== " " && event.key !== "Spacebar") return;
|
||||
const target = event.target as HTMLElement | null;
|
||||
if (
|
||||
target?.closest(
|
||||
"input, select, textarea, button, a, [role='button'], [role='link'], [contenteditable='true']",
|
||||
)
|
||||
) {
|
||||
if (target?.closest(INTERACTIVE_SELECTOR)) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
|
||||
@@ -42,11 +42,21 @@ export interface TableRowContextValue {
|
||||
isDisabled: ComputedRef<boolean>;
|
||||
isExpanded: ComputedRef<boolean>;
|
||||
hasChildItems: boolean;
|
||||
/** 1-based tree level when provided by TableRow. */
|
||||
level?: number;
|
||||
}
|
||||
|
||||
export const tableRowContextKey: InjectionKey<TableRowContextValue> =
|
||||
Symbol("hv-table-row");
|
||||
|
||||
export interface TableBodyContextValue {
|
||||
/** Register a mounted body row; returns an unregister function. */
|
||||
registerRow: () => () => void;
|
||||
}
|
||||
|
||||
export const tableBodyContextKey: InjectionKey<TableBodyContextValue> =
|
||||
Symbol("hv-table-body");
|
||||
|
||||
export interface TableColumnContextValue {
|
||||
key: ComputedRef<string | undefined>;
|
||||
allowsSorting: ComputedRef<boolean>;
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
provide,
|
||||
ref,
|
||||
useAttrs,
|
||||
useId,
|
||||
type PropType,
|
||||
} from "vue";
|
||||
import { toastVariants, type ToastVariants } from "@heroui-vue/styles";
|
||||
@@ -35,19 +36,15 @@ function renderDefaultContent(content: ToastContentValue, close: () => void) {
|
||||
// explicit hide
|
||||
} else if (isLoading) {
|
||||
nodes.push(
|
||||
h(
|
||||
"div",
|
||||
{ class: "hv-toast__indicator", "data-slot": "toast-indicator" },
|
||||
[h(Spinner, { size: "sm" })],
|
||||
),
|
||||
h(ToastIndicator, { variant: (variant as never) ?? "default" }, () => [
|
||||
h(Spinner, { size: "sm" }),
|
||||
]),
|
||||
);
|
||||
} else if (indicator !== undefined) {
|
||||
nodes.push(
|
||||
h(
|
||||
"div",
|
||||
{ class: "hv-toast__indicator", "data-slot": "toast-indicator" },
|
||||
[indicator as VNodeChild],
|
||||
),
|
||||
h(ToastIndicator, { variant: (variant as never) ?? "default" }, () => [
|
||||
indicator as VNodeChild,
|
||||
]),
|
||||
);
|
||||
} else {
|
||||
nodes.push(h(ToastIndicator, { variant: (variant as never) ?? "default" }));
|
||||
@@ -111,6 +108,13 @@ export default defineComponent({
|
||||
const ctx = inject(toastContextKey, null);
|
||||
const el = ref<HTMLElement | null>(null);
|
||||
const isEntering = ref(true);
|
||||
// NVDA only announces once the alert payload flips from hidden to visible.
|
||||
const isAnnounced = ref(false);
|
||||
const fallbackTitleId = useId();
|
||||
const fallbackDescriptionId = useId();
|
||||
/** Live part ids (explicit or generated) registered by Title/Description. */
|
||||
const titleId = ref<string | null>(null);
|
||||
const descriptionId = ref<string | null>(null);
|
||||
let enterFrame = 0;
|
||||
let innerFrame = 0;
|
||||
let observer: ResizeObserver | undefined;
|
||||
@@ -136,6 +140,8 @@ export default defineComponent({
|
||||
|
||||
onMounted(() => {
|
||||
measure();
|
||||
// Flip after paint so screen readers announce the toast content once.
|
||||
isAnnounced.value = true;
|
||||
if (typeof ResizeObserver !== "undefined") {
|
||||
observer = new ResizeObserver(measure);
|
||||
if (el.value) {
|
||||
@@ -196,6 +202,25 @@ export default defineComponent({
|
||||
isHidden,
|
||||
isExpanded: computed(() => ctx?.isExpanded.value ?? false),
|
||||
index,
|
||||
titleId: computed(() => titleId.value ?? fallbackTitleId),
|
||||
descriptionId: computed(
|
||||
() => descriptionId.value ?? fallbackDescriptionId,
|
||||
),
|
||||
isAnnounced: computed(() => isAnnounced.value),
|
||||
hasTitle: computed(() => titleId.value != null),
|
||||
hasDescription: computed(() => descriptionId.value != null),
|
||||
registerTitle(id: string) {
|
||||
titleId.value = id;
|
||||
return () => {
|
||||
if (titleId.value === id) titleId.value = null;
|
||||
};
|
||||
},
|
||||
registerDescription(id: string) {
|
||||
descriptionId.value = id;
|
||||
return () => {
|
||||
if (descriptionId.value === id) descriptionId.value = null;
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
function setEl(node: unknown) {
|
||||
@@ -256,14 +281,24 @@ export default defineComponent({
|
||||
"data-index": index.value,
|
||||
"data-placement": placement.value,
|
||||
"data-slot": "toast",
|
||||
role: "status",
|
||||
// Matches the fixed upstream Toast primitive (react-aria useToast).
|
||||
role: "alertdialog",
|
||||
"aria-modal": "false",
|
||||
// Caller-supplied root ARIA wins; otherwise reference live parts only.
|
||||
"aria-labelledby":
|
||||
(attrs["aria-labelledby"] as string | undefined) ??
|
||||
titleId.value ??
|
||||
undefined,
|
||||
"aria-describedby":
|
||||
(attrs["aria-describedby"] as string | undefined) ??
|
||||
descriptionId.value ??
|
||||
undefined,
|
||||
tabindex:
|
||||
isFrontmost.value ||
|
||||
((ctx?.isExpanded.value ?? false) &&
|
||||
!isHidden.value &&
|
||||
!isExiting.value)
|
||||
? 0
|
||||
: -1,
|
||||
isHidden.value || isExiting.value
|
||||
? -1
|
||||
: isFrontmost.value || (ctx?.isExpanded.value ?? false)
|
||||
? 0
|
||||
: -1,
|
||||
inert:
|
||||
isHidden.value ||
|
||||
isExiting.value ||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h, inject, useAttrs } from "vue";
|
||||
import { toastContextKey } from "./context";
|
||||
import { toastContextKey, toastItemContextKey } from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "ToastContent",
|
||||
@@ -9,6 +9,7 @@ export default defineComponent({
|
||||
setup(props, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(toastContextKey, null);
|
||||
const item = inject(toastItemContextKey, null);
|
||||
return () =>
|
||||
h(
|
||||
props.as,
|
||||
@@ -16,6 +17,10 @@ export default defineComponent({
|
||||
...attrs,
|
||||
class: [ctx?.slots.value.content(), attrs.class],
|
||||
"data-slot": "toast-content",
|
||||
// Matches upstream contentProps: alert payload, atomic, announced once visible.
|
||||
role: (attrs.role as string | undefined) ?? "alert",
|
||||
"aria-atomic": "true",
|
||||
"aria-hidden": item && !item.isAnnounced.value ? "true" : undefined,
|
||||
},
|
||||
slots.default?.(),
|
||||
);
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h, inject, useAttrs } from "vue";
|
||||
import { toastContextKey } from "./context";
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
onBeforeUnmount,
|
||||
useAttrs,
|
||||
watch,
|
||||
} from "vue";
|
||||
import { toastContextKey, toastItemContextKey } from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "ToastDescription",
|
||||
@@ -9,11 +17,29 @@ export default defineComponent({
|
||||
setup(props, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(toastContextKey, null);
|
||||
const item = inject(toastItemContextKey, null);
|
||||
const id = computed(
|
||||
() => (attrs.id as string | undefined) ?? item?.descriptionId.value,
|
||||
);
|
||||
let unregister: (() => void) | undefined;
|
||||
watch(
|
||||
id,
|
||||
(next) => {
|
||||
unregister?.();
|
||||
unregister = next ? item?.registerDescription(next) : undefined;
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
onBeforeUnmount(() => {
|
||||
unregister?.();
|
||||
unregister = undefined;
|
||||
});
|
||||
return () =>
|
||||
h(
|
||||
props.as,
|
||||
{
|
||||
...attrs,
|
||||
id: id.value,
|
||||
class: [ctx?.slots.value.description(), attrs.class],
|
||||
"data-slot": "toast-description",
|
||||
slot: "description",
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h, inject, useAttrs, type PropType } from "vue";
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
isVNode,
|
||||
ref,
|
||||
useAttrs,
|
||||
watch,
|
||||
type PropType,
|
||||
type VNodeChild,
|
||||
} from "vue";
|
||||
import type { ToastVariants } from "@heroui-vue/styles";
|
||||
import { toastContextKey } from "./context";
|
||||
import {
|
||||
@@ -9,6 +20,19 @@ import {
|
||||
ToastWarningIcon,
|
||||
} from "./icons";
|
||||
|
||||
/** Identity of the indicator payload: element type, or "default" for the built-in icon. */
|
||||
function childrenKind(children: VNodeChild): unknown {
|
||||
if (children == null || children === false) return "default";
|
||||
if (Array.isArray(children)) {
|
||||
const first = children.find(
|
||||
(c) => isVNode(c) || (typeof c === "string" && c.trim() !== ""),
|
||||
);
|
||||
return first === undefined ? "default" : childrenKind(first as VNodeChild);
|
||||
}
|
||||
if (isVNode(children)) return children.type;
|
||||
return "default";
|
||||
}
|
||||
|
||||
export default defineComponent({
|
||||
name: "ToastIndicator",
|
||||
inheritAttrs: false,
|
||||
@@ -22,6 +46,11 @@ export default defineComponent({
|
||||
setup(props, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(toastContextKey, null);
|
||||
// Swap marker: animate content that replaces earlier content (promise
|
||||
// settle) without animating the first paint.
|
||||
const hasSwapped = ref(false);
|
||||
let previousKind: unknown;
|
||||
|
||||
function defaultIcon() {
|
||||
switch (props.variant) {
|
||||
case "success":
|
||||
@@ -34,16 +63,28 @@ export default defineComponent({
|
||||
return h(ToastInfoIcon, { "data-slot": "toast-default-icon" });
|
||||
}
|
||||
}
|
||||
return () =>
|
||||
h(
|
||||
|
||||
return () => {
|
||||
const custom = slots.default?.();
|
||||
const kind = custom ? childrenKind(custom) : childrenKind(null);
|
||||
if (previousKind === undefined) {
|
||||
previousKind = kind;
|
||||
} else if (previousKind !== kind) {
|
||||
previousKind = kind;
|
||||
hasSwapped.value = true;
|
||||
}
|
||||
|
||||
return h(
|
||||
props.as,
|
||||
{
|
||||
...attrs,
|
||||
class: [ctx?.slots.value.indicator(), attrs.class],
|
||||
"data-slot": "toast-indicator",
|
||||
"data-swapped": hasSwapped.value || undefined,
|
||||
},
|
||||
slots.default?.() ?? [defaultIcon()],
|
||||
custom ?? [defaultIcon()],
|
||||
);
|
||||
};
|
||||
},
|
||||
});
|
||||
</script>
|
||||
|
||||
@@ -1,6 +1,14 @@
|
||||
<script lang="ts">
|
||||
import { defineComponent, h, inject, useAttrs } from "vue";
|
||||
import { toastContextKey } from "./context";
|
||||
import {
|
||||
computed,
|
||||
defineComponent,
|
||||
h,
|
||||
inject,
|
||||
onBeforeUnmount,
|
||||
useAttrs,
|
||||
watch,
|
||||
} from "vue";
|
||||
import { toastContextKey, toastItemContextKey } from "./context";
|
||||
|
||||
export default defineComponent({
|
||||
name: "ToastTitle",
|
||||
@@ -9,11 +17,29 @@ export default defineComponent({
|
||||
setup(props, { slots }) {
|
||||
const attrs = useAttrs();
|
||||
const ctx = inject(toastContextKey, null);
|
||||
const item = inject(toastItemContextKey, null);
|
||||
const id = computed(
|
||||
() => (attrs.id as string | undefined) ?? item?.titleId.value,
|
||||
);
|
||||
let unregister: (() => void) | undefined;
|
||||
watch(
|
||||
id,
|
||||
(next) => {
|
||||
unregister?.();
|
||||
unregister = next ? item?.registerTitle(next) : undefined;
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
onBeforeUnmount(() => {
|
||||
unregister?.();
|
||||
unregister = undefined;
|
||||
});
|
||||
return () =>
|
||||
h(
|
||||
props.as,
|
||||
{
|
||||
...attrs,
|
||||
id: id.value,
|
||||
class: [ctx?.slots.value.title(), attrs.class],
|
||||
"data-slot": "toast-title",
|
||||
slot: "title",
|
||||
|
||||
@@ -34,6 +34,20 @@ export interface ToastItemContextValue {
|
||||
isHidden: ComputedRef<boolean>;
|
||||
isExpanded: ComputedRef<boolean>;
|
||||
index: ComputedRef<number>;
|
||||
/** Stable fallback ids when parts do not supply their own. */
|
||||
titleId: ComputedRef<string>;
|
||||
descriptionId: ComputedRef<string>;
|
||||
/** False until the toast has painted once (NVDA needs this flip to announce). */
|
||||
isAnnounced: ComputedRef<boolean>;
|
||||
/**
|
||||
* Register the live Title/Description element id (explicit or generated).
|
||||
* Returns an unregister function. Root ARIA uses the registered id.
|
||||
*/
|
||||
registerTitle: (id: string) => () => void;
|
||||
registerDescription: (id: string) => () => void;
|
||||
/** Whether a Title/Description part is currently mounted. */
|
||||
hasTitle: ComputedRef<boolean>;
|
||||
hasDescription: ComputedRef<boolean>;
|
||||
}
|
||||
|
||||
export const toastItemContextKey: InjectionKey<ToastItemContextValue> =
|
||||
|
||||
Reference in New Issue
Block a user