UNPKG

@clayui/shared

Version:
93 lines (92 loc) 3.51 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var FocusScope_exports = {}; __export(FocusScope_exports, { FocusScope: () => FocusScope }); module.exports = __toCommonJS(FocusScope_exports); var import_react = __toESM(require("react")); var import_Keys = require("./Keys"); var import_useFocusManagement = require("./useFocusManagement"); const FocusConflictContext = import_react.default.createContext(false); function FocusScope({ arrowKeysLeftRight = false, arrowKeysUpDown = true, children, onFocus, stopPropagation = true }) { const elRef = import_react.default.useRef(null); const focusManager = (0, import_useFocusManagement.useFocusManagement)(elRef); const onKeyDown = (event) => { const { key, shiftKey } = event; if (arrowKeysUpDown && key === import_Keys.Keys.Down || arrowKeysLeftRight && key === import_Keys.Keys.Right || key === import_Keys.Keys.Tab && !shiftKey) { const next = focusManager.focusNext(); if (next) { event.preventDefault(); if (onFocus) { onFocus(next); } } } else if (arrowKeysUpDown && key === import_Keys.Keys.Up || arrowKeysLeftRight && key === import_Keys.Keys.Left || key === import_Keys.Keys.Tab && shiftKey) { const previous = focusManager.focusPrevious(); if (previous) { event.preventDefault(); if (onFocus) { onFocus(previous); } } } }; const child = typeof children === "function" ? children(focusManager) : children; return /* @__PURE__ */ import_react.default.createElement(FocusConflictContext.Provider, { value: true }, import_react.default.cloneElement(child, { onKeyDown: (event) => { if (stopPropagation) { event.stopPropagation(); } if (child.props.onKeyDown) { child.props.onKeyDown(event); } onKeyDown(event); }, ref: (r) => { if (r) { elRef.current = r; const { ref } = child; if (ref) { if (typeof ref === "object") { ref.current = r; } else if (typeof ref === "function") { ref(r); } } } } })); }