Mirror: React hooks for accessible, common web interactions. UI super powers without the UI.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Fix input element typing in selection.ts

+12 -13
+12 -13
src/utils/selection.ts
··· 1 1 import { contains } from './element'; 2 2 3 3 interface RestoreInputSelection { 4 - element: HTMLElement; 4 + element: HTMLInputElement; 5 5 method: 'setSelectionRange'; 6 6 arguments: [number, number, 'forward' | 'backward' | 'none' | undefined]; 7 7 } ··· 22 22 | RestoreActiveNode 23 23 | RestoreSelectionRange; 24 24 25 - const isInputElement = (node: HTMLElement): node is HTMLInputElement => 25 + const hasSelection = (node: HTMLElement): node is HTMLInputElement => 26 26 (node.nodeName === 'INPUT' || node.nodeName === 'TEXTAREA') && 27 27 typeof (node as HTMLInputElement).selectionStart === 'number' && 28 28 typeof (node as HTMLInputElement).selectionEnd === 'number'; ··· 35 35 const element = node && target && node !== target ? node : target; 36 36 if (!element || !target) { 37 37 return null; 38 - } else if (isInputElement(target)) { 38 + } else if (hasSelection(element)) { 39 39 return { 40 40 element, 41 41 method: 'setSelectionRange', 42 42 arguments: [ 43 - target.selectionStart!, 44 - target.selectionEnd!, 45 - target.selectionDirection || undefined, 43 + element.selectionStart!, 44 + element.selectionEnd!, 45 + element.selectionDirection || undefined, 46 46 ], 47 47 }; 48 48 } ··· 60 60 61 61 /** Restores a given snapshot of a selection, falling back to a simple focus. */ 62 62 export const restoreSelection = (restore: RestoreSelection | null) => { 63 - const target = restore && restore.element; 64 - if (!restore || !target || !target.parentNode) { 63 + if (!restore || !restore.element.parentNode) { 65 64 return; 66 - } else if (restore.method === 'setSelectionRange' && isInputElement(target)) { 67 - target.focus(); 68 - target.setSelectionRange(...restore.arguments); 65 + } else if (restore.method === 'setSelectionRange') { 66 + restore.element.focus(); 67 + restore.element.setSelectionRange(...restore.arguments); 69 68 } else if (restore.method === 'range') { 70 69 const selection = window.getSelection()!; 71 - target.focus(); 70 + restore.element.focus(); 72 71 selection.removeAllRanges(); 73 72 selection.addRange(restore.range); 74 73 } else { 75 - target.focus(); 74 + restore.element.focus(); 76 75 } 77 76 };