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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 9x 2x 2x 30x 30x 30x 15x 14x 15x 1x 1x 30x 30x 30x 30x 1x 1x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 1x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 9x 30x 30x 1x 1x 1x 1x 1x 1x 1x 1x 1x 30x 30x 30x 166x 83x 83x 83x 83x 83x 83x 83x 83x 166x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 120x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 95x 30x 30x 30x 30x 30x 30x 30x 1x | import { useEffect, useRef, useState } from 'react';
import { c } from '../../../helpers';
import {
EventTargetWithDatasetIndex,
SelectOptionsListProps,
} from './SelectOptionsListProps';
import styles from './SelectOptionsList.module.css';
const MAX_VISIBLE_ITEMS = 8;
const LIST_VERTICAL_MARGIN = 2;
const usePersistLastScroll = ({
visible,
selectedIndex,
}: {
visible?: boolean;
selectedIndex?: number;
}): { containerRef: React.RefObject<HTMLDivElement> } => {
const containerRef = useRef<HTMLDivElement>(null);
const [lastScroll, setLastScroll] = useState<number>(0);
useEffect(() => {
if (visible && containerRef.current) {
containerRef.current.scrollTop = lastScroll;
}
}, [visible, lastScroll]);
useEffect(() => {
if (!selectedIndex) {
setLastScroll(0);
} else {
setLastScroll(containerRef?.current?.scrollTop ?? 0);
}
}, [selectedIndex]);
return { containerRef };
};
const getVisibilityStyles = ({
visible,
triggerDOMRect,
itemHeight,
}: {
visible?: boolean;
triggerDOMRect?: DOMRect;
itemHeight?: number;
}): {
left: number;
width: number;
top: number;
maxHeight: number;
} => {
if (visible && triggerDOMRect && itemHeight) {
const { top, height, width, left } = triggerDOMRect;
const elementTop = top + height + LIST_VERTICAL_MARGIN;
const maxMaxHeight = itemHeight * MAX_VISIBLE_ITEMS;
let maxHeight = window.innerHeight - elementTop - LIST_VERTICAL_MARGIN * 2;
if (maxHeight > maxMaxHeight) {
maxHeight = maxMaxHeight;
}
return {
left,
width,
top: elementTop,
maxHeight,
};
}
return {
left: 0,
width: 0,
top: 0,
maxHeight: 0,
};
};
function SelectOptionsList<T>({
noResultsElement,
onChange,
options = [],
selectedIndex,
focusedIndex,
setFocusedItemIndex,
triggerDOMRect,
visible,
}: SelectOptionsListProps<T>): JSX.Element {
const { containerRef } = usePersistLastScroll({ visible, selectedIndex });
const handleItemClick = (
event: React.MouseEvent<HTMLAnchorElement>
): void => {
event.preventDefault();
const element = event.target as EventTargetWithDatasetIndex;
const index = Number(element.dataset?.index);
if (!isNaN(index)) {
onChange && onChange(index);
}
};
const [mouseEnterItemIndex, setMouseEnterItemIndex] = useState<number>();
useEffect(() => {
setMouseEnterItemIndex(undefined);
}, [visible]);
const handleItemMouseEnter = (
event: React.MouseEvent<HTMLAnchorElement>
): void => {
const element = event.target as EventTargetWithDatasetIndex;
const index = Number(element.dataset?.index);
if (!isNaN(index)) {
setFocusedItemIndex(index);
setMouseEnterItemIndex(index);
}
};
const [itemHeight, setItemHeight] = useState<number>();
const handleItemRef = (current: HTMLLIElement): void => {
if (current) {
const itemRectHeight = current.getBoundingClientRect().height;
if (itemRectHeight > (itemHeight ?? 0)) {
setItemHeight(itemRectHeight);
}
const index = Number(current.dataset.index);
const mustScrollIntoView =
index === focusedIndex && mouseEnterItemIndex !== focusedIndex;
if (mustScrollIntoView) {
current.scrollIntoView({ block: 'nearest' });
}
}
};
const visibilityStyles = getVisibilityStyles({
visible,
triggerDOMRect,
itemHeight,
});
const classNames = [
styles.select_list,
visible ? styles.visible : styles.hidden,
];
const hasSomeVisibleOption = options.some(({ visible }) => visible !== false);
return (
<div ref={containerRef} className={c(classNames)} style={visibilityStyles}>
<ul>
{hasSomeVisibleOption ? (
options.map(
(option, index) =>
option.visible !== false && (
<li
className={c(
selectedIndex === index ? styles.selected : '',
focusedIndex === index ? styles.focused : ''
)}
data-index={index}
key={index}
ref={handleItemRef}
>
<a
href="#"
title={option.label}
data-index={index}
onClick={handleItemClick}
onMouseEnter={handleItemMouseEnter}
>
{option.labelElement ?? option.label}
</a>
</li>
)
)
) : (
<li>{noResultsElement || <span>...</span>}</li>
)}
</ul>
</div>
);
}
export default SelectOptionsList;
|