UNPKG

@nextcloud/vue

Version:
1 lines 2.33 kB
{"version":3,"file":"focusTrap-HJQ4pqHV.mjs","sources":["../../src/utils/focusTrap.ts"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\n\nimport type { FocusTrap } from 'focus-trap'\n\ndeclare global {\n\tinterface Window {\n\t\t_nc_focus_trap: FocusTrap[]\n\t}\n}\n\n/**\n * Get the default global focus trap stack\n */\nexport function getTrapStack() {\n\t// Create global stack if undefined\n\twindow._nc_focus_trap ??= []\n\n\treturn window._nc_focus_trap\n}\n\n/**\n * Create controller to pause/unpause the global focus trap stack.\n *\n * When some component manually implements its own focus-trap without using global focus-trap stack,\n * it also needs to pause the global stack to avoid conflict.\n *\n * Note: if global focus-trap stack was modified outside TrapStackController (likely due to user actions),\n * trap queue should be correctly handled by focus-trap itself.\n * It is no longer safe to unpause cached `pausedStack`, as it can disrupt the current focus trap state.\n * We assume that the focus-trap stack is self-regulated and ignore unpause.\n */\nexport function createTrapStackController() {\n\t/**\n\t * Stack of focus traps that were paused to unpause the exact stack later\n\t */\n\tlet pausedStack: FocusTrap[] = []\n\n\treturn {\n\t\t/**\n\t\t * Pause the current focus-trap stack\n\t\t */\n\t\tpause() {\n\t\t\tpausedStack = [...getTrapStack()]\n\t\t\tfor (const trap of pausedStack) {\n\t\t\t\ttrap.pause()\n\t\t\t}\n\t\t},\n\t\t/**\n\t\t * Unpause the paused focus trap stack\n\t\t * If the actual stack is different from the paused one, ignore unpause.\n\t\t */\n\t\tunpause() {\n\t\t\tif (pausedStack.length === getTrapStack().length) {\n\t\t\t\tfor (const trap of pausedStack) {\n\t\t\t\t\ttrap.unpause()\n\t\t\t\t}\n\t\t\t}\n\t\t\tpausedStack = []\n\t\t},\n\t}\n}\n"],"names":[],"mappings":"AAgBO,SAAS,eAAe;AAE9B,SAAO,mBAAmB,CAAA;AAE1B,SAAO,OAAO;AACf;AAaO,SAAS,4BAA4B;AAI3C,MAAI,cAA2B,CAAA;AAE/B,SAAO;AAAA;AAAA;AAAA;AAAA,IAIN,QAAQ;AACP,oBAAc,CAAC,GAAG,cAAc;AAChC,iBAAW,QAAQ,aAAa;AAC/B,aAAK,MAAA;AAAA,MACN;AAAA,IACD;AAAA;AAAA;AAAA;AAAA;AAAA,IAKA,UAAU;AACT,UAAI,YAAY,WAAW,aAAA,EAAe,QAAQ;AACjD,mBAAW,QAAQ,aAAa;AAC/B,eAAK,QAAA;AAAA,QACN;AAAA,MACD;AACA,oBAAc,CAAA;AAAA,IACf;AAAA,EAAA;AAEF;"}