Snackbar A component that temporarily appears at the bottom of the screen to inform users of status or results.
@seed-design/react@0.0.1, @seed-design/css@0.0.1
import { ActionButton } from "seed-design/ui/action-button" ;
import { Snackbar, SnackbarProvider, useSnackbarAdapter } from "seed-design/ui/snackbar" ;
function Component () {
const adapter = useSnackbarAdapter ();
return (
< ActionButton
onClick = {() =>
adapter. create ({
onClose : () => {},
render : () => < Snackbar message = "Notification message" actionLabel = "OK" onAction = {() => {}} />,
})
}
>
Run
</ ActionButton >
);
}
export default function SnackbarPreview () {
return (
< SnackbarProvider >
< Component />
</ SnackbarProvider >
);
}
npx @seed-design/cli@latest add ui:snackbar pnpm dlx @seed-design/cli@latest add ui:snackbar yarn dlx @seed-design/cli@latest add ui:snackbar bun x @seed-design/cli@latest add ui:snackbar
Install dependencies
npm install @karrotmarket/react-monochrome-icon @seed-design/react yarn add @karrotmarket/react-monochrome-icon @seed-design/react pnpm add @karrotmarket/react-monochrome-icon @seed-design/react bun add @karrotmarket/react-monochrome-icon @seed-design/react Copy the code below and paste it in /**
* @file ui:snackbar
* @requires @seed-design/react@^2.0.0
* @requires @seed-design/css@^2.0.0
**/
"use client" ;
import {
Snackbar as SeedSnackbar,
useSnackbarAdapter as useSeedSnackbarAdapter,
type CreateSnackbarOptions as SeedCreateSnackbarOptions,
} from "@seed-design/react" ;
import * as React from "react" ;
import IconCheckmarkCircleFill from "@karrotmarket/react-monochrome-icon/IconCheckmarkCircleFill" ;
import IconExclamationmarkCircleFill from "@karrotmarket/react-monochrome-icon/IconExclamationmarkCircleFill" ;
export interface SnackbarProviderProps extends SeedSnackbar . RootProviderProps {}
export const SnackbarProvider = ( props : SnackbarProviderProps ) => {
const { children , ... otherProps } = props;
return (
< SeedSnackbar.RootProvider { ... otherProps}>
{children}
< SeedSnackbar.Region >
< SeedSnackbar.Renderer />
</ SeedSnackbar.Region >
</ SeedSnackbar.RootProvider >
);
};
export interface SnackbarProps extends SeedSnackbar . RootProps {
/**
* 스낵바에 표시할 메시지
*/
message : string ;
/**
* 스낵바에 표시할 액션 버튼의 라벨
*/
actionLabel ?: string ;
/**
* 액션 버튼 클릭 시 호출되는 콜백
*/
onAction ?: () => void ;
}
/**
* @see https://crispy.carrot-soft.tech/react/components/snackbar
*/
export const Snackbar = React. forwardRef < HTMLDivElement , SnackbarProps >(
({ variant = "default" , children , message , actionLabel , onAction , ... otherProps }, ref ) => {
return (
< SeedSnackbar.Root ref = {ref} variant = {variant} { ... otherProps}>
{variant !== "default" && (
< SeedSnackbar.PrefixIcon
svg = {
variant === "positive" ? (
< IconCheckmarkCircleFill />
) : variant === "critical" ? (
< IconExclamationmarkCircleFill />
) : null
}
/>
)}
< SeedSnackbar.Content >
< SeedSnackbar.Message >{message}</ SeedSnackbar.Message >
{actionLabel && (
< SeedSnackbar.ActionButton onClick = {onAction}>{actionLabel}</ SeedSnackbar.ActionButton >
)}
</ SeedSnackbar.Content >
{ /* You may implement your own i18n for dismiss label */ }
< SeedSnackbar.HiddenCloseButton >닫기</ SeedSnackbar.HiddenCloseButton >
</ SeedSnackbar.Root >
);
},
);
Snackbar.displayName = "Snackbar" ;
// TODO: re-export is ugly; should we namespace CreateSnackbarOptions into Snackbar?
export interface CreateSnackbarOptions extends SeedCreateSnackbarOptions {}
export const useSnackbarAdapter = useSeedSnackbarAdapter;
export const SnackbarAvoidOverlap = SeedSnackbar.AvoidOverlap;
/**
* This file is a snippet from Crispy, helping you get started quickly with @seed-design/* packages.
* You can extend this snippet however you want.
*/
import { ActionButton } from "seed-design/ui/action-button" ;
import { Snackbar, SnackbarProvider, useSnackbarAdapter } from "seed-design/ui/snackbar" ;
function Component () {
const adapter = useSnackbarAdapter ();
return (
< ActionButton
onClick = {() =>
adapter. create ({
onClose : () => {},
render : () => (
< Snackbar
variant = "positive"
message = "알림 메세지"
actionLabel = "확인"
onAction = {() => {}}
/>
),
})
}
>
실행
</ ActionButton >
);
}
export default function SnackbarPositive () {
return (
< SnackbarProvider >
< Component />
</ SnackbarProvider >
);
}
import { ActionButton } from "seed-design/ui/action-button" ;
import { Snackbar, SnackbarProvider, useSnackbarAdapter } from "seed-design/ui/snackbar" ;
function Component () {
const adapter = useSnackbarAdapter ();
return (
< ActionButton
onClick = {() =>
adapter. create ({
timeout: 50000000 ,
onClose : () => {},
render : () => (
< Snackbar
variant = "critical"
message = "알림 메세지"
actionLabel = "확인"
onAction = {() => {}}
/>
),
})
}
>
실행
</ ActionButton >
);
}
export default function SnackbarNegative () {
return (
< SnackbarProvider >
< Component />
</ SnackbarProvider >
);
}
To prevent Snackbar from being dismissed due to timeout while the user interacts with Snackbar (hover and active), set pauseOnInteraction to true on SnackbarProvider.
Default
Through @seed-design/react@0.1.14: false: If not explicitly set, timeout dismisses the Snackbar regardless of interaction.
Later versions: true: If not explicitly set, timeout pauses while the user interacts.
import { VStack } from "@seed-design/react" ;
import { ActionButton } from "seed-design/ui/action-button" ;
import { Snackbar, SnackbarProvider, useSnackbarAdapter } from "seed-design/ui/snackbar" ;
import { SegmentedControl, SegmentedControlItem } from "seed-design/ui/segmented-control" ;
import { useState } from "react" ;
function Component () {
const adapter = useSnackbarAdapter ();
return (
< ActionButton
onClick = {() =>
adapter. create ({
onClose : () => {},
render : () => < Snackbar message = "알림 메세지" actionLabel = "확인" onAction = {() => {}} />,
})
}
>
실행
</ ActionButton >
);
}
export default function SnackbarPauseOnInteraction () {
const [ pauseOnInteraction , setPauseOnInteraction ] = useState ( true );
return (
< VStack gap = "spacingY.componentDefault" alignItems = "center" >
< SnackbarProvider pauseOnInteraction = {pauseOnInteraction}>
< Component />
</ SnackbarProvider >
< SegmentedControl
aria-label = "Pause on interaction"
value = { `${ pauseOnInteraction }` }
onValueChange = {( value ) => setPauseOnInteraction (value === "true" )}
>
< SegmentedControlItem value = "false" >false</ SegmentedControlItem >
< SegmentedControlItem value = "true" >true</ SegmentedControlItem >
</ SegmentedControl >
</ VStack >
);
}
When you create a new Snackbar while one is already displayed, by default it immediately replaces the existing Snackbar (immediate).
To queue and show them sequentially, use strategy: "queued".
strategy can be set as a default in SnackbarProvider, or specified individually when calling create().
import { ActionButton } from "seed-design/ui/action-button" ;
import { Snackbar, SnackbarProvider, useSnackbarAdapter } from "seed-design/ui/snackbar" ;
function Component () {
const adapter = useSnackbarAdapter ();
return (
< div style = {{ display: "flex" , gap: 8 }}>
< ActionButton
variant = "neutralSolid"
onClick = {() =>
adapter. create ({
render : () => < Snackbar variant = "positive" message = "저장되었습니다" />,
})
}
>
Immediate (positive)
</ ActionButton >
< ActionButton
variant = "neutralSolid"
onClick = {() =>
adapter. create ({
strategy: "queued" ,
render : () => < Snackbar variant = "critical" message = "오류가 발생했습니다" />,
})
}
>
Queued (critical)
</ ActionButton >
</ div >
);
}
export default function SnackbarStrategy () {
return (
< SnackbarProvider >
< Component />
</ SnackbarProvider >
);
}
You can specify an area where snackbars should not overlap using the <SnackbarAvoidOverlap /> component.
Child components of <SnackbarAvoidOverlap /> must receive ref via forwardRef (React 18) or ref prop (React 19).
The offset is updated in the following situations:
When a child component of SnackbarAvoidOverlap is mounted
When a child component of SnackbarAvoidOverlap is unmounted
When the height of a child component of SnackbarAvoidOverlap changes
When the height of SnackbarRegion changes
import { Flex, VStack } from "@seed-design/react" ;
import { ActionButton } from "seed-design/ui/action-button" ;
import {
Snackbar,
SnackbarAvoidOverlap,
SnackbarProvider,
useSnackbarAdapter,
} from "seed-design/ui/snackbar" ;
function Component () {
const adapter = useSnackbarAdapter ();
return (
< VStack gap = "x4" >
< ActionButton
onClick = {() =>
adapter. create ({
onClose : () => {},
render : () => < Snackbar message = "알림 메세지" actionLabel = "확인" onAction = {() => {}} />,
})
}
>
실행
</ ActionButton >
< SnackbarAvoidOverlap >
< Flex width = "full" height = "x16" bg = "bg.criticalWeak" justify = "center" align = "center" >
Snackbar가 이 영역과 겹치지 않게 조정됩니다. 스크롤은 무시합니다.
</ Flex >
</ SnackbarAvoidOverlap >
</ VStack >
);
}
export default function SnackbarPreview () {
return (
< SnackbarProvider >
< Component />
</ SnackbarProvider >
);
}