| 
									
										
										
										
											2024-08-22 22:50:37 +02:00
										 |  |  | export function focusWithArrows(htmlElement: HTMLDivElement, classname: string) { | 
					
						
							|  |  |  |     const destroy: () => void = undefined | 
					
						
							| 
									
										
										
										
											2024-10-19 14:44:55 +02:00
										 |  |  |     htmlElement.addEventListener("keyup", (e) => { | 
					
						
							| 
									
										
										
										
											2024-08-22 22:50:37 +02:00
										 |  |  |         const currentElement = document.activeElement | 
					
						
							| 
									
										
										
										
											2024-10-19 14:44:55 +02:00
										 |  |  |         const canBeSelected = <HTMLElement[]>( | 
					
						
							|  |  |  |             Array.from(htmlElement.getElementsByClassName(classname)) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-08-22 22:50:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-10-19 14:44:55 +02:00
										 |  |  |         const i = canBeSelected.findIndex( | 
					
						
							|  |  |  |             (el) => el.contains(currentElement) || el === currentElement | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2024-08-22 22:50:37 +02:00
										 |  |  |         const l = canBeSelected.length | 
					
						
							|  |  |  |         if (e.key === "ArrowDown") { | 
					
						
							|  |  |  |             canBeSelected.at((i + 1) % l).focus() | 
					
						
							|  |  |  |             return | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (e.key === "ArrowUp") { | 
					
						
							| 
									
										
										
										
											2024-10-19 14:44:55 +02:00
										 |  |  |             canBeSelected.at(i - 1).focus() | 
					
						
							| 
									
										
										
										
											2024-08-22 22:50:37 +02:00
										 |  |  |             return | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     return { destroy } | 
					
						
							|  |  |  | } |