2025-12-03 21:56:50 +00:00
|
|
|
import { useState, useEffect, useRef, useCallback } from 'react';
|
|
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
|
|
|
|
|
|
export interface DropdownProps {
|
|
|
|
|
trigger: React.ReactNode;
|
|
|
|
|
children: React.ReactNode;
|
|
|
|
|
align?: 'left' | 'right' | 'center';
|
|
|
|
|
className?: string;
|
2026-02-06 01:27:29 +00:00
|
|
|
/** Controlled open state */
|
|
|
|
|
open?: boolean;
|
|
|
|
|
/** Uncontrolled default open */
|
|
|
|
|
defaultOpen?: boolean;
|
2025-12-03 21:56:50 +00:00
|
|
|
onOpenChange?: (open: boolean) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Composant Dropdown réutilisable avec menu et gestion du clavier.
|
|
|
|
|
*/
|
|
|
|
|
export function Dropdown({
|
|
|
|
|
trigger,
|
|
|
|
|
children,
|
|
|
|
|
align = 'left',
|
|
|
|
|
className,
|
2026-02-06 01:27:29 +00:00
|
|
|
open: openProp,
|
|
|
|
|
defaultOpen = false,
|
2025-12-03 21:56:50 +00:00
|
|
|
onOpenChange,
|
|
|
|
|
}: DropdownProps) {
|
2026-02-06 01:27:29 +00:00
|
|
|
const [internalOpen, setInternalOpen] = useState(defaultOpen);
|
|
|
|
|
const isControlled = openProp !== undefined;
|
|
|
|
|
const open = isControlled ? openProp : internalOpen;
|
|
|
|
|
|
2025-12-03 21:56:50 +00:00
|
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const menuRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const triggerRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
const focusedIndexRef = useRef<number>(-1);
|
|
|
|
|
|
|
|
|
|
const handleOpenChange = useCallback(
|
|
|
|
|
(newOpen: boolean) => {
|
2026-02-06 01:27:29 +00:00
|
|
|
if (!isControlled) {
|
|
|
|
|
setInternalOpen(newOpen);
|
|
|
|
|
}
|
2025-12-03 21:56:50 +00:00
|
|
|
onOpenChange?.(newOpen);
|
|
|
|
|
if (!newOpen) {
|
|
|
|
|
focusedIndexRef.current = -1;
|
|
|
|
|
}
|
|
|
|
|
},
|
2026-02-06 01:27:29 +00:00
|
|
|
[onOpenChange, isControlled],
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Fermer le dropdown quand on clique en dehors
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!open) return;
|
|
|
|
|
|
|
|
|
|
const handleClickOutside = (event: MouseEvent) => {
|
|
|
|
|
if (
|
|
|
|
|
containerRef.current &&
|
|
|
|
|
!containerRef.current.contains(event.target as Node)
|
|
|
|
|
) {
|
|
|
|
|
handleOpenChange(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.addEventListener('mousedown', handleClickOutside);
|
|
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener('mousedown', handleClickOutside);
|
|
|
|
|
};
|
|
|
|
|
}, [open, handleOpenChange]);
|
|
|
|
|
|
|
|
|
|
// Gestion du clavier
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!open) return;
|
|
|
|
|
|
|
|
|
|
const handleKeyDown = (event: KeyboardEvent) => {
|
|
|
|
|
if (!menuRef.current) return;
|
|
|
|
|
|
|
|
|
|
const focusableElements = menuRef.current.querySelectorAll<HTMLElement>(
|
2025-12-13 02:34:34 +00:00
|
|
|
'button, [href], input, select, textarea, [role="menuitem"], [tabindex]:not([tabindex="-1"])',
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const elements = Array.from(focusableElements);
|
|
|
|
|
|
|
|
|
|
switch (event.key) {
|
|
|
|
|
case 'Escape':
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
handleOpenChange(false);
|
|
|
|
|
triggerRef.current?.focus();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'ArrowDown':
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
focusedIndexRef.current =
|
|
|
|
|
focusedIndexRef.current < elements.length - 1
|
|
|
|
|
? focusedIndexRef.current + 1
|
|
|
|
|
: 0;
|
|
|
|
|
elements[focusedIndexRef.current]?.focus();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'ArrowUp':
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
focusedIndexRef.current =
|
|
|
|
|
focusedIndexRef.current > 0
|
|
|
|
|
? focusedIndexRef.current - 1
|
|
|
|
|
: elements.length - 1;
|
|
|
|
|
elements[focusedIndexRef.current]?.focus();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'Enter':
|
|
|
|
|
case ' ':
|
|
|
|
|
event.preventDefault();
|
2025-12-13 02:34:34 +00:00
|
|
|
if (
|
|
|
|
|
focusedIndexRef.current >= 0 &&
|
|
|
|
|
elements[focusedIndexRef.current]
|
|
|
|
|
) {
|
2025-12-03 21:56:50 +00:00
|
|
|
elements[focusedIndexRef.current].click();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'Home':
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
focusedIndexRef.current = 0;
|
|
|
|
|
elements[0]?.focus();
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'End':
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
focusedIndexRef.current = elements.length - 1;
|
|
|
|
|
elements[elements.length - 1]?.focus();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.addEventListener('keydown', handleKeyDown);
|
|
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener('keydown', handleKeyDown);
|
|
|
|
|
};
|
|
|
|
|
}, [open, handleOpenChange]);
|
|
|
|
|
|
|
|
|
|
// Focuser le premier élément quand le menu s'ouvre
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (open && menuRef.current) {
|
|
|
|
|
const focusableElements = menuRef.current.querySelectorAll<HTMLElement>(
|
2025-12-13 02:34:34 +00:00
|
|
|
'button, [href], input, select, textarea, [role="menuitem"], [tabindex]:not([tabindex="-1"])',
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
|
|
|
|
if (focusableElements.length > 0) {
|
|
|
|
|
focusedIndexRef.current = 0;
|
|
|
|
|
// Petit délai pour s'assurer que le menu est rendu
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
(focusableElements[0] as HTMLElement)?.focus();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}, [open]);
|
|
|
|
|
|
|
|
|
|
const alignClasses = {
|
|
|
|
|
left: 'left-0',
|
|
|
|
|
right: 'right-0',
|
|
|
|
|
center: 'left-1/2 -translate-x-1/2',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div ref={containerRef} className={cn('relative', className)}>
|
|
|
|
|
<div
|
|
|
|
|
ref={triggerRef}
|
|
|
|
|
onClick={() => handleOpenChange(!open)}
|
|
|
|
|
role="button"
|
|
|
|
|
aria-haspopup="true"
|
|
|
|
|
aria-expanded={open}
|
|
|
|
|
tabIndex={0}
|
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === 'Enter' || e.key === ' ') {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
handleOpenChange(!open);
|
|
|
|
|
} else if (e.key === 'ArrowDown') {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
handleOpenChange(true);
|
|
|
|
|
}
|
|
|
|
|
}}
|
ui(a11y): add focus-visible ring to 16 interactive components
Add consistent focus-visible:ring-2 focus-visible:ring-ring pattern to
elements using role="button" / tabIndex={0} that lacked visible focus
indicators.
Affected: TrackCard, 2FA setup steps, ProjectsManager cards,
NotificationMenuItem, SelectOptionItem, DropdownMenuItem,
ConversationItem, VirtualizedChatMessageItem, GearInventoryGrid,
UploadModal, SearchPageResults, SocialViewFeedItem, SocialViewSidebar,
FileManagerViewTable.
Improves keyboard navigation across the application.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 21:48:24 +00:00
|
|
|
className="focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background rounded-[var(--radius)]"
|
2025-12-03 21:56:50 +00:00
|
|
|
style={{ display: 'inline-block' }}
|
|
|
|
|
>
|
|
|
|
|
{trigger}
|
|
|
|
|
</div>
|
|
|
|
|
{open && (
|
|
|
|
|
<>
|
|
|
|
|
{/* Backdrop pour fermer en cliquant en dehors */}
|
|
|
|
|
<div
|
|
|
|
|
className="fixed inset-0 z-40"
|
|
|
|
|
onClick={() => handleOpenChange(false)}
|
|
|
|
|
aria-hidden="true"
|
|
|
|
|
/>
|
|
|
|
|
{/* Menu */}
|
|
|
|
|
<div
|
|
|
|
|
ref={menuRef}
|
|
|
|
|
className={cn(
|
feat(web): UI premium Discord/Spotify-like — tokens, shadows, focus, layout
Plan UI premium 6–8 semaines (design system, shell, Storybook, a11y):
- Design system: DESIGN_TOKENS.md, APP_SHELL.md, FULL_LAYOUT_PAGE.md. Single source
for layout/shell (index.css), shadows (design-system.css), durations/easing.
- Tokens: shadow-cover-depth, shadow-gold-glow, shadow-fab-glow; layout max-height
(max-h-layout-drawer, max-h-layout-panel, max-h-layout-list). All duration-200/300/500
replaced by --duration-fast/normal/slow. Arbitrary shadows replaced by token classes.
- Shell & player: Sidebar, Header, GlobalPlayer, MiniPlayer, PlayerQueue, PlayerControls,
AudioPlayer use tokens; focus-visible on Sidebar, PlayerQueue, DropdownMenuTrigger/Item,
TabsTrigger. Typography: text-[10px]/[9px] → text-xs where applicable.
- ESLint: no-restricted-syntax (warn) for w-/h-/rounded-/shadow-/text-/spacing arbitrary.
- Scripts: report-arbitrary-values.mjs, capture/compare/generate visual; visual-complete.spec.ts.
- Stories full layout: Dashboard, Playlists, Library, Settings, Profile in DashboardLayout.stories.
- .cursorrules + README: DESIGN_TOKENS, APP_SHELL, visual commands, no arbitrary without justification.
- apps/web/.gitignore: e2e test artifacts (test-results-visual, playwright-report-visual).
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 16:15:58 +00:00
|
|
|
'absolute z-50 mt-2 min-w-32 bg-card border border-border rounded-xl shadow-lg',
|
2026-02-08 23:11:25 +00:00
|
|
|
'overflow-hidden animate-scaleIn',
|
2025-12-13 02:34:34 +00:00
|
|
|
alignClasses[align],
|
2026-02-08 23:11:25 +00:00
|
|
|
align === 'right' ? 'origin-top-right' : align === 'center' ? 'origin-top' : 'origin-top-left',
|
2025-12-03 21:56:50 +00:00
|
|
|
)}
|
|
|
|
|
role="menu"
|
|
|
|
|
aria-orientation="vertical"
|
|
|
|
|
>
|
|
|
|
|
{children}
|
|
|
|
|
</div>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|