Extract equipment planner components
This commit is contained in:
parent
588c1298cd
commit
fb5feb8c98
7 changed files with 907 additions and 881 deletions
|
|
@ -0,0 +1,193 @@
|
|||
// Rôle : regroupe les menus flottants de sélection d'icônes, formes et couleurs de l'équipement.
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import { Icon } from "../../../components/Icon.jsx";
|
||||
import { SOCKET_COLORS, SOCKET_COLOR_VALUES, SOCKET_SHAPES } from "./equipmentPlannerOptions.js";
|
||||
|
||||
export function SocketShapeIcon({ shape, color = "gray", className = "" }) {
|
||||
return (
|
||||
<span className={`equipment-planner-socket-icon color-${color} ${className}`.trim()} style={{ "--equipment-socket-icon-color": SOCKET_COLOR_VALUES[color] || SOCKET_COLOR_VALUES.gray }} aria-hidden="true">
|
||||
<Icon name={shape} />
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
export function IconChoiceDropdown({ className = "", label, value, options, onChange }) {
|
||||
const selected = options.find((option) => option.icon === value) || options[0];
|
||||
return (
|
||||
<FloatingPicker
|
||||
className={className}
|
||||
label={label}
|
||||
columns={Math.min(options.length, 5)}
|
||||
renderTrigger={({ open, toggle }) => (
|
||||
<button
|
||||
className={`equipment-planner-picker-trigger ${open ? "active" : ""}`}
|
||||
type="button"
|
||||
onClick={toggle}
|
||||
aria-label={label}
|
||||
aria-expanded={open}
|
||||
title={selected.label}
|
||||
>
|
||||
<Icon name={selected.icon} />
|
||||
<Icon name="dropdown" />
|
||||
</button>
|
||||
)}
|
||||
>
|
||||
{({ close }) => (
|
||||
<div className="equipment-planner-picker-grid">
|
||||
{options.map((option) => (
|
||||
<button
|
||||
key={option.icon}
|
||||
className={`equipment-planner-picker-option ${option.icon === value ? "active" : ""}`}
|
||||
type="button"
|
||||
onClick={() => {
|
||||
onChange(option.icon);
|
||||
close();
|
||||
}}
|
||||
aria-label={option.label}
|
||||
title={option.label}
|
||||
>
|
||||
<Icon name={option.icon} />
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</FloatingPicker>
|
||||
);
|
||||
}
|
||||
|
||||
export function ShapeChoiceDropdown({ label, value, onChange }) {
|
||||
return (
|
||||
<FloatingPicker
|
||||
className="equipment-planner-shape-picker"
|
||||
label={label}
|
||||
columns={SOCKET_SHAPES.length}
|
||||
renderTrigger={({ open, toggle }) => (
|
||||
<button className={`equipment-planner-picker-trigger ${open ? "active" : ""}`} type="button" onClick={toggle} aria-label={label} aria-expanded={open} title={label}>
|
||||
<SocketShapeIcon shape={value} />
|
||||
<Icon name="dropdown" />
|
||||
</button>
|
||||
)}
|
||||
>
|
||||
{({ close }) => (
|
||||
<div className="equipment-planner-picker-grid">
|
||||
{SOCKET_SHAPES.map((shape) => (
|
||||
<button
|
||||
key={shape}
|
||||
className={`equipment-planner-picker-option ${shape === value ? "active" : ""}`}
|
||||
type="button"
|
||||
onClick={() => {
|
||||
onChange(shape);
|
||||
close();
|
||||
}}
|
||||
aria-label={shape}
|
||||
title={shape}
|
||||
>
|
||||
<SocketShapeIcon shape={shape} />
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</FloatingPicker>
|
||||
);
|
||||
}
|
||||
|
||||
export function ColorChoiceDropdown({ label, value, onChange }) {
|
||||
return (
|
||||
<FloatingPicker
|
||||
label={label}
|
||||
columns={6}
|
||||
renderTrigger={({ open, toggle }) => (
|
||||
<button className={`equipment-planner-picker-trigger ${open ? "active" : ""}`} type="button" onClick={toggle} aria-label={label} aria-expanded={open} title={value}>
|
||||
<span className={`equipment-planner-color-swatch color-${value}`} aria-hidden="true" />
|
||||
<Icon name="dropdown" />
|
||||
</button>
|
||||
)}
|
||||
>
|
||||
{({ close }) => (
|
||||
<div className="equipment-planner-color-grid">
|
||||
{SOCKET_COLORS.map((color) => (
|
||||
<button
|
||||
key={color}
|
||||
className={`equipment-planner-color-option ${color === value ? "active" : ""}`}
|
||||
type="button"
|
||||
onClick={() => {
|
||||
onChange(color);
|
||||
close();
|
||||
}}
|
||||
aria-label={color}
|
||||
title={color}
|
||||
>
|
||||
<span className={`equipment-planner-color-swatch color-${color}`} aria-hidden="true" />
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</FloatingPicker>
|
||||
);
|
||||
}
|
||||
|
||||
function FloatingPicker({ className = "", label, columns = 5, renderTrigger, children }) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [menuStyle, setMenuStyle] = useState({});
|
||||
const rootRef = useRef(null);
|
||||
const menuRef = useRef(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) return undefined;
|
||||
|
||||
function placeMenu() {
|
||||
const trigger = rootRef.current;
|
||||
if (!trigger) return;
|
||||
const rect = trigger.getBoundingClientRect();
|
||||
const width = Math.max(0, Math.min(220, window.innerWidth - 16));
|
||||
const left = Math.min(Math.max(8, rect.left), Math.max(8, window.innerWidth - width - 8));
|
||||
setMenuStyle({
|
||||
top: `${Math.min(rect.bottom + 6, window.innerHeight - 8)}px`,
|
||||
left: `${left}px`,
|
||||
maxWidth: `${width}px`
|
||||
});
|
||||
}
|
||||
|
||||
function handlePointerDown(event) {
|
||||
if (rootRef.current?.contains(event.target) || menuRef.current?.contains(event.target)) return;
|
||||
setOpen(false);
|
||||
}
|
||||
|
||||
function handleKeyDown(event) {
|
||||
if (event.key === "Escape") setOpen(false);
|
||||
}
|
||||
|
||||
placeMenu();
|
||||
window.addEventListener("resize", placeMenu);
|
||||
window.addEventListener("scroll", placeMenu, true);
|
||||
document.addEventListener("pointerdown", handlePointerDown);
|
||||
document.addEventListener("keydown", handleKeyDown);
|
||||
return () => {
|
||||
window.removeEventListener("resize", placeMenu);
|
||||
window.removeEventListener("scroll", placeMenu, true);
|
||||
document.removeEventListener("pointerdown", handlePointerDown);
|
||||
document.removeEventListener("keydown", handleKeyDown);
|
||||
};
|
||||
}, [open]);
|
||||
|
||||
function close() {
|
||||
setOpen(false);
|
||||
}
|
||||
|
||||
function toggle() {
|
||||
setOpen((value) => !value);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={`equipment-planner-picker ${className} ${open ? "is-open" : ""}`.trim()} ref={rootRef} aria-label={label}>
|
||||
{renderTrigger({ open, toggle, close })}
|
||||
{open && createPortal(
|
||||
<div className="equipment-planner-picker-menu is-open" ref={menuRef} style={{ ...menuStyle, "--picker-columns": columns }} aria-label={label}>
|
||||
{children({ close })}
|
||||
</div>,
|
||||
document.body
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue