-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
115 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...k/plugins/lens/public/editor_frame_service/editor_frame/config_panel/use_focus_update.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useEffect, useState, useCallback } from 'react'; | ||
|
||
const focusableSelector = 'button, [href], input, select, textarea, [tabindex]'; | ||
|
||
const getFirstFocusable = (el: HTMLElement | null) => { | ||
if (!el) { | ||
return null; | ||
} | ||
const firstFocusable = el.querySelector(focusableSelector); | ||
if (!firstFocusable) { | ||
return null; | ||
} | ||
return (firstFocusable as unknown) as { focus: () => void }; | ||
}; | ||
|
||
export function useFocusUpdate(ids: string[]) { | ||
const [nextFocusedId, setNextFocusedId] = useState<string | null>(null); | ||
const [refsById, setRefsById] = useState<Record<string, HTMLElement | null>>({}); | ||
|
||
useEffect(() => { | ||
const element = nextFocusedId && refsById[nextFocusedId]; | ||
if (element) { | ||
const focusable = element.matches(focusableSelector) ? element : getFirstFocusable(element); | ||
focusable?.focus(); | ||
setNextFocusedId(null); | ||
} | ||
}, [ids, refsById, nextFocusedId]); | ||
|
||
const registerNewRef = useCallback((id, el) => { | ||
if (el) { | ||
setRefsById((r) => ({ | ||
...r, | ||
[id]: el, | ||
})); | ||
} | ||
}, []); | ||
|
||
const removeRef = useCallback( | ||
(id) => { | ||
if (ids.length <= 1) { | ||
return setNextFocusedId(id); | ||
} | ||
|
||
const removedIndex = ids.findIndex((l) => l === id); | ||
const next = removedIndex === 0 ? ids[1] : ids[removedIndex - 1]; | ||
|
||
setRefsById((refs) => { | ||
const newRefsById = { ...refs }; | ||
delete newRefsById[id]; | ||
return newRefsById; | ||
}); | ||
return setNextFocusedId(next); | ||
}, | ||
[ids] | ||
); | ||
|
||
return { setNextFocusedId, removeRef, registerNewRef }; | ||
} |