import * as stylex from "@stylexjs/stylex";
import {
createToaster,
Toast as UiSdkToast,
ToastAction,
ToastClose,
ToastDescription,
ToastRegion,
ToastTitle,
type Toaster as ToasterQueue,
} from "@ui-sdk/react/toast";
import { createSlotRecipeContext } from "#lib/ui-sdk/create-slot-recipe-context.tsx";
import { defineSlotRecipe, resolveSlotStyles } from "#lib/recipe-props.ts";
import { zIndices } from "@stylexblocks/ui/tokens/z-indices.stylex.ts";
import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { cursor } from '@stylexblocks/ui/tokens/cursor.stylex.ts'
import { easings } from '@stylexblocks/ui/tokens/easings.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'
import { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticShadows } from '@stylexblocks/ui/semantic-tokens/shadows.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'
const toastRoot = stylex.create({
base: {
width: sizes.full,
display: 'flex',
alignItems: 'flex-start',
position: 'relative',
gap: spacing._2,
paddingBlock: spacing._3,
paddingInlineStart: spacing._3,
paddingInlineEnd: spacing._6,
borderRadius: radii.md,
borderWidth: '1px',
translate: 'var(--x) var(--y)',
scale: 'var(--scale)',
zIndex: 'var(--z-index)',
height: 'var(--height, var(--toast-height))',
opacity: 'var(--opacity)',
willChange: 'translate, opacity, scale, height',
transition:
'translate 400ms, scale 400ms, opacity 400ms, height 200ms, box-shadow 200ms',
transitionTimingFunction: easings.bounceIn,
backgroundColor: semanticColors.bgPanel,
color: semanticColors.fg,
boxShadow: semanticShadows.lg,
overflow: 'hidden',
'--toast-indicated-color': semanticColors.fgMuted,
},
})
const toastTitle = stylex.create({
base: {
fontWeight: fontWeights.medium,
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
marginInlineEnd: spacing._2,
},
})
const toastDescription = stylex.create({
base: {
display: 'inline',
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
opacity: 0.8,
},
})
const toastIndicator = stylex.create({
base: {
flexShrink: 0,
width: sizes._5,
height: sizes._5,
color: 'var(--toast-indicated-color)',
},
})
const toastActionTrigger = stylex.create({
base: {
cursor: cursor.button,
fontSize: fontSizes.sm,
lineHeight: lineHeights.sm,
fontWeight: fontWeights.medium,
height: sizes._6,
paddingInline: spacing._3,
marginInlineStart: `calc(${spacing._3} * -1)`,
borderRadius: radii.sm,
alignSelf: 'flex-start',
transition: 'background 200ms',
color: {
default: `color-mix(in oklch, ${colorPalette.solid} 80%, transparent)`,
':hover': colorPalette.solid,
},
backgroundColor: {
default: null,
':hover': semanticColors.bgSubtle,
},
},
})
const toastCloseTrigger = stylex.create({
base: {
position: 'absolute',
top: spacing._2,
insetInlineEnd: spacing._2,
},
})
// Local chrome the recipe leaves to the composition layer: logical-property
// fixes for generated Chakra shorthands (`ps`/`pe`/`ms`/`marginEnd`/
// `insetEnd`), the viewport's fixed stack in the bottom-end corner (above
// all page chrome), enter/leave transitions keyed off UI SDK's
// `data-starting-style` / `data-ending-style`, swipe-to-dismiss following
// `--toast-swipe-x/y`, the close trigger's icon-button look, and per-type
// indicator colors. The region is a plain stacked list, like the Base UI
// version: toasts lay out in a column rather than a collapsed pile.
const chrome = stylex.create({
viewport: {
position: "fixed",
bottom: spacing._4,
insetInlineEnd: spacing._4,
display: "flex",
flexDirection: "column",
gap: spacing._2,
width: "22rem",
maxWidth: "calc(100vw - 2rem)",
margin: 0,
padding: 0,
listStyle: "none",
zIndex: zIndices.toast,
outline: 0,
},
root: {
maxWidth: "calc(100vw - 2rem)",
paddingInlineStart: spacing._3,
paddingInlineEnd: spacing._6,
width: "22.5rem",
transitionProperty: {
default: "opacity, transform",
":is([data-swiping])": "none",
},
transitionDuration: "250ms",
transitionTimingFunction: "cubic-bezier(0.21, 1.02, 0.73, 1)",
opacity: {
default: 1,
":is([data-starting-style])": 0,
":is([data-ending-style])": 0,
},
transform: {
default: "translateY(0)",
":is([data-starting-style])": "translateY(0.75rem)",
":is([data-ending-style])": "translateY(0.75rem)",
},
translate: "var(--toast-swipe-x, 0px) var(--toast-swipe-y, 0px)",
// the region lays toasts out in its own `ol`, out of reach of the
// viewport's flex gap, so each toast spaces itself from the next
marginBottom: {
default: 0,
":not(:last-child)": spacing._2,
},
userSelect: "none",
},
title: {
marginInlineEnd: spacing._2,
},
actionTrigger: {
marginInlineStart: "-0.75rem",
backgroundColor: "transparent",
borderStyle: "none",
},
closeTrigger: {
insetInlineEnd: spacing._2,
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
width: "1.5rem",
height: "1.5rem",
borderRadius: radii.sm,
borderStyle: "none",
color: semanticColors.fgMuted,
backgroundColor: { default: "transparent", ":hover": semanticColors.bgSubtle },
cursor: "pointer",
},
body: {
flex: 1,
minWidth: 0,
},
});
function typeColorFor(type?: string) {
return (
(type === "success" && typeColor.success) ||
(type === "error" && typeColor.error) ||
(type === "warning" && typeColor.warning) ||
(type === "info" && typeColor.info) ||
undefined
);
}
const typeColor = stylex.create({
success: { "--toast-indicated-color": semanticColors.fgSuccess },
error: { "--toast-indicated-color": semanticColors.fgError },
warning: { "--toast-indicated-color": semanticColors.fgWarning },
info: { "--toast-indicated-color": semanticColors.fgInfo },
});
const toastRecipe = defineSlotRecipe({
slots: {
root: { styles: toastRoot },
title: { styles: toastTitle },
description: { styles: toastDescription },
indicator: { styles: toastIndicator },
actionTrigger: { styles: toastActionTrigger },
closeTrigger: { styles: toastCloseTrigger },
},
defaultVariants: {},
});
const { withProvider, withContext } = createSlotRecipeContext(toastRecipe);
function TypeIcon({ type }: { type?: string }) {
if (type === "success") {
return (
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" aria-hidden="true">
<circle cx="10" cy="10" r="7.5" stroke="currentColor" strokeWidth="1.5" />
<path d="M7 10.2l2 2 4-4.4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
</svg>
);
}
if (type === "error" || type === "warning") {
return (
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" aria-hidden="true">
<circle cx="10" cy="10" r="7.5" stroke="currentColor" strokeWidth="1.5" />
<path d="M10 6.5v4.2M10 13.4v.1" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
</svg>
);
}
return (
<svg width="20" height="20" viewBox="0 0 20 20" fill="none" aria-hidden="true">
<circle cx="10" cy="10" r="7.5" stroke="currentColor" strokeWidth="1.5" />
<path d="M10 9.4v4M10 6.6v.1" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
</svg>
);
}
function CloseIcon() {
return (
<svg width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true">
<path d="M2.5 2.5l7 7m0-7l-7 7" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" />
</svg>
);
}
/**
* The default queue, like Ark's `toaster`: call `toaster.success(...)`,
* `toaster("Saved", { description })` or `toaster.dismiss(id)` from
* anywhere. `createToaster()` makes another queue for a second region.
*/
export const toaster = createToaster();
/**
* Toast — UI SDK's real toast queue: mount `<Toaster />` once and call
* `toaster({ title, description, type })` from anywhere (no provider). The
* region renders every queued toast through the toast slot recipe with
* timers, pause on hover/focus, swipe-to-dismiss, Escape, and a11y
* announcements handled by the engine. Each toast's type shows as
* `data-type` and tints the indicator.
*/
export const Toast = {
Viewport: withContext(ToastRegion, () => [chrome.viewport], "ToastViewport"),
Root: withProvider(
UiSdkToast,
() => [...resolveSlotStyles(toastRecipe.slots.root, {}), chrome.root],
"ToastRoot",
),
Title: withContext(
ToastTitle,
() => [...resolveSlotStyles(toastRecipe.slots.title, {}), chrome.title],
"ToastTitle",
),
Description: withContext(ToastDescription, "description", "ToastDescription"),
Indicator: withContext("span", "indicator", "ToastIndicator"),
Action: withContext(
ToastAction,
() => [...resolveSlotStyles(toastRecipe.slots.actionTrigger, {}), chrome.actionTrigger],
"ToastAction",
),
Close: withContext(
ToastClose,
() => [...resolveSlotStyles(toastRecipe.slots.closeTrigger, {}), chrome.closeTrigger],
"ToastClose",
),
createToaster,
};
/**
* Mount once: the bottom-end region showing `toaster`'s queue (or the
* queue passed as `toaster`).
*/
export function Toaster({ toaster: queue = toaster }: { toaster?: ToasterQueue<React.ReactNode> }) {
return (
<Toast.Viewport toaster={queue} side="bottom" align="end">
{(item) => (
<Toast.Root style={typeColorFor(item.type)}>
<Toast.Indicator>
<TypeIcon type={item.type} />
</Toast.Indicator>
<div {...stylex.props(chrome.body)}>
<Toast.Title />
<Toast.Description />
</div>
{item.action ? <Toast.Action /> : null}
<Toast.Close aria-label="Dismiss notification">
<CloseIcon />
</Toast.Close>
</Toast.Root>
)}
</Toast.Viewport>
);
}
Toaster.displayName = "Toaster";