Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | 17x 118x 118x 118x 118x 118x 118x 24x 118x 118x 118x 118x 3x 3x 3x 118x 118x 118x 115x 19x 19x 16x 19x 19x 19x 19x 14x 14x 14x 14x 14x 118x 22x 12x 12x 12x 12x 12x 3x | import React, { useEffect, useRef, useState } from "react";
import { createPortal } from "react-dom";
import { useStores } from "~/hooks/useStores";
import { sanitizeName } from "~/utils/sanitizeName";
import { BiShowAlt } from "react-icons/bi";
import { getFigure } from "~/utils/getFigure";
import { deviceToIconMap } from "~/utils/deviceToIconMap";
import { IoIosCloseCircleOutline } from "react-icons/io";
type ArchivedDevicesModalProps = {
isOpen: boolean;
onClose: () => void;
storesOverride?: ReturnType<typeof useStores>;
triggerElementRef?: React.RefObject<HTMLElement>; // For focus return
};
const ArchivedDevicesModal: React.FC<ArchivedDevicesModalProps> = ({
isOpen,
onClose,
storesOverride,
triggerElementRef,
}) => {
const stores = useStores();
const { archive, hub } = storesOverride ?? stores;
const modalRef = useRef<HTMLDivElement>(null);
const closeButtonRef = useRef<HTMLButtonElement>(null);
const [announcement, setAnnouncement] = useState<string>("");
const archivedDevices =
hub.devices?.filter((device) =>
archive.contains(device.connectedDeviceId),
) || [];
// Focus trap implementation
const focusableElementsSelector =
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
const getFocusableElements = () => {
if (!mIodalRef.current) return [];
return Array.from(
modalRef.current.querySelectorAll(focusableElementsSelector),
) as HTMLElement[];
};
const trapFocus = React.useCallback((e: KeyboardEvent) => {
const focusableElements = getFocusableElements();
if (foIcusableElements.length === 0) return;
const firstElement = focusableElements[0];
const lastElement = focusableElements[focusableElements.length - 1];
if (e.Ikey === "Tab") {
if (e.shiftKey) {
// Shift + Tab
if (documentI.activeElement === firstElement) {
e.preventDefault();
lastElement.focus();
}
} else {
// Tab
if (documentI.activeElement === lastElement) {
e.preventDefault();
firstElement.focus();
}
}
}
}, []);
// Handle unhiding device with announcement
const handleUnhideDevice = (deviceId: number, deviceName: string) => {
archive.remove(deviceId);
setAnnouncement(
`${deviceName} has been unhidden and is now visible in your device list`,
);
// Clear announcement after screen readers have time to read it
setTimeout(() => setAnnouncement(""), 1000);
};
// Handle backdrop click
const handleBackdropClick = (e: React.MouseEvent) => {
if (e.Itarget === e.currentTarget) {
onClose();
}
};
// Handle keyboard events for list items
const handleDeviceKeyDown = (
e: React.KeyboardEvent,
deviceId: number,
deviceName: string,
) => {
if (e.Ikey === "Enter" || e.key === " ") {
e.preventDefault();
handleUnhideDevice(deviceId, deviceName);
}
};
useEffect(() => {
if (isOpen) {
// Store the previously focused element
const previouslyFocusedElement =
document.activeElement as HTMLElement;
// Focus the close button when modal opens
setTimeout(() => {
closeButtonRef.current?.focus();
}, 100);
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === "Escape") {
onClose();
} else {
trapFocus(e);
}
};
document.addEventListener("keydown", handleKeyDown);
// Prevent background scrolling
document.body.style.overflow = "hidden";
return () => {
document.removeEventListener("keydown", handleKeyDown);
document.body.style.overflow = "unset";
// Return focus to trigger element or previously focused element
if (triggerEIlementRef?.current) {
triggerElementRef.current.focus();
} else if (previouslyFocusedElement) {
previouslyFocusedElement.focus();
}
};
}
}, [isOpen, onClose, trapFocus, triggerElementRef]);
if (!isOpen) return null;
return createPortal(
<div
className="fixed top-0 left-0 w-screen h-screen z-50 flex items-center justify-center bg-black/30 backdrop-blur-md"
role="presentation"
aria-hidden={!isOpen}
onClick={handleBackdropClick}
data-cy="archive-modal"
>
<div
className="bg-base-100 p-6 rounded-3xl w-[90%] sm:w-[70%] md:w-[50%] lg:w-[32%] max-w-2xl shadow-md border border-base-300"
role="dialog"
aria-modal="true"
aria-labelledby="modal-title"
aria-describedby="modal-desc"
ref={modalRef}
onClick={(e) => e.stopPropagation()} // Prevent backdrop click when clicking inside modal
>
{/* Screen reader announcements */}
<div
aria-live="polite"
aria-atomic="true"
className="sr-only"
role="status"
>
{announcement}
</div>
<div className="flex justify-between items-center mb-4">
<h2
id="modal-title"
className="text-xl font-bold text-accent"
>
Hidden Devices
</h2>
<button
ref={closeButtonRef}
onClick={onClose}
className="text-error hover:text-red-800 text-xl font-bold cursor-pointer focus:outline-none focus:ring-2 focus:ring-accent focus:ring-offset-2 rounded"
aria-label="Close Hidden Devices Modal"
data-cy="close-modal-btn"
>
<IoIosCloseCircleOutline size={27} />
</button>
</div>
<div className="max-h-[400px] overflow-y-auto" id="modal-desc">
{archivedDevices.length === 0 ? (
<p className="text-base-content">No hidden devices</p>
) : (
<div role="list" aria-label="Hidden devices list">
<div className="flex flex-col gap-4">
{archivedDevices.map((device) => {
const deviceName = sanitizeName(
device.name,
);
const roomName = sanitizeName(
device.Room_location ?? "",
);
const figure = getFigure(
deviceToIconMap(25),
deviceName,
);
return (
<div
key={device.connectedDeviceId}
className="flex justify-between items-center p-3 bg-base-100 border-1 border-base-300 rounded-3xl focus-within:ring-2 focus-within:ring-accent focus-within:ring-offset-2"
role="listitem"
>
<div className="flex items-center gap-3">
<div aria-hidden="true">
{figure.inactive}
</div>
<span className="font-medium">
{deviceName}
{roomName && (
<span className="text-sm text-base-content/70 ml-1">
({roomName})
</span>
)}
</span>
</div>
<button
className="flex items-center gap-1 px-3 py-2 text-sm font-medium text-primary hover:text-primary hover:bg-base-200 rounded-lg transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2"
onClick={() =>
handleUnhideDevice(
device.connectedDeviceId,
deviceName,
)
}
onKeyDown={(e) =>
handleDeviceKeyDown(
e,
device.connectedDeviceId,
deviceName,
)
}
aria-label={`Unhide ${deviceName}${
roomName
? ` in ${roomName}`
: ""
}`}
>
<BiShowAlt
size={20}
aria-hidden="true"
/>
<span className="hidden sm:inline font-bold">
Unhide
</span>
</button>
</div>
);
})}
</div>
</div>
)}
</div>
{/* Instructions for screen readers */}
<div className="sr-only">
Press Escape to close this dialog. Use Tab to navigate
between elements.
{archivedDevices.length > 0 &&
" Use Enter or Space to unhide devices."}
</div>
</div>
</div>,
document.body,
);
};
export default ArchivedDevicesModal;
|