Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
fix(ui): Focus first text input by default#4134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
1524ec43630d04e4c05b89261f15faf40daFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -10,6 +10,7 @@ import { | ||
| Badge, | ||
| Button, | ||
| Combobox, | ||
| focusFirstTextInputIn, | ||
| Input, | ||
| Label, | ||
| Modal, | ||
| @@ -84,6 +85,15 @@ export function IntegrationsManager() { | ||
| const [selectedDescriptionDraft, setSelectedDescriptionDraft] = useState('') | ||
| const [selectedDisplayNameDraft, setSelectedDisplayNameDraft] = useState('') | ||
| const [createStep, setCreateStep] = useState<1 | 2>(1) | ||
| const createModalContentRef = useRef<HTMLDivElement>(null) | ||
| useEffect(() => { | ||
| if (!showCreateModal || createStep !== 2) return | ||
| const id = window.setTimeout(() => { | ||
| focusFirstTextInputIn(createModalContentRef.current) | ||
| }, 0) | ||
| return () => window.clearTimeout(id) | ||
| }, [showCreateModal, createStep]) | ||
| const [serviceSearch, setServiceSearch] = useState('') | ||
TheodoreSpeaks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const [copyIdSuccess, setCopyIdSuccess] = useState(false) | ||
| const [credentialToDelete, setCredentialToDelete] = useState<WorkspaceCredential | null>(null) | ||
| @@ -769,7 +779,7 @@ export function IntegrationsManager() { | ||
| if (!open) resetCreateForm() | ||
| }} | ||
| > | ||
| <ModalContent size='md'> | ||
| <ModalContent size='md' ref={createModalContentRef}> | ||
| {createStep === 1 ? ( | ||
| <> | ||
| <ModalHeader>Connect Integration</ModalHeader> | ||
| @@ -785,7 +795,6 @@ export function IntegrationsManager() { | ||
| value={serviceSearch} | ||
| onChange={(e) => setServiceSearch(e.target.value)} | ||
| className='h-auto flex-1 border-0 bg-transparent p-0 font-base leading-none placeholder:text-[var(--text-tertiary)] focus-visible:ring-0 focus-visible:ring-offset-0' | ||
| autoFocus | ||
| /> | ||
| </div> | ||
| <div className='flex max-h-[320px] flex-col overflow-y-auto'> | ||
| @@ -916,7 +925,6 @@ export function IntegrationsManager() { | ||
| autoComplete='off' | ||
| data-lpignore='true' | ||
| className='mt-1.5' | ||
| autoFocus | ||
TheodoreSpeaks marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| /> | ||
| </div> | ||
| <div> | ||
| @@ -1053,7 +1061,6 @@ export function IntegrationsManager() { | ||
| 'min-h-[120px] resize-none border-0 font-mono text-[12px]', | ||
| saDragActive && 'opacity-30' | ||
| )} | ||
| autoFocus | ||
| /> | ||
| </div> | ||
| <div className='mt-1.5'> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /** | ||
| * Default `onOpenAutoFocus` handler for emcn modals. | ||
| * | ||
| * Radix's native behavior focuses the first focusable descendant — usually the close | ||
| * button in `ModalHeader`. We instead focus the first visible text-entry control | ||
| * (input/textarea/contenteditable) inside the dialog, with the caret at the end. | ||
| * | ||
| * If no such control exists, we let Radix's default behavior run by not calling | ||
| * `preventDefault()`. | ||
| */ | ||
| const TEXT_INPUT_SELECTOR = [ | ||
| 'input:not([type="hidden"]):not([type="checkbox"]):not([type="radio"])' + | ||
| ':not([type="button"]):not([type="submit"]):not([type="reset"])' + | ||
| ':not([disabled]):not([readonly]):not([tabindex="-1"])', | ||
| 'textarea:not([disabled]):not([readonly]):not([tabindex="-1"])', | ||
| '[contenteditable="true"]:not([tabindex="-1"])', | ||
| '[contenteditable=""]:not([tabindex="-1"])', | ||
| ].join(',') | ||
| function isVisible(el: HTMLElement): boolean { | ||
| return el.offsetParent !== null || el.getClientRects().length > 0 | ||
| } | ||
| /** | ||
| * Focus the first visible text-entry input within `root`, placing caret at end. | ||
| * Returns true if an element was focused. | ||
| */ | ||
| export function focusFirstTextInputIn(root: HTMLElement | null): boolean { | ||
| if (!root) return false | ||
| const target = Array.from(root.querySelectorAll<HTMLElement>(TEXT_INPUT_SELECTOR)).find(isVisible) | ||
| if (!target) return false | ||
| target.focus({ preventScroll: false }) | ||
| if (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement) { | ||
| const end = target.value.length | ||
| try { | ||
| target.setSelectionRange(end, end) | ||
| } catch { | ||
| // Some input types (number, email, etc.) reject setSelectionRange — ignore. | ||
| } | ||
| } else if (target.isContentEditable) { | ||
| const range = document.createRange() | ||
| range.selectNodeContents(target) | ||
| range.collapse(false) | ||
| const sel = window.getSelection() | ||
| sel?.removeAllRanges() | ||
| sel?.addRange(range) | ||
| } | ||
| return true | ||
| } | ||
| export function focusFirstTextInput(event: Event): void { | ||
| if (focusFirstTextInputIn(event.currentTarget as HTMLElement | null)) { | ||
| event.preventDefault() | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.