UNPKG

@carbon/react

Version:

React components for the Carbon Design System

57 lines (50 loc) 1.78 kB
/** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ 'use strict'; var keys = require('./keys.js'); var match = require('./match.js'); /** * A "ring buffer" function that takes an array and, depending on an ArrowRight * or ArrowLeft key input, loops from last index to first or first index to last. * * @param key - the left or right arrow key (KeyboardEvent, number, or string) * @param index - the current index in the array * @param arrayLength - the total length of the array * * @example * getNextIndex(keyCodes.RIGHT, 0, 4) */ const getNextIndex = (key, index, arrayLength) => { if (match.match(key, keys.ArrowRight)) { return (index + 1) % arrayLength; } if (match.match(key, keys.ArrowLeft)) { return (index + arrayLength - 1) % arrayLength; } return; }; /** * CSS selector that selects major nodes that are sequentially focusable. */ const selectorTabbable = ` a[href], area[href], input:not([disabled]):not([tabindex='-1']), button:not([disabled]):not([tabindex='-1']),select:not([disabled]):not([tabindex='-1']), textarea:not([disabled]):not([tabindex='-1']), iframe, object, embed, *[tabindex]:not([tabindex='-1']):not([disabled]), *[contenteditable=true] `; /** * CSS selector that selects major nodes that are click focusable. */ const selectorFocusable = ` a[href], area[href], input:not([disabled]), button:not([disabled]),select:not([disabled]), textarea:not([disabled]), iframe, object, embed, *[tabindex]:not([disabled]), *[contenteditable=true] `; exports.getNextIndex = getNextIndex; exports.selectorFocusable = selectorFocusable; exports.selectorTabbable = selectorTabbable;