@octanejs/aria 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (500) hide show
  1. package/package.json +6 -3
  2. package/src/breadcrumbs/useBreadcrumbItem.ts +95 -0
  3. package/src/breadcrumbs/useBreadcrumbs.ts +51 -0
  4. package/src/button/useButton.ts +273 -0
  5. package/src/button/useToggleButton.ts +131 -0
  6. package/src/button/useToggleButtonGroup.ts +131 -0
  7. package/src/checkbox/useCheckbox.ts +114 -0
  8. package/src/checkbox/useCheckboxGroup.ts +107 -0
  9. package/src/checkbox/useCheckboxGroupItem.ts +132 -0
  10. package/src/checkbox/utils.ts +15 -0
  11. package/src/combobox/useComboBox.ts +602 -0
  12. package/src/dialog/useDialog.ts +147 -0
  13. package/src/disclosure/useDisclosure.ts +180 -0
  14. package/src/focus/FocusRing.ts +69 -0
  15. package/src/focus/FocusScope.ts +1205 -0
  16. package/src/focus/useFocusRing.ts +125 -0
  17. package/src/focus/useHasTabbableChild.ts +82 -0
  18. package/src/focus/virtualFocus.ts +38 -0
  19. package/src/form/useFormValidation.ts +205 -0
  20. package/src/grid/useGridSelectionAnnouncement.ts +177 -0
  21. package/src/grid/useGridSelectionCheckbox.ts +86 -0
  22. package/src/grid/useHighlightSelectionDescription.ts +81 -0
  23. package/src/gridlist/useGridList.ts +236 -0
  24. package/src/gridlist/useGridListItem.ts +512 -0
  25. package/src/gridlist/useGridListSection.ts +78 -0
  26. package/src/gridlist/useGridListSelectionCheckbox.ts +47 -0
  27. package/src/gridlist/utils.ts +36 -0
  28. package/src/i18n/I18nProvider.ts +97 -0
  29. package/src/i18n/server.ts +115 -0
  30. package/src/i18n/useCollator.ts +42 -0
  31. package/src/i18n/useDateFormatter.ts +64 -0
  32. package/src/i18n/useDefaultLocale.ts +93 -0
  33. package/src/i18n/useFilter.ts +107 -0
  34. package/src/i18n/useListFormatter.ts +33 -0
  35. package/src/i18n/useLocalizedStringFormatter.ts +90 -0
  36. package/src/i18n/useNumberFormatter.ts +33 -0
  37. package/src/i18n/utils.ts +67 -0
  38. package/src/index.ts +268 -0
  39. package/src/intl/breadcrumbs/ar-AE.json +3 -0
  40. package/src/intl/breadcrumbs/bg-BG.json +3 -0
  41. package/src/intl/breadcrumbs/cs-CZ.json +3 -0
  42. package/src/intl/breadcrumbs/da-DK.json +3 -0
  43. package/src/intl/breadcrumbs/de-DE.json +3 -0
  44. package/src/intl/breadcrumbs/el-GR.json +3 -0
  45. package/src/intl/breadcrumbs/en-US.json +3 -0
  46. package/src/intl/breadcrumbs/es-ES.json +3 -0
  47. package/src/intl/breadcrumbs/et-EE.json +3 -0
  48. package/src/intl/breadcrumbs/fi-FI.json +3 -0
  49. package/src/intl/breadcrumbs/fr-FR.json +3 -0
  50. package/src/intl/breadcrumbs/he-IL.json +3 -0
  51. package/src/intl/breadcrumbs/hr-HR.json +3 -0
  52. package/src/intl/breadcrumbs/hu-HU.json +3 -0
  53. package/src/intl/breadcrumbs/index.ts +75 -0
  54. package/src/intl/breadcrumbs/it-IT.json +3 -0
  55. package/src/intl/breadcrumbs/ja-JP.json +3 -0
  56. package/src/intl/breadcrumbs/ko-KR.json +3 -0
  57. package/src/intl/breadcrumbs/lt-LT.json +3 -0
  58. package/src/intl/breadcrumbs/lv-LV.json +3 -0
  59. package/src/intl/breadcrumbs/nb-NO.json +3 -0
  60. package/src/intl/breadcrumbs/nl-NL.json +3 -0
  61. package/src/intl/breadcrumbs/pl-PL.json +3 -0
  62. package/src/intl/breadcrumbs/pt-BR.json +3 -0
  63. package/src/intl/breadcrumbs/pt-PT.json +3 -0
  64. package/src/intl/breadcrumbs/ro-RO.json +3 -0
  65. package/src/intl/breadcrumbs/ru-RU.json +3 -0
  66. package/src/intl/breadcrumbs/sk-SK.json +3 -0
  67. package/src/intl/breadcrumbs/sl-SI.json +3 -0
  68. package/src/intl/breadcrumbs/sr-SP.json +3 -0
  69. package/src/intl/breadcrumbs/sv-SE.json +3 -0
  70. package/src/intl/breadcrumbs/tr-TR.json +3 -0
  71. package/src/intl/breadcrumbs/uk-UA.json +3 -0
  72. package/src/intl/breadcrumbs/zh-CN.json +3 -0
  73. package/src/intl/breadcrumbs/zh-TW.json +3 -0
  74. package/src/intl/combobox/ar-AE.json +7 -0
  75. package/src/intl/combobox/bg-BG.json +7 -0
  76. package/src/intl/combobox/cs-CZ.json +7 -0
  77. package/src/intl/combobox/da-DK.json +7 -0
  78. package/src/intl/combobox/de-DE.json +7 -0
  79. package/src/intl/combobox/el-GR.json +7 -0
  80. package/src/intl/combobox/en-US.json +7 -0
  81. package/src/intl/combobox/es-ES.json +7 -0
  82. package/src/intl/combobox/et-EE.json +7 -0
  83. package/src/intl/combobox/fi-FI.json +7 -0
  84. package/src/intl/combobox/fr-FR.json +7 -0
  85. package/src/intl/combobox/he-IL.json +7 -0
  86. package/src/intl/combobox/hr-HR.json +7 -0
  87. package/src/intl/combobox/hu-HU.json +7 -0
  88. package/src/intl/combobox/index.ts +75 -0
  89. package/src/intl/combobox/it-IT.json +7 -0
  90. package/src/intl/combobox/ja-JP.json +7 -0
  91. package/src/intl/combobox/ko-KR.json +7 -0
  92. package/src/intl/combobox/lt-LT.json +7 -0
  93. package/src/intl/combobox/lv-LV.json +7 -0
  94. package/src/intl/combobox/nb-NO.json +7 -0
  95. package/src/intl/combobox/nl-NL.json +7 -0
  96. package/src/intl/combobox/pl-PL.json +7 -0
  97. package/src/intl/combobox/pt-BR.json +7 -0
  98. package/src/intl/combobox/pt-PT.json +7 -0
  99. package/src/intl/combobox/ro-RO.json +7 -0
  100. package/src/intl/combobox/ru-RU.json +7 -0
  101. package/src/intl/combobox/sk-SK.json +7 -0
  102. package/src/intl/combobox/sl-SI.json +7 -0
  103. package/src/intl/combobox/sr-SP.json +7 -0
  104. package/src/intl/combobox/sv-SE.json +7 -0
  105. package/src/intl/combobox/tr-TR.json +7 -0
  106. package/src/intl/combobox/uk-UA.json +7 -0
  107. package/src/intl/combobox/zh-CN.json +7 -0
  108. package/src/intl/combobox/zh-TW.json +7 -0
  109. package/src/intl/grid/ar-AE.json +8 -0
  110. package/src/intl/grid/bg-BG.json +8 -0
  111. package/src/intl/grid/cs-CZ.json +8 -0
  112. package/src/intl/grid/da-DK.json +8 -0
  113. package/src/intl/grid/de-DE.json +8 -0
  114. package/src/intl/grid/el-GR.json +8 -0
  115. package/src/intl/grid/en-US.json +8 -0
  116. package/src/intl/grid/es-ES.json +8 -0
  117. package/src/intl/grid/et-EE.json +8 -0
  118. package/src/intl/grid/fi-FI.json +8 -0
  119. package/src/intl/grid/fr-FR.json +8 -0
  120. package/src/intl/grid/he-IL.json +8 -0
  121. package/src/intl/grid/hr-HR.json +8 -0
  122. package/src/intl/grid/hu-HU.json +8 -0
  123. package/src/intl/grid/index.ts +75 -0
  124. package/src/intl/grid/it-IT.json +8 -0
  125. package/src/intl/grid/ja-JP.json +8 -0
  126. package/src/intl/grid/ko-KR.json +8 -0
  127. package/src/intl/grid/lt-LT.json +8 -0
  128. package/src/intl/grid/lv-LV.json +8 -0
  129. package/src/intl/grid/nb-NO.json +8 -0
  130. package/src/intl/grid/nl-NL.json +8 -0
  131. package/src/intl/grid/pl-PL.json +8 -0
  132. package/src/intl/grid/pt-BR.json +8 -0
  133. package/src/intl/grid/pt-PT.json +8 -0
  134. package/src/intl/grid/ro-RO.json +8 -0
  135. package/src/intl/grid/ru-RU.json +8 -0
  136. package/src/intl/grid/sk-SK.json +8 -0
  137. package/src/intl/grid/sl-SI.json +8 -0
  138. package/src/intl/grid/sr-SP.json +8 -0
  139. package/src/intl/grid/sv-SE.json +8 -0
  140. package/src/intl/grid/tr-TR.json +8 -0
  141. package/src/intl/grid/uk-UA.json +8 -0
  142. package/src/intl/grid/zh-CN.json +8 -0
  143. package/src/intl/grid/zh-TW.json +8 -0
  144. package/src/intl/gridlist/ar-AE.json +4 -0
  145. package/src/intl/gridlist/bg-BG.json +4 -0
  146. package/src/intl/gridlist/cs-CZ.json +4 -0
  147. package/src/intl/gridlist/da-DK.json +4 -0
  148. package/src/intl/gridlist/de-DE.json +4 -0
  149. package/src/intl/gridlist/el-GR.json +4 -0
  150. package/src/intl/gridlist/en-US.json +4 -0
  151. package/src/intl/gridlist/es-ES.json +4 -0
  152. package/src/intl/gridlist/et-EE.json +4 -0
  153. package/src/intl/gridlist/fi-FI.json +4 -0
  154. package/src/intl/gridlist/fr-FR.json +4 -0
  155. package/src/intl/gridlist/he-IL.json +4 -0
  156. package/src/intl/gridlist/hr-HR.json +4 -0
  157. package/src/intl/gridlist/hu-HU.json +4 -0
  158. package/src/intl/gridlist/index.ts +75 -0
  159. package/src/intl/gridlist/it-IT.json +4 -0
  160. package/src/intl/gridlist/ja-JP.json +4 -0
  161. package/src/intl/gridlist/ko-KR.json +4 -0
  162. package/src/intl/gridlist/lt-LT.json +4 -0
  163. package/src/intl/gridlist/lv-LV.json +4 -0
  164. package/src/intl/gridlist/nb-NO.json +4 -0
  165. package/src/intl/gridlist/nl-NL.json +4 -0
  166. package/src/intl/gridlist/pl-PL.json +4 -0
  167. package/src/intl/gridlist/pt-BR.json +4 -0
  168. package/src/intl/gridlist/pt-PT.json +4 -0
  169. package/src/intl/gridlist/ro-RO.json +4 -0
  170. package/src/intl/gridlist/ru-RU.json +4 -0
  171. package/src/intl/gridlist/sk-SK.json +4 -0
  172. package/src/intl/gridlist/sl-SI.json +4 -0
  173. package/src/intl/gridlist/sr-SP.json +4 -0
  174. package/src/intl/gridlist/sv-SE.json +4 -0
  175. package/src/intl/gridlist/tr-TR.json +4 -0
  176. package/src/intl/gridlist/uk-UA.json +4 -0
  177. package/src/intl/gridlist/zh-CN.json +4 -0
  178. package/src/intl/gridlist/zh-TW.json +4 -0
  179. package/src/intl/menu/ar-AE.json +3 -0
  180. package/src/intl/menu/bg-BG.json +3 -0
  181. package/src/intl/menu/cs-CZ.json +3 -0
  182. package/src/intl/menu/da-DK.json +3 -0
  183. package/src/intl/menu/de-DE.json +3 -0
  184. package/src/intl/menu/el-GR.json +3 -0
  185. package/src/intl/menu/en-US.json +3 -0
  186. package/src/intl/menu/es-ES.json +3 -0
  187. package/src/intl/menu/et-EE.json +3 -0
  188. package/src/intl/menu/fi-FI.json +3 -0
  189. package/src/intl/menu/fr-FR.json +3 -0
  190. package/src/intl/menu/he-IL.json +3 -0
  191. package/src/intl/menu/hr-HR.json +3 -0
  192. package/src/intl/menu/hu-HU.json +3 -0
  193. package/src/intl/menu/index.ts +75 -0
  194. package/src/intl/menu/it-IT.json +3 -0
  195. package/src/intl/menu/ja-JP.json +3 -0
  196. package/src/intl/menu/ko-KR.json +3 -0
  197. package/src/intl/menu/lt-LT.json +3 -0
  198. package/src/intl/menu/lv-LV.json +3 -0
  199. package/src/intl/menu/nb-NO.json +3 -0
  200. package/src/intl/menu/nl-NL.json +3 -0
  201. package/src/intl/menu/pl-PL.json +3 -0
  202. package/src/intl/menu/pt-BR.json +3 -0
  203. package/src/intl/menu/pt-PT.json +3 -0
  204. package/src/intl/menu/ro-RO.json +3 -0
  205. package/src/intl/menu/ru-RU.json +3 -0
  206. package/src/intl/menu/sk-SK.json +3 -0
  207. package/src/intl/menu/sl-SI.json +3 -0
  208. package/src/intl/menu/sr-SP.json +3 -0
  209. package/src/intl/menu/sv-SE.json +3 -0
  210. package/src/intl/menu/tr-TR.json +3 -0
  211. package/src/intl/menu/uk-UA.json +3 -0
  212. package/src/intl/menu/zh-CN.json +3 -0
  213. package/src/intl/menu/zh-TW.json +3 -0
  214. package/src/intl/numberfield/ar-AE.json +5 -0
  215. package/src/intl/numberfield/bg-BG.json +5 -0
  216. package/src/intl/numberfield/cs-CZ.json +5 -0
  217. package/src/intl/numberfield/da-DK.json +5 -0
  218. package/src/intl/numberfield/de-DE.json +5 -0
  219. package/src/intl/numberfield/el-GR.json +5 -0
  220. package/src/intl/numberfield/en-US.json +5 -0
  221. package/src/intl/numberfield/es-ES.json +5 -0
  222. package/src/intl/numberfield/et-EE.json +5 -0
  223. package/src/intl/numberfield/fi-FI.json +5 -0
  224. package/src/intl/numberfield/fr-FR.json +5 -0
  225. package/src/intl/numberfield/he-IL.json +5 -0
  226. package/src/intl/numberfield/hr-HR.json +5 -0
  227. package/src/intl/numberfield/hu-HU.json +5 -0
  228. package/src/intl/numberfield/index.ts +75 -0
  229. package/src/intl/numberfield/it-IT.json +5 -0
  230. package/src/intl/numberfield/ja-JP.json +5 -0
  231. package/src/intl/numberfield/ko-KR.json +5 -0
  232. package/src/intl/numberfield/lt-LT.json +5 -0
  233. package/src/intl/numberfield/lv-LV.json +5 -0
  234. package/src/intl/numberfield/nb-NO.json +5 -0
  235. package/src/intl/numberfield/nl-NL.json +5 -0
  236. package/src/intl/numberfield/pl-PL.json +5 -0
  237. package/src/intl/numberfield/pt-BR.json +5 -0
  238. package/src/intl/numberfield/pt-PT.json +5 -0
  239. package/src/intl/numberfield/ro-RO.json +5 -0
  240. package/src/intl/numberfield/ru-RU.json +5 -0
  241. package/src/intl/numberfield/sk-SK.json +5 -0
  242. package/src/intl/numberfield/sl-SI.json +5 -0
  243. package/src/intl/numberfield/sr-SP.json +5 -0
  244. package/src/intl/numberfield/sv-SE.json +5 -0
  245. package/src/intl/numberfield/tr-TR.json +5 -0
  246. package/src/intl/numberfield/uk-UA.json +5 -0
  247. package/src/intl/numberfield/zh-CN.json +5 -0
  248. package/src/intl/numberfield/zh-TW.json +5 -0
  249. package/src/intl/overlays/ar-AE.json +3 -0
  250. package/src/intl/overlays/bg-BG.json +3 -0
  251. package/src/intl/overlays/cs-CZ.json +3 -0
  252. package/src/intl/overlays/da-DK.json +3 -0
  253. package/src/intl/overlays/de-DE.json +3 -0
  254. package/src/intl/overlays/el-GR.json +3 -0
  255. package/src/intl/overlays/en-US.json +3 -0
  256. package/src/intl/overlays/es-ES.json +3 -0
  257. package/src/intl/overlays/et-EE.json +3 -0
  258. package/src/intl/overlays/fi-FI.json +3 -0
  259. package/src/intl/overlays/fr-FR.json +3 -0
  260. package/src/intl/overlays/he-IL.json +3 -0
  261. package/src/intl/overlays/hr-HR.json +3 -0
  262. package/src/intl/overlays/hu-HU.json +3 -0
  263. package/src/intl/overlays/index.ts +75 -0
  264. package/src/intl/overlays/it-IT.json +3 -0
  265. package/src/intl/overlays/ja-JP.json +3 -0
  266. package/src/intl/overlays/ko-KR.json +3 -0
  267. package/src/intl/overlays/lt-LT.json +3 -0
  268. package/src/intl/overlays/lv-LV.json +3 -0
  269. package/src/intl/overlays/nb-NO.json +3 -0
  270. package/src/intl/overlays/nl-NL.json +3 -0
  271. package/src/intl/overlays/pl-PL.json +3 -0
  272. package/src/intl/overlays/pt-BR.json +3 -0
  273. package/src/intl/overlays/pt-PT.json +3 -0
  274. package/src/intl/overlays/ro-RO.json +3 -0
  275. package/src/intl/overlays/ru-RU.json +3 -0
  276. package/src/intl/overlays/sk-SK.json +3 -0
  277. package/src/intl/overlays/sl-SI.json +3 -0
  278. package/src/intl/overlays/sr-SP.json +3 -0
  279. package/src/intl/overlays/sv-SE.json +3 -0
  280. package/src/intl/overlays/tr-TR.json +3 -0
  281. package/src/intl/overlays/uk-UA.json +3 -0
  282. package/src/intl/overlays/zh-CN.json +3 -0
  283. package/src/intl/overlays/zh-TW.json +3 -0
  284. package/src/intl/searchfield/ar-AE.json +3 -0
  285. package/src/intl/searchfield/bg-BG.json +3 -0
  286. package/src/intl/searchfield/cs-CZ.json +3 -0
  287. package/src/intl/searchfield/da-DK.json +3 -0
  288. package/src/intl/searchfield/de-DE.json +3 -0
  289. package/src/intl/searchfield/el-GR.json +3 -0
  290. package/src/intl/searchfield/en-US.json +3 -0
  291. package/src/intl/searchfield/es-ES.json +3 -0
  292. package/src/intl/searchfield/et-EE.json +3 -0
  293. package/src/intl/searchfield/fi-FI.json +3 -0
  294. package/src/intl/searchfield/fr-FR.json +3 -0
  295. package/src/intl/searchfield/he-IL.json +3 -0
  296. package/src/intl/searchfield/hr-HR.json +3 -0
  297. package/src/intl/searchfield/hu-HU.json +3 -0
  298. package/src/intl/searchfield/index.ts +75 -0
  299. package/src/intl/searchfield/it-IT.json +3 -0
  300. package/src/intl/searchfield/ja-JP.json +3 -0
  301. package/src/intl/searchfield/ko-KR.json +3 -0
  302. package/src/intl/searchfield/lt-LT.json +3 -0
  303. package/src/intl/searchfield/lv-LV.json +3 -0
  304. package/src/intl/searchfield/nb-NO.json +3 -0
  305. package/src/intl/searchfield/nl-NL.json +3 -0
  306. package/src/intl/searchfield/pl-PL.json +3 -0
  307. package/src/intl/searchfield/pt-BR.json +3 -0
  308. package/src/intl/searchfield/pt-PT.json +3 -0
  309. package/src/intl/searchfield/ro-RO.json +3 -0
  310. package/src/intl/searchfield/ru-RU.json +3 -0
  311. package/src/intl/searchfield/sk-SK.json +3 -0
  312. package/src/intl/searchfield/sl-SI.json +3 -0
  313. package/src/intl/searchfield/sr-SP.json +3 -0
  314. package/src/intl/searchfield/sv-SE.json +3 -0
  315. package/src/intl/searchfield/tr-TR.json +3 -0
  316. package/src/intl/searchfield/uk-UA.json +3 -0
  317. package/src/intl/searchfield/zh-CN.json +3 -0
  318. package/src/intl/searchfield/zh-TW.json +3 -0
  319. package/src/intl/spinbutton/ar-AE.json +3 -0
  320. package/src/intl/spinbutton/bg-BG.json +3 -0
  321. package/src/intl/spinbutton/cs-CZ.json +3 -0
  322. package/src/intl/spinbutton/da-DK.json +3 -0
  323. package/src/intl/spinbutton/de-DE.json +3 -0
  324. package/src/intl/spinbutton/el-GR.json +3 -0
  325. package/src/intl/spinbutton/en-US.json +3 -0
  326. package/src/intl/spinbutton/es-ES.json +3 -0
  327. package/src/intl/spinbutton/et-EE.json +3 -0
  328. package/src/intl/spinbutton/fi-FI.json +3 -0
  329. package/src/intl/spinbutton/fr-FR.json +3 -0
  330. package/src/intl/spinbutton/he-IL.json +3 -0
  331. package/src/intl/spinbutton/hr-HR.json +3 -0
  332. package/src/intl/spinbutton/hu-HU.json +3 -0
  333. package/src/intl/spinbutton/index.ts +75 -0
  334. package/src/intl/spinbutton/it-IT.json +3 -0
  335. package/src/intl/spinbutton/ja-JP.json +3 -0
  336. package/src/intl/spinbutton/ko-KR.json +3 -0
  337. package/src/intl/spinbutton/lt-LT.json +3 -0
  338. package/src/intl/spinbutton/lv-LV.json +3 -0
  339. package/src/intl/spinbutton/nb-NO.json +3 -0
  340. package/src/intl/spinbutton/nl-NL.json +3 -0
  341. package/src/intl/spinbutton/pl-PL.json +3 -0
  342. package/src/intl/spinbutton/pt-BR.json +3 -0
  343. package/src/intl/spinbutton/pt-PT.json +3 -0
  344. package/src/intl/spinbutton/ro-RO.json +3 -0
  345. package/src/intl/spinbutton/ru-RU.json +3 -0
  346. package/src/intl/spinbutton/sk-SK.json +3 -0
  347. package/src/intl/spinbutton/sl-SI.json +3 -0
  348. package/src/intl/spinbutton/sr-SP.json +3 -0
  349. package/src/intl/spinbutton/sv-SE.json +3 -0
  350. package/src/intl/spinbutton/tr-TR.json +3 -0
  351. package/src/intl/spinbutton/uk-UA.json +3 -0
  352. package/src/intl/spinbutton/zh-CN.json +3 -0
  353. package/src/intl/spinbutton/zh-TW.json +3 -0
  354. package/src/intl/tag/ar-AE.json +4 -0
  355. package/src/intl/tag/bg-BG.json +4 -0
  356. package/src/intl/tag/cs-CZ.json +4 -0
  357. package/src/intl/tag/da-DK.json +4 -0
  358. package/src/intl/tag/de-DE.json +4 -0
  359. package/src/intl/tag/el-GR.json +4 -0
  360. package/src/intl/tag/en-US.json +4 -0
  361. package/src/intl/tag/es-ES.json +4 -0
  362. package/src/intl/tag/et-EE.json +4 -0
  363. package/src/intl/tag/fi-FI.json +4 -0
  364. package/src/intl/tag/fr-FR.json +4 -0
  365. package/src/intl/tag/he-IL.json +4 -0
  366. package/src/intl/tag/hr-HR.json +4 -0
  367. package/src/intl/tag/hu-HU.json +4 -0
  368. package/src/intl/tag/index.ts +75 -0
  369. package/src/intl/tag/it-IT.json +4 -0
  370. package/src/intl/tag/ja-JP.json +4 -0
  371. package/src/intl/tag/ko-KR.json +4 -0
  372. package/src/intl/tag/lt-LT.json +4 -0
  373. package/src/intl/tag/lv-LV.json +4 -0
  374. package/src/intl/tag/nb-NO.json +4 -0
  375. package/src/intl/tag/nl-NL.json +4 -0
  376. package/src/intl/tag/pl-PL.json +4 -0
  377. package/src/intl/tag/pt-BR.json +4 -0
  378. package/src/intl/tag/pt-PT.json +4 -0
  379. package/src/intl/tag/ro-RO.json +4 -0
  380. package/src/intl/tag/ru-RU.json +4 -0
  381. package/src/intl/tag/sk-SK.json +4 -0
  382. package/src/intl/tag/sl-SI.json +4 -0
  383. package/src/intl/tag/sr-SP.json +4 -0
  384. package/src/intl/tag/sv-SE.json +4 -0
  385. package/src/intl/tag/tr-TR.json +4 -0
  386. package/src/intl/tag/uk-UA.json +4 -0
  387. package/src/intl/tag/zh-CN.json +4 -0
  388. package/src/intl/tag/zh-TW.json +4 -0
  389. package/src/label/useField.ts +67 -0
  390. package/src/label/useLabel.ts +78 -0
  391. package/src/link/useLink.ts +108 -0
  392. package/src/listbox/useListBox.ts +224 -0
  393. package/src/listbox/useListBoxSection.ts +74 -0
  394. package/src/listbox/useOption.ts +212 -0
  395. package/src/listbox/utils.ts +38 -0
  396. package/src/live-announcer/LiveAnnouncer.ts +172 -0
  397. package/src/menu/useMenu.ts +139 -0
  398. package/src/menu/useMenuItem.ts +453 -0
  399. package/src/menu/useMenuSection.ts +70 -0
  400. package/src/menu/useMenuTrigger.ts +189 -0
  401. package/src/menu/useSafelyMouseToSubmenu.ts +236 -0
  402. package/src/menu/useSubmenuTrigger.ts +357 -0
  403. package/src/menu/utils.ts +16 -0
  404. package/src/meter/useMeter.ts +43 -0
  405. package/src/numberfield/useNumberField.ts +572 -0
  406. package/src/overlays/DismissButton.ts +50 -0
  407. package/src/overlays/Overlay.ts +106 -0
  408. package/src/overlays/PortalProvider.ts +37 -0
  409. package/src/overlays/ariaHideOutside.ts +299 -0
  410. package/src/overlays/calculatePosition.ts +856 -0
  411. package/src/overlays/useCloseOnScroll.ts +75 -0
  412. package/src/overlays/useModal.ts +224 -0
  413. package/src/overlays/useModalOverlay.ts +103 -0
  414. package/src/overlays/useOverlay.ts +199 -0
  415. package/src/overlays/useOverlayPosition.ts +462 -0
  416. package/src/overlays/useOverlayTrigger.ts +94 -0
  417. package/src/overlays/usePopover.ts +170 -0
  418. package/src/overlays/usePreventScroll.ts +316 -0
  419. package/src/progress/useProgressBar.ts +115 -0
  420. package/src/radio/useRadio.ts +219 -0
  421. package/src/radio/useRadioGroup.ts +184 -0
  422. package/src/radio/utils.ts +15 -0
  423. package/src/searchfield/useSearchField.ts +156 -0
  424. package/src/select/HiddenSelect.ts +261 -0
  425. package/src/select/useSelect.ts +332 -0
  426. package/src/selection/DOMLayoutDelegate.ts +51 -0
  427. package/src/selection/ListKeyboardDelegate.ts +392 -0
  428. package/src/selection/useSelectableCollection.ts +750 -0
  429. package/src/selection/useSelectableItem.ts +499 -0
  430. package/src/selection/useSelectableList.ts +132 -0
  431. package/src/selection/useTypeSelect.ts +192 -0
  432. package/src/selection/utils.ts +50 -0
  433. package/src/separator/useSeparator.ts +41 -0
  434. package/src/slider/useSlider.ts +277 -0
  435. package/src/slider/useSliderThumb.ts +375 -0
  436. package/src/slider/utils.ts +20 -0
  437. package/src/spinbutton/useSpinButton.ts +380 -0
  438. package/src/stately/checkbox/useCheckboxGroupState.ts +177 -0
  439. package/src/stately/collections/CollectionBuilder.ts +332 -0
  440. package/src/stately/collections/Item.ts +78 -0
  441. package/src/stately/collections/Section.ts +52 -0
  442. package/src/stately/collections/getChildNodes.ts +94 -0
  443. package/src/stately/collections/getItemCount.ts +29 -0
  444. package/src/stately/collections/types.ts +20 -0
  445. package/src/stately/collections/useCollection.ts +54 -0
  446. package/src/stately/combobox/useComboBoxState.ts +763 -0
  447. package/src/stately/disclosure/useDisclosureGroupState.ts +108 -0
  448. package/src/stately/disclosure/useDisclosureState.ts +84 -0
  449. package/src/stately/form/useFormValidationState.ts +331 -0
  450. package/src/stately/index.ts +111 -0
  451. package/src/stately/list/ListCollection.ts +104 -0
  452. package/src/stately/list/useListState.ts +178 -0
  453. package/src/stately/list/useSingleSelectListState.ts +101 -0
  454. package/src/stately/menu/useMenuTriggerState.ts +120 -0
  455. package/src/stately/menu/useSubmenuTriggerState.ts +116 -0
  456. package/src/stately/numberfield/useNumberFieldState.ts +430 -0
  457. package/src/stately/overlays/useOverlayTriggerState.ts +85 -0
  458. package/src/stately/radio/useRadioGroupState.ts +150 -0
  459. package/src/stately/searchfield/useSearchFieldState.ts +79 -0
  460. package/src/stately/select/useSelectState.ts +322 -0
  461. package/src/stately/selection/Selection.ts +23 -0
  462. package/src/stately/selection/SelectionManager.ts +541 -0
  463. package/src/stately/selection/types.ts +117 -0
  464. package/src/stately/selection/useMultipleSelectionState.ts +178 -0
  465. package/src/stately/slider/useSliderState.ts +458 -0
  466. package/src/stately/tabs/useTabListState.ts +149 -0
  467. package/src/stately/toggle/useToggleGroupState.ts +109 -0
  468. package/src/stately/toggle/useToggleState.ts +100 -0
  469. package/src/stately/tooltip/useTooltipTriggerState.ts +218 -0
  470. package/src/stately/tree/TreeCollection.ts +92 -0
  471. package/src/stately/tree/useTreeState.ts +121 -0
  472. package/src/stately/utils/useControlledState.ts +7 -0
  473. package/src/switch/useSwitch.ts +77 -0
  474. package/src/tabs/TabsKeyboardDelegate.ts +99 -0
  475. package/src/tabs/useTab.ts +117 -0
  476. package/src/tabs/useTabList.ts +105 -0
  477. package/src/tabs/useTabPanel.ts +75 -0
  478. package/src/tabs/utils.ts +32 -0
  479. package/src/tag/useTag.ts +162 -0
  480. package/src/tag/useTagGroup.ts +192 -0
  481. package/src/textfield/useFormattedTextField.ts +203 -0
  482. package/src/textfield/useTextField.ts +248 -0
  483. package/src/toggle/useToggle.ts +265 -0
  484. package/src/toolbar/useToolbar.ts +147 -0
  485. package/src/tooltip/useTooltip.ts +64 -0
  486. package/src/tooltip/useTooltipTrigger.ts +187 -0
  487. package/src/utils/filterDOMProps.ts +121 -0
  488. package/src/utils/getScrollParent.ts +16 -0
  489. package/src/utils/getScrollParents.ts +20 -0
  490. package/src/utils/keyboard.ts +38 -0
  491. package/src/utils/scrollIntoView.ts +178 -0
  492. package/src/utils/useDeepMemo.ts +30 -0
  493. package/src/utils/useFormReset.ts +54 -0
  494. package/src/utils/useId.ts +5 -0
  495. package/src/utils/useLabels.ts +53 -0
  496. package/src/utils/useResizeObserver.ts +70 -0
  497. package/src/utils/useSlot.ts +78 -0
  498. package/src/utils/useUpdateEffect.ts +54 -0
  499. package/src/utils/useUpdateLayoutEffect.ts +58 -0
  500. package/src/visually-hidden/VisuallyHidden.ts +100 -0
@@ -0,0 +1,1205 @@
1
+ // Ported from react-aria (source: .react-spectrum/packages/react-aria/src/focus/FocusScope.tsx).
2
+ // octane adaptations:
3
+ // - `FocusScope` is built with `createElement` (published `.ts`, no JSX): the Provider's
4
+ // positional children are ALWAYS `[start sentinel, children, end sentinel]` — a stable
5
+ // shape every render (see the provider children shape-flip note in the binding docs) —
6
+ // and octane mounts them in source order, so the sentinel-walk effect collects exactly
7
+ // the rendered nodes between the two spans. octane's comment markers land in the scope
8
+ // array alongside real nodes, the same way React's text nodes do upstream; they are
9
+ // inert for focus (never focusable) but valid `nodeContains`/listener targets.
10
+ // - All scope listeners (focusin/focusout/keydown) attach RAW on the owner document and
11
+ // scope nodes, exactly like upstream — octane's capture-delegated focus events are not
12
+ // involved, so containment/restore never race the framework's own event plumbing.
13
+ // - Component/base hooks per the binding convention: `FocusScope` derives per-instance
14
+ // slots from `S('FocusScope')`; the private scope hooks take a trailing slot param;
15
+ // `useFocusManager` is a public hook (typed overloads + splitSlot; `useContext` itself
16
+ // is context-identity keyed and needs no sub-slot).
17
+ // - React's `ReactNode` children / `JSX.Element` return types → octane children values
18
+ // (`any`); `React.RefObject` → the structural `RefObject` from '@react-types/shared'.
19
+ // - Effect timing preserved verbatim (layout vs passive, dep arrays): octane attaches
20
+ // refs before layout effect bodies and runs unmount cleanups in declaration order like
21
+ // React, which the restore-on-unmount path (clone-then-RAF) relies on.
22
+
23
+ import { createShadowTreeWalker, ShadowTreeWalker } from '../utils/shadowdom/ShadowTreeWalker';
24
+
25
+ import type { FocusableElement, RefObject } from '@react-types/shared';
26
+ import { focusSafely } from '../interactions/focusSafely';
27
+ import { getActiveElement, getEventTarget, nodeContains } from '../utils/shadowdom/DOMFunctions';
28
+ import { getInteractionModality } from '../interactions/useFocusVisible';
29
+ import { getOwnerDocument, getOwnerWindow } from '../utils/domHelpers';
30
+ import { isAndroid, isChrome } from '../utils/platform';
31
+ import { isFocusable, isTabbable } from '../utils/isFocusable';
32
+ import { createContext, createElement, useContext, useEffect, useMemo, useRef } from 'octane';
33
+ import { useLayoutEffect } from '../utils/useLayoutEffect';
34
+
35
+ import { S, splitSlot, subSlot } from '../internal';
36
+
37
+ export interface FocusScopeProps {
38
+ /** The contents of the focus scope. */
39
+ children: any;
40
+
41
+ /**
42
+ * Whether to contain focus inside the scope, so users cannot
43
+ * move focus outside, for example in a modal dialog.
44
+ */
45
+ contain?: boolean;
46
+
47
+ /**
48
+ * Whether to restore focus back to the element that was focused
49
+ * when the focus scope mounted, after the focus scope unmounts.
50
+ */
51
+ restoreFocus?: boolean;
52
+
53
+ /** Whether to auto focus the first focusable element in the focus scope on mount. */
54
+ autoFocus?: boolean;
55
+ }
56
+
57
+ export interface FocusManagerOptions {
58
+ /** The element to start searching from. The currently focused element by default. */
59
+ from?: Element;
60
+ /** Whether to only include tabbable elements, or all focusable elements. */
61
+ tabbable?: boolean;
62
+ /** Whether focus should wrap around when it reaches the end of the scope. */
63
+ wrap?: boolean;
64
+ /** A callback that determines whether the given element is focused. */
65
+ accept?: (node: Element) => boolean;
66
+ }
67
+
68
+ export interface FocusManager {
69
+ /** Moves focus to the next focusable or tabbable element in the focus scope. */
70
+ focusNext(opts?: FocusManagerOptions): FocusableElement | null;
71
+ /** Moves focus to the previous focusable or tabbable element in the focus scope. */
72
+ focusPrevious(opts?: FocusManagerOptions): FocusableElement | null;
73
+ /** Moves focus to the first focusable or tabbable element in the focus scope. */
74
+ focusFirst(opts?: FocusManagerOptions): FocusableElement | null;
75
+ /** Moves focus to the last focusable or tabbable element in the focus scope. */
76
+ focusLast(opts?: FocusManagerOptions): FocusableElement | null;
77
+ }
78
+
79
+ type ScopeRef = RefObject<Element[] | null> | null;
80
+ interface IFocusContext {
81
+ focusManager: FocusManager;
82
+ parentNode: TreeNode | null;
83
+ }
84
+
85
+ const FocusContext = createContext<IFocusContext | null>(null);
86
+ const RESTORE_FOCUS_EVENT = 'react-aria-focus-scope-restore';
87
+
88
+ let activeScope: ScopeRef = null;
89
+
90
+ // This is a hacky DOM-based implementation of a FocusScope until this RFC lands in React:
91
+ // https://github.com/reactjs/rfcs/pull/109
92
+
93
+ /**
94
+ * A FocusScope manages focus for its descendants. It supports containing focus inside
95
+ * the scope, restoring focus to the previously focused element on unmount, and auto
96
+ * focusing children on mount. It also acts as a container for a programmatic focus
97
+ * management interface that can be used to move focus forward and back in response
98
+ * to user events.
99
+ */
100
+ export function FocusScope(props: FocusScopeProps): any {
101
+ const slot = S('FocusScope');
102
+ let { children, contain, restoreFocus, autoFocus } = props;
103
+ let startRef = useRef<HTMLSpanElement | null>(null, subSlot(slot, 'start'));
104
+ let endRef = useRef<HTMLSpanElement | null>(null, subSlot(slot, 'end'));
105
+ let scopeRef = useRef<Element[]>([], subSlot(slot, 'scope'));
106
+ let { parentNode } = useContext(FocusContext) || {};
107
+
108
+ // Create a tree node here so we can add children to it even before it is added to the tree.
109
+ let node = useMemo(() => new TreeNode({ scopeRef }), [scopeRef], subSlot(slot, 'node'));
110
+
111
+ useLayoutEffect(
112
+ () => {
113
+ // If a new scope mounts outside the active scope, (e.g. DialogContainer launched from a menu),
114
+ // use the active scope as the parent instead of the parent from context. Layout effects run bottom
115
+ // up, so if the parent is not yet added to the tree, don't do this. Only the outer-most FocusScope
116
+ // that is being added should get the activeScope as its parent.
117
+ let parent = parentNode || focusScopeTree.root;
118
+ if (
119
+ focusScopeTree.getTreeNode(parent.scopeRef) &&
120
+ activeScope &&
121
+ !isAncestorScope(activeScope, parent.scopeRef)
122
+ ) {
123
+ let activeNode = focusScopeTree.getTreeNode(activeScope);
124
+ if (activeNode) {
125
+ parent = activeNode;
126
+ }
127
+ }
128
+
129
+ // Add the node to the parent, and to the tree.
130
+ parent.addChild(node);
131
+ focusScopeTree.addNode(node);
132
+ },
133
+ [node, parentNode],
134
+ subSlot(slot, 'tree'),
135
+ );
136
+
137
+ useLayoutEffect(
138
+ () => {
139
+ let node = focusScopeTree.getTreeNode(scopeRef);
140
+ if (node) {
141
+ node.contain = !!contain;
142
+ }
143
+ },
144
+ [contain],
145
+ subSlot(slot, 'contain'),
146
+ );
147
+
148
+ useLayoutEffect(
149
+ () => {
150
+ // Find all rendered nodes between the sentinels and add them to the scope.
151
+ let node = startRef.current?.nextSibling!;
152
+ let nodes: Element[] = [];
153
+ let stopPropagation = (e: Event) => e.stopPropagation();
154
+ while (node && node !== endRef.current) {
155
+ nodes.push(node as Element);
156
+ // Stop custom restore focus event from propagating to parent focus scopes.
157
+ node.addEventListener(RESTORE_FOCUS_EVENT, stopPropagation);
158
+ node = node.nextSibling as Element;
159
+ }
160
+
161
+ scopeRef.current = nodes;
162
+
163
+ return () => {
164
+ for (let node of nodes) {
165
+ node.removeEventListener(RESTORE_FOCUS_EVENT, stopPropagation);
166
+ }
167
+ };
168
+ },
169
+ [children],
170
+ subSlot(slot, 'collect'),
171
+ );
172
+
173
+ useActiveScopeTracker(scopeRef, restoreFocus, contain, subSlot(slot, 'tracker'));
174
+ useFocusContainment(scopeRef, contain, subSlot(slot, 'containment'));
175
+ useRestoreFocus(scopeRef, restoreFocus, contain, subSlot(slot, 'restore'));
176
+ useAutoFocus(scopeRef, autoFocus, subSlot(slot, 'autoFocus'));
177
+
178
+ // This needs to be an effect so that activeScope is updated after the FocusScope tree is complete.
179
+ // It cannot be a useLayoutEffect because the parent of this node hasn't been attached in the tree yet.
180
+ useEffect(
181
+ () => {
182
+ const activeElement = getActiveElement(
183
+ getOwnerDocument(scopeRef.current ? scopeRef.current[0] : undefined),
184
+ );
185
+ let scope: TreeNode | null = null;
186
+
187
+ if (isElementInScope(activeElement, scopeRef.current)) {
188
+ // We need to traverse the focusScope tree and find the bottom most scope that
189
+ // contains the active element and set that as the activeScope.
190
+ for (let node of focusScopeTree.traverse()) {
191
+ if (node.scopeRef && isElementInScope(activeElement, node.scopeRef.current)) {
192
+ scope = node;
193
+ }
194
+ }
195
+
196
+ if (scope === focusScopeTree.getTreeNode(scopeRef)) {
197
+ activeScope = scope.scopeRef;
198
+ }
199
+ }
200
+ },
201
+ [scopeRef],
202
+ subSlot(slot, 'active'),
203
+ );
204
+
205
+ // This layout effect cleanup is so that the tree node is removed synchronously with react before the RAF
206
+ // in useRestoreFocus cleanup runs.
207
+ useLayoutEffect(
208
+ () => {
209
+ return () => {
210
+ // Scope may have been re-parented.
211
+ let parentScope = focusScopeTree.getTreeNode(scopeRef)?.parent?.scopeRef ?? null;
212
+
213
+ if (
214
+ (scopeRef === activeScope || isAncestorScope(scopeRef, activeScope)) &&
215
+ (!parentScope || focusScopeTree.getTreeNode(parentScope))
216
+ ) {
217
+ activeScope = parentScope;
218
+ }
219
+ focusScopeTree.removeTreeNode(scopeRef);
220
+ };
221
+ },
222
+ [scopeRef],
223
+ subSlot(slot, 'remove'),
224
+ );
225
+
226
+ let focusManager = useMemo(
227
+ () => createFocusManagerForScope(scopeRef),
228
+ [],
229
+ subSlot(slot, 'manager'),
230
+ );
231
+ let value = useMemo(
232
+ () => ({
233
+ focusManager,
234
+ parentNode: node,
235
+ }),
236
+ [node, focusManager],
237
+ subSlot(slot, 'value'),
238
+ );
239
+
240
+ return createElement(
241
+ FocusContext.Provider,
242
+ { value },
243
+ createElement('span', { 'data-focus-scope-start': true, hidden: true, ref: startRef }),
244
+ children,
245
+ createElement('span', { 'data-focus-scope-end': true, hidden: true, ref: endRef }),
246
+ );
247
+ }
248
+
249
+ /**
250
+ * Returns a FocusManager interface for the parent FocusScope.
251
+ * A FocusManager can be used to programmatically move focus within
252
+ * a FocusScope, e.g. in response to user events like keyboard navigation.
253
+ */
254
+ export function useFocusManager(): FocusManager | undefined;
255
+ // Slot-threading form: sibling ported hooks pass their derived sub-slot as the trailing arg.
256
+ export function useFocusManager(slot: symbol | undefined): FocusManager | undefined;
257
+ export function useFocusManager(...args: any[]): FocusManager | undefined {
258
+ // The injected slot is absorbed but unused: useContext is context-identity keyed.
259
+ splitSlot(args);
260
+ return useContext(FocusContext)?.focusManager;
261
+ }
262
+
263
+ function createFocusManagerForScope(scopeRef: RefObject<Element[] | null>): FocusManager {
264
+ return {
265
+ focusNext(opts: FocusManagerOptions = {}) {
266
+ let scope = scopeRef.current!;
267
+ let { from, tabbable, wrap, accept } = opts;
268
+ let node = from || getActiveElement(getOwnerDocument(scope[0] ?? undefined))!;
269
+ let sentinel = scope[0].previousElementSibling!;
270
+ let scopeRoot = getScopeRoot(scope);
271
+ let walker = getFocusableTreeWalker(scopeRoot, { tabbable, accept }, scope);
272
+ walker.currentNode = isElementInScope(node, scope) ? node : sentinel;
273
+ let nextNode = walker.nextNode() as FocusableElement;
274
+ if (!nextNode && wrap) {
275
+ walker.currentNode = sentinel;
276
+ nextNode = walker.nextNode() as FocusableElement;
277
+ }
278
+ if (nextNode) {
279
+ focusElement(nextNode, true);
280
+ }
281
+ return nextNode;
282
+ },
283
+ focusPrevious(opts: FocusManagerOptions = {}) {
284
+ let scope = scopeRef.current!;
285
+ let { from, tabbable, wrap, accept } = opts;
286
+ let node = from || getActiveElement(getOwnerDocument(scope[0] ?? undefined))!;
287
+ let sentinel = scope[scope.length - 1].nextElementSibling!;
288
+ let scopeRoot = getScopeRoot(scope);
289
+ let walker = getFocusableTreeWalker(scopeRoot, { tabbable, accept }, scope);
290
+ walker.currentNode = isElementInScope(node, scope) ? node : sentinel;
291
+ let previousNode = walker.previousNode() as FocusableElement;
292
+ if (!previousNode && wrap) {
293
+ walker.currentNode = sentinel;
294
+ previousNode = walker.previousNode() as FocusableElement;
295
+ }
296
+ if (previousNode) {
297
+ focusElement(previousNode, true);
298
+ }
299
+ return previousNode;
300
+ },
301
+ focusFirst(opts = {}) {
302
+ let scope = scopeRef.current!;
303
+ let { tabbable, accept } = opts;
304
+ let scopeRoot = getScopeRoot(scope);
305
+ let walker = getFocusableTreeWalker(scopeRoot, { tabbable, accept }, scope);
306
+ walker.currentNode = scope[0].previousElementSibling!;
307
+ let nextNode = walker.nextNode() as FocusableElement;
308
+ if (nextNode) {
309
+ focusElement(nextNode, true);
310
+ }
311
+ return nextNode;
312
+ },
313
+ focusLast(opts = {}) {
314
+ let scope = scopeRef.current!;
315
+ let { tabbable, accept } = opts;
316
+ let scopeRoot = getScopeRoot(scope);
317
+ let walker = getFocusableTreeWalker(scopeRoot, { tabbable, accept }, scope);
318
+ walker.currentNode = scope[scope.length - 1].nextElementSibling!;
319
+ let previousNode = walker.previousNode() as FocusableElement;
320
+ if (previousNode) {
321
+ focusElement(previousNode, true);
322
+ }
323
+ return previousNode;
324
+ },
325
+ };
326
+ }
327
+
328
+ function getScopeRoot(scope: Element[]) {
329
+ return scope[0].parentElement!;
330
+ }
331
+
332
+ function shouldContainFocus(scopeRef: ScopeRef) {
333
+ let scope = focusScopeTree.getTreeNode(activeScope);
334
+ while (scope && scope.scopeRef !== scopeRef) {
335
+ if (scope.contain) {
336
+ return false;
337
+ }
338
+
339
+ scope = scope.parent;
340
+ }
341
+
342
+ return true;
343
+ }
344
+
345
+ function getRadiosInGroup(element: HTMLInputElement): HTMLInputElement[] {
346
+ if (!element.form) {
347
+ // Radio buttons outside a form - query the document
348
+ return Array.from(
349
+ getOwnerDocument(element).querySelectorAll<HTMLInputElement>(
350
+ `input[type="radio"][name="${CSS.escape(element.name)}"]`,
351
+ ),
352
+ ).filter((radio) => !radio.form);
353
+ }
354
+
355
+ // namedItem returns RadioNodeList (iterable) for 2+ elements, but a single Element for exactly 1.
356
+ // https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormControlsCollection/namedItem
357
+ const radioList = element.form.elements.namedItem(element.name);
358
+ let ownerWindow = getOwnerWindow(element);
359
+ if (radioList instanceof ownerWindow.RadioNodeList) {
360
+ return Array.from(radioList).filter(
361
+ (el): el is HTMLInputElement => el instanceof ownerWindow.HTMLInputElement,
362
+ );
363
+ }
364
+ if (radioList instanceof ownerWindow.HTMLInputElement) {
365
+ return [radioList];
366
+ }
367
+ return [];
368
+ }
369
+
370
+ function isTabbableRadio(element: HTMLInputElement): boolean {
371
+ if (element.checked) {
372
+ return true;
373
+ }
374
+ const radios = getRadiosInGroup(element);
375
+ return radios.length > 0 && !radios.some((radio) => radio.checked);
376
+ }
377
+
378
+ function useFocusContainment(
379
+ scopeRef: RefObject<Element[] | null>,
380
+ contain: boolean | undefined,
381
+ slot: symbol | undefined,
382
+ ) {
383
+ let focusedNode = useRef<FocusableElement | undefined>(undefined, subSlot(slot, 'focusedNode'));
384
+
385
+ let raf = useRef<ReturnType<typeof requestAnimationFrame> | undefined>(
386
+ undefined,
387
+ subSlot(slot, 'raf'),
388
+ );
389
+ useLayoutEffect(
390
+ () => {
391
+ let scope = scopeRef.current;
392
+ if (!contain) {
393
+ // if contain was changed, then we should cancel any ongoing waits to pull focus back into containment
394
+ if (raf.current) {
395
+ cancelAnimationFrame(raf.current);
396
+ raf.current = undefined;
397
+ }
398
+ return;
399
+ }
400
+
401
+ const ownerDocument = getOwnerDocument(scope ? scope[0] : undefined);
402
+
403
+ // Handle the Tab key to contain focus within the scope
404
+ let onKeyDown = (e: KeyboardEvent) => {
405
+ if (
406
+ e.key !== 'Tab' ||
407
+ e.altKey ||
408
+ e.ctrlKey ||
409
+ e.metaKey ||
410
+ !shouldContainFocus(scopeRef) ||
411
+ e.isComposing
412
+ ) {
413
+ return;
414
+ }
415
+
416
+ let focusedElement = getActiveElement(ownerDocument);
417
+ let scope = scopeRef.current;
418
+ if (!scope || !isElementInScope(focusedElement, scope)) {
419
+ return;
420
+ }
421
+
422
+ let scopeRoot = getScopeRoot(scope);
423
+ let walker = getFocusableTreeWalker(scopeRoot, { tabbable: true }, scope);
424
+ if (!focusedElement) {
425
+ return;
426
+ }
427
+ walker.currentNode = focusedElement;
428
+ let nextElement = (
429
+ e.shiftKey ? walker.previousNode() : walker.nextNode()
430
+ ) as FocusableElement;
431
+ if (!nextElement) {
432
+ walker.currentNode = e.shiftKey
433
+ ? scope[scope.length - 1].nextElementSibling!
434
+ : scope[0].previousElementSibling!;
435
+ nextElement = (
436
+ e.shiftKey ? walker.previousNode() : walker.nextNode()
437
+ ) as FocusableElement;
438
+ }
439
+
440
+ e.preventDefault();
441
+ if (nextElement) {
442
+ focusElement(nextElement, true);
443
+ if (nextElement instanceof getOwnerWindow(nextElement).HTMLInputElement) {
444
+ nextElement.select();
445
+ }
446
+ }
447
+ };
448
+
449
+ let onFocus: EventListener = (e) => {
450
+ // If focusing an element in a child scope of the currently active scope, the child becomes active.
451
+ // Moving out of the active scope to an ancestor is not allowed.
452
+ if (
453
+ (!activeScope || isAncestorScope(activeScope, scopeRef)) &&
454
+ isElementInScope(getEventTarget(e) as Element, scopeRef.current)
455
+ ) {
456
+ activeScope = scopeRef;
457
+ focusedNode.current = getEventTarget(e) as FocusableElement;
458
+ } else if (
459
+ shouldContainFocus(scopeRef) &&
460
+ !isElementInChildScope(getEventTarget(e) as Element, scopeRef)
461
+ ) {
462
+ // If a focus event occurs outside the active scope (e.g. user tabs from browser location bar),
463
+ // restore focus to the previously focused node or the first tabbable element in the active scope.
464
+ if (focusedNode.current) {
465
+ focusedNode.current.focus();
466
+ } else if (activeScope && activeScope.current) {
467
+ focusFirstInScope(activeScope.current);
468
+ }
469
+ } else if (shouldContainFocus(scopeRef)) {
470
+ focusedNode.current = getEventTarget(e) as FocusableElement;
471
+ }
472
+ };
473
+
474
+ let onBlur: EventListener = (e) => {
475
+ // Firefox doesn't shift focus back to the Dialog properly without this
476
+ if (raf.current) {
477
+ cancelAnimationFrame(raf.current);
478
+ }
479
+ raf.current = requestAnimationFrame(() => {
480
+ // Patches infinite focus coersion loop for Android Talkback where the user isn't able to move the virtual cursor
481
+ // if within a containing focus scope. Bug filed against Chrome: https://issuetracker.google.com/issues/384844019.
482
+ // Note that this means focus can leave focus containing modals due to this, but it is isolated to Chrome Talkback.
483
+ let modality = getInteractionModality();
484
+ let shouldSkipFocusRestore =
485
+ (modality === 'virtual' || modality === null) && isAndroid() && isChrome();
486
+
487
+ // Use document.activeElement instead of e.relatedTarget so we can tell if user clicked into iframe
488
+ let activeElement = getActiveElement(ownerDocument);
489
+ if (
490
+ !shouldSkipFocusRestore &&
491
+ activeElement &&
492
+ shouldContainFocus(scopeRef) &&
493
+ !isElementInChildScope(activeElement, scopeRef)
494
+ ) {
495
+ activeScope = scopeRef;
496
+ let target = getEventTarget(e) as FocusableElement;
497
+ if (target && target.isConnected) {
498
+ focusedNode.current = target;
499
+ focusedNode.current?.focus();
500
+ } else if (activeScope.current) {
501
+ focusFirstInScope(activeScope.current);
502
+ }
503
+ }
504
+ });
505
+ };
506
+
507
+ ownerDocument.addEventListener('keydown', onKeyDown, false);
508
+ ownerDocument.addEventListener('focusin', onFocus, false);
509
+ scope?.forEach((element) => element.addEventListener('focusin', onFocus, false));
510
+ scope?.forEach((element) => element.addEventListener('focusout', onBlur, false));
511
+ return () => {
512
+ ownerDocument.removeEventListener('keydown', onKeyDown, false);
513
+ ownerDocument.removeEventListener('focusin', onFocus, false);
514
+ scope?.forEach((element) => element.removeEventListener('focusin', onFocus, false));
515
+ scope?.forEach((element) => element.removeEventListener('focusout', onBlur, false));
516
+ };
517
+ },
518
+ [scopeRef, contain],
519
+ subSlot(slot, 'listeners'),
520
+ );
521
+
522
+ // This is a useLayoutEffect so it is guaranteed to run before our async synthetic blur
523
+
524
+ useLayoutEffect(
525
+ () => {
526
+ return () => {
527
+ if (raf.current) {
528
+ cancelAnimationFrame(raf.current);
529
+ }
530
+ };
531
+ },
532
+ [raf],
533
+ subSlot(slot, 'rafCleanup'),
534
+ );
535
+ }
536
+
537
+ function isElementInAnyScope(element: Element) {
538
+ return isElementInChildScope(element);
539
+ }
540
+
541
+ function isElementInScope(element?: Element | null, scope?: Element[] | null) {
542
+ if (!element) {
543
+ return false;
544
+ }
545
+ if (!scope) {
546
+ return false;
547
+ }
548
+ return scope.some((node) => nodeContains(node, element));
549
+ }
550
+
551
+ function isElementInChildScope(element: Element, scope: ScopeRef = null) {
552
+ // If the element is within a top layer element (e.g. toasts), always allow moving focus there.
553
+ if (element instanceof Element && element.closest('[data-react-aria-top-layer]')) {
554
+ return true;
555
+ }
556
+
557
+ // node.contains in isElementInScope covers child scopes that are also DOM children,
558
+ // but does not cover child scopes in portals.
559
+ for (let { scopeRef: s } of focusScopeTree.traverse(focusScopeTree.getTreeNode(scope))) {
560
+ if (s && isElementInScope(element, s.current)) {
561
+ return true;
562
+ }
563
+ }
564
+
565
+ return false;
566
+ }
567
+
568
+ /** @private */
569
+ export function isElementInChildOfActiveScope(element: Element): boolean {
570
+ return isElementInChildScope(element, activeScope);
571
+ }
572
+
573
+ function isAncestorScope(ancestor: ScopeRef, scope: ScopeRef) {
574
+ let parent = focusScopeTree.getTreeNode(scope)?.parent;
575
+ while (parent) {
576
+ if (parent.scopeRef === ancestor) {
577
+ return true;
578
+ }
579
+ parent = parent.parent;
580
+ }
581
+ return false;
582
+ }
583
+
584
+ function focusElement(element: FocusableElement | null, scroll = false) {
585
+ if (element != null && !scroll) {
586
+ try {
587
+ focusSafely(element);
588
+ } catch {
589
+ // ignore
590
+ }
591
+ } else if (element != null) {
592
+ try {
593
+ element.focus();
594
+ } catch {
595
+ // ignore
596
+ }
597
+ }
598
+ }
599
+
600
+ function getFirstInScope(scope: Element[], tabbable = true) {
601
+ let sentinel = scope[0].previousElementSibling!;
602
+ let scopeRoot = getScopeRoot(scope);
603
+ let walker = getFocusableTreeWalker(scopeRoot, { tabbable }, scope);
604
+ walker.currentNode = sentinel;
605
+ let nextNode = walker.nextNode();
606
+
607
+ // If the scope does not contain a tabbable element, use the first focusable element.
608
+ if (tabbable && !nextNode) {
609
+ scopeRoot = getScopeRoot(scope);
610
+ walker = getFocusableTreeWalker(scopeRoot, { tabbable: false }, scope);
611
+ walker.currentNode = sentinel;
612
+ nextNode = walker.nextNode();
613
+ }
614
+
615
+ return nextNode as FocusableElement;
616
+ }
617
+
618
+ function focusFirstInScope(scope: Element[], tabbable: boolean = true) {
619
+ focusElement(getFirstInScope(scope, tabbable));
620
+ }
621
+
622
+ function useAutoFocus(
623
+ scopeRef: RefObject<Element[] | null>,
624
+ autoFocus: boolean | undefined,
625
+ slot: symbol | undefined,
626
+ ) {
627
+ const autoFocusRef = useRef(autoFocus, subSlot(slot, 'ref'));
628
+ useEffect(
629
+ () => {
630
+ if (autoFocusRef.current) {
631
+ activeScope = scopeRef;
632
+ const ownerDocument = getOwnerDocument(scopeRef.current ? scopeRef.current[0] : undefined);
633
+ if (
634
+ !isElementInScope(getActiveElement(ownerDocument), activeScope.current) &&
635
+ scopeRef.current
636
+ ) {
637
+ focusFirstInScope(scopeRef.current);
638
+ }
639
+ }
640
+ autoFocusRef.current = false;
641
+ },
642
+ [scopeRef],
643
+ subSlot(slot, 'focus'),
644
+ );
645
+ }
646
+
647
+ function useActiveScopeTracker(
648
+ scopeRef: RefObject<Element[] | null>,
649
+ restore: boolean | undefined,
650
+ contain: boolean | undefined,
651
+ slot: symbol | undefined,
652
+ ) {
653
+ // tracks the active scope, in case restore and contain are both false.
654
+ // if either are true, this is tracked in useRestoreFocus or useFocusContainment.
655
+ useLayoutEffect(
656
+ () => {
657
+ if (restore || contain) {
658
+ return;
659
+ }
660
+
661
+ let scope = scopeRef.current;
662
+ const ownerDocument = getOwnerDocument(scope ? scope[0] : undefined);
663
+
664
+ let onFocus: EventListener = (e) => {
665
+ let target = getEventTarget(e) as Element;
666
+ if (isElementInScope(target, scopeRef.current)) {
667
+ activeScope = scopeRef;
668
+ } else if (!isElementInAnyScope(target)) {
669
+ activeScope = null;
670
+ }
671
+ };
672
+
673
+ ownerDocument.addEventListener('focusin', onFocus, false);
674
+ scope?.forEach((element) => element.addEventListener('focusin', onFocus, false));
675
+ return () => {
676
+ ownerDocument.removeEventListener('focusin', onFocus, false);
677
+ scope?.forEach((element) => element.removeEventListener('focusin', onFocus, false));
678
+ };
679
+ },
680
+ [scopeRef, restore, contain],
681
+ subSlot(slot, 'track'),
682
+ );
683
+ }
684
+
685
+ function shouldRestoreFocus(scopeRef: ScopeRef) {
686
+ let scope = focusScopeTree.getTreeNode(activeScope);
687
+ while (scope && scope.scopeRef !== scopeRef) {
688
+ if (scope.nodeToRestore) {
689
+ return false;
690
+ }
691
+
692
+ scope = scope.parent;
693
+ }
694
+
695
+ return scope?.scopeRef === scopeRef;
696
+ }
697
+
698
+ function useRestoreFocus(
699
+ scopeRef: RefObject<Element[] | null>,
700
+ restoreFocus: boolean | undefined,
701
+ contain: boolean | undefined,
702
+ slot: symbol | undefined,
703
+ ) {
704
+ // create a ref during render instead of useLayoutEffect so the active element is saved before a child with autoFocus=true mounts.
705
+
706
+ const nodeToRestoreRef = useRef(
707
+ typeof document !== 'undefined'
708
+ ? (getActiveElement(
709
+ getOwnerDocument(scopeRef.current ? scopeRef.current[0] : undefined),
710
+ ) as FocusableElement)
711
+ : null,
712
+ subSlot(slot, 'node'),
713
+ );
714
+
715
+ // restoring scopes should all track if they are active regardless of contain, but contain already tracks it plus logic to contain the focus
716
+ // restoring-non-containing scopes should only care if they become active so they can perform the restore
717
+ useLayoutEffect(
718
+ () => {
719
+ let scope = scopeRef.current;
720
+ const ownerDocument = getOwnerDocument(scope ? scope[0] : undefined);
721
+ if (!restoreFocus || contain) {
722
+ return;
723
+ }
724
+
725
+ let onFocus = () => {
726
+ // If focusing an element in a child scope of the currently active scope, the child becomes active.
727
+ // Moving out of the active scope to an ancestor is not allowed.
728
+ if (
729
+ (!activeScope || isAncestorScope(activeScope, scopeRef)) &&
730
+ isElementInScope(getActiveElement(ownerDocument), scopeRef.current)
731
+ ) {
732
+ activeScope = scopeRef;
733
+ }
734
+ };
735
+
736
+ ownerDocument.addEventListener('focusin', onFocus, false);
737
+ scope?.forEach((element) => element.addEventListener('focusin', onFocus, false));
738
+ return () => {
739
+ ownerDocument.removeEventListener('focusin', onFocus, false);
740
+ scope?.forEach((element) => element.removeEventListener('focusin', onFocus, false));
741
+ };
742
+ // `restoreFocus` is intentionally omitted, matching upstream's exhaustive-deps opt-out.
743
+ },
744
+ [scopeRef, contain],
745
+ subSlot(slot, 'track'),
746
+ );
747
+
748
+ useLayoutEffect(
749
+ () => {
750
+ const ownerDocument = getOwnerDocument(scopeRef.current ? scopeRef.current[0] : undefined);
751
+
752
+ if (!restoreFocus) {
753
+ return;
754
+ }
755
+
756
+ // Handle the Tab key so that tabbing out of the scope goes to the next element
757
+ // after the node that had focus when the scope mounted. This is important when
758
+ // using portals for overlays, so that focus goes to the expected element when
759
+ // tabbing out of the overlay.
760
+ let onKeyDown = (e: KeyboardEvent) => {
761
+ if (
762
+ e.key !== 'Tab' ||
763
+ e.altKey ||
764
+ e.ctrlKey ||
765
+ e.metaKey ||
766
+ !shouldContainFocus(scopeRef) ||
767
+ e.isComposing
768
+ ) {
769
+ return;
770
+ }
771
+
772
+ let focusedElement = ownerDocument.activeElement as FocusableElement;
773
+ if (!isElementInChildScope(focusedElement, scopeRef) || !shouldRestoreFocus(scopeRef)) {
774
+ return;
775
+ }
776
+ let treeNode = focusScopeTree.getTreeNode(scopeRef);
777
+ if (!treeNode) {
778
+ return;
779
+ }
780
+ let nodeToRestore = treeNode.nodeToRestore;
781
+
782
+ // Create a DOM tree walker that matches all tabbable elements
783
+ let walker = getFocusableTreeWalker(ownerDocument.body, { tabbable: true });
784
+
785
+ // Find the next tabbable element after the currently focused element
786
+ walker.currentNode = focusedElement;
787
+ let nextElement = (
788
+ e.shiftKey ? walker.previousNode() : walker.nextNode()
789
+ ) as FocusableElement;
790
+
791
+ if (!nodeToRestore || !nodeToRestore.isConnected || nodeToRestore === ownerDocument.body) {
792
+ nodeToRestore = undefined;
793
+ treeNode.nodeToRestore = undefined;
794
+ }
795
+
796
+ // If there is no next element, or it is outside the current scope, move focus to the
797
+ // next element after the node to restore to instead.
798
+ if ((!nextElement || !isElementInChildScope(nextElement, scopeRef)) && nodeToRestore) {
799
+ walker.currentNode = nodeToRestore;
800
+
801
+ // Skip over elements within the scope, in case the scope immediately follows the node to restore.
802
+ do {
803
+ nextElement = (
804
+ e.shiftKey ? walker.previousNode() : walker.nextNode()
805
+ ) as FocusableElement;
806
+ } while (isElementInChildScope(nextElement, scopeRef));
807
+
808
+ e.preventDefault();
809
+ e.stopPropagation();
810
+ if (nextElement) {
811
+ focusElement(nextElement, true);
812
+ } else {
813
+ // If there is no next element and the nodeToRestore isn't within a FocusScope (i.e. we are leaving the top level focus scope)
814
+ // then move focus to the body.
815
+ // Otherwise restore focus to the nodeToRestore (e.g menu within a popover -> tabbing to close the menu should move focus to menu trigger)
816
+ if (!isElementInAnyScope(nodeToRestore)) {
817
+ focusedElement.blur();
818
+ } else {
819
+ focusElement(nodeToRestore, true);
820
+ }
821
+ }
822
+ }
823
+ };
824
+
825
+ if (!contain) {
826
+ ownerDocument.addEventListener('keydown', onKeyDown as EventListener, true);
827
+ }
828
+
829
+ return () => {
830
+ if (!contain) {
831
+ ownerDocument.removeEventListener('keydown', onKeyDown as EventListener, true);
832
+ }
833
+ };
834
+ },
835
+ [scopeRef, restoreFocus, contain],
836
+ subSlot(slot, 'tabOut'),
837
+ );
838
+
839
+ // useLayoutEffect instead of useEffect so the active element is saved synchronously instead of asynchronously.
840
+ useLayoutEffect(
841
+ () => {
842
+ const ownerDocument = getOwnerDocument(scopeRef.current ? scopeRef.current[0] : undefined);
843
+
844
+ if (!restoreFocus) {
845
+ return;
846
+ }
847
+
848
+ let treeNode = focusScopeTree.getTreeNode(scopeRef);
849
+ if (!treeNode) {
850
+ return;
851
+ }
852
+ treeNode.nodeToRestore = nodeToRestoreRef.current ?? undefined;
853
+ return () => {
854
+ let treeNode = focusScopeTree.getTreeNode(scopeRef);
855
+ if (!treeNode) {
856
+ return;
857
+ }
858
+ let nodeToRestore = treeNode.nodeToRestore;
859
+
860
+ // if we already lost focus to the body and this was the active scope, then we should attempt to restore
861
+ let activeElement = getActiveElement(ownerDocument);
862
+ if (
863
+ restoreFocus &&
864
+ nodeToRestore &&
865
+ ((activeElement && isElementInChildScope(activeElement, scopeRef)) ||
866
+ (activeElement === ownerDocument.body && shouldRestoreFocus(scopeRef)))
867
+ ) {
868
+ // freeze the focusScopeTree so it persists after the raf, otherwise during unmount nodes are removed from it
869
+ let clonedTree = focusScopeTree.clone();
870
+ requestAnimationFrame(() => {
871
+ // Only restore focus if we've lost focus to the body, the alternative is that focus has been purposefully moved elsewhere
872
+ if (ownerDocument.activeElement === ownerDocument.body) {
873
+ // look up the tree starting with our scope to find a nodeToRestore still in the DOM
874
+ let treeNode = clonedTree.getTreeNode(scopeRef);
875
+ while (treeNode) {
876
+ if (treeNode.nodeToRestore && treeNode.nodeToRestore.isConnected) {
877
+ restoreFocusToElement(treeNode.nodeToRestore);
878
+ return;
879
+ }
880
+ treeNode = treeNode.parent;
881
+ }
882
+
883
+ // If no nodeToRestore was found, focus the first element in the nearest
884
+ // ancestor scope that is still in the tree.
885
+ treeNode = clonedTree.getTreeNode(scopeRef);
886
+ while (treeNode) {
887
+ if (
888
+ treeNode.scopeRef &&
889
+ treeNode.scopeRef.current &&
890
+ focusScopeTree.getTreeNode(treeNode.scopeRef)
891
+ ) {
892
+ let node = getFirstInScope(treeNode.scopeRef.current, true);
893
+ restoreFocusToElement(node);
894
+ return;
895
+ }
896
+ treeNode = treeNode.parent;
897
+ }
898
+ }
899
+ });
900
+ }
901
+ };
902
+ },
903
+ [scopeRef, restoreFocus],
904
+ subSlot(slot, 'unmount'),
905
+ );
906
+ }
907
+
908
+ function restoreFocusToElement(node: FocusableElement) {
909
+ // Dispatch a custom event that parent elements can intercept to customize focus restoration.
910
+ // For example, virtualized collection components reuse DOM elements, so the original element
911
+ // might still exist in the DOM but representing a different item.
912
+ if (
913
+ node.dispatchEvent(new CustomEvent(RESTORE_FOCUS_EVENT, { bubbles: true, cancelable: true }))
914
+ ) {
915
+ focusElement(node);
916
+ }
917
+ }
918
+
919
+ /**
920
+ * Create a [TreeWalker]{@link https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker}
921
+ * that matches all focusable/tabbable elements.
922
+ */
923
+ export function getFocusableTreeWalker(
924
+ root: Element,
925
+ opts?: FocusManagerOptions,
926
+ scope?: Element[],
927
+ ): ShadowTreeWalker | TreeWalker {
928
+ let filter = opts?.tabbable ? isTabbable : isFocusable;
929
+
930
+ // Ensure that root is an Element or fall back appropriately
931
+ let rootElement = root?.nodeType === Node.ELEMENT_NODE ? (root as Element) : null;
932
+
933
+ // Determine the document to use
934
+ let doc = getOwnerDocument(rootElement);
935
+
936
+ // Create a TreeWalker, ensuring the root is an Element or Document
937
+ let walker = createShadowTreeWalker(doc, root || doc, NodeFilter.SHOW_ELEMENT, {
938
+ acceptNode(node) {
939
+ // Skip nodes inside the starting node.
940
+ if (nodeContains(opts?.from, node)) {
941
+ return NodeFilter.FILTER_REJECT;
942
+ }
943
+
944
+ if (
945
+ opts?.tabbable &&
946
+ (node as Element).tagName === 'INPUT' &&
947
+ (node as HTMLInputElement).getAttribute('type') === 'radio'
948
+ ) {
949
+ // If the radio is in a form, we can get all the other radios by name
950
+ if (!isTabbableRadio(node as HTMLInputElement)) {
951
+ return NodeFilter.FILTER_REJECT;
952
+ }
953
+ // If the radio is in the same group as the current node and none are selected, we can skip it
954
+ if (
955
+ (walker.currentNode as Element).tagName === 'INPUT' &&
956
+ (walker.currentNode as HTMLInputElement).type === 'radio' &&
957
+ (walker.currentNode as HTMLInputElement).name === (node as HTMLInputElement).name
958
+ ) {
959
+ return NodeFilter.FILTER_REJECT;
960
+ }
961
+ }
962
+
963
+ if (
964
+ filter(node as Element) &&
965
+ (!scope || isElementInScope(node as Element, scope)) &&
966
+ (!opts?.accept || opts.accept(node as Element))
967
+ ) {
968
+ return NodeFilter.FILTER_ACCEPT;
969
+ }
970
+
971
+ return NodeFilter.FILTER_SKIP;
972
+ },
973
+ });
974
+
975
+ if (opts?.from) {
976
+ walker.currentNode = opts.from;
977
+ }
978
+
979
+ return walker;
980
+ }
981
+
982
+ /**
983
+ * Creates a FocusManager object that can be used to move focus within an element.
984
+ */
985
+ export function createFocusManager(
986
+ ref: RefObject<Element | null>,
987
+ defaultOptions: FocusManagerOptions = {},
988
+ ): FocusManager {
989
+ return {
990
+ focusNext(opts: FocusManagerOptions = {}) {
991
+ let root = ref.current;
992
+ if (!root) {
993
+ return null;
994
+ }
995
+ let {
996
+ from,
997
+ tabbable = defaultOptions.tabbable,
998
+ wrap = defaultOptions.wrap,
999
+ accept = defaultOptions.accept,
1000
+ } = opts;
1001
+ let node = from || getActiveElement(getOwnerDocument(root));
1002
+ let walker = getFocusableTreeWalker(root, { tabbable, accept });
1003
+ if (nodeContains(root, node)) {
1004
+ walker.currentNode = node!;
1005
+ }
1006
+ let nextNode = walker.nextNode() as FocusableElement;
1007
+ if (!nextNode && wrap) {
1008
+ walker.currentNode = root;
1009
+ nextNode = walker.nextNode() as FocusableElement;
1010
+ }
1011
+ if (nextNode) {
1012
+ focusElement(nextNode, true);
1013
+ }
1014
+ return nextNode;
1015
+ },
1016
+ focusPrevious(opts: FocusManagerOptions = defaultOptions) {
1017
+ let root = ref.current;
1018
+ if (!root) {
1019
+ return null;
1020
+ }
1021
+ let {
1022
+ from,
1023
+ tabbable = defaultOptions.tabbable,
1024
+ wrap = defaultOptions.wrap,
1025
+ accept = defaultOptions.accept,
1026
+ } = opts;
1027
+ let node = from || getActiveElement(getOwnerDocument(root));
1028
+ let walker = getFocusableTreeWalker(root, { tabbable, accept });
1029
+ if (nodeContains(root, node)) {
1030
+ walker.currentNode = node!;
1031
+ } else {
1032
+ let next = last(walker);
1033
+ if (next) {
1034
+ focusElement(next, true);
1035
+ }
1036
+ return next ?? null;
1037
+ }
1038
+ let previousNode = walker.previousNode() as FocusableElement;
1039
+ if (!previousNode && wrap) {
1040
+ walker.currentNode = root;
1041
+ let lastNode = last(walker);
1042
+ if (!lastNode) {
1043
+ // couldn't wrap
1044
+ return null;
1045
+ }
1046
+ previousNode = lastNode;
1047
+ }
1048
+ if (previousNode) {
1049
+ focusElement(previousNode, true);
1050
+ }
1051
+ return previousNode ?? null;
1052
+ },
1053
+ focusFirst(opts = defaultOptions) {
1054
+ let root = ref.current;
1055
+ if (!root) {
1056
+ return null;
1057
+ }
1058
+ let { tabbable = defaultOptions.tabbable, accept = defaultOptions.accept } = opts;
1059
+ let walker = getFocusableTreeWalker(root, { tabbable, accept });
1060
+ let nextNode = walker.nextNode() as FocusableElement;
1061
+ if (nextNode) {
1062
+ focusElement(nextNode, true);
1063
+ }
1064
+ return nextNode;
1065
+ },
1066
+ focusLast(opts = defaultOptions) {
1067
+ let root = ref.current;
1068
+ if (!root) {
1069
+ return null;
1070
+ }
1071
+ let { tabbable = defaultOptions.tabbable, accept = defaultOptions.accept } = opts;
1072
+ let walker = getFocusableTreeWalker(root, { tabbable, accept });
1073
+ let next = last(walker);
1074
+ if (next) {
1075
+ focusElement(next, true);
1076
+ }
1077
+ return next ?? null;
1078
+ },
1079
+ };
1080
+ }
1081
+
1082
+ function last(walker: ShadowTreeWalker | TreeWalker) {
1083
+ let next: FocusableElement | undefined = undefined;
1084
+ let last: FocusableElement;
1085
+ do {
1086
+ last = walker.lastChild() as FocusableElement;
1087
+ if (last) {
1088
+ next = last;
1089
+ }
1090
+ } while (last);
1091
+ return next;
1092
+ }
1093
+
1094
+ class Tree {
1095
+ root: TreeNode;
1096
+ private fastMap = new Map<ScopeRef, TreeNode>();
1097
+
1098
+ constructor() {
1099
+ this.root = new TreeNode({ scopeRef: null });
1100
+ this.fastMap.set(null, this.root);
1101
+ }
1102
+
1103
+ get size(): number {
1104
+ return this.fastMap.size;
1105
+ }
1106
+
1107
+ getTreeNode(data: ScopeRef): TreeNode | undefined {
1108
+ return this.fastMap.get(data);
1109
+ }
1110
+
1111
+ addTreeNode(scopeRef: ScopeRef, parent: ScopeRef, nodeToRestore?: FocusableElement): void {
1112
+ let parentNode = this.fastMap.get(parent ?? null);
1113
+ if (!parentNode) {
1114
+ return;
1115
+ }
1116
+ let node = new TreeNode({ scopeRef });
1117
+ parentNode.addChild(node);
1118
+ node.parent = parentNode;
1119
+ this.fastMap.set(scopeRef, node);
1120
+ if (nodeToRestore) {
1121
+ node.nodeToRestore = nodeToRestore;
1122
+ }
1123
+ }
1124
+
1125
+ addNode(node: TreeNode): void {
1126
+ this.fastMap.set(node.scopeRef, node);
1127
+ }
1128
+
1129
+ removeTreeNode(scopeRef: ScopeRef): void {
1130
+ // never remove the root
1131
+ if (scopeRef === null) {
1132
+ return;
1133
+ }
1134
+ let node = this.fastMap.get(scopeRef);
1135
+ if (!node) {
1136
+ return;
1137
+ }
1138
+ let parentNode = node.parent;
1139
+ // when we remove a scope, check if any sibling scopes are trying to restore focus to something inside the scope we're removing
1140
+ // if we are, then replace the siblings restore with the restore from the scope we're removing
1141
+ for (let current of this.traverse()) {
1142
+ if (
1143
+ current !== node &&
1144
+ node.nodeToRestore &&
1145
+ current.nodeToRestore &&
1146
+ node.scopeRef &&
1147
+ node.scopeRef.current &&
1148
+ isElementInScope(current.nodeToRestore, node.scopeRef.current)
1149
+ ) {
1150
+ current.nodeToRestore = node.nodeToRestore;
1151
+ }
1152
+ }
1153
+ let children = node.children;
1154
+ if (parentNode) {
1155
+ parentNode.removeChild(node);
1156
+ if (children.size > 0) {
1157
+ children.forEach((child) => parentNode && parentNode.addChild(child));
1158
+ }
1159
+ }
1160
+
1161
+ this.fastMap.delete(node.scopeRef);
1162
+ }
1163
+
1164
+ // Pre Order Depth First
1165
+ *traverse(node: TreeNode = this.root): Generator<TreeNode> {
1166
+ if (node.scopeRef != null) {
1167
+ yield node;
1168
+ }
1169
+ if (node.children.size > 0) {
1170
+ for (let child of node.children) {
1171
+ yield* this.traverse(child);
1172
+ }
1173
+ }
1174
+ }
1175
+
1176
+ clone(): Tree {
1177
+ let newTree = new Tree();
1178
+ for (let node of this.traverse()) {
1179
+ newTree.addTreeNode(node.scopeRef, node.parent?.scopeRef ?? null, node.nodeToRestore);
1180
+ }
1181
+ return newTree;
1182
+ }
1183
+ }
1184
+
1185
+ class TreeNode {
1186
+ public scopeRef: ScopeRef;
1187
+ public nodeToRestore?: FocusableElement;
1188
+ public parent?: TreeNode;
1189
+ public children: Set<TreeNode> = new Set();
1190
+ public contain = false;
1191
+
1192
+ constructor(props: { scopeRef: ScopeRef }) {
1193
+ this.scopeRef = props.scopeRef;
1194
+ }
1195
+ addChild(node: TreeNode): void {
1196
+ this.children.add(node);
1197
+ node.parent = this;
1198
+ }
1199
+ removeChild(node: TreeNode): void {
1200
+ this.children.delete(node);
1201
+ node.parent = undefined;
1202
+ }
1203
+ }
1204
+
1205
+ export let focusScopeTree: Tree = new Tree();