|
| 1 | +import { |
| 2 | + useCallback, |
| 3 | + useEffect, |
| 4 | + useId, |
| 5 | + useMemo, |
| 6 | + useRef, |
| 7 | + useState, |
| 8 | +} from 'react'; |
| 9 | +import { InputStyled, InputWrapper } from './forms/InputStyles'; |
| 10 | +import { IconButton } from './IconButton/IconButton'; |
| 11 | +import { FaChevronDown } from 'react-icons/fa6'; |
| 12 | +import { useCombobox } from 'downshift'; |
| 13 | +import { Column } from './Row'; |
| 14 | +import styled from 'styled-components'; |
| 15 | +import { QuickScore } from 'quick-score'; |
| 16 | + |
| 17 | +const supportsAnchorPositioning = |
| 18 | + 'anchorName' in document.documentElement.style; |
| 19 | + |
| 20 | +export type ComboBoxOption = { |
| 21 | + label: string; |
| 22 | + searchLabel: string; |
| 23 | + description?: string; |
| 24 | + value: string; |
| 25 | +}; |
| 26 | + |
| 27 | +type ComboBoxProps = { |
| 28 | + options: ComboBoxOption[]; |
| 29 | + selectedItem: string | undefined; |
| 30 | + onSelect: (value: string | undefined) => void; |
| 31 | +}; |
| 32 | + |
| 33 | +export const ComboBox: React.FC<ComboBoxProps> = ({ |
| 34 | + options, |
| 35 | + selectedItem, |
| 36 | + onSelect, |
| 37 | +}) => { |
| 38 | + // Use Combobox does not work with the compiler. |
| 39 | + // eslint-disable-next-line react-compiler/react-compiler |
| 40 | + 'use no memo'; |
| 41 | + const id = useId(); |
| 42 | + const anchorName = `--combo-box-${id.trim().replaceAll(':', '-')}`; |
| 43 | + const menuRef = useRef<HTMLUListElement>(null); |
| 44 | + const inputWrapperRef = useRef<HTMLDivElement>(null); |
| 45 | + const [menuAboveInput, setMenuAboveInput] = useState(false); |
| 46 | + |
| 47 | + const [items, setItems] = useState(options); |
| 48 | + |
| 49 | + const quickScore = useMemo(() => { |
| 50 | + return new QuickScore(options, ['label']); |
| 51 | + }, [options]); |
| 52 | + |
| 53 | + const { |
| 54 | + isOpen, |
| 55 | + getInputProps, |
| 56 | + getToggleButtonProps, |
| 57 | + getMenuProps, |
| 58 | + getItemProps, |
| 59 | + highlightedIndex, |
| 60 | + setHighlightedIndex, |
| 61 | + } = useCombobox({ |
| 62 | + items, |
| 63 | + onInputValueChange: ({ inputValue }) => { |
| 64 | + setHighlightedIndex(0); |
| 65 | + |
| 66 | + if (inputValue === '') { |
| 67 | + setItems(options); |
| 68 | + } |
| 69 | + |
| 70 | + setItems(quickScore.search(inputValue).map(r => r.item)); |
| 71 | + }, |
| 72 | + itemToString: item => item?.label ?? '', |
| 73 | + initialSelectedItem: options.find(option => option.value === selectedItem), |
| 74 | + onSelectedItemChange: ({ selectedItem: item }) => { |
| 75 | + onSelect(item?.value); |
| 76 | + }, |
| 77 | + }); |
| 78 | + |
| 79 | + useEffect(() => { |
| 80 | + setItems(options); |
| 81 | + }, [options]); |
| 82 | + |
| 83 | + const { ref: downShiftMenuRef, ...menuRest } = getMenuProps(); |
| 84 | + |
| 85 | + const setMenuRef = useCallback((node: HTMLUListElement) => { |
| 86 | + // @ts-expect-error - downshift types are not correct, it's a callback ref, not a ref object |
| 87 | + downShiftMenuRef(node); |
| 88 | + menuRef.current = node; |
| 89 | + }, []); |
| 90 | + |
| 91 | + const checkMenuPosition = useCallback(() => { |
| 92 | + if (!inputWrapperRef.current || !menuRef.current) return; |
| 93 | + // NOTE: For some reason firefox does not measure the position correctly, could be due to the polyfill, not fixing this now. |
| 94 | + const inputWrapperPosition = |
| 95 | + inputWrapperRef.current.getBoundingClientRect(); |
| 96 | + const menuPosition = menuRef.current.getBoundingClientRect(); |
| 97 | + setMenuAboveInput(menuPosition.top < inputWrapperPosition.top); |
| 98 | + }, []); |
| 99 | + |
| 100 | + useEffect(() => { |
| 101 | + if (!menuRef || !menuRef.current) return; |
| 102 | + |
| 103 | + if (isOpen) { |
| 104 | + menuRef.current.showPopover(); |
| 105 | + } else { |
| 106 | + menuRef.current.hidePopover(); |
| 107 | + } |
| 108 | + |
| 109 | + if (supportsAnchorPositioning) |
| 110 | + requestAnimationFrame(() => { |
| 111 | + checkMenuPosition(); |
| 112 | + }); |
| 113 | + else checkMenuPosition(); |
| 114 | + }, [isOpen]); |
| 115 | + |
| 116 | + useEffect(() => { |
| 117 | + requestAnimationFrame(() => { |
| 118 | + checkMenuPosition(); |
| 119 | + }); |
| 120 | + }, [items]); |
| 121 | + |
| 122 | + useEffect(() => { |
| 123 | + if (!menuRef.current || !inputWrapperRef.current) return; |
| 124 | + |
| 125 | + if (!supportsAnchorPositioning) { |
| 126 | + import('@oddbird/css-anchor-positioning/fn').then(module => { |
| 127 | + module.default(); |
| 128 | + }); |
| 129 | + } |
| 130 | + }, [menuRef, inputWrapperRef]); |
| 131 | + |
| 132 | + return ( |
| 133 | + <Wrapper> |
| 134 | + <StyledInputWrapper |
| 135 | + anchorName={anchorName} |
| 136 | + ref={inputWrapperRef} |
| 137 | + className={menuAboveInput ? 'menu-above-input' : ''} |
| 138 | + > |
| 139 | + <InputStyled {...getInputProps()} /> |
| 140 | + <IconButton {...getToggleButtonProps()}> |
| 141 | + <FaChevronDown /> |
| 142 | + </IconButton> |
| 143 | + </StyledInputWrapper> |
| 144 | + <List |
| 145 | + $open={isOpen} |
| 146 | + anchorName={anchorName} |
| 147 | + {...menuRest} |
| 148 | + ref={setMenuRef} |
| 149 | + popover='manual' |
| 150 | + className={menuAboveInput ? 'menu-above-input' : ''} |
| 151 | + > |
| 152 | + {isOpen && ( |
| 153 | + <> |
| 154 | + {items.map((item, index) => ( |
| 155 | + <ListItem |
| 156 | + key={item.value} |
| 157 | + data-selected={index === highlightedIndex} |
| 158 | + {...getItemProps({ item, index })} |
| 159 | + > |
| 160 | + <Column gap='0.2rem'> |
| 161 | + <span>{item.label}</span> |
| 162 | + {item.description && ( |
| 163 | + <Description>{item.description}</Description> |
| 164 | + )} |
| 165 | + </Column> |
| 166 | + </ListItem> |
| 167 | + ))} |
| 168 | + {items.length === 0 && ( |
| 169 | + <ListItem> |
| 170 | + <Description>No results</Description> |
| 171 | + </ListItem> |
| 172 | + )} |
| 173 | + </> |
| 174 | + )} |
| 175 | + </List> |
| 176 | + </Wrapper> |
| 177 | + ); |
| 178 | +}; |
| 179 | + |
| 180 | +const Wrapper = styled.div` |
| 181 | + position: relative; |
| 182 | +
|
| 183 | + &:has(li) { |
| 184 | + ${InputWrapper} { |
| 185 | + box-shadow: ${p => p.theme.boxShadowSoft}; |
| 186 | + border-radius: ${p => p.theme.radius} ${p => p.theme.radius} 0 0; |
| 187 | + border-bottom: none; |
| 188 | +
|
| 189 | + &.menu-above-input { |
| 190 | + border-radius: 0 0 ${p => p.theme.radius} ${p => p.theme.radius}; |
| 191 | + border-bottom: solid 1px ${p => p.theme.colors.main}; |
| 192 | + border-top: none; |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +`; |
| 197 | + |
| 198 | +const ListItem = styled.li` |
| 199 | + list-style: none; |
| 200 | + margin: 0; |
| 201 | + padding: ${p => p.theme.size(1)} ${p => p.theme.size(2)}; |
| 202 | + font-size: 0.9rem; |
| 203 | + &[data-selected='true'] { |
| 204 | + background-color: ${p => p.theme.colors.mainSelectedBg}; |
| 205 | + color: ${p => p.theme.colors.mainSelectedFg}; |
| 206 | + } |
| 207 | +`; |
| 208 | + |
| 209 | +const Description = styled.span` |
| 210 | + font-size: 0.8rem; |
| 211 | + color: ${p => p.theme.colors.textLight}; |
| 212 | +`; |
| 213 | + |
| 214 | +const List = styled.ul<{ $open: boolean; anchorName: string }>` |
| 215 | + max-height: ${p => p.theme.size(15)}; |
| 216 | + overflow: auto; |
| 217 | + margin: 0; |
| 218 | + box-shadow: ${p => p.theme.boxShadowSoft}; |
| 219 | + border-radius: 0 0 ${p => p.theme.radius} ${p => p.theme.radius}; |
| 220 | +
|
| 221 | + position-anchor: ${p => p.anchorName}; |
| 222 | + top: anchor(bottom); |
| 223 | + left: anchor(left); |
| 224 | + right: anchor(right); |
| 225 | + bottom: unset; |
| 226 | + width: stretch; |
| 227 | + width: -webkit-fill-available; |
| 228 | + width: -moz-available; |
| 229 | + background-color: ${p => p.theme.colors.bg}; |
| 230 | + scrollbar-color: ${p => p.theme.colors.bg2} transparent; |
| 231 | + border: solid 1px ${p => p.theme.colors.main}; |
| 232 | + border-top: none; |
| 233 | + position-try: flip-block; |
| 234 | +
|
| 235 | + &.menu-above-input { |
| 236 | + border-radius: ${p => p.theme.radius} ${p => p.theme.radius} 0 0; |
| 237 | + border-bottom: none; |
| 238 | + border-top: solid 1px ${p => p.theme.colors.main}; |
| 239 | + box-shadow: none; |
| 240 | + } |
| 241 | +`; |
| 242 | + |
| 243 | +const StyledInputWrapper = styled(InputWrapper)<{ anchorName: string }>` |
| 244 | + anchor-name: ${p => p.anchorName}; |
| 245 | +`; |
0 commit comments