UNPKG

@workday/canvas-kit-react

Version:

The parent module that contains all Workday Canvas Kit React components

32 lines (31 loc) 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.changeFocus = void 0; /** * Programmatically change focus in a way that causes a focus ring around the new element. * @param element Element that should be focused */ const changeFocus = (element) => { // Normally the browser will ignore calls to `focus` on an element that already has focus, but we // don't want to accidentally show the focus ring in this case, so we'll detect and bail early. // We'll also bail if there is no element if (document.activeElement === element || !element) { return; } if (hasCallableProperty(element, 'focus')) { // Dispatch an unidentified keyboard event for an input provider prior to a focus change so that // the ring will appear. if (typeof KeyboardEvent === 'function' && hasCallableProperty(element, 'dispatchEvent')) { const event = new KeyboardEvent('keydown', { bubbles: true, key: 'Unidentified' }); element.dispatchEvent(event); } element.focus(); } }; exports.changeFocus = changeFocus; function hasCallableProperty(input, method) { return (!!input && typeof input === 'object' && method in input && typeof input[method] === 'function'); }