AlertAPI
React Alert 元件的 API 參考文件。瞭解此匯出模組的 props、CSS 和其他 API。
範例
Import
import Alert from '@mui/material/Alert';
// or
import { Alert } from '@mui/material';
閱讀這份關於最小化 bundle size 的指南,以瞭解差異。
Paper 元件的 props 也可在此使用。
名稱 | 類型 | 預設值 | 描述 |
---|---|---|---|
action | node | - | 要顯示的動作。它會在訊息之後,在警示的結尾處呈現。 |
children | node | - | 元件的內容。 |
classes | object | - | 覆寫或擴充套用至元件的樣式。 請參閱下方CSS 類別 API 以瞭解更多詳細資訊。 |
closeText | string | 'Close' | 覆寫關閉彈出視窗圖示按鈕的預設標籤。 |
color | 'error' | 'info' | 'success' | 'warning' | string | - | 元件的顏色。除非另有提供,否則該值取自 |
components | { CloseButton?: elementType, CloseIcon?: elementType } | {} | 用於每個 slot 內部的元件。 |
componentsProps | { closeButton?: object, closeIcon?: object } | {} | slot 元件的額外 props。您可以覆寫現有的 props 或新增 props。 |
icon | node | - | 覆寫 children 之前顯示的圖示。除非另有提供,否則圖示會對應到 |
iconMapping | { error?: node, info?: node, success?: node, warning?: node } | - | 元件將 |
onClose | func | - | 當元件要求關閉時觸發的回呼。當提供此回呼且未設定 簽名: function(event: React.SyntheticEvent) => void
|
role | string | 'alert' | 元素的 ARIA role 屬性。 |
severity | 'error' | 'info' | 'success' | 'warning' | string | 'success' | 警示的嚴重程度。這定義了使用的顏色和圖示。 |
slotProps | { action?: func | object, closeButton?: func | object, closeIcon?: func | object, icon?: func | object, message?: func | object, root?: func | object } | {} | 用於每個 slot 內部的 props。 |
slots | { action?: elementType, closeButton?: elementType, closeIcon?: elementType, icon?: elementType, message?: elementType, root?: elementType } | {} | 用於每個 slot 內部的元件。 |
sx | Array<func | object | bool> | func | object | - | 系統 prop,允許定義系統覆寫以及額外的 CSS 樣式。 請參閱 `sx` 頁面 以瞭解更多詳細資訊。 |
variant | 'filled' | 'outlined' | 'standard' | string | 'standard' | 要使用的 variant。 |
ref
會傳遞到 root 元素。繼承
雖然上方未明確記錄,但 Paper 元件的 props 也可在 Alert 中使用。您可以利用這一點來目標巢狀元件。
主題預設 props
您可以使用 MuiAlert
來變更此元件的預設 props 使用主題。
Slot 名稱 | 類別名稱 | 預設元件 | 描述 |
---|---|---|---|
root | .MuiAlert-root | Paper | 呈現 root slot 的元件。 |
icon | .MuiAlert-icon | div | 呈現 icon slot 的元件。 |
message | .MuiAlert-message | div | 呈現 message slot 的元件。 |
action | .MuiAlert-action | div | 呈現 action slot 的元件。 |
closeButton | IconButton | 呈現關閉按鈕的元件。 | |
closeIcon | svg | 呈現關閉圖示的元件。 |
這些類別名稱對於使用 CSS 設定樣式很有用。當觸發特定狀態時,它們會套用至元件的 slots。
類別名稱 | 規則名稱 | 描述 |
---|---|---|
.MuiAlert-colorError | colorError | 如果 color="error" ,則套用至 root 元素的樣式。 |
.MuiAlert-colorInfo | colorInfo | 如果 color="info" ,則套用至 root 元素的樣式。 |
.MuiAlert-colorSuccess | colorSuccess | 如果 color="success" ,則套用至 root 元素的樣式。 |
.MuiAlert-colorWarning | colorWarning | 如果 color="warning" ,則套用至 root 元素的樣式。 |
.MuiAlert-filled | filled | 如果 variant="filled" ,則套用至 root 元素的樣式。 |
.MuiAlert-filledError | filledError | 如果 variant="filled" 且 color="error" ,則套用至 root 元素的樣式。 |
.MuiAlert-filledInfo | filledInfo | 如果 variant="filled" 且 color="info" ,則套用至 root 元素的樣式。 |
.MuiAlert-filledSuccess | filledSuccess | 如果 variant="filled" 且 color="success" ,則套用至 root 元素的樣式。 |
.MuiAlert-filledWarning | filledWarning | 如果 variant="filled" 且 color="warning" ,則套用至 root 元素的樣式。 |
.MuiAlert-outlined | outlined | 如果 variant="outlined" ,則套用至 root 元素的樣式。 |
.MuiAlert-outlinedError | outlinedError | 如果 variant="outlined" 且 color="error" ,則套用至 root 元素的樣式。 |
.MuiAlert-outlinedInfo | outlinedInfo | 如果 variant="outlined" 且 color="info" ,則套用至 root 元素的樣式。 |
.MuiAlert-outlinedSuccess | outlinedSuccess | 如果 variant="outlined" 且 color="success" ,則套用至 root 元素的樣式。 |
.MuiAlert-outlinedWarning | outlinedWarning | 如果 variant="outlined" 且 color="warning" ,則套用至 root 元素的樣式。 |
.MuiAlert-standard | standard | 如果 variant="standard" ,則套用至 root 元素的樣式。 |
.MuiAlert-standardError | standardError | 如果 variant="standard" 且 color="error" ,則套用至 root 元素的樣式。 |
.MuiAlert-standardInfo | standardInfo | 如果 variant="standard" 且 color="info" ,則套用至 root 元素的樣式。 |
.MuiAlert-standardSuccess | standardSuccess | 如果 variant="standard" 且 color="success" ,則套用至 root 元素的樣式。 |
.MuiAlert-standardWarning | standardWarning | 如果 variant="standard" 且 color="warning" ,則套用至 root 元素的樣式。 |
您可以使用以下其中一種自訂選項來覆寫元件的樣式
- 使用全域類別名稱。
- 在自訂主題中使用規則名稱作為元件
styleOverrides
屬性的一部分。
原始碼
如果您在本頁中找不到所需的資訊,請考慮查看元件的實作以瞭解更多詳細資訊。