feat(web): add Modal + EmptyState primitives and a11y hooks
- Modal: shared backdrop/panel with ESC-close, backdrop-click-close, focus trap, body scroll lock; supports center + drawer-right variants, matrix/violet accents, default/wide widths. - EmptyState: icon + title + hint + optional CTA; compact variant for tight rails. - useEscapeKey, useFocusTrap: reusable hooks powering Modal; will also be adopted by CommandPalette and ContextMenu in follow-up commits. No retrofits yet — primitives only. tsc clean.
This commit is contained in:
52
decnet_web/src/components/EmptyState/EmptyState.css
Normal file
52
decnet_web/src/components/EmptyState/EmptyState.css
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
/* Shared EmptyState styling. Component-scoped .empty-state rules
|
||||||
|
(Attackers/Bounty/LiveLogs) still win over these base rules. */
|
||||||
|
|
||||||
|
.empty-state-icon {
|
||||||
|
opacity: 0.4;
|
||||||
|
color: var(--violet);
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty-state-title {
|
||||||
|
font-size: 0.75rem;
|
||||||
|
letter-spacing: 2px;
|
||||||
|
opacity: 0.7;
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty-state-hint {
|
||||||
|
font-size: 0.65rem;
|
||||||
|
opacity: 0.45;
|
||||||
|
letter-spacing: 1px;
|
||||||
|
margin-top: -4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty-state-cta {
|
||||||
|
margin-top: 6px;
|
||||||
|
padding: 6px 12px;
|
||||||
|
background: transparent;
|
||||||
|
border: 1px solid var(--matrix);
|
||||||
|
color: var(--matrix);
|
||||||
|
font-family: var(--font-mono);
|
||||||
|
font-size: 0.65rem;
|
||||||
|
letter-spacing: 2px;
|
||||||
|
cursor: pointer;
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
text-transform: uppercase;
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty-state-cta:hover {
|
||||||
|
background: rgba(0, 255, 65, 0.1);
|
||||||
|
box-shadow: 0 0 8px rgba(0, 255, 65, 0.3);
|
||||||
|
}
|
||||||
|
|
||||||
|
.empty-state.empty-state-compact {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 6px;
|
||||||
|
padding: 6px 4px;
|
||||||
|
font-size: 0.65rem;
|
||||||
|
letter-spacing: 1.5px;
|
||||||
|
opacity: 0.5;
|
||||||
|
color: var(--text-dim, #888);
|
||||||
|
}
|
||||||
46
decnet_web/src/components/EmptyState/EmptyState.tsx
Normal file
46
decnet_web/src/components/EmptyState/EmptyState.tsx
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import type { LucideIcon } from 'lucide-react';
|
||||||
|
import './EmptyState.css';
|
||||||
|
|
||||||
|
interface CTA {
|
||||||
|
label: string;
|
||||||
|
onClick: () => void;
|
||||||
|
icon?: LucideIcon;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
icon?: LucideIcon;
|
||||||
|
title: string;
|
||||||
|
hint?: string;
|
||||||
|
cta?: CTA;
|
||||||
|
size?: 'default' | 'compact';
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const EmptyState: React.FC<Props> = ({ icon: Icon, title, hint, cta, size = 'default', className = '' }) => {
|
||||||
|
if (size === 'compact') {
|
||||||
|
return (
|
||||||
|
<div className={`empty-state empty-state-compact ${className}`}>
|
||||||
|
{Icon && <Icon size={12} />}
|
||||||
|
<span>{title}</span>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const CtaIcon = cta?.icon;
|
||||||
|
return (
|
||||||
|
<div className={`empty-state ${className}`}>
|
||||||
|
{Icon && <Icon size={28} className="empty-state-icon" />}
|
||||||
|
<div className="type-label empty-state-title">{title}</div>
|
||||||
|
{hint && <div className="empty-state-hint">{hint}</div>}
|
||||||
|
{cta && (
|
||||||
|
<button type="button" className="empty-state-cta" onClick={cta.onClick}>
|
||||||
|
{CtaIcon && <CtaIcon size={12} />}
|
||||||
|
<span>{cta.label}</span>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default EmptyState;
|
||||||
24
decnet_web/src/components/Modal/Modal.css
Normal file
24
decnet_web/src/components/Modal/Modal.css
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
/* Drawer-right variant: reuses .modal-backdrop + .modal base from DeckyFleet.css
|
||||||
|
and shifts the panel to the right edge as a full-height side panel. */
|
||||||
|
|
||||||
|
.modal-backdrop.drawer {
|
||||||
|
justify-content: flex-end;
|
||||||
|
align-items: stretch;
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal.modal-drawer-right {
|
||||||
|
width: 520px;
|
||||||
|
max-width: 96vw;
|
||||||
|
max-height: 100vh;
|
||||||
|
height: 100vh;
|
||||||
|
border-left: 1px solid var(--matrix);
|
||||||
|
border-top: none;
|
||||||
|
border-right: none;
|
||||||
|
border-bottom: none;
|
||||||
|
box-shadow: -8px 0 30px rgba(0, 0, 0, 0.6);
|
||||||
|
}
|
||||||
|
|
||||||
|
.modal.modal-drawer-right.violet {
|
||||||
|
border-left-color: var(--violet);
|
||||||
|
box-shadow: -8px 0 30px rgba(238, 130, 238, 0.25);
|
||||||
|
}
|
||||||
85
decnet_web/src/components/Modal/Modal.tsx
Normal file
85
decnet_web/src/components/Modal/Modal.tsx
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
import React, { useEffect, useRef } from 'react';
|
||||||
|
import { X, type LucideIcon } from 'lucide-react';
|
||||||
|
import { useEscapeKey } from '../../hooks/useEscapeKey';
|
||||||
|
import { useFocusTrap } from '../../hooks/useFocusTrap';
|
||||||
|
import './Modal.css';
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
open: boolean;
|
||||||
|
onClose: () => void;
|
||||||
|
title?: string;
|
||||||
|
icon?: LucideIcon;
|
||||||
|
footer?: React.ReactNode;
|
||||||
|
accent?: 'matrix' | 'violet';
|
||||||
|
width?: 'default' | 'wide';
|
||||||
|
variant?: 'center' | 'drawer-right';
|
||||||
|
children: React.ReactNode;
|
||||||
|
className?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Modal: React.FC<Props> = ({
|
||||||
|
open,
|
||||||
|
onClose,
|
||||||
|
title,
|
||||||
|
icon: Icon,
|
||||||
|
footer,
|
||||||
|
accent = 'matrix',
|
||||||
|
width = 'default',
|
||||||
|
variant = 'center',
|
||||||
|
children,
|
||||||
|
className = '',
|
||||||
|
}) => {
|
||||||
|
const panelRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
|
||||||
|
useEscapeKey(onClose, open);
|
||||||
|
useFocusTrap(panelRef, open);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open) return;
|
||||||
|
const prev = document.body.style.overflow;
|
||||||
|
document.body.style.overflow = 'hidden';
|
||||||
|
return () => {
|
||||||
|
document.body.style.overflow = prev;
|
||||||
|
};
|
||||||
|
}, [open]);
|
||||||
|
|
||||||
|
if (!open) return null;
|
||||||
|
|
||||||
|
const panelClasses = [
|
||||||
|
'modal',
|
||||||
|
accent === 'violet' ? 'violet' : '',
|
||||||
|
width === 'wide' ? 'wide' : '',
|
||||||
|
variant === 'drawer-right' ? 'modal-drawer-right' : '',
|
||||||
|
className,
|
||||||
|
].filter(Boolean).join(' ');
|
||||||
|
|
||||||
|
const backdropClass = variant === 'drawer-right' ? 'modal-backdrop drawer' : 'modal-backdrop';
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={backdropClass} onClick={onClose}>
|
||||||
|
<div
|
||||||
|
ref={panelRef}
|
||||||
|
className={panelClasses}
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
role="dialog"
|
||||||
|
aria-modal="true"
|
||||||
|
>
|
||||||
|
{title && (
|
||||||
|
<div className="modal-head">
|
||||||
|
<h3>
|
||||||
|
{Icon && <Icon size={14} />}
|
||||||
|
{title}
|
||||||
|
</h3>
|
||||||
|
<button className="close-btn" onClick={onClose} aria-label="Close">
|
||||||
|
<X size={16} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{children}
|
||||||
|
{footer && <div className="modal-foot">{footer}</div>}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Modal;
|
||||||
15
decnet_web/src/hooks/useEscapeKey.ts
Normal file
15
decnet_web/src/hooks/useEscapeKey.ts
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
import { useEffect } from 'react';
|
||||||
|
|
||||||
|
export function useEscapeKey(onEscape: () => void, active: boolean = true): void {
|
||||||
|
useEffect(() => {
|
||||||
|
if (!active) return;
|
||||||
|
const handler = (e: KeyboardEvent) => {
|
||||||
|
if (e.key === 'Escape') {
|
||||||
|
e.stopPropagation();
|
||||||
|
onEscape();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
window.addEventListener('keydown', handler);
|
||||||
|
return () => window.removeEventListener('keydown', handler);
|
||||||
|
}, [onEscape, active]);
|
||||||
|
}
|
||||||
49
decnet_web/src/hooks/useFocusTrap.ts
Normal file
49
decnet_web/src/hooks/useFocusTrap.ts
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
import { useEffect, type RefObject } from 'react';
|
||||||
|
|
||||||
|
const FOCUSABLE =
|
||||||
|
'input:not([disabled]), button:not([disabled]), textarea:not([disabled]), select:not([disabled]), a[href], [tabindex]:not([tabindex="-1"])';
|
||||||
|
|
||||||
|
export function useFocusTrap(
|
||||||
|
ref: RefObject<HTMLElement | null>,
|
||||||
|
active: boolean,
|
||||||
|
): void {
|
||||||
|
useEffect(() => {
|
||||||
|
if (!active || !ref.current) return;
|
||||||
|
const root = ref.current;
|
||||||
|
const previouslyFocused = document.activeElement as HTMLElement | null;
|
||||||
|
|
||||||
|
const focusables = () =>
|
||||||
|
Array.from(root.querySelectorAll<HTMLElement>(FOCUSABLE)).filter(
|
||||||
|
(el) => !el.hasAttribute('aria-hidden') && el.offsetParent !== null,
|
||||||
|
);
|
||||||
|
|
||||||
|
const autoFocus =
|
||||||
|
root.querySelector<HTMLElement>('[data-autofocus]') ?? focusables()[0];
|
||||||
|
autoFocus?.focus();
|
||||||
|
|
||||||
|
const handler = (e: KeyboardEvent) => {
|
||||||
|
if (e.key !== 'Tab') return;
|
||||||
|
const items = focusables();
|
||||||
|
if (items.length === 0) {
|
||||||
|
e.preventDefault();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const first = items[0];
|
||||||
|
const last = items[items.length - 1];
|
||||||
|
const current = document.activeElement as HTMLElement | null;
|
||||||
|
if (e.shiftKey && current === first) {
|
||||||
|
e.preventDefault();
|
||||||
|
last.focus();
|
||||||
|
} else if (!e.shiftKey && current === last) {
|
||||||
|
e.preventDefault();
|
||||||
|
first.focus();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
root.addEventListener('keydown', handler);
|
||||||
|
return () => {
|
||||||
|
root.removeEventListener('keydown', handler);
|
||||||
|
previouslyFocused?.focus?.();
|
||||||
|
};
|
||||||
|
}, [ref, active]);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user