Tailwind CSS Alerts (알림) 컴포넌트
Alert 컴포넌트는 성공이나 오류 메시지 같은 정보를 사용자에게 보여줄 때 사용해요. 페이지의 문단과 제목 흐름을 보완하는 강조 정보를 표시할 수도 있어요. Flowbite에는 사용자가 닫기 아이콘을 클릭하면 숨길 수 있는 dismissible(닫을 수 있는) 알림도 포함되어 있어요.
출처: 문서
본문
기본 알림 (Default alert)
사용자에게 피드백 메시지를 보여주는 기본 알림 컴포넌트 예시들이에요.
<div class="p-4 mb-4 text-sm text-fg-brand-strong rounded-base bg-brand-softer" role="alert">
<span class="font-medium">Info alert!</span> Change a few things up and try submitting again.
</div>
<div class="p-4 mb-4 text-sm text-fg-danger-strong rounded-base bg-danger-soft" role="alert">
<span class="font-medium">Danger alert!</span> Change a few things up and try submitting again.
</div>
<div class="p-4 mb-4 text-sm text-fg-success-strong rounded-base bg-success-soft" role="alert">
<span class="font-medium">Success alert!</span> Change a few things up and try submitting again.
</div>
<div class="p-4 mb-4 text-sm text-fg-warning rounded-base bg-warning-soft" role="alert">
<span class="font-medium">Warning alert!</span> Change a few things up and try submitting again.
</div>
<div class="p-4 text-sm text-heading rounded-base bg-neutral-secondary-medium" role="alert">
<span class="font-medium">Dark alert!</span> Change a few things up and try submitting again.
</div>
아이콘이 있는 알림 (Alerts with icon)
알림 안의 메시지를 보완하는 설명 아이콘도 함께 넣을 수 있어요.
<div class="flex items-start sm:items-center p-4 mb-4 text-sm text-fg-brand-strong rounded-base bg-brand-softer" role="alert">
<svg class="w-4 h-4 me-2 shrink-0 mt-0.5 sm:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<p><span class="font-medium me-1">Info alert!</span> Change a few things up and try submitting again.</p>
</div>
<div class="flex items-start sm:items-center p-4 mb-4 text-sm text-fg-danger-strong rounded-base bg-danger-soft" role="alert">
<svg class="w-4 h-4 me-2 shrink-0 mt-0.5 sm:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<p><span class="font-medium me-1">Danger alert!</span> Change a few things up and try submitting again.</p>
</div>
<div class="flex items-start sm:items-center p-4 mb-4 text-sm text-fg-success-strong rounded-base bg-success-soft" role="alert">
<svg class="w-4 h-4 me-2 shrink-0 mt-0.5 sm:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<p><span class="font-medium me-1">Success alert!</span> Change a few things up and try submitting again.</p>
</div>
<div class="flex items-start sm:items-center p-4 mb-4 text-sm text-fg-warning rounded-base bg-warning-soft" role="alert">
<svg class="w-4 h-4 me-2 shrink-0 mt-0.5 sm:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<p><span class="font-medium me-1">Warning alert!</span> Change a few things up and try submitting again.</p>
</div>
<div class="flex items-start sm:items-center p-4 text-sm text-heading rounded-base bg-neutral-secondary-medium" role="alert">
<svg class="w-4 h-4 me-2 shrink-0 mt-0.5 sm:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<p><span class="font-medium me-1">Dark alert!</span> Change a few things up and try submitting again.</p>
</div>
테두리 있는 알림 (Bordered alerts)
평범한 배경 대신 테두리 액센트를 추가한 예시예요.
<div class="flex items-start sm:items-center p-4 mb-4 text-sm text-fg-brand-strong rounded-base bg-brand-softer border border-brand-subtle" role="alert">
<svg class="w-4 h-4 me-2 shrink-0 mt-0.5 sm:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<p><span class="font-medium me-1">Info alert!</span> Change a few things up and try submitting again.</p>
</div>
<div class="flex items-start sm:items-center p-4 mb-4 text-sm text-fg-danger-strong rounded-base bg-danger-soft border border-danger-subtle" role="alert">
<svg class="w-4 h-4 me-2 shrink-0 mt-0.5 sm:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<p><span class="font-medium me-1">Danger alert!</span> Change a few things up and try submitting again.</p>
</div>
<div class="flex items-start sm:items-center p-4 mb-4 text-sm text-fg-success-strong rounded-base bg-success-soft border border-success-subtle" role="alert">
<svg class="w-4 h-4 me-2 shrink-0 mt-0.5 sm:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<p><span class="font-medium me-1">Success alert!</span> Change a few things up and try submitting again.</p>
</div>
<div class="flex items-start sm:items-center p-4 mb-4 text-sm text-fg-warning rounded-base bg-warning-soft border border-warning-subtle" role="alert">
<svg class="w-4 h-4 me-2 shrink-0 mt-0.5 sm:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<p><span class="font-medium me-1">Warning alert!</span> Change a few things up and try submitting again.</p>
</div>
<div class="flex items-start sm:items-center p-4 text-sm text-heading rounded-base bg-neutral-secondary-medium border border-default-medium" role="alert">
<svg class="w-4 h-4 me-2 shrink-0 mt-0.5 sm:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<p><span class="font-medium me-1">Dark alert!</span> Change a few things up and try submitting again.</p>
</div>
목록이 있는 알림 (Alerts with list)
알림 안에 목록과 설명을 함께 보여주는 예시예요.
<div class="flex p-4 mb-4 text-sm text-fg-brand-strong rounded-base bg-brand-softer border border-brand-subtle" role="alert">
<svg class="w-4 h-4 me-2 shrink-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<span class="sr-only">Info</span>
<div>
<span class="font-medium">Ensure that these requirements are met:</span>
<ul class="mt-2 list-disc list-outside space-y-1 ps-2.5">
<li>At least 10 characters (and up to 100 characters)</li>
<li>At least one lowercase character</li>
<li>Inclusion of at least one special character, e.g., ! @ # ?</li>
</ul>
</div>
</div>
<div class="flex p-4 mb-4 text-sm text-fg-danger-strong rounded-base bg-danger-soft border border-danger-subtle" role="alert">
<svg class="w-4 h-4 me-2 shrink-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<span class="sr-only">Danger</span>
<div>
<span class="font-medium">Ensure that these requirements are met:</span>
<ul class="mt-2 list-disc list-outside space-y-1 ps-2.5">
<li>At least 10 characters (and up to 100 characters)</li>
<li>At least one lowercase character</li>
<li>Inclusion of at least one special character, e.g., ! @ # ?</li>
</ul>
</div>
</div>
닫기 가능 (Dismissing)
닫기(닫기)할 수 있는 알림 요소예요. Flowbite JS가 필요해요.
<div id="alert-1" class="flex sm:items-center p-4 mb-4 text-sm text-fg-brand-strong rounded-base bg-brand-softer" role="alert">
<svg class="w-4 h-4 shrink-0 mt-0.5 md:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<span class="sr-only">Info</span>
<div class="ms-2 text-sm">
A simple info alert with an <a href="#" class="font-medium underline hover:no-underline">example link</a>. Give it a click if you like.
</div>
<button type="button" class="ms-auto -mx-1.5 -my-1.5 rounded focus:ring-2 focus:ring-brand-medium hover:bg-brand-soft inline-flex items-center justify-center h-8 w-8 shrink-0 shrink-0" data-dismiss-target="#alert-1" aria-label="Close">
<span class="sr-only">Close</span>
<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18 17.94 6M18 18 6.06 6"/></svg>
</button>
</div>
닫기 버튼은 data-dismiss-target="#alert-1" 속성을 사용해서 해당 알림 요소를 숨겨요.
테두리 액센트 (Border accent)
알림 컴포넌트 위에 테두리 액센트를 더해 시각적으로 더 구분되게 하는 예시예요. 위쪽 테두리(border-t-4)를 강조합니다.
<div id="alert-border-1" class="flex sm:items-center p-4 mb-4 text-sm text-fg-brand-strong bg-brand-softer border-t-4 border-brand-subtle" role="alert">
<svg class="w-4 h-4 shrink-0 mt-0.5 md:mt-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<span class="sr-only">Info</span>
<div class="ms-2 text-sm">
A simple info alert with an <a href="#" class="font-medium underline hover:no-underline">example link</a>. Give it a click if you like.
</div>
<button type="button" class="ms-auto -mx-1.5 -my-1.5 rounded focus:ring-2 focus:ring-brand-medium hover:bg-brand-soft inline-flex items-center justify-center h-8 w-8 shrink-0" data-dismiss-target="#alert-border-1" aria-label="Close">
<span class="sr-only">Close</span>
<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18 17.94 6M18 18 6.06 6"/></svg>
</button>
</div>
추가 콘텐츠 (Additional content)
알림 요소 안에서 더 많은 정보를 펼쳐 보여주고 싶다면 이 컴포넌트를 사용해요. 제목, 설명, 액션 버튼을 함께 배치할 수 있어요.
<div id="alert-additional-content-1" class="p-4 mb-4 text-sm text-fg-brand-strong rounded-base bg-brand-softer border border-brand-subtle" role="alert">
<div class="flex items-center justify-between">
<div class="flex items-center">
<svg class="w-4 h-4 shrink-0 me-2" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 11h2v5m-2 0h4m-2.592-8.5h.01M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
<span class="sr-only">Info</span>
<h3 class="font-medium">This is a info alert</h3>
</div>
<button type="button" data-dismiss-target="#alert-additional-content-1" aria-label="Close" class="ms-auto -mx-1.5 -my-1.5 rounded focus:ring-2 focus:ring-brand-medium hover:bg-brand-soft inline-flex items-center justify-center h-8 w-8 shrink-0">
<span class="sr-only">Close</span>
<svg class="w-4 h-4" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18 17.94 6M18 18 6.06 6"/></svg>
</button>
</div>
<div class="mt-2 mb-4">
More info about this info alert goes here. This example text is going to run a bit longer so that you can see how spacing within an alert works with this kind of content.
</div>
<button type="button" class="inline-flex items-center text-white bg-brand hover:bg-brand-strong box-border border border-transparent focus:ring-4 focus:ring-brand-medium shadow-xs font-medium leading-5 rounded-base text-xs px-3 py-1.5 focus:outline-none">
<svg class="w-3.5 h-3.5 me-1.5" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-width="2" d="M21 12c0 1.2-4.03 6-9 6s-9-4.8-9-6c0-1.2 4.03-6 9-6s9 4.8 9 6Z"/><path stroke="currentColor" stroke-width="2" d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"/></svg>
View more
</button>
</div>
공지 알림 (Announcement alerts)
CTA 링크와 함께 공지를 할 때 사용하는 알림 컴포넌트예요.
<div id="alert-small-1" class="w-auto inline-flex items-center p-1 pe-2 mb-4 text-sm text-fg-brand-strong rounded-full bg-brand-softer border border-brand-subtle" role="alert">
<span class="bg-brand-soft text-fg-brand-strong py-0.5 px-2 rounded-full">New</span>
<div class="ms-2 text-sm">
Great job! You've acknowledged this <a href="#" class="font-medium underline hover:no-underline">significant</a> alert message.
</div>
<svg class="w-4 h-4 ms-1 shrink-0" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m9 5 7 7-7 7"/></svg>
</div>
JavaScript 동작 (JavaScript behaviour)
Flowbite의 Dismiss 클래스를 사용하면 제공하는 파라미터, 옵션, 메서드에 따라 대상 요소를 숨기는 객체를 만들 수 있어요.
객체 파라미터 (Object parameters):
| Parameter | Type | Required | Description |
|---|---|---|---|
targetEl |
Element | Required | 닫힐 요소 객체를 전달해요. |
triggerEl |
Element | Optional | 클릭 시 targetEl을 닫게 하는 트리거 요소 객체를 전달해요. |
options |
Object | Optional | 트리거 요소, 전환, 지속시간, 타이밍 클래스와 콜백 함수를 설정하는 옵션 객체예요. |
instanceOptions |
Object | Optional | Instance Manager에 추가되는 인스턴스에 커스텀 ID를 설정하고 기존 인스턴스를 덮어쓸지 여부를 정할 수 있는 옵션 객체예요. |
옵션 (Options):
| Option | Type | Description |
|---|---|---|
transition |
String | 주 요소의 전환 타입을 설정하는 Tailwind CSS Transition Property 유틸리티 클래스예요. 기본값은 transition-opacity예요. |
duration |
Integer | 닫기 애니메이션의 지속시간(밀리초)이에요. 기본값은 300이에요. |
timing |
String | Tailwind CSS의 전환 타이밍 함수 유틸리티 클래스예요. 기본값은 ease-out이에요. |
onHide |
Function | 항목이 닫혔을 때 호출할 콜백 함수예요. |
메서드 (Methods):
| Method | Description |
|---|---|
hide() |
대상 요소를 숨기는 메서드예요. |
updateOnHide(callback) |
항목이 닫혔을 때 실행할 콜백 함수를 설정하는 메서드예요. |
예시 (Example):
먼저 필요한 대상 요소를 설정하고, 클릭하면 대상 요소를 닫도록 선택적으로 트리거 요소와 애니메이션 커스텀 옵션을 설정해요.
// target element that will be dismissed
const $targetEl = document.getElementById('targetElement');
// optional trigger element
const $triggerEl = document.getElementById('triggerElement');
// options object
const options = {
transition: 'transition-opacity',
duration: 1000,
timing: 'ease-out',
// callback functions
onHide: (context, targetEl) => {
console.log('element has been dismissed')
console.log(targetEl)
}
};
// instance options object
const instanceOptions = {
id: 'targetElement',
override: true
};
위에서 설정한 옵션을 바탕으로 새로운 Dismiss 객체를 만들어요.
import { Dismiss } from 'flowbite';
/*
* $targetEl (required)
* $triggerEl (optional)
* options (optional)
* instanceOptions (optional)
*/
const dismiss = new Dismiss($targetEl, $triggerEl, options, instanceOptions);
이제 Dismiss 객체의 메서드를 사용할 수 있어요.
// hide the target element
dismiss.hide();
HTML 마크업 (HTML Markup):
위 JavaScript 예시에서 사용하는 HTML 코드예요.
<button id="triggerElement" type="button" class="text-white bg-brand box-border border border-transparent hover:bg-brand-strong focus:ring-4 focus:ring-brand-medium shadow-xs font-medium leading-5 rounded-base text-sm px-4 py-2.5 focus:outline-none">Hide alert</button>
<div id="targetElement" class="p-4 mb-4 text-sm text-fg-brand-strong rounded-base bg-brand-softer" role="alert">
<span class="font-medium">Info alert!</span> Change a few things up and try submitting again.
</div>
TypeScript:
Flowbite의 TypeScript 설정을 사용한다면 Dismiss 클래스와 파라미터, 옵션에 대한 타입을 import 해서 사용할 수 있어요.
import { Dismiss } from "flowbite";
import type { DismissOptions, DismissInterface } from "flowbite";
import type { InstanceOptions } from 'flowbite';
// target element that will be dismissed
const $targetEl: HTMLElement = document.getElementById('targetElement');
// optional trigger element
const $triggerEl: HTMLElement = document.getElementById('triggerElement');
// options object
const options: DismissOptions = {
transition: 'transition-opacity',
duration: 1000,
timing: 'ease-out',
// callback functions
onHide: (context, targetEl) => {
console.log('element has been dismissed')
console.log(targetEl)
}
};
// instance options object
const instanceOptions: InstanceOptions = {
id: 'targetElement',
override: true
};
/*
* $targetEl (required)
* $triggerEl (optional)
* options (optional)
* instanceOptions (optional)
*/
const dismiss: DismissInterface = new Dismiss($targetEl, $triggerEl, options, instanceOptions);
// programmatically hide it
dismiss.hide();
더 알아보기 (Learn more)
- 알림은 성공/오류/경고/정보 메시지를 사용자에게 보여줘요.
- 아이콘, 테두리, 목록, 추가 콘텐츠, 공지 등 다양한 변형을 제공해요.
- Flowbite의
Dismiss클래스와data-dismiss-target속성으로 알림을 닫을 수 있어요.