refactor(NotifyProvider, ToastContainer): update toast notifications to Tabler style with improved layout, animations, and accessibility features
Publish Fast Tabler Docker image / build-and-push-fast (push) Successful in 2m2s

This commit is contained in:
2026-02-17 15:49:47 +07:00
parent c3d41a32d8
commit e071d5060f
2 changed files with 144 additions and 93 deletions
+40 -21
View File
@@ -121,31 +121,50 @@ function iconByType(type) {
function NotifyViewport({ items, onClose }) { function NotifyViewport({ items, onClose }) {
return ( return (
<div className="position-fixed top-0 end-0 p-3" style={{ zIndex: 1080, pointerEvents: 'none' }}> <div className="toast-container toast-container-end notify-viewport" style={{ zIndex: 1079 }}>
<div className="d-flex flex-column gap-2 align-items-end"> {items.map((n) => (
{items.map((n) => ( <div
<div key={n.id} className={`alert alert-${colorByType(n.type)} alert-dismissible shadow-sm`} role="alert" style={{ minWidth: 320, pointerEvents: 'auto' }}> key={n.id}
<div className="d-flex align-items-start"> className="toast show toast-alert"
<div className="me-1 mt-1">{iconByType(n.type)}</div> role="alert"
<div className="flex-grow-1"> aria-live="assertive"
aria-atomic="true"
style={{
minWidth: '320px',
maxWidth: '420px',
overflow: 'hidden',
borderRadius: 'var(--tblr-border-radius, 0.25rem)',
animation: 'slideInRight 0.3s ease-out forwards',
}}
>
<div className={`alert alert-${colorByType(n.type)} toast-alert-inner mb-0`}>
<div className="toast-header toast-alert-header">
<span className="toast-alert-icon me-2">{iconByType(n.type)}</span>
<strong className="me-auto">
{n.type === 'success' && 'Успех'}
{n.type === 'error' && 'Ошибка'}
{n.type === 'warning' && 'Внимание'}
{n.type === 'info' && 'Информация'}
</strong>
<button type="button" className="btn-close" aria-label="Закрыть" onClick={() => onClose(n.id)} />
</div>
<div className="toast-body">
<div className="alert-description">
{n.message} {n.message}
{n.details && ( {n.count > 1 && <span className="badge bg-white text-body border ms-2">×{n.count}</span>}
<details className="small mt-1">
<summary>Подробнее</summary>
<pre className="mb-0 mt-1" style={{ whiteSpace: 'pre-wrap' }}>{typeof n.details === 'string' ? n.details : JSON.stringify(n.details, null, 2)}</pre>
</details>
)}
{n.count > 1 && (
<span className="badge bg-white text-body border ms-2">×{n.count}</span>
)}
</div> </div>
<button type="button" className="btn-close" aria-label="Close" onClick={() => onClose(n.id)}> {n.details && (
<IconX size={16} /> <details className="small mt-1">
</button> <summary>Подробнее</summary>
<pre className="mb-0 mt-1" style={{ whiteSpace: 'pre-wrap' }}>
{typeof n.details === 'string' ? n.details : JSON.stringify(n.details, null, 2)}
</pre>
</details>
)}
</div> </div>
</div> </div>
))} </div>
</div> ))}
</div> </div>
); );
} }
+104 -72
View File
@@ -3,12 +3,12 @@ import {
IconCheck, IconCheck,
IconX, IconX,
IconAlertTriangle, IconAlertTriangle,
IconInfoCircle, IconInfoCircle
IconRefresh
} from '@tabler/icons-react' } from '@tabler/icons-react'
/** /**
* Контекст для глобального управления toast уведомлениями * Контекст для глобального управления toast уведомлениями.
* Тосты в стиле Tabler Alerts, всплывают справа как Tabler Toasts.
*/ */
const ToastContext = createContext({ const ToastContext = createContext({
success: () => {}, success: () => {},
@@ -19,24 +19,29 @@ const ToastContext = createContext({
export const useToast = () => useContext(ToastContext) export const useToast = () => useContext(ToastContext)
const DEFAULT_TITLES = {
success: 'Выполнено успешно',
error: 'Произошла ошибка',
warning: 'Внимание',
info: 'Информация',
}
/** /**
* Toast уведомление (отдельный элемент) * Один toast: разметка Tabler (toast + toast-header + toast-body), стиль Tabler Alerts.
*/ */
function Toast({ toast, onClose }) { function Toast({ toast, onClose }) {
const [isLeaving, setIsLeaving] = useState(false) const [isLeaving, setIsLeaving] = useState(false)
useEffect(() => { useEffect(() => {
if (toast.duration && toast.duration > 0) { if (toast.duration && toast.duration > 0) {
const timer = setTimeout(() => { const timer = setTimeout(() => handleClose(), toast.duration)
handleClose()
}, toast.duration)
return () => clearTimeout(timer) return () => clearTimeout(timer)
} }
}, [toast.duration]) }, [toast.duration])
const handleClose = () => { const handleClose = () => {
setIsLeaving(true) setIsLeaving(true)
setTimeout(() => onClose(toast.id), 300) // анимация 300ms setTimeout(() => onClose(toast.id), 300)
} }
const getIcon = () => { const getIcon = () => {
@@ -45,47 +50,53 @@ function Toast({ toast, onClose }) {
case 'error': return <IconX size={20} /> case 'error': return <IconX size={20} />
case 'warning': return <IconAlertTriangle size={20} /> case 'warning': return <IconAlertTriangle size={20} />
case 'info': return <IconInfoCircle size={20} /> case 'info': return <IconInfoCircle size={20} />
default: return null default: return <IconInfoCircle size={20} />
} }
} }
const getColorClass = () => { const alertClass = {
switch (toast.type) { success: 'alert-success',
case 'success': return 'alert-success' error: 'alert-danger',
case 'error': return 'alert-danger' warning: 'alert-warning',
case 'warning': return 'alert-warning' info: 'alert-info',
case 'info': return 'alert-info' }[toast.type] || 'alert-info'
default: return 'alert-info'
} const title = toast.title ?? DEFAULT_TITLES[toast.type]
}
return ( return (
<div <div
className={`alert ${getColorClass()} alert-dismissible fade ${isLeaving ? '' : 'show'}`} className={`toast show toast-alert ${isLeaving ? 'toast-leaving' : ''}`}
role="alert" role="alert"
aria-live="assertive"
aria-atomic="true"
data-toast-type={toast.type}
style={{ style={{
minWidth: '300px', minWidth: '320px',
maxWidth: '400px', maxWidth: '420px',
boxShadow: '0 0.5rem 1rem rgba(0,0,0,.15)', overflow: 'hidden',
animation: isLeaving ? 'slideOutRight 0.3s ease-out' : 'slideInRight 0.3s ease-out' borderRadius: 'var(--tblr-border-radius, 0.25rem)',
animation: isLeaving ? 'slideOutRight 0.3s ease-out forwards' : 'slideInRight 0.3s ease-out forwards',
}} }}
> >
<div className="d-flex align-items-start"> <div className={`alert ${alertClass} toast-alert-inner mb-0`}>
<div className="me-2 mt-1"> <div className="toast-header toast-alert-header">
{getIcon()} <span className="toast-alert-icon me-2">{getIcon()}</span>
<strong className="me-auto">{title}</strong>
<button
type="button"
className="btn-close"
onClick={handleClose}
aria-label="Закрыть"
/>
</div> </div>
<div className="flex-grow-1"> <div className="toast-body">
{toast.title && ( <div className="alert-description">{toast.message}</div>
<h4 className="alert-title">{toast.title}</h4>
)}
<div className="text-secondary">{toast.message}</div>
{/* Действия */}
{toast.actions && toast.actions.length > 0 && ( {toast.actions && toast.actions.length > 0 && (
<div className="btn-list mt-2"> <div className="btn-list mt-2">
{toast.actions.map((action, idx) => ( {toast.actions.map((action, idx) => (
<button <button
key={idx} key={idx}
type="button"
className={`btn btn-sm ${action.variant || 'btn-outline-primary'}`} className={`btn btn-sm ${action.variant || 'btn-outline-primary'}`}
onClick={() => { onClick={() => {
action.onClick?.() action.onClick?.()
@@ -99,12 +110,6 @@ function Toast({ toast, onClose }) {
</div> </div>
)} )}
</div> </div>
<button
type="button"
className="btn-close"
onClick={handleClose}
aria-label="Close"
></button>
</div> </div>
</div> </div>
) )
@@ -152,46 +157,73 @@ export function ToastContainer({ children }) {
return ( return (
<ToastContext.Provider value={contextValue}> <ToastContext.Provider value={contextValue}>
{children} {children}
{/* Контейнер с toast'ами */} {/* Контейнер тостов справа (как в Tabler) */}
<div <div className="toast-container toast-container-end">
style={{ {toasts.map((t) => (
position: 'fixed', <Toast key={t.id} toast={t} onClose={removeToast} />
bottom: '20px',
right: '20px',
zIndex: 9999,
display: 'flex',
flexDirection: 'column',
gap: '10px'
}}
>
{toasts.map(toast => (
<Toast key={toast.id} toast={toast} onClose={removeToast} />
))} ))}
</div> </div>
{/* CSS анимации */} {/* Стили: тосты в стиле Tabler Alerts, всплывают справа */}
<style>{` <style>{`
@keyframes slideInRight { .toast-container-end {
from { position: fixed;
transform: translateX(100%); top: 1rem;
opacity: 0; right: 1rem;
} z-index: 9999;
to { display: flex;
transform: translateX(0); flex-direction: column;
opacity: 1; gap: 0.5rem;
} pointer-events: none;
}
.toast-container-end .toast {
pointer-events: auto;
}
/* NotifyProvider тосты ниже, чтобы не перекрывать useToast */
.toast-container-end.notify-viewport {
top: 5rem;
} }
/* Toast в стиле Tabler Alert: колоночный layout, без дублирования отступов */
.toast-alert .toast-alert-inner {
display: block;
flex-direction: column;
padding: 0;
gap: 0;
border-radius: var(--tblr-border-radius, 0.25rem);
box-shadow: var(--tblr-shadow, 0 0.5rem 1rem rgba(0, 0, 0, 0.15));
}
.toast-alert .toast-alert-header {
display: flex;
align-items: center;
padding: 0.5rem 0.75rem;
background: transparent;
border-bottom: 1px solid rgba(0, 0, 0, 0.08);
font-weight: 500;
}
.toast-alert .toast-alert-icon {
color: inherit;
flex-shrink: 0;
}
.toast-alert .toast-body {
padding: 0.75rem;
}
.toast-alert .toast-body .alert-description {
color: var(--tblr-secondary);
}
.toast-alert.alert-danger .toast-alert-header { border-bottom-color: rgba(0, 0, 0, 0.1); }
.toast-alert .btn-close {
margin-left: auto;
}
@keyframes slideInRight {
from { transform: translateX(100%); opacity: 0; }
to { transform: translateX(0); opacity: 1; }
}
@keyframes slideOutRight { @keyframes slideOutRight {
from { from { transform: translateX(0); opacity: 1; }
transform: translateX(0); to { transform: translateX(100%); opacity: 0; }
opacity: 1;
}
to {
transform: translateX(100%);
opacity: 0;
}
} }
`}</style> `}</style>
</ToastContext.Provider> </ToastContext.Provider>