Toast
A message that appears on the screen to provide feedback on an action.
'use client'
import { XIcon } from 'lucide-react'
import { Button } from '~/components/ui/button'
import { IconButton } from '~/components/ui/icon-button'
import { Toast } from '~/components/ui/toast'
const toaster = Toast.createToaster({
placement: 'bottom-end',
overlap: true,
gap: 16,
})
export const Demo = () => (
<>
<Button
variant="outline"
onClick={() =>
toaster.create({
title: 'Toast Title',
description: 'Toast Description',
type: 'info',
})
}
>
Add Toast
</Button>
<Toast.Toaster toaster={toaster}>
{(toast) => (
<Toast.Root key={toast.id}>
<Toast.Title>{toast.title}</Toast.Title>
<Toast.Description>{toast.description}</Toast.Description>
<Toast.ActionTrigger asChild>
<Button variant="link" size="sm">
Action
</Button>
</Toast.ActionTrigger>
<Toast.CloseTrigger asChild>
<IconButton size="sm" variant="link">
<XIcon />
</IconButton>
</Toast.CloseTrigger>
</Toast.Root>
)}
</Toast.Toaster>
</>
)
import { XIcon } from 'lucide-solid'
import { Button } from '~/components/ui/button'
import { IconButton } from '~/components/ui/icon-button'
import { Toast } from '~/components/ui/toast'
const toaster = Toast.createToaster({
placement: 'bottom-end',
overlap: true,
gap: 16,
})
export const Demo = () => (
<>
<Button
variant="outline"
onClick={() =>
toaster.create({
title: 'Toast Title',
description: 'Toast Description',
type: 'info',
})
}
>
Add Toast
</Button>
<Toast.Toaster toaster={toaster}>
{(toast) => (
<Toast.Root>
<Toast.Title>{toast().title}</Toast.Title>
<Toast.Description>{toast().description}</Toast.Description>
<Toast.ActionTrigger
asChild={(actionProps) => (
<Button {...actionProps()} variant="link" size="sm">
Action
</Button>
)}
/>
<Toast.CloseTrigger
asChild={(closeProps) => (
<IconButton {...closeProps()} size="sm" variant="link">
<XIcon />
</IconButton>
)}
/>
</Toast.Root>
)}
</Toast.Toaster>
</>
)
Usage
import { Toast } from '~/components/ui/toast'
Installation
npx @park-ui/cli components add toast
1
Add Styled Primitive
Copy the code snippet below into ~/components/ui/styled/toast.tsx
'use client'
import type { Assign } from '@ark-ui/react'
import { Toast } from '@ark-ui/react/toast'
import { toast } from 'styled-system/recipes'
import type { HTMLStyledProps } from 'styled-system/types'
import { createStyleContext } from './utils/create-style-context'
const { withProvider, withContext } = createStyleContext(toast)
export const Root = withProvider<
HTMLDivElement,
Assign<HTMLStyledProps<'div'>, Toast.ActionTriggerProps>
>(Toast.Root, 'root')
export const ActionTrigger = withContext<
HTMLButtonElement,
Assign<HTMLStyledProps<'button'>, Toast.ActionTriggerProps>
>(Toast.ActionTrigger, 'actionTrigger')
export const CloseTrigger = withContext<
HTMLDivElement,
Assign<HTMLStyledProps<'div'>, Toast.CloseTriggerProps>
>(Toast.CloseTrigger, 'closeTrigger')
export const Description = withContext<
HTMLDivElement,
Assign<HTMLStyledProps<'div'>, Toast.DescriptionProps>
>(Toast.Description, 'description')
export const Title = withContext<HTMLDivElement, Assign<HTMLStyledProps<'div'>, Toast.TitleProps>>(
Toast.Title,
'title',
)
export {
ToastContext as Context,
Toaster,
createToaster,
type ToastContextProps as ContextProps,
type ToasterProps,
} from '@ark-ui/react/toast'
import { type Assign, Toast } from '@ark-ui/solid'
import type { ComponentProps } from 'solid-js'
import { toast } from 'styled-system/recipes'
import type { HTMLStyledProps } from 'styled-system/types'
import { createStyleContext } from './utils/create-style-context'
const { withProvider, withContext } = createStyleContext(toast)
export type RootProps = ComponentProps<typeof Root>
export const Root = withProvider<Assign<HTMLStyledProps<'div'>, Toast.RootProps>>(
Toast.Root,
'root',
)
export const ActionTrigger = withContext<
Assign<HTMLStyledProps<'button'>, Toast.ActionTriggerProps>
>(Toast.ActionTrigger, 'actionTrigger')
export const CloseTrigger = withContext<Assign<HTMLStyledProps<'div'>, Toast.CloseTriggerProps>>(
Toast.CloseTrigger,
'closeTrigger',
)
export const Description = withContext<Assign<HTMLStyledProps<'div'>, Toast.DescriptionProps>>(
Toast.Description,
'description',
)
export const Title = withContext<Assign<HTMLStyledProps<'div'>, Toast.TitleProps>>(
Toast.Title,
'title',
)
export {
ToastContext as Context,
createToaster,
Toaster,
type ToastContextProps as ContextProps,
type ToasterProps,
} from '@ark-ui/solid'
No snippet found
2
Add Re-Export
To improve the developer experience, re-export the styled primitives in~/components/ui/toast.tsx
.
export * as Toast from './styled/toast'
export * as Toast from './styled/toast'
3
Integrate Recipe
If you're not using @park-ui/preset
, add the following recipe to yourpanda.config.ts
:
import { toastAnatomy } from '@ark-ui/anatomy'
import { defineSlotRecipe } from '@pandacss/dev'
export const toast = defineSlotRecipe({
className: 'toast',
slots: toastAnatomy.keys(),
base: {
root: {
background: 'bg.default',
borderRadius: 'l3',
boxShadow: 'lg',
minWidth: 'xs',
height: 'var(--height)',
opacity: 'var(--opacity)',
overflowWrap: 'anywhere',
p: '4',
position: 'relative',
scale: 'var(--scale)',
translate: 'var(--x) var(--y) 0',
willChange: 'translate, opacity, scale',
zIndex: 'var(--z-index)',
transitionDuration: 'slow',
transitionProperty: 'translate, scale, opacity, height',
transitionTimingFunction: 'default',
},
title: {
color: 'fg.default',
fontWeight: 'semibold',
textStyle: 'sm',
},
description: {
color: 'fg.muted',
textStyle: 'sm',
},
actionTrigger: {
mt: '2',
},
closeTrigger: {
position: 'absolute',
top: '3',
right: '3',
},
},
})