import { matches } from './dom' ; export const FOCUSABLE_SELECTOR = 'a,frame,iframe,input:not([type=hidden]):not(:disabled),select:not(:disabled),textarea:not(:disabled),button:not(:disabled),*[tabindex]' ; export function findFirstFocusableNode ( element , onlyDescendants = true ) { if ( ! onlyDescendants && matches ( element , FOCUSABLE_SELECTOR ) ) { return element ; } return element . querySelector ( FOCUSABLE_SELECTOR ) ; } export function focusFirstFocusableNode ( element , onlyDescendants = true ) { const firstFocusable = findFirstFocusableNode ( element , onlyDescendants ) ; if ( firstFocusable ) { firstFocusable . focus ( ) ; } } export function findLastFocusableNode ( element , onlyDescendants = true ) { if ( ! onlyDescendants && matches ( element , FOCUSABLE_SELECTOR ) ) { return element ; } const allFocusable = element . querySelectorAll ( FOCUSABLE_SELECTOR ) ; return allFocusable [ allFocusable . length - 1 ] as HTMLElement | null ; } export function focusLastFocusableNode ( element , onlyDescendants = true ) { const lastFocusable = findLastFocusableNode ( element , onlyDescendants ) ; if ( lastFocusable ) { lastFocusable . focus ( ) ; } }