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 | 12x 225x 225x 225x 225x 225x 225x 61x 61x 61x 57x 4x 4x 4x 4x 4x 4x 4x 4x 4x 61x 61x 61x 4x 4x 225x 61x 57x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 225x | import { Button } from "@wintermuted/ui-react";
import React, { useEffect, useId, useLayoutEffect, useRef, useState } from "react";
import { createPortal } from "react-dom";
import FontAwesome from "react-fontawesome";
interface Props {
buttonLabel: string;
icon: string;
activeCount?: number;
disabled?: boolean;
label: string;
children: React.ReactNode;
}
interface Position {
left: number;
top: number;
}
const viewportPadding = 8;
export default function NearbyRouteControlPopover({
buttonLabel,
icon,
activeCount,
disabled,
label,
children,
}: Props) {
const id = useId();
const buttonRef = useRef<HTMLButtonElement>(null);
const panelRef = useRef<HTMLDivElement>(null);
const [open, setOpen] = useState(false);
const [position, setPosition] = useState<Position>();
useLayoutEffect(() => {
const button = buttonRef.current;
const panel = panelRef.current;
if (!open || !button || !panel) {
return;
}
function updatePosition() {
const buttonBounds = button.getBoundingClientRect();
const panelBounds = panel.getBoundingClientRect();
const below = buttonBounds.bottom + viewportPadding;
const above = buttonBounds.top - panelBounds.height - viewportPadding;
const top =
below + panelBounds.height <= window.innerHeight - viewportPadding
? below
: Math.max(
viewportPadding,
Math.min(
above,
window.innerHeight - panelBounds.height - viewportPadding
)
);
const left = Math.max(
viewportPadding,
Math.min(
buttonBounds.right - panelBounds.width,
window.innerWidth - panelBounds.width - viewportPadding
)
);
setPosition({ left, top });
}
updatePosition();
panel
.querySelector<HTMLElement>(
"input:not([disabled]), select:not([disabled]), button:not([disabled])"
)
?.focus();
window.addEventListener("resize", updatePosition);
window.addEventListener("scroll", updatePosition, true);
return () => {
window.removeEventListener("resize", updatePosition);
window.removeEventListener("scroll", updatePosition, true);
};
}, [open]);
useEffect(() => {
if (!open) {
return;
}
function closeOnOutsidePointer(event: PointerEvent) {
closeIfOutside(event.target);
}
function closeOnOutsideClick(event: MouseEvent) {
closeIfOutside(event.target);
}
function closeIfOutside(target: EventTarget | null) {
Eif (
target instanceof Node &&
!buttonRef.current?.contains(target) &&
!panelRef.current?.contains(target)
) {
setOpen(false);
setPosition(undefined);
}
}
function closeOnEscape(event: KeyboardEvent) {
Eif (event.key === "Escape" && !event.defaultPrevented) {
setOpen(false);
setPosition(undefined);
buttonRef.current?.focus();
}
}
document.addEventListener("pointerdown", closeOnOutsidePointer);
document.addEventListener("click", closeOnOutsideClick);
document.addEventListener("keydown", closeOnEscape);
return () => {
document.removeEventListener("pointerdown", closeOnOutsidePointer);
document.removeEventListener("click", closeOnOutsideClick);
document.removeEventListener("keydown", closeOnEscape);
};
}, [open]);
function toggle() {
setOpen(!open);
setPosition(undefined);
}
return (
<>
<Button
ref={buttonRef}
type="button"
variant="secondary"
className="transit-route-control-button"
aria-label={buttonLabel}
title={buttonLabel}
aria-expanded={open}
aria-controls={id}
disabled={disabled}
onClick={toggle}
>
<FontAwesome name={icon} aria-hidden="true" />
{activeCount > 0 && (
<span className="transit-route-control-count" aria-hidden="true">
{activeCount}
</span>
)}
</Button>
{createPortal(
<div
ref={panelRef}
id={id}
className="transit-route-control-popover"
role="group"
aria-label={label}
hidden={!open}
style={position}
>
{children}
</div>,
document.body
)}
</>
);
}
|