Tansy UI: a themeable React component kit
Tansy UI is a React component kit by OpenKhalix, named after the tansy
flower ("golden buttons"). It has two surfaces:
- A landing page built entirely from the kit's own components, with
five featured parts, a palette-switching playground form, and links
into Storybook
- A Storybook workshop with Tansy branding, a Welcome page, palette
toolbar and accessibility checks
Includes 15 components with colocated tokens and stories, seven brand
palettes, a palette-aware logo, a Docker/nginx image, a scripted
cPanel deploy, and a generator that keeps the landing page catalogue
in sync with Storybook.
This commit is contained in:
commit
9d406d6d2a
81 files changed
+14240
No files matched your search
@@ -0,0 +1,156 @@
|
||||
import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useRef,
|
||||
useState,
|
||||
forwardRef,
|
||||
type HTMLAttributes,
|
||||
type ReactNode,
|
||||
} from 'react'
|
||||
import * as Icons from 'lucide-react'
|
||||
import { cn } from '@/lib/cn'
|
||||
import {
|
||||
ALERT_COLORS,
|
||||
ALERT_GRADIENT,
|
||||
type AlertColor,
|
||||
type AlertVariant,
|
||||
} from './alert.tokens'
|
||||
|
||||
/**
|
||||
* Dismissible inline alert.
|
||||
*
|
||||
* Ported from the original `Alert` component.
|
||||
*
|
||||
* `iconName` takes a Lucide icon name as a string (looked up on the module), so
|
||||
* the icon is controllable from Storybook without importing every icon.
|
||||
*/
|
||||
|
||||
export type AlertProps = HTMLAttributes<HTMLDivElement> & {
|
||||
title?: string
|
||||
message?: ReactNode
|
||||
/** Name of a Lucide icon, e.g. `"Info"`, `"CheckCircle2"`. Falls back to `Info`. */
|
||||
iconName?: string
|
||||
onClose?: () => void
|
||||
variant?: AlertVariant
|
||||
color?: AlertColor
|
||||
showCloseButton?: boolean
|
||||
width?: string
|
||||
height?: string
|
||||
closeOnOutsideClick?: boolean
|
||||
/** Auto-close delay in ms. `0` disables. */
|
||||
autoClose?: number
|
||||
showMessage?: boolean
|
||||
customClose?: ReactNode
|
||||
}
|
||||
|
||||
export const Alert = forwardRef<HTMLDivElement, AlertProps>(function Alert(
|
||||
{
|
||||
title,
|
||||
message,
|
||||
iconName = 'Info',
|
||||
onClose,
|
||||
variant = 'default',
|
||||
color = 'success',
|
||||
className = '',
|
||||
showCloseButton = true,
|
||||
width = 'w-full',
|
||||
height = 'h-auto',
|
||||
closeOnOutsideClick = false,
|
||||
autoClose = 0,
|
||||
showMessage = true,
|
||||
customClose,
|
||||
...props
|
||||
},
|
||||
forwardedRef,
|
||||
) {
|
||||
const [visible, setVisible] = useState(true)
|
||||
const internalRef = useRef<HTMLDivElement>(null)
|
||||
|
||||
const handleClose = useCallback(() => {
|
||||
setVisible(false)
|
||||
onClose?.()
|
||||
}, [onClose])
|
||||
|
||||
// Mirror the internal node onto the forwarded ref.
|
||||
useEffect(() => {
|
||||
const node = internalRef.current
|
||||
if (typeof forwardedRef === 'function') forwardedRef(node)
|
||||
else if (forwardedRef) forwardedRef.current = node
|
||||
}, [forwardedRef])
|
||||
|
||||
useEffect(() => {
|
||||
if (autoClose > 0) {
|
||||
const timer = setTimeout(handleClose, autoClose)
|
||||
return () => clearTimeout(timer)
|
||||
}
|
||||
}, [autoClose, handleClose])
|
||||
|
||||
useEffect(() => {
|
||||
if (!closeOnOutsideClick) return
|
||||
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
if (internalRef.current && !internalRef.current.contains(event.target as Node)) {
|
||||
handleClose()
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('mousedown', handleClickOutside)
|
||||
return () => document.removeEventListener('mousedown', handleClickOutside)
|
||||
}, [closeOnOutsideClick, handleClose])
|
||||
|
||||
if (!visible) return null
|
||||
|
||||
const iconMap = Icons as unknown as Record<string, Icons.LucideIcon>
|
||||
const LucideIcon = (iconName && iconMap[iconName]) || Icons.Info
|
||||
const theme = ALERT_COLORS[color] ?? ALERT_COLORS.success
|
||||
|
||||
let variantClasses: string
|
||||
if (variant === 'filled') {
|
||||
variantClasses = cn(theme.bg, 'text-white')
|
||||
} else if (variant === 'outlined') {
|
||||
variantClasses = cn('bg-transparent', theme.text, theme.border, 'border')
|
||||
} else if (variant === 'gradient') {
|
||||
variantClasses = ALERT_GRADIENT
|
||||
} else {
|
||||
variantClasses = cn(theme.lightBg, theme.text)
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
ref={internalRef}
|
||||
role="alert"
|
||||
className={cn(
|
||||
'rounded-md p-3 transition-all duration-300',
|
||||
width,
|
||||
height,
|
||||
variantClasses,
|
||||
className,
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
<div className="flex justify-between gap-3">
|
||||
<div className="flex items-start gap-3 w-full">
|
||||
{LucideIcon && <LucideIcon className="w-5 h-5 mt-0.5 shrink-0" aria-hidden="true" />}
|
||||
|
||||
<div className="flex flex-col gap-1 w-full">
|
||||
{title && <h5 className="font-semibold text-sm leading-tight">{title}</h5>}
|
||||
{showMessage && message && <p className="text-sm opacity-90 leading-normal">{message}</p>}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{showCloseButton && (
|
||||
<button
|
||||
onClick={handleClose}
|
||||
className="shrink-0 p-1 rounded-md hover:bg-black/10 transition-colors cursor-pointer"
|
||||
type="button"
|
||||
aria-label="Close alert"
|
||||
>
|
||||
{customClose ?? <Icons.X className="w-4 h-4" />}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
})
|
||||
|
||||
export default Alert
|
||||
Reference in new issue
Block a user