Toast
A Discord-inspired transient notification in four variants, info, success, warning and danger, that slides in, wraps long text and dismisses itself after a timeout, on a swipe or when closed.
A Discord-inspired transient notification with a status icon, built on Radix Toast in React and Reka UI Toast in Vue. The primitive owns the behaviour; newt/ui owns the look. It dismisses itself after a timeout, on a swipe, or when the reader closes it.
newt-toast"use client"
import * as React from "react"
import { Button } from "@/registry/bases/newt/ui/button"
import {
Toast,
ToastClose,
ToastContent,
ToastDescription,
ToastIcon,
ToastProvider,
ToastTitle,
ToastViewport,
} from "@/registry/bases/newt/ui/toast"
/*
* `static` keeps the viewport in the page flow, so the toast shows inside
* the preview. An application leaves the class out and the toasts float in
* the corner of the screen.
*/
export default function ToastDemo() {
const [key, setKey] = React.useState(0)
return (
<ToastProvider>
<div className="flex flex-col items-start gap-4">
<ToastViewport className="static items-start" />
<Toast key={key} variant="success" duration={5000}>
<ToastIcon />
<ToastContent>
<ToastTitle>Success title</ToastTitle>
<ToastDescription>Description text.</ToastDescription>
</ToastContent>
<ToastClose />
</Toast>
<Button
variant="secondary"
size="sm"
onClick={() => setKey((k) => k + 1)}
>
Show toast again
</Button>
</div>
</ToastProvider>
)
}<script setup lang="ts">
import { ref } from "vue"
import { Button } from "~~/registry/bases/newt/ui/button"
import {
Toast,
ToastClose,
ToastContent,
ToastDescription,
ToastIcon,
ToastProvider,
ToastTitle,
ToastViewport,
} from "~~/registry/bases/newt/ui/toast"
/*
* `static` keeps the viewport in the page flow, so the toast shows inside
* the preview. An application leaves the class out and the toasts float in
* the corner of the screen.
*/
const key = ref(0)
</script>
<template>
<ToastProvider>
<div class="flex flex-col items-start gap-4">
<ToastViewport class="static items-start" />
<Toast :key="key" variant="success" :duration="5000">
<ToastIcon />
<ToastContent>
<ToastTitle>Success title</ToastTitle>
<ToastDescription>Description text.</ToastDescription>
</ToastContent>
<ToastClose />
</Toast>
<Button variant="secondary" size="sm" @click="key++">
Show toast again
</Button>
</div>
</ToastProvider>
</template>A toast is a floating surface at most 280px wide, with 12px of padding, a 12px radius, a subtle edge and the high shadow. It holds an 18px icon, a 14px title and an optional description, and a close control. It fades in and slides from the inline-end edge, where the viewport sits, in --newt-duration-slow; it leaves the same way in --newt-duration-fast. The slide mirrors in a right-to-left page.
Installation
npx newtui@latest add toastnpx shadcn@latest add @newtui/toastnpx shadcn-vue@latest add @newtui/toastpnpm dlx newtui@latest add toastpnpm dlx shadcn@latest add @newtui/toastpnpm dlx shadcn-vue@latest add @newtui/toastyarn dlx newtui@latest add toastyarn dlx shadcn@latest add @newtui/toastyarn dlx shadcn-vue@latest add @newtui/toastbunx newtui@latest add toastbunx shadcn@latest add @newtui/toastbunx shadcn-vue@latest add @newtui/toastdeno run -A npm:newtui@latest add toastdeno run -A npm:shadcn@latest add @newtui/toastdeno run -A npm:shadcn-vue@latest add @newtui/toastvlx newtui@latest add toastvlx shadcn@latest add @newtui/toastvlx shadcn-vue@latest add @newtui/toastvp dlx newtui@latest add toastvp dlx shadcn@latest add @newtui/toastvp dlx shadcn-vue@latest add @newtui/toastnubx newtui@latest add toastnubx shadcn@latest add @newtui/toastnubx shadcn-vue@latest add @newtui/toastnlx newtui@latest add toastnlx shadcn@latest add @newtui/toastnlx shadcn-vue@latest add @newtui/toastupx newtui@latest add toastupx shadcn@latest add @newtui/toastupx shadcn-vue@latest add @newtui/toast- newtui
- shadcn
- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
The shadcn CLI needs the @newtui registry in your components.json. Set it up.
Install the dependencies
npm install class-variance-authority radix-uipnpm add class-variance-authority radix-uiyarn add class-variance-authority radix-uibun add class-variance-authority radix-uideno add npm:class-variance-authority npm:radix-uivlt install class-variance-authority radix-uivp add class-variance-authority radix-uinub add class-variance-authority radix-uini class-variance-authority radix-uiupm add class-variance-authority radix-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
npm install class-variance-authority reka-uipnpm add class-variance-authority reka-uiyarn add class-variance-authority reka-uibun add class-variance-authority reka-uideno add npm:class-variance-authority npm:reka-uivlt install class-variance-authority reka-uivp add class-variance-authority reka-uinub add class-variance-authority reka-uini class-variance-authority reka-uiupm add class-variance-authority reka-ui- npm
- pnpm
- yarn
- bun
- deno
- vlt
- vp
- nub
- ni
- upm
Add the components it builds on
Copy the source into your project
"use client"
import { cva, type VariantProps } from "class-variance-authority"
import { Toast as ToastPrimitive } from "radix-ui"
import * as React from "react"
import { cn } from "@/lib/utils"
import { Button } from "@/registry/bases/newt/ui/button"
/*
* A floating surface at most 280px wide; long text wraps inside it. The
* enter animation fades in and slides from `--newt-toast-slide`, which the
* viewport sets, in `--newt-duration-slow`; the exit plays it backwards in
* `--newt-duration-fast`, and the primitive keeps the toast mounted until it
* ends. Both animate `translate`, so a swipe owns `transform` and works
* during the enter animation too. Reduced motion turns both off.
*/
const toastVariants = cva(
[
"box-border flex w-fit max-w-[min(280px,100%)] items-start gap-[var(--newt-space-2)] rounded-[var(--newt-radius-lg)] bg-newt-bg-floating p-[var(--newt-space-3)] font-[family-name:var(--newt-font-sans)] text-[length:var(--newt-font-size-sm)] leading-[18px] text-newt-text-primary [overflow-wrap:anywhere] shadow-[inset_0_0_0_1px_var(--newt-border-subtle),var(--newt-shadow-elevation-high)]",
"focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-focus-ring",
"data-[state=open]:animate-[newt-toast-in_var(--newt-duration-slow)_var(--newt-ease-overlay)] data-[state=closed]:animate-[newt-toast-out_var(--newt-duration-fast)_var(--newt-ease-in)_forwards] motion-reduce:data-[state=open]:animate-none motion-reduce:data-[state=closed]:animate-none",
/* The toast follows the pointer while it is swiped away. */
"data-[swipe=move]:[transform:translate(var(--radix-toast-swipe-move-x,0),var(--radix-toast-swipe-move-y,0))] data-[swipe=cancel]:[transform:none] data-[swipe=cancel]:transition-[transform] data-[swipe=cancel]:duration-fast data-[swipe=cancel]:ease-newt motion-reduce:data-[swipe=cancel]:transition-none data-[swipe=end]:[transform:translate(var(--radix-toast-swipe-end-x,0),var(--radix-toast-swipe-end-y,0))]",
],
{
variants: {
variant: {
info: "",
success: "",
warning: "",
danger: "",
},
},
defaultVariants: {
variant: "success",
},
}
)
/*
* The icon colour of each variant reaches 3:1 on the floating surface. Danger
* takes the danger text colour: the danger fill is too dark on that surface.
*/
const toastIconVariants = cva("flex size-[18px] shrink-0 [&>svg]:size-full", {
variants: {
variant: {
info: "text-newt-text-link",
success: "text-newt-online",
warning: "text-newt-idle",
danger: "text-newt-text-danger",
},
},
defaultVariants: {
variant: "success",
},
})
type ToastVariant = NonNullable<VariantProps<typeof toastVariants>["variant"]>
/* The variant of the enclosing toast, which picks the icon's colour and glyph. */
const ToastVariantContext = React.createContext<ToastVariant>("success")
/* Each glyph on a 24px grid: the outline first, then the marks inside. */
const RING = "M12 3a9 9 0 1 0 0 18a9 9 0 1 0 0-18Z"
const glyphs = {
info: [RING, "M12 11v5", "M12 8h.01"],
success: [RING, "M8.5 12.5l2.5 2.5l4.5-5"],
warning: ["M12 4L21 19.5H3Z", "M12 10v4", "M12 16.8h.01"],
danger: [RING, "M9.5 9.5l5 5", "M14.5 9.5l-5 5"],
} satisfies Record<ToastVariant, readonly string[]>
const CROSS = ["M7 7l10 10", "M17 7L7 17"]
const Glyph = ({ paths }: { paths: readonly string[] }) => (
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
focusable="false"
>
{paths.map((d) => (
<path key={d} d={d} />
))}
</svg>
)
/* `translate` leaves `transform` to a swipe. */
const keyframes = `
@keyframes newt-toast-in { from { opacity: 0; translate: var(--newt-toast-slide); } }
@keyframes newt-toast-out { to { opacity: 0; translate: var(--newt-toast-slide); } }`
/*
* Radix owns the behaviour: the dismiss timer and its pause on hover, focus
* and window blur, swipe to dismiss, Escape, and the announcement to screen
* readers. Only the look is newt/ui's. Every toast needs a `ToastProvider`
* above it and a `ToastViewport` inside that provider.
*/
const ToastProvider = ToastPrimitive.Provider
/*
* The list every toast of the provider is rendered into, wherever the toast
* itself is written. It floats at the bottom inline-end corner, so a toast
* slides in from the inline-end edge, mirrored in a right-to-left page. Pass
* `className="static items-start"` to lay it out in the page flow instead.
* A viewport placed elsewhere sets `--newt-toast-slide` to the offset a toast
* enters from, such as `[--newt-toast-slide:0_-100%]` at the top.
*/
const ToastViewport = React.forwardRef<
React.ComponentRef<typeof ToastPrimitive.Viewport>,
React.ComponentPropsWithoutRef<typeof ToastPrimitive.Viewport>
>(({ className, ...props }, ref) => (
<>
<style>{keyframes}</style>
<ToastPrimitive.Viewport
ref={ref}
className={cn(
"fixed bottom-[var(--newt-space-4)] end-[var(--newt-space-4)] z-[130] m-0 flex max-w-[calc(100vw-2*var(--newt-space-4))] list-none flex-col items-end gap-[var(--newt-space-2)] p-0 [--newt-toast-slide:calc(var(--newt-dir,1)*100%)_0] focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-focus-ring",
className
)}
{...props}
/>
</>
))
ToastViewport.displayName = "ToastViewport"
export interface ToastProps
extends
React.ComponentPropsWithoutRef<typeof ToastPrimitive.Root>,
VariantProps<typeof toastVariants> {
/**
* Auto-dismiss after this many ms. Defaults to the provider's `duration`,
* which is 5000. Pass 0 to disable.
*/
duration?: number
}
/*
* How the variant is announced. Radix reads a `foreground` toast through an
* assertive live region, which interrupts the screen reader, and a
* `background` one politely. Only a danger toast is urgent.
*/
const announcement = (variant: ToastVariant) =>
variant === "danger" ? "foreground" : "background"
const Toast = React.forwardRef<
React.ComponentRef<typeof ToastPrimitive.Root>,
ToastProps
>(({ className, variant, duration, type, ...props }, ref) => {
const resolved = variant ?? "success"
return (
<ToastVariantContext.Provider value={resolved}>
<ToastPrimitive.Root
ref={ref}
type={type ?? announcement(resolved)}
/* Radix reads 0 as "not set" and falls back to the provider's value. */
duration={duration === 0 ? Infinity : duration}
data-variant={resolved}
className={cn(toastVariants({ variant }), className)}
{...props}
/>
</ToastVariantContext.Provider>
)
})
Toast.displayName = "Toast"
/* The variant's glyph, hidden from assistive technology; the text names it. */
const ToastIcon = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, children, ...props }, ref) => {
const variant = React.useContext(ToastVariantContext)
return (
<div
ref={ref}
aria-hidden="true"
className={cn(toastIconVariants({ variant }), className)}
{...props}
>
{children ?? <Glyph paths={glyphs[variant]} />}
</div>
)
})
ToastIcon.displayName = "ToastIcon"
const ToastContent = React.forwardRef<
HTMLDivElement,
React.HTMLAttributes<HTMLDivElement>
>(({ className, ...props }, ref) => (
<div
ref={ref}
className={cn(
"flex min-w-0 flex-1 flex-col items-start gap-[var(--newt-space-1)]",
className
)}
{...props}
/>
))
ToastContent.displayName = "ToastContent"
const ToastTitle = React.forwardRef<
React.ComponentRef<typeof ToastPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof ToastPrimitive.Title>
>(({ className, ...props }, ref) => (
<ToastPrimitive.Title
ref={ref}
className={cn("font-[number:var(--newt-font-weight-medium)]", className)}
{...props}
/>
))
ToastTitle.displayName = "ToastTitle"
const ToastDescription = React.forwardRef<
React.ComponentRef<typeof ToastPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof ToastPrimitive.Description>
>(({ className, ...props }, ref) => (
<ToastPrimitive.Description
ref={ref}
className={cn("text-newt-text-secondary", className)}
{...props}
/>
))
ToastDescription.displayName = "ToastDescription"
/*
* A button that acts on the toast and then closes it. It has no look of its
* own; wrap a Button with `asChild`. `altText` tells a screen reader user
* another way to do the same thing, because the toast can be gone before
* they reach the button.
*/
const ToastAction = ToastPrimitive.Action
/*
* The secondary icon Button, pulled 4px into the toast's padding so a
* one-line toast keeps its height. Its accessible name is "Dismiss". With
* `asChild`, the close behaviour and the "Dismiss" name go on your own element
* instead; an `aria-label` on that element or on ToastClose wins.
*/
const ToastClose = React.forwardRef<
React.ComponentRef<typeof ToastPrimitive.Close>,
React.ComponentPropsWithoutRef<typeof ToastPrimitive.Close>
>(({ className, children, asChild = false, ...props }, ref) =>
asChild ? (
<ToastPrimitive.Close
ref={ref}
aria-label="Dismiss"
asChild
className={className}
{...props}
>
{children}
</ToastPrimitive.Close>
) : (
<ToastPrimitive.Close ref={ref} aria-label="Dismiss" asChild {...props}>
<Button
variant="secondary"
size="icon"
className={cn(
"-my-[var(--newt-space-1)] -me-[var(--newt-space-1)] shrink-0 [&_svg]:size-[var(--newt-space-4)]",
className
)}
>
{children ?? <Glyph paths={CROSS} />}
</Button>
</ToastPrimitive.Close>
)
)
ToastClose.displayName = "ToastClose"
export {
ToastProvider,
ToastViewport,
Toast,
ToastIcon,
ToastContent,
ToastTitle,
ToastDescription,
ToastAction,
ToastClose,
toastVariants,
toastIconVariants,
}<script setup lang="ts">
import {
ToastProvider,
useForwardProps,
type ToastProviderProps,
} from "reka-ui"
/*
* Reka owns the behaviour: the dismiss timer and its pause on hover, focus
* and window blur, swipe to dismiss, Escape, and the announcement to screen
* readers. Only the look is newt/ui's. Every toast needs a `ToastProvider`
* above it and a `ToastViewport` inside that provider.
*/
const props = defineProps<ToastProviderProps>()
const forwarded = useForwardProps(props)
</script>
<template>
<ToastProvider v-bind="forwarded">
<slot />
</ToastProvider>
</template><script setup lang="ts">
import {
ToastViewport,
useForwardProps,
type ToastViewportProps,
} from "reka-ui"
import type { HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
/*
* The list every toast of the provider is rendered into, wherever the toast
* itself is written. It floats at the bottom inline-end corner, so a toast
* slides in from the inline-end edge, mirrored in a right-to-left page. Pass
* `class="static items-start"` to lay it out in the page flow instead. A
* viewport placed elsewhere sets `--newt-toast-slide` to the offset a toast
* enters from, such as `[--newt-toast-slide:0_-100%]` at the top.
*/
const { class: className, ...delegated } = defineProps<
ToastViewportProps & { class?: HTMLAttributes["class"] }
>()
const forwarded = useForwardProps(delegated)
</script>
<template>
<ToastViewport
v-bind="forwarded"
:class="
cn(
'fixed bottom-[var(--newt-space-4)] end-[var(--newt-space-4)] z-[130] m-0 flex max-w-[calc(100vw-2*var(--newt-space-4))] list-none flex-col items-end gap-[var(--newt-space-2)] p-0 [--newt-toast-slide:calc(var(--newt-dir,1)*100%)_0] focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-focus-ring',
className
)
"
/>
</template>
<style>
/* `translate` leaves `transform` to a swipe. */
@keyframes newt-toast-in {
from {
opacity: 0;
translate: var(--newt-toast-slide);
}
}
@keyframes newt-toast-out {
to {
opacity: 0;
translate: var(--newt-toast-slide);
}
}
</style><script setup lang="ts">
import {
ToastRoot,
useForwardPropsEmits,
type ToastRootEmits,
type ToastRootProps,
} from "reka-ui"
import { computed, provide, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
import {
TOAST_INJECTION_KEY,
toastAnnouncement,
toastVariants,
type ToastVariant,
} from "."
/*
* Use `v-model:open` to control it. `duration` is the time in ms before it
* dismisses itself: it defaults to the provider's `duration`, which is 5000,
* and 0 disables it. A `danger` toast is announced as `foreground`, which
* interrupts; the others as `background`. `type` overrides that.
*/
const {
class: className,
variant = "success",
type,
...delegated
} = defineProps<
ToastRootProps & {
class?: HTMLAttributes["class"]
variant?: ToastVariant
}
>()
const emits = defineEmits<ToastRootEmits>()
const forwarded = useForwardPropsEmits(delegated, emits)
provide(TOAST_INJECTION_KEY, {
variant: computed(() => variant),
})
</script>
<template>
<ToastRoot
v-bind="forwarded"
:type="type ?? toastAnnouncement(variant)"
:data-variant="variant"
:class="cn(toastVariants({ variant }), className)"
>
<slot />
</ToastRoot>
</template><script setup lang="ts">
import { computed, inject, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
import { TOAST_INJECTION_KEY, toastGlyphs, toastIconVariants } from "."
/* The variant's glyph, hidden from assistive technology; the text names it. */
const { class: className } = defineProps<{ class?: HTMLAttributes["class"] }>()
const ctx = inject(TOAST_INJECTION_KEY, null)
const variant = computed(() => ctx?.variant.value ?? "success")
</script>
<template>
<div
aria-hidden="true"
:class="cn(toastIconVariants({ variant }), className)"
>
<slot>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path v-for="d in toastGlyphs[variant]" :key="d" :d="d" />
</svg>
</slot>
</div>
</template><script setup lang="ts">
import type { HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
const { class: className } = defineProps<{ class?: HTMLAttributes["class"] }>()
</script>
<template>
<div
:class="
cn(
'flex min-w-0 flex-1 flex-col items-start gap-[var(--newt-space-1)]',
className
)
"
>
<slot />
</div>
</template><script setup lang="ts">
import { ToastTitle, useForwardProps, type ToastTitleProps } from "reka-ui"
import { computed, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
const props = defineProps<
ToastTitleProps & { class?: HTMLAttributes["class"] }
>()
const delegated = computed(() => {
const { class: _class, ...rest } = props
return rest
})
const forwarded = useForwardProps(delegated)
</script>
<template>
<ToastTitle
v-bind="forwarded"
:class="cn('font-[number:var(--newt-font-weight-medium)]', props.class)"
>
<slot />
</ToastTitle>
</template><script setup lang="ts">
import {
ToastDescription,
useForwardProps,
type ToastDescriptionProps,
} from "reka-ui"
import { computed, type HTMLAttributes } from "vue"
import { cn } from "@/lib/utils"
const props = defineProps<
ToastDescriptionProps & { class?: HTMLAttributes["class"] }
>()
const delegated = computed(() => {
const { class: _class, ...rest } = props
return rest
})
const forwarded = useForwardProps(delegated)
</script>
<template>
<ToastDescription
v-bind="forwarded"
:class="cn('text-newt-text-secondary', props.class)"
>
<slot />
</ToastDescription>
</template><script setup lang="ts">
import { ToastAction, useForwardProps, type ToastActionProps } from "reka-ui"
/*
* A button that acts on the toast and then closes it. It has no look of its
* own; wrap a Button with `as-child`. `altText` tells a screen reader user
* another way to do the same thing, because the toast can be gone before
* they reach the button.
*/
const props = defineProps<ToastActionProps & { altText: string }>()
const forwarded = useForwardProps(props)
</script>
<template>
<ToastAction v-bind="forwarded">
<slot />
</ToastAction>
</template><script setup lang="ts">
import { ToastClose, useForwardProps, type ToastCloseProps } from "reka-ui"
import type { HTMLAttributes } from "vue"
import { Button } from "~~/registry/bases/newt/ui/button"
import { cn } from "@/lib/utils"
import { toastCloseGlyph } from "."
/*
* The secondary icon Button, pulled 4px into the toast's padding so a
* one-line toast keeps its height. Its accessible name is "Dismiss". With
* `as-child`, the close behaviour and the "Dismiss" name go on your own element
* instead; an `aria-label` on that element or on ToastClose wins.
*/
const {
class: className,
asChild = false,
...delegated
} = defineProps<ToastCloseProps & { class?: HTMLAttributes["class"] }>()
const forwarded = useForwardProps(delegated)
</script>
<template>
<ToastClose
v-if="asChild"
aria-label="Dismiss"
v-bind="forwarded"
as-child
:class="className"
>
<slot />
</ToastClose>
<ToastClose v-else aria-label="Dismiss" v-bind="forwarded" as-child>
<Button
variant="secondary"
size="icon"
:class="
cn(
'-my-[var(--newt-space-1)] -me-[var(--newt-space-1)] shrink-0 [&_svg]:size-[var(--newt-space-4)]',
className
)
"
>
<slot>
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
focusable="false"
>
<path v-for="d in toastCloseGlyph" :key="d" :d="d" />
</svg>
</slot>
</Button>
</ToastClose>
</template>import { cva, type VariantProps } from "class-variance-authority"
import type { ComputedRef, InjectionKey } from "vue"
export { default as ToastProvider } from "./ToastProvider.vue"
export { default as ToastViewport } from "./ToastViewport.vue"
export { default as Toast } from "./Toast.vue"
export { default as ToastIcon } from "./ToastIcon.vue"
export { default as ToastContent } from "./ToastContent.vue"
export { default as ToastTitle } from "./ToastTitle.vue"
export { default as ToastDescription } from "./ToastDescription.vue"
export { default as ToastAction } from "./ToastAction.vue"
export { default as ToastClose } from "./ToastClose.vue"
/*
* A floating surface at most 280px wide; long text wraps inside it. The
* enter animation fades in and slides from `--newt-toast-slide`, which the
* viewport sets, in `--newt-duration-slow`; the exit plays it backwards in
* `--newt-duration-fast`, and the primitive keeps the toast mounted until it
* ends. Both animate `translate`, so a swipe owns `transform` and works
* during the enter animation too. Reduced motion turns both off.
*/
export const toastVariants = cva(
[
"box-border flex w-fit max-w-[min(280px,100%)] items-start gap-[var(--newt-space-2)] rounded-[var(--newt-radius-lg)] bg-newt-bg-floating p-[var(--newt-space-3)] font-[family-name:var(--newt-font-sans)] text-[length:var(--newt-font-size-sm)] leading-[18px] text-newt-text-primary [overflow-wrap:anywhere] shadow-[inset_0_0_0_1px_var(--newt-border-subtle),var(--newt-shadow-elevation-high)]",
"focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-newt-focus-ring",
"data-[state=open]:animate-[newt-toast-in_var(--newt-duration-slow)_var(--newt-ease-overlay)] data-[state=closed]:animate-[newt-toast-out_var(--newt-duration-fast)_var(--newt-ease-in)_forwards] motion-reduce:data-[state=open]:animate-none motion-reduce:data-[state=closed]:animate-none",
/* The toast follows the pointer while it is swiped away. */
"data-[swipe=move]:[transform:translate(var(--reka-toast-swipe-move-x,0),var(--reka-toast-swipe-move-y,0))] data-[swipe=cancel]:[transform:none] data-[swipe=cancel]:transition-[transform] data-[swipe=cancel]:duration-fast data-[swipe=cancel]:ease-newt motion-reduce:data-[swipe=cancel]:transition-none data-[swipe=end]:[transform:translate(var(--reka-toast-swipe-end-x,0),var(--reka-toast-swipe-end-y,0))]",
],
{
variants: {
variant: {
info: "",
success: "",
warning: "",
danger: "",
},
},
defaultVariants: {
variant: "success",
},
}
)
/*
* The icon colour of each variant reaches 3:1 on the floating surface. Danger
* takes the danger text colour: the danger fill is too dark on that surface.
*/
export const toastIconVariants = cva(
"flex size-[18px] shrink-0 [&>svg]:size-full",
{
variants: {
variant: {
info: "text-newt-text-link",
success: "text-newt-online",
warning: "text-newt-idle",
danger: "text-newt-text-danger",
},
},
defaultVariants: {
variant: "success",
},
}
)
export type ToastVariants = VariantProps<typeof toastVariants>
export type ToastVariant = NonNullable<ToastVariants["variant"]>
/* Each glyph on a 24px grid: the outline first, then the marks inside. */
const RING = "M12 3a9 9 0 1 0 0 18a9 9 0 1 0 0-18Z"
export const toastGlyphs = {
info: [RING, "M12 11v5", "M12 8h.01"],
success: [RING, "M8.5 12.5l2.5 2.5l4.5-5"],
warning: ["M12 4L21 19.5H3Z", "M12 10v4", "M12 16.8h.01"],
danger: [RING, "M9.5 9.5l5 5", "M14.5 9.5l-5 5"],
} satisfies Record<ToastVariant, readonly string[]>
export const toastCloseGlyph = ["M7 7l10 10", "M17 7L7 17"]
/*
* How the variant is announced. Reka reads a `foreground` toast through an
* assertive live region, which interrupts the screen reader, and a
* `background` one politely. Only a danger toast is urgent.
*/
export const toastAnnouncement = (variant: ToastVariant) =>
variant === "danger" ? "foreground" : "background"
/* The variant of the enclosing toast, which picks the icon's colour and glyph. */
export interface ToastContext {
variant: ComputedRef<ToastVariant>
}
export const TOAST_INJECTION_KEY: InjectionKey<ToastContext> =
Symbol("newt-toast")Update the import paths
The files import @/lib/utils and sibling components through the aliases in your components.json. Point them at wherever those live in your project.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Toast to your React project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
No CLI, no components.json: paste this into Claude, Cursor, Copilot or any coding assistant and it adds Toast to your Vue project by hand. The prompt carries the source of every file the CLI would write — this component's and those of the components it builds on — with the imports already pointed at the default aliases.
Usage
import { Toast } from "@/components/ui/toast"
export function Example() {
return <Toast />
}<script setup lang="ts">
import { ToastProvider, ToastViewport, Toast, ToastIcon, ToastContent, ToastTitle, ToastDescription, ToastAction, ToastClose } from "@/components/ui/toast"
</script>
<template>
<ToastProvider />
</template>Wrap the application in one ToastProvider and put one ToastViewport inside it. Every Toast below the provider is rendered into the viewport, wherever you write the toast. The viewport floats at the bottom inline-end corner of the screen, 16px from both edges, with 8px between toasts. If you move it, set --newt-toast-slide on it to the offset a toast enters from: className="top-[var(--newt-space-4)] bottom-auto [--newt-toast-slide:0_-100%]" puts the viewport at the top and slides each toast down from above.
The previews on this page pass static items-start to ToastViewport. Those classes keep the viewport in the page flow, so each toast shows inside its preview.
Examples
Variants
variant is info, success (the default), warning or danger. It picks the glyph and colour of ToastIcon, and how a screen reader hears the toast: a danger toast interrupts, the others wait. Write a title that says what happened, so the colour is never the only cue. These toasts set duration to 0 and leave out ToastClose, so they stay on screen.
newt-toastimport {
Toast,
ToastContent,
ToastDescription,
ToastIcon,
ToastProvider,
ToastTitle,
ToastViewport,
} from "@/registry/bases/newt/ui/toast"
/*
* `variant` picks the icon's glyph and colour, and how a screen reader hears
* the toast: `danger` interrupts, the others wait. The title says what
* happened, so the colour is never the only cue. `duration={0}` keeps all
* four on screen, and without a `ToastClose` only a swipe or Escape
* dismisses them.
*/
export default function ToastVariantsDemo() {
return (
<ToastProvider>
<ToastViewport className="static items-start" />
<Toast variant="info" duration={0}>
<ToastIcon />
<ToastContent>
<ToastTitle>New version available</ToastTitle>
<ToastDescription>Reload to get the latest update.</ToastDescription>
</ToastContent>
</Toast>
<Toast variant="success" duration={0}>
<ToastIcon />
<ToastContent>
<ToastTitle>Changes saved</ToastTitle>
<ToastDescription>
Your server settings are up to date.
</ToastDescription>
</ToastContent>
</Toast>
<Toast variant="warning" duration={0}>
<ToastIcon />
<ToastContent>
<ToastTitle>Almost out of space</ToastTitle>
<ToastDescription>Uploads stop at 10 MB.</ToastDescription>
</ToastContent>
</Toast>
<Toast variant="danger" duration={0}>
<ToastIcon />
<ToastContent>
<ToastTitle>Message not sent</ToastTitle>
<ToastDescription>
You are offline. It will send when you reconnect.
</ToastDescription>
</ToastContent>
</Toast>
</ToastProvider>
)
}<script setup lang="ts">
import {
Toast,
ToastContent,
ToastDescription,
ToastIcon,
ToastProvider,
ToastTitle,
ToastViewport,
} from "~~/registry/bases/newt/ui/toast"
/*
* `variant` picks the icon's glyph and colour, and how a screen reader hears
* the toast: `danger` interrupts, the others wait. The title says what
* happened, so the colour is never the only cue. `:duration="0"` keeps all
* four on screen, and without a `ToastClose` only a swipe or Escape
* dismisses them.
*/
</script>
<template>
<ToastProvider>
<ToastViewport class="static items-start" />
<Toast variant="info" :duration="0">
<ToastIcon />
<ToastContent>
<ToastTitle>New version available</ToastTitle>
<ToastDescription>Reload to get the latest update.</ToastDescription>
</ToastContent>
</Toast>
<Toast variant="success" :duration="0">
<ToastIcon />
<ToastContent>
<ToastTitle>Changes saved</ToastTitle>
<ToastDescription>
Your server settings are up to date.
</ToastDescription>
</ToastContent>
</Toast>
<Toast variant="warning" :duration="0">
<ToastIcon />
<ToastContent>
<ToastTitle>Almost out of space</ToastTitle>
<ToastDescription>Uploads stop at 10 MB.</ToastDescription>
</ToastContent>
</Toast>
<Toast variant="danger" :duration="0">
<ToastIcon />
<ToastContent>
<ToastTitle>Message not sent</ToastTitle>
<ToastDescription>
You are offline. It will send when you reconnect.
</ToastDescription>
</ToastContent>
</Toast>
</ToastProvider>
</template>Long text
A toast never grows wider than 280px. A long title or description wraps, and a long word such as a link breaks anywhere, so the toast stays inside a 320px screen.
newt-toastimport {
Toast,
ToastClose,
ToastContent,
ToastDescription,
ToastIcon,
ToastProvider,
ToastTitle,
ToastViewport,
} from "@/registry/bases/newt/ui/toast"
/*
* A toast is never wider than 280px. Longer text wraps, and a word with no
* break point breaks anywhere, so the toast stays inside a narrow screen.
*/
export default function ToastLongDemo() {
return (
<ToastProvider>
<ToastViewport className="static items-start" />
<Toast variant="info" duration={0}>
<ToastIcon />
<ToastContent>
<ToastTitle>
A long notification title that wraps onto a second line
</ToastTitle>
<ToastDescription>
The invite to https://example.com/invite/a-very-long-invite-code
expires in seven days. Share it before then.
</ToastDescription>
</ToastContent>
<ToastClose />
</Toast>
</ToastProvider>
)
}<script setup lang="ts">
import {
Toast,
ToastClose,
ToastContent,
ToastDescription,
ToastIcon,
ToastProvider,
ToastTitle,
ToastViewport,
} from "~~/registry/bases/newt/ui/toast"
/*
* A toast is never wider than 280px. Longer text wraps, and a word with no
* break point breaks anywhere, so the toast stays inside a narrow screen.
*/
</script>
<template>
<ToastProvider>
<ToastViewport class="static items-start" />
<Toast variant="info" :duration="0">
<ToastIcon />
<ToastContent>
<ToastTitle>
A long notification title that wraps onto a second line
</ToastTitle>
<ToastDescription>
The invite to https://example.com/invite/a-very-long-invite-code
expires in seven days. Share it before then.
</ToastDescription>
</ToastContent>
<ToastClose />
</Toast>
</ToastProvider>
</template>Controlled
Hold open yourself to raise a toast in answer to an action. The timer and the close button both report false through onOpenChange in React, or update:open in Vue, which v-model:open handles.
newt-toast"use client"
import * as React from "react"
import { Button } from "@/registry/bases/newt/ui/button"
import {
Toast,
ToastClose,
ToastContent,
ToastIcon,
ToastProvider,
ToastTitle,
ToastViewport,
} from "@/registry/bases/newt/ui/toast"
/*
* The parent owns `open`: an action raises the toast, and the timer or the
* close button hands `false` back through `onOpenChange`.
*/
export default function ToastControlledDemo() {
const [open, setOpen] = React.useState(false)
return (
<ToastProvider>
<div className="flex min-h-28 flex-col items-start gap-4">
<Button onClick={() => setOpen(true)}>Copy invite link</Button>
<ToastViewport className="static items-start" />
<Toast open={open} onOpenChange={setOpen} duration={3000}>
<ToastIcon />
<ToastContent>
<ToastTitle>Invite link copied</ToastTitle>
</ToastContent>
<ToastClose />
</Toast>
</div>
</ToastProvider>
)
}<script setup lang="ts">
import { ref } from "vue"
import { Button } from "~~/registry/bases/newt/ui/button"
import {
Toast,
ToastClose,
ToastContent,
ToastIcon,
ToastProvider,
ToastTitle,
ToastViewport,
} from "~~/registry/bases/newt/ui/toast"
/*
* The parent owns `open`: an action raises the toast, and the timer or the
* close button hands `false` back through `update:open`.
*/
const open = ref(false)
</script>
<template>
<ToastProvider>
<div class="flex min-h-28 flex-col items-start gap-4">
<Button @click="open = true">Copy invite link</Button>
<ToastViewport class="static items-start" />
<Toast v-model:open="open" :duration="3000">
<ToastIcon />
<ToastContent>
<ToastTitle>Invite link copied</ToastTitle>
</ToastContent>
<ToastClose />
</Toast>
</div>
</ToastProvider>
</template>With an action
Wrap a Button in ToastAction to offer a way back. It goes inside ToastContent, under the text. The action closes the toast after its own click handler runs. Set duration to 0 on a toast like this one: an undo that disappears after five seconds is one most readers miss.
newt-toast"use client"
import * as React from "react"
import { Button } from "@/registry/bases/newt/ui/button"
import {
Toast,
ToastAction,
ToastClose,
ToastContent,
ToastDescription,
ToastIcon,
ToastProvider,
ToastTitle,
ToastViewport,
} from "@/registry/bases/newt/ui/toast"
/*
* A toast that offers a way back must not leave on its own: `duration={0}`
* keeps it until the reader takes the action or dismisses it.
*/
export default function ToastActionDemo() {
const [open, setOpen] = React.useState(true)
return (
<ToastProvider>
<div className="flex min-h-28 flex-col items-start gap-4">
<ToastViewport className="static items-start" />
<Toast open={open} onOpenChange={setOpen} duration={0}>
<ToastIcon />
<ToastContent>
<ToastTitle>Message deleted</ToastTitle>
<ToastDescription>It is gone for everyone.</ToastDescription>
<ToastAction altText="Restore the message from its menu" asChild>
<Button
variant="secondary"
size="sm"
className="mt-[var(--newt-space-1)]"
>
Undo
</Button>
</ToastAction>
</ToastContent>
<ToastClose />
</Toast>
{!open && (
<Button variant="secondary" size="sm" onClick={() => setOpen(true)}>
Delete another message
</Button>
)}
</div>
</ToastProvider>
)
}<script setup lang="ts">
import { ref } from "vue"
import { Button } from "~~/registry/bases/newt/ui/button"
import {
Toast,
ToastAction,
ToastClose,
ToastContent,
ToastDescription,
ToastIcon,
ToastProvider,
ToastTitle,
ToastViewport,
} from "~~/registry/bases/newt/ui/toast"
/*
* A toast that offers a way back must not leave on its own: `:duration="0"`
* keeps it until the reader takes the action or dismisses it.
*/
const open = ref(true)
</script>
<template>
<ToastProvider>
<div class="flex min-h-28 flex-col items-start gap-4">
<ToastViewport class="static items-start" />
<Toast v-model:open="open" :duration="0">
<ToastIcon />
<ToastContent>
<ToastTitle>Message deleted</ToastTitle>
<ToastDescription>It is gone for everyone.</ToastDescription>
<ToastAction alt-text="Restore the message from its menu" as-child>
<Button
variant="secondary"
size="sm"
class="mt-[var(--newt-space-1)]"
>Undo</Button
>
</ToastAction>
</ToastContent>
<ToastClose />
</Toast>
<Button v-if="!open" variant="secondary" size="sm" @click="open = true">
Delete another message
</Button>
</div>
</ToastProvider>
</template>API reference
ToastProvider
duration?number
Time in ms before each toast dismisses itself.
number5000swipeDirection?"right" | "left" | "up" | "down"
The direction of the swipe that dismisses a toast.
"right" | "left" | "up" | "down""right"label?string
The word a screen reader says before each toast. Translate it.
string"Notification"ToastViewport
hotkey?string[]
The keys that move focus to the viewport.
string[]["F8"]label?string
Accessible name of the region. `{hotkey}` is replaced with the keys.
string"Notifications ({hotkey})"Toast
variant?"info" | "success" | "warning" | "danger"
The glyph and colour of `ToastIcon`, and the default `type`: `foreground` for `danger`, `background` for the others.
"info" | "success" | "warning" | "danger""success"duration?number
Time in ms before the toast dismisses itself. It defaults to the provider's `duration`. Pass 0 to disable.
numberopen / v-model:open?boolean
Whether the toast is shown. Leave it out and the toast starts open and owns its state.
booleanonOpenChange?(open: boolean) => void
React only. Called when the toast closes by timeout, swipe, Escape, the action or the close button; Vue emits `update:open`.
(open: boolean) => voidtype?"background" | "foreground"
`background` is announced politely. `foreground` interrupts the screen reader. Leave it out: it follows `variant`.
"background" | "foreground""foreground" for danger, else "background"ToastAction
altText?string
Required. Another way to do the same thing, for a screen reader user who cannot reach the button in time.
stringToastAction renders a button with no look of its own; pass asChild (as-child in Vue) to put the behaviour on a Button. ToastClose renders the secondary icon Button with a cross, pulled 4px into the padding so a one-line toast keeps its height; pass your own glyph as its children, or asChild to put the behaviour on your own element. ToastIcon and ToastContent are plain elements. ToastTitle and ToastDescription hold the text a screen reader announces. Every part also accepts the native attributes of the element it renders, and a className that is merged rather than replaced.
Accessibility
The primitive announces each toast through a live region, so a screen reader reads the title and the description without a move of focus. A danger toast is announced as foreground, which interrupts the screen reader; every other variant is background, which waits for it to finish its sentence. In React, Radix reads the toast through a role="status" region whose aria-live is assertive for foreground and polite for background. In Vue, Reka uses a role="alert" region with the same aria-live values. The icon is decorative and hidden from assistive technology, so the title must name what happened. The timer pauses while the pointer is over the viewport, while focus is inside it, and while the window is in the background. F8 moves focus to the viewport, Tab then reaches each toast and its buttons, and Escape closes the focused toast. A focused toast shows the focus ring. A swipe moves the toast with transform, and the enter and exit animations use translate, so a toast can be swiped while it slides in. The swipe direction stays swipeDirection on ToastProvider, "right" by default; in a right-to-left page, pass "left" so the swipe leaves toward the same edge the toast came from. With reduced motion, toasts appear and leave without animation. ToastClose has the accessible name “Dismiss”; pass aria-label to translate it. Do not put the only route to an action in a toast: give ToastAction an altText that names another route.
In plain HTML, showToast({ title, description, variant, duration }) from toast.js creates the viewport the first time, adds a close control, pauses its timer while the pointer is over the toast or focus is inside it, closes on Escape, and makes a closing toast inert until its exit animation ends. The live region is .newt-toast__content, so the close control is not read with the text: role="alert" for a danger toast, role="status" with aria-live="polite" for the others. A duration of 0, a negative number or Infinity keeps the toast open. An unknown variant is read as success. When a focused toast closes, focus moves to the close control of the next open toast, or back to where it was before it entered the toast.
Translating labels lists every English default.
Tokens
Toast reads its colours, radii and motion from the --newt-* tokens installed with the library. Override a token and every component that reads it follows, including this one — see the token layer. In the plain HTML and CSS distribution the same component is newt-toast.
--newt-dir--newt-space-4--newt-space-2--newt-online--newt-space-3--newt-radius-lg--newt-bg-floating--newt-border-subtle--newt-shadow-elevation-high--newt-text-primary--newt-font-sans--newt-font-size-sm--newt-duration-slow--newt-ease-overlay--newt-duration-fast--newt-ease-in--newt-text-link--newt-idle--newt-text-danger--newt-space-1--newt-font-weight-medium--newt-text-secondary