Skip to content

Commit

Permalink
Move function to common utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
MisRob committed Feb 24, 2023
1 parent 7434f74 commit 577f9a5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
3 changes: 2 additions & 1 deletion lib/tabs/KTabsPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

<script>
import { getTabElementId, getTabPanelElementId, getFirstFocusableChild } from './utils';
import { getFirstFocusableChild } from '../utils/focusManagement';
import { getTabElementId, getTabPanelElementId } from './utils';
/**
* A part of a tabbed interface that displays
Expand Down
17 changes: 0 additions & 17 deletions lib/tabs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,3 @@ export function getTabElementId(tabInterfaceId, tabId) {
export function getTabPanelElementId(tabInterfaceId, tabId) {
return getTabElementId(tabInterfaceId, tabId) + '-panel';
}

/**
* @param {HTMLElement} parent A parent element
* @returns {HTMLElement, null} The first focusable child of the parent
* element or `null` when none is found
*/
export function getFirstFocusableChild(parent) {
if (!parent) {
console.error(`[getFirstFocusableChild] 'parent' parameter is required`);
return null;
}
// https://gomakethings.com/how-to-get-the-first-and-last-focusable-elements-in-the-dom/
const focusableElements = parent.querySelectorAll(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
);
return focusableElements.length > 0 ? focusableElements[0] : null;
}
16 changes: 16 additions & 0 deletions lib/utils/focusManagement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @param {HTMLElement} parent A parent element
* @returns {HTMLElement, null} The first focusable child of the parent
* element or `null` when none is found
*/
export function getFirstFocusableChild(parent) {
if (!parent) {
console.error(`[getFirstFocusableChild] 'parent' parameter is required`);
return null;
}
// https://gomakethings.com/how-to-get-the-first-and-last-focusable-elements-in-the-dom/
const focusableElements = parent.querySelectorAll(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
);
return focusableElements.length > 0 ? focusableElements[0] : null;
}

0 comments on commit 577f9a5

Please sign in to comment.