Alert
ユーザーの邪魔をせずに注意を引くメッセージ。
AlertはAlert WAI-ARIAデザインパターンに従います。
Payment failed
Check your card details and try again.
import { component$, useStyles$ } from '@builder.io/qwik';
import { Alert } from '@leve-ui/alert';
import { LuAlertCircle } from '@qwikest/icons/lucide';
import styles from './default.css?inline';
export default component$(() => {
useStyles$(styles);
return (
<Alert.Root class="alert-default">
<LuAlertCircle class="alert-default-icon" aria-hidden="true" />
<Alert.Title class="alert-default-title">Payment failed</Alert.Title>
<Alert.Description class="alert-default-description">
Check your card details and try again.
</Alert.Description>
</Alert.Root>
);
});.alert-default {
display: grid;
grid-template-columns: auto 1fr;
column-gap: 12px;
row-gap: 4px;
width: 320px;
padding: 14px 16px;
border: 1px solid light-dark(#fecaca, #f8717166);
border-radius: 6px;
background: light-dark(#fef2f2, #f871711f);
font-family: inherit;
}
/* Decorative icon spanning the title and description rows */
.alert-default-icon {
grid-row: span 2;
width: 18px;
height: 18px;
margin-top: 1px;
color: light-dark(#dc2626, #f87171);
}
.alert-default-title {
margin: 0;
font-size: 14px;
font-weight: 600;
color: light-dark(#dc2626, #f87171);
}
.alert-default-description {
margin: 0;
font-size: 13px;
color: light-dark(#475569, #c9ddf0);
}インストール#
個別のパッケージをインストールするか、@leve-ui/coreを使用してすべてのコンポーネントを一度に取得します。
pnpm add @leve-ui/alertnpm install @leve-ui/alertyarn add @leve-ui/alertbun add @leve-ui/alertpnpm add @leve-ui/corenpm install @leve-ui/coreyarn add @leve-ui/corebun add @leve-ui/core構造#
import { Alert } from '@leve-ui/alert'; // or '@leve-ui/core'
<Alert.Root>
<Alert.Title />
<Alert.Description />
</Alert.Root>
| パーツ | 要素 | 説明 |
|---|---|---|
Alert.Root | <div> | メッセージをラップし、role="alert"を持ちます(live={false}の場合を除く)。 |
Alert.Title | <div> | メッセージの見出し。 |
Alert.Description | <div> | メッセージの本文。 |
例#
送信時#
送信の失敗後など、メッセージを通知すべき瞬間にAlert.Rootをレンダリングします。
import { component$, useSignal, useStyles$ } from '@builder.io/qwik';
import { Alert } from '@leve-ui/alert';
import { LuAlertCircle } from '@qwikest/icons/lucide';
import styles from './on-submit.css?inline';
export default component$(() => {
useStyles$(styles);
const error = useSignal(false);
return (
<div class="alert-on-submit-wrapper">
<button type="button" class="alert-on-submit-button" onClick$={() => (error.value = true)}>
Submit
</button>
{/* Render the alert when it should be announced */}
{error.value && (
<Alert.Root class="alert-on-submit">
<LuAlertCircle class="alert-on-submit-icon" aria-hidden="true" />
<Alert.Title class="alert-on-submit-title">Submission failed</Alert.Title>
<Alert.Description class="alert-on-submit-description">
Email is required.
</Alert.Description>
</Alert.Root>
)}
</div>
);
});.alert-on-submit-wrapper {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 12px;
font-family: inherit;
}
.alert-on-submit-button {
height: 32px;
padding: 0 12px;
border: none;
border-radius: 6px;
background: #009dfd;
color: #ffffff;
font: inherit;
font-size: 13px;
font-weight: 500;
cursor: pointer;
}
.alert-on-submit-button:hover {
background: #006ce9;
}
.alert-on-submit-button:focus-visible {
outline: 2px solid #009dfd;
outline-offset: 2px;
}
.alert-on-submit {
display: grid;
grid-template-columns: auto 1fr;
column-gap: 12px;
row-gap: 4px;
width: 320px;
padding: 14px 16px;
border: 1px solid light-dark(#fecaca, #f8717166);
border-radius: 6px;
background: light-dark(#fef2f2, #f871711f);
}
/* Decorative icon spanning the title and description rows */
.alert-on-submit-icon {
grid-row: span 2;
width: 18px;
height: 18px;
margin-top: 1px;
color: light-dark(#dc2626, #f87171);
}
.alert-on-submit-title {
margin: 0;
font-size: 14px;
font-weight: 600;
color: light-dark(#dc2626, #f87171);
}
.alert-on-submit-description {
margin: 0;
font-size: 13px;
color: light-dark(#475569, #c9ddf0);
}APIリファレンス#
Root#
| Prop | デフォルト | 型 |
|---|---|---|
| live | true | boolean スクリーンリーダーによって読み上げられるライブリージョン( |
すべての<div>属性も受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-alert | 常に存在 |
live(デフォルトはtrue)はRootがライブリージョンであるかどうかを決定します。静的な通知にはlive={false}を使用してください。
タイトル#
独自のpropはありません。 すべての<div>属性を受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-alert-title | 常に存在 |
説明#
独自のpropはありません。 すべての<div>属性を受け入れます。
| データ属性 | 値 |
|---|---|
data-leve-alert-description | 常に存在 |
アクセシビリティ#
Alert WAI-ARIAデザインパターンに準拠しています。
Alert.Rootはrole="alert"をレンダリングするため、スクリーンリーダーはそのコンテンツを読み上げます。- ページの読み込み時にすでに存在するアラートは通知されない場合があります。表示すべきタイミングでレンダリングしてください。
- 最初からページ上にある通知(例えばページ自体に関するバナーなど)の場合は、
live={false}を渡します。Rootはroleをレンダリングせず、通常のコンテンツとして読み上げられます。 - Alertはフォーカスを移動せず、自動的に閉じることもありません。一時的な通知には、代わりにトーストを使用してください。
- Alertにはキーボード操作はありません。