@octanejs/aria 0.0.40 → 0.0.42

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 (416) hide show
  1. package/LICENSE-APACHE-2.0 +201 -0
  2. package/README.md +20 -7
  3. package/UPSTREAM.md +108 -0
  4. package/package.json +10 -6
  5. package/src/calendar/useCalendar.tsx +36 -0
  6. package/src/calendar/useCalendarBase.tsx +142 -0
  7. package/src/calendar/useCalendarCell.tsx +416 -0
  8. package/src/calendar/useCalendarGrid.tsx +202 -0
  9. package/src/calendar/useCalendarHeading.tsx +85 -0
  10. package/src/calendar/useCalendarMonthPicker.tsx +92 -0
  11. package/src/calendar/useCalendarYearPicker.tsx +127 -0
  12. package/src/calendar/useRangeCalendar.tsx +121 -0
  13. package/src/calendar/utils.tsx +183 -0
  14. package/src/color/useColorArea.tsx +599 -0
  15. package/src/color/useColorAreaGradient.tsx +157 -0
  16. package/src/color/useColorChannelField.tsx +53 -0
  17. package/src/color/useColorField.tsx +170 -0
  18. package/src/color/useColorSlider.tsx +177 -0
  19. package/src/color/useColorSwatch.tsx +83 -0
  20. package/src/color/useColorWheel.tsx +401 -0
  21. package/src/compat/react.ts +111 -0
  22. package/src/components/Calendar.tsx +842 -0
  23. package/src/components/ColorArea.tsx +120 -0
  24. package/src/components/ColorField.tsx +304 -0
  25. package/src/components/ColorPicker.tsx +86 -0
  26. package/src/components/ColorSlider.tsx +144 -0
  27. package/src/components/ColorSwatch.tsx +72 -0
  28. package/src/components/ColorSwatchPicker.tsx +176 -0
  29. package/src/components/ColorThumb.tsx +140 -0
  30. package/src/components/ColorWheel.tsx +163 -0
  31. package/src/components/DateField.tsx +580 -0
  32. package/src/components/DatePicker.tsx +386 -0
  33. package/src/components/DropZone.tsx +181 -0
  34. package/src/components/FileTrigger.tsx +96 -0
  35. package/src/components/GridLayout.tsx +33 -0
  36. package/src/components/HiddenDateInput.tsx +169 -0
  37. package/src/components/TableLayout.tsx +39 -0
  38. package/src/components/Toast.tsx +336 -0
  39. package/src/components/Virtualizer.tsx +219 -0
  40. package/src/components/index.ts +257 -7
  41. package/src/components/useDragAndDrop.tsx +221 -0
  42. package/src/components/utils.ts +12 -89
  43. package/src/datepicker/useDateField.tsx +272 -0
  44. package/src/datepicker/useDatePicker.tsx +229 -0
  45. package/src/datepicker/useDatePickerGroup.tsx +147 -0
  46. package/src/datepicker/useDateRangePicker.tsx +288 -0
  47. package/src/datepicker/useDateSegment.tsx +439 -0
  48. package/src/datepicker/useDisplayNames.tsx +53 -0
  49. package/src/dnd/DragManager.tsx +718 -0
  50. package/src/dnd/DragPreview.tsx +100 -0
  51. package/src/dnd/DropTargetKeyboardNavigation.tsx +317 -0
  52. package/src/dnd/ListDropTargetDelegate.tsx +215 -0
  53. package/src/dnd/constants.tsx +93 -0
  54. package/src/dnd/useAutoScroll.tsx +116 -0
  55. package/src/dnd/useClipboard.tsx +160 -0
  56. package/src/dnd/useDrag.tsx +437 -0
  57. package/src/dnd/useDraggableCollection.tsx +35 -0
  58. package/src/dnd/useDraggableItem.tsx +168 -0
  59. package/src/dnd/useDrop.tsx +518 -0
  60. package/src/dnd/useDropIndicator.tsx +129 -0
  61. package/src/dnd/useDroppableCollection.tsx +808 -0
  62. package/src/dnd/useDroppableItem.tsx +99 -0
  63. package/src/dnd/useVirtualDrop.tsx +52 -0
  64. package/src/dnd/utils.tsx +430 -0
  65. package/src/intl/calendar/ar-AE.json +14 -0
  66. package/src/intl/calendar/bg-BG.json +14 -0
  67. package/src/intl/calendar/cs-CZ.json +14 -0
  68. package/src/intl/calendar/da-DK.json +14 -0
  69. package/src/intl/calendar/de-DE.json +14 -0
  70. package/src/intl/calendar/el-GR.json +14 -0
  71. package/src/intl/calendar/en-US.json +14 -0
  72. package/src/intl/calendar/es-ES.json +14 -0
  73. package/src/intl/calendar/et-EE.json +14 -0
  74. package/src/intl/calendar/fi-FI.json +14 -0
  75. package/src/intl/calendar/fr-FR.json +14 -0
  76. package/src/intl/calendar/he-IL.json +14 -0
  77. package/src/intl/calendar/hr-HR.json +14 -0
  78. package/src/intl/calendar/hu-HU.json +14 -0
  79. package/src/intl/calendar/index.ts +73 -0
  80. package/src/intl/calendar/it-IT.json +14 -0
  81. package/src/intl/calendar/ja-JP.json +14 -0
  82. package/src/intl/calendar/ko-KR.json +14 -0
  83. package/src/intl/calendar/lt-LT.json +14 -0
  84. package/src/intl/calendar/lv-LV.json +14 -0
  85. package/src/intl/calendar/nb-NO.json +14 -0
  86. package/src/intl/calendar/nl-NL.json +14 -0
  87. package/src/intl/calendar/pl-PL.json +14 -0
  88. package/src/intl/calendar/pt-BR.json +14 -0
  89. package/src/intl/calendar/pt-PT.json +14 -0
  90. package/src/intl/calendar/ro-RO.json +14 -0
  91. package/src/intl/calendar/ru-RU.json +14 -0
  92. package/src/intl/calendar/sk-SK.json +14 -0
  93. package/src/intl/calendar/sl-SI.json +14 -0
  94. package/src/intl/calendar/sr-SP.json +14 -0
  95. package/src/intl/calendar/sv-SE.json +14 -0
  96. package/src/intl/calendar/tr-TR.json +14 -0
  97. package/src/intl/calendar/uk-UA.json +14 -0
  98. package/src/intl/calendar/zh-CN.json +14 -0
  99. package/src/intl/calendar/zh-TW.json +14 -0
  100. package/src/intl/color/ar-AE.json +8 -0
  101. package/src/intl/color/bg-BG.json +8 -0
  102. package/src/intl/color/cs-CZ.json +8 -0
  103. package/src/intl/color/da-DK.json +8 -0
  104. package/src/intl/color/de-DE.json +8 -0
  105. package/src/intl/color/el-GR.json +8 -0
  106. package/src/intl/color/en-US.json +8 -0
  107. package/src/intl/color/es-ES.json +8 -0
  108. package/src/intl/color/et-EE.json +8 -0
  109. package/src/intl/color/fi-FI.json +8 -0
  110. package/src/intl/color/fr-FR.json +8 -0
  111. package/src/intl/color/he-IL.json +8 -0
  112. package/src/intl/color/hr-HR.json +8 -0
  113. package/src/intl/color/hu-HU.json +8 -0
  114. package/src/intl/color/index.ts +73 -0
  115. package/src/intl/color/it-IT.json +8 -0
  116. package/src/intl/color/ja-JP.json +8 -0
  117. package/src/intl/color/ko-KR.json +8 -0
  118. package/src/intl/color/lt-LT.json +8 -0
  119. package/src/intl/color/lv-LV.json +8 -0
  120. package/src/intl/color/nb-NO.json +8 -0
  121. package/src/intl/color/nl-NL.json +8 -0
  122. package/src/intl/color/pl-PL.json +8 -0
  123. package/src/intl/color/pt-BR.json +8 -0
  124. package/src/intl/color/pt-PT.json +8 -0
  125. package/src/intl/color/ro-RO.json +8 -0
  126. package/src/intl/color/ru-RU.json +8 -0
  127. package/src/intl/color/sk-SK.json +8 -0
  128. package/src/intl/color/sl-SI.json +8 -0
  129. package/src/intl/color/sr-SP.json +8 -0
  130. package/src/intl/color/sv-SE.json +8 -0
  131. package/src/intl/color/tr-TR.json +8 -0
  132. package/src/intl/color/uk-UA.json +8 -0
  133. package/src/intl/color/zh-CN.json +8 -0
  134. package/src/intl/color/zh-TW.json +8 -0
  135. package/src/intl/datepicker/ar-AE.json +18 -0
  136. package/src/intl/datepicker/bg-BG.json +18 -0
  137. package/src/intl/datepicker/cs-CZ.json +18 -0
  138. package/src/intl/datepicker/da-DK.json +18 -0
  139. package/src/intl/datepicker/de-DE.json +18 -0
  140. package/src/intl/datepicker/el-GR.json +18 -0
  141. package/src/intl/datepicker/en-US.json +18 -0
  142. package/src/intl/datepicker/es-ES.json +18 -0
  143. package/src/intl/datepicker/et-EE.json +18 -0
  144. package/src/intl/datepicker/fi-FI.json +18 -0
  145. package/src/intl/datepicker/fr-FR.json +18 -0
  146. package/src/intl/datepicker/he-IL.json +18 -0
  147. package/src/intl/datepicker/hr-HR.json +18 -0
  148. package/src/intl/datepicker/hu-HU.json +18 -0
  149. package/src/intl/datepicker/index.ts +73 -0
  150. package/src/intl/datepicker/it-IT.json +18 -0
  151. package/src/intl/datepicker/ja-JP.json +18 -0
  152. package/src/intl/datepicker/ko-KR.json +18 -0
  153. package/src/intl/datepicker/lt-LT.json +18 -0
  154. package/src/intl/datepicker/lv-LV.json +18 -0
  155. package/src/intl/datepicker/nb-NO.json +18 -0
  156. package/src/intl/datepicker/nl-NL.json +18 -0
  157. package/src/intl/datepicker/pl-PL.json +18 -0
  158. package/src/intl/datepicker/pt-BR.json +18 -0
  159. package/src/intl/datepicker/pt-PT.json +18 -0
  160. package/src/intl/datepicker/ro-RO.json +18 -0
  161. package/src/intl/datepicker/ru-RU.json +18 -0
  162. package/src/intl/datepicker/sk-SK.json +18 -0
  163. package/src/intl/datepicker/sl-SI.json +18 -0
  164. package/src/intl/datepicker/sr-SP.json +18 -0
  165. package/src/intl/datepicker/sv-SE.json +18 -0
  166. package/src/intl/datepicker/tr-TR.json +18 -0
  167. package/src/intl/datepicker/uk-UA.json +18 -0
  168. package/src/intl/datepicker/zh-CN.json +18 -0
  169. package/src/intl/datepicker/zh-TW.json +18 -0
  170. package/src/intl/dnd/ar-AE.json +29 -0
  171. package/src/intl/dnd/bg-BG.json +29 -0
  172. package/src/intl/dnd/cs-CZ.json +29 -0
  173. package/src/intl/dnd/da-DK.json +29 -0
  174. package/src/intl/dnd/de-DE.json +29 -0
  175. package/src/intl/dnd/el-GR.json +29 -0
  176. package/src/intl/dnd/en-US.json +29 -0
  177. package/src/intl/dnd/es-ES.json +29 -0
  178. package/src/intl/dnd/et-EE.json +29 -0
  179. package/src/intl/dnd/fi-FI.json +29 -0
  180. package/src/intl/dnd/fr-FR.json +29 -0
  181. package/src/intl/dnd/he-IL.json +29 -0
  182. package/src/intl/dnd/hr-HR.json +29 -0
  183. package/src/intl/dnd/hu-HU.json +29 -0
  184. package/src/intl/dnd/index.ts +73 -0
  185. package/src/intl/dnd/it-IT.json +29 -0
  186. package/src/intl/dnd/ja-JP.json +29 -0
  187. package/src/intl/dnd/ko-KR.json +29 -0
  188. package/src/intl/dnd/lt-LT.json +29 -0
  189. package/src/intl/dnd/lv-LV.json +29 -0
  190. package/src/intl/dnd/nb-NO.json +29 -0
  191. package/src/intl/dnd/nl-NL.json +29 -0
  192. package/src/intl/dnd/pl-PL.json +29 -0
  193. package/src/intl/dnd/pt-BR.json +29 -0
  194. package/src/intl/dnd/pt-PT.json +29 -0
  195. package/src/intl/dnd/ro-RO.json +29 -0
  196. package/src/intl/dnd/ru-RU.json +29 -0
  197. package/src/intl/dnd/sk-SK.json +29 -0
  198. package/src/intl/dnd/sl-SI.json +29 -0
  199. package/src/intl/dnd/sr-SP.json +29 -0
  200. package/src/intl/dnd/sv-SE.json +29 -0
  201. package/src/intl/dnd/tr-TR.json +29 -0
  202. package/src/intl/dnd/uk-UA.json +29 -0
  203. package/src/intl/dnd/zh-CN.json +29 -0
  204. package/src/intl/dnd/zh-TW.json +29 -0
  205. package/src/intl/react-aria-components/ar-AE.json +6 -0
  206. package/src/intl/react-aria-components/bg-BG.json +6 -0
  207. package/src/intl/react-aria-components/cs-CZ.json +6 -0
  208. package/src/intl/react-aria-components/da-DK.json +6 -0
  209. package/src/intl/react-aria-components/de-DE.json +6 -0
  210. package/src/intl/react-aria-components/el-GR.json +6 -0
  211. package/src/intl/react-aria-components/en-US.json +6 -0
  212. package/src/intl/react-aria-components/es-ES.json +6 -0
  213. package/src/intl/react-aria-components/et-EE.json +6 -0
  214. package/src/intl/react-aria-components/fi-FI.json +6 -0
  215. package/src/intl/react-aria-components/fr-FR.json +6 -0
  216. package/src/intl/react-aria-components/he-IL.json +6 -0
  217. package/src/intl/react-aria-components/hr-HR.json +6 -0
  218. package/src/intl/react-aria-components/hu-HU.json +6 -0
  219. package/src/intl/react-aria-components/index.ts +73 -0
  220. package/src/intl/react-aria-components/it-IT.json +6 -0
  221. package/src/intl/react-aria-components/ja-JP.json +6 -0
  222. package/src/intl/react-aria-components/ko-KR.json +6 -0
  223. package/src/intl/react-aria-components/lt-LT.json +6 -0
  224. package/src/intl/react-aria-components/lv-LV.json +6 -0
  225. package/src/intl/react-aria-components/nb-NO.json +6 -0
  226. package/src/intl/react-aria-components/nl-NL.json +6 -0
  227. package/src/intl/react-aria-components/pl-PL.json +6 -0
  228. package/src/intl/react-aria-components/pt-BR.json +6 -0
  229. package/src/intl/react-aria-components/pt-PT.json +6 -0
  230. package/src/intl/react-aria-components/ro-RO.json +6 -0
  231. package/src/intl/react-aria-components/ru-RU.json +6 -0
  232. package/src/intl/react-aria-components/sk-SK.json +6 -0
  233. package/src/intl/react-aria-components/sl-SI.json +6 -0
  234. package/src/intl/react-aria-components/sr-SP.json +6 -0
  235. package/src/intl/react-aria-components/sv-SE.json +6 -0
  236. package/src/intl/react-aria-components/tr-TR.json +6 -0
  237. package/src/intl/react-aria-components/uk-UA.json +6 -0
  238. package/src/intl/react-aria-components/zh-CN.json +6 -0
  239. package/src/intl/react-aria-components/zh-TW.json +6 -0
  240. package/src/intl/stately-color/ar-AE.json +39 -0
  241. package/src/intl/stately-color/bg-BG.json +39 -0
  242. package/src/intl/stately-color/cs-CZ.json +39 -0
  243. package/src/intl/stately-color/da-DK.json +39 -0
  244. package/src/intl/stately-color/de-DE.json +39 -0
  245. package/src/intl/stately-color/el-GR.json +39 -0
  246. package/src/intl/stately-color/en-US.json +39 -0
  247. package/src/intl/stately-color/es-ES.json +39 -0
  248. package/src/intl/stately-color/et-EE.json +39 -0
  249. package/src/intl/stately-color/fi-FI.json +39 -0
  250. package/src/intl/stately-color/fr-FR.json +39 -0
  251. package/src/intl/stately-color/he-IL.json +39 -0
  252. package/src/intl/stately-color/hr-HR.json +39 -0
  253. package/src/intl/stately-color/hu-HU.json +39 -0
  254. package/src/intl/stately-color/index.ts +73 -0
  255. package/src/intl/stately-color/it-IT.json +39 -0
  256. package/src/intl/stately-color/ja-JP.json +39 -0
  257. package/src/intl/stately-color/ko-KR.json +39 -0
  258. package/src/intl/stately-color/lt-LT.json +39 -0
  259. package/src/intl/stately-color/lv-LV.json +39 -0
  260. package/src/intl/stately-color/nb-NO.json +39 -0
  261. package/src/intl/stately-color/nl-NL.json +39 -0
  262. package/src/intl/stately-color/pl-PL.json +39 -0
  263. package/src/intl/stately-color/pt-BR.json +39 -0
  264. package/src/intl/stately-color/pt-PT.json +39 -0
  265. package/src/intl/stately-color/ro-RO.json +39 -0
  266. package/src/intl/stately-color/ru-RU.json +39 -0
  267. package/src/intl/stately-color/sk-SK.json +39 -0
  268. package/src/intl/stately-color/sl-SI.json +39 -0
  269. package/src/intl/stately-color/sr-SP.json +39 -0
  270. package/src/intl/stately-color/sv-SE.json +39 -0
  271. package/src/intl/stately-color/tr-TR.json +39 -0
  272. package/src/intl/stately-color/uk-UA.json +39 -0
  273. package/src/intl/stately-color/zh-CN.json +39 -0
  274. package/src/intl/stately-color/zh-TW.json +39 -0
  275. package/src/intl/toast/ar-AE.json +4 -0
  276. package/src/intl/toast/bg-BG.json +4 -0
  277. package/src/intl/toast/cs-CZ.json +4 -0
  278. package/src/intl/toast/da-DK.json +4 -0
  279. package/src/intl/toast/de-DE.json +4 -0
  280. package/src/intl/toast/el-GR.json +4 -0
  281. package/src/intl/toast/en-US.json +4 -0
  282. package/src/intl/toast/es-ES.json +4 -0
  283. package/src/intl/toast/et-EE.json +4 -0
  284. package/src/intl/toast/fi-FI.json +4 -0
  285. package/src/intl/toast/fr-FR.json +4 -0
  286. package/src/intl/toast/he-IL.json +4 -0
  287. package/src/intl/toast/hr-HR.json +4 -0
  288. package/src/intl/toast/hu-HU.json +4 -0
  289. package/src/intl/toast/index.ts +73 -0
  290. package/src/intl/toast/it-IT.json +4 -0
  291. package/src/intl/toast/ja-JP.json +4 -0
  292. package/src/intl/toast/ko-KR.json +4 -0
  293. package/src/intl/toast/lt-LT.json +4 -0
  294. package/src/intl/toast/lv-LV.json +4 -0
  295. package/src/intl/toast/nb-NO.json +4 -0
  296. package/src/intl/toast/nl-NL.json +4 -0
  297. package/src/intl/toast/pl-PL.json +4 -0
  298. package/src/intl/toast/pt-BR.json +4 -0
  299. package/src/intl/toast/pt-PT.json +4 -0
  300. package/src/intl/toast/ro-RO.json +4 -0
  301. package/src/intl/toast/ru-RU.json +4 -0
  302. package/src/intl/toast/sk-SK.json +4 -0
  303. package/src/intl/toast/sl-SI.json +4 -0
  304. package/src/intl/toast/sr-SP.json +4 -0
  305. package/src/intl/toast/sv-SE.json +4 -0
  306. package/src/intl/toast/tr-TR.json +4 -0
  307. package/src/intl/toast/uk-UA.json +4 -0
  308. package/src/intl/toast/zh-CN.json +4 -0
  309. package/src/intl/toast/zh-TW.json +4 -0
  310. package/src/landmark/useLandmark.tsx +569 -0
  311. package/src/stately/calendar/types.tsx +239 -0
  312. package/src/stately/calendar/useCalendarState.tsx +527 -0
  313. package/src/stately/calendar/useRangeCalendarState.tsx +323 -0
  314. package/src/stately/calendar/utils.tsx +175 -0
  315. package/src/stately/color/Color.tsx +889 -0
  316. package/src/stately/color/types.tsx +82 -0
  317. package/src/stately/color/useColor.tsx +30 -0
  318. package/src/stately/color/useColorAreaState.tsx +255 -0
  319. package/src/stately/color/useColorChannelFieldState.tsx +86 -0
  320. package/src/stately/color/useColorFieldState.tsx +220 -0
  321. package/src/stately/color/useColorPickerState.tsx +31 -0
  322. package/src/stately/color/useColorSliderState.tsx +143 -0
  323. package/src/stately/color/useColorWheelState.tsx +225 -0
  324. package/src/stately/data/useAsyncList.tsx +382 -0
  325. package/src/stately/data/useListData.tsx +447 -0
  326. package/src/stately/data/useTreeData.tsx +603 -0
  327. package/src/stately/datepicker/IncompleteDate.tsx +427 -0
  328. package/src/stately/datepicker/placeholders.tsx +116 -0
  329. package/src/stately/datepicker/types.tsx +169 -0
  330. package/src/stately/datepicker/useDateFieldState.tsx +564 -0
  331. package/src/stately/datepicker/useDatePickerState.tsx +244 -0
  332. package/src/stately/datepicker/useDateRangePickerState.tsx +397 -0
  333. package/src/stately/datepicker/useTimeFieldState.tsx +123 -0
  334. package/src/stately/datepicker/utils.tsx +316 -0
  335. package/src/stately/dnd/useDraggableCollectionState.tsx +191 -0
  336. package/src/stately/dnd/useDroppableCollectionState.tsx +288 -0
  337. package/src/stately/layout/GridLayout.tsx +449 -0
  338. package/src/stately/layout/ListLayout.tsx +872 -0
  339. package/src/stately/layout/TableLayout.tsx +793 -0
  340. package/src/stately/layout/WaterfallLayout.tsx +399 -0
  341. package/src/stately/toast/useToastState.tsx +219 -0
  342. package/src/stately/virtualizer/Layout.tsx +105 -0
  343. package/src/stately/virtualizer/LayoutInfo.tsx +127 -0
  344. package/src/stately/virtualizer/OverscanManager.tsx +56 -0
  345. package/src/stately/virtualizer/Point.tsx +47 -0
  346. package/src/stately/virtualizer/Rect.tsx +194 -0
  347. package/src/stately/virtualizer/ReusableView.tsx +101 -0
  348. package/src/stately/virtualizer/Size.tsx +48 -0
  349. package/src/stately/virtualizer/Virtualizer.tsx +408 -0
  350. package/src/stately/virtualizer/types.tsx +48 -0
  351. package/src/stately/virtualizer/useVirtualizerState.tsx +138 -0
  352. package/src/stately/virtualizer/utils.tsx +32 -0
  353. package/src/textfield/useTextField.ts +3 -0
  354. package/src/toast/useToast.tsx +105 -0
  355. package/src/toast/useToastRegion.tsx +223 -0
  356. package/src/upstream-exports/react-aria/I18nProvider.tsx +17 -0
  357. package/src/upstream-exports/react-aria/ListDropTargetDelegate.tsx +15 -0
  358. package/src/upstream-exports/react-aria/PortalProvider.tsx +19 -0
  359. package/src/upstream-exports/react-aria/SSRProvider.tsx +15 -0
  360. package/src/upstream-exports/react-aria/VisuallyHidden.tsx +15 -0
  361. package/src/upstream-exports/react-aria/filterDOMProps.tsx +3 -0
  362. package/src/upstream-exports/react-aria/mergeProps.tsx +14 -0
  363. package/src/upstream-exports/react-aria/private/dnd/DragManager.tsx +3 -0
  364. package/src/upstream-exports/react-aria/private/interactions/PressResponder.tsx +3 -0
  365. package/src/upstream-exports/react-aria/private/utils/isFocusable.tsx +3 -0
  366. package/src/upstream-exports/react-aria/private/utils/shadowdom/DOMFunctions.tsx +8 -0
  367. package/src/upstream-exports/react-aria/private/utils/useId.tsx +3 -0
  368. package/src/upstream-exports/react-aria/private/utils/useLabels.tsx +3 -0
  369. package/src/upstream-exports/react-aria/private/virtualizer/ScrollView.tsx +3 -0
  370. package/src/upstream-exports/react-aria/private/virtualizer/VirtualizerItem.tsx +3 -0
  371. package/src/upstream-exports/react-aria/useButton.tsx +22 -0
  372. package/src/upstream-exports/react-aria/useCalendar.tsx +37 -0
  373. package/src/upstream-exports/react-aria/useClipboard.tsx +23 -0
  374. package/src/upstream-exports/react-aria/useColorArea.tsx +19 -0
  375. package/src/upstream-exports/react-aria/useColorField.tsx +20 -0
  376. package/src/upstream-exports/react-aria/useColorSlider.tsx +19 -0
  377. package/src/upstream-exports/react-aria/useColorSwatch.tsx +15 -0
  378. package/src/upstream-exports/react-aria/useColorWheel.tsx +19 -0
  379. package/src/upstream-exports/react-aria/useDateField.tsx +23 -0
  380. package/src/upstream-exports/react-aria/useDatePicker.tsx +17 -0
  381. package/src/upstream-exports/react-aria/useDateRangePicker.tsx +24 -0
  382. package/src/upstream-exports/react-aria/useDraggableCollection.tsx +31 -0
  383. package/src/upstream-exports/react-aria/useDrop.tsx +33 -0
  384. package/src/upstream-exports/react-aria/useDroppableCollection.tsx +47 -0
  385. package/src/upstream-exports/react-aria/useFocusRing.tsx +16 -0
  386. package/src/upstream-exports/react-aria/useHover.tsx +17 -0
  387. package/src/upstream-exports/react-aria/useLocalizedStringFormatter.tsx +19 -0
  388. package/src/upstream-exports/react-aria/useObjectRef.tsx +14 -0
  389. package/src/upstream-exports/react-aria/useRangeCalendar.tsx +38 -0
  390. package/src/upstream-exports/react-aria/useTimeField.tsx +23 -0
  391. package/src/upstream-exports/react-aria/useToast.tsx +18 -0
  392. package/src/upstream-exports/react-stately/Color.tsx +23 -0
  393. package/src/upstream-exports/react-stately/private/form/useFormValidationState.tsx +11 -0
  394. package/src/upstream-exports/react-stately/useCalendarState.tsx +24 -0
  395. package/src/upstream-exports/react-stately/useColorAreaState.tsx +24 -0
  396. package/src/upstream-exports/react-stately/useColorFieldState.tsx +22 -0
  397. package/src/upstream-exports/react-stately/useColorPickerState.tsx +25 -0
  398. package/src/upstream-exports/react-stately/useColorSliderState.tsx +25 -0
  399. package/src/upstream-exports/react-stately/useColorWheelState.tsx +24 -0
  400. package/src/upstream-exports/react-stately/useDateFieldState.tsx +29 -0
  401. package/src/upstream-exports/react-stately/useDatePickerState.tsx +25 -0
  402. package/src/upstream-exports/react-stately/useDateRangePickerState.tsx +26 -0
  403. package/src/upstream-exports/react-stately/useDraggableCollectionState.tsx +19 -0
  404. package/src/upstream-exports/react-stately/useDroppableCollectionState.tsx +19 -0
  405. package/src/upstream-exports/react-stately/useRangeCalendarState.tsx +27 -0
  406. package/src/upstream-exports/react-stately/useTimeFieldState.tsx +26 -0
  407. package/src/upstream-exports/react-stately/useToastState.tsx +21 -0
  408. package/src/upstream-exports/react-stately/useVirtualizerState.tsx +22 -0
  409. package/src/utils/useLoadMore.tsx +89 -0
  410. package/src/virtualizer/ScrollView.tsx +401 -0
  411. package/src/virtualizer/Virtualizer.tsx +147 -0
  412. package/src/virtualizer/VirtualizerItem.tsx +108 -0
  413. package/src/virtualizer/useVirtualizerItem.tsx +57 -0
  414. package/src/virtualizer/utils.tsx +105 -0
  415. package/LICENSE +0 -21
  416. package/src/components/useDragAndDrop.ts +0 -164
@@ -0,0 +1,82 @@
1
+ /** @jsxImportSource octane */
2
+ // Ported from adobe/react-spectrum@1c84a49a1faf50b571c84e00bcf9c60b22ddd03e (packages/react-stately/src/color/types.ts).
3
+ /** A list of supported color formats. */
4
+ export type ColorFormat = 'hex' | 'hexa' | 'rgb' | 'rgba' | 'hsl' | 'hsla' | 'hsb' | 'hsba';
5
+
6
+ export type ColorSpace = 'rgb' | 'hsl' | 'hsb';
7
+
8
+ /** A list of color channels. */
9
+ export type ColorChannel =
10
+ 'hue' | 'saturation' | 'brightness' | 'lightness' | 'red' | 'green' | 'blue' | 'alpha';
11
+
12
+ export type ColorAxes = { xChannel: ColorChannel; yChannel: ColorChannel; zChannel: ColorChannel };
13
+
14
+ export interface ColorChannelRange {
15
+ /** The minimum value of the color channel. */
16
+ minValue: number;
17
+ /** The maximum value of the color channel. */
18
+ maxValue: number;
19
+ /** The step value of the color channel, used when incrementing and decrementing. */
20
+ step: number;
21
+ /** The page step value of the color channel, used when incrementing and decrementing. */
22
+ pageSize: number;
23
+ }
24
+
25
+ /** Represents a color value. */
26
+ export interface Color {
27
+ /** Converts the color to the given color format, and returns a new Color object. */
28
+ toFormat(format: ColorFormat): Color;
29
+ /** Converts the color to a string in the given format. */
30
+ toString(format?: ColorFormat | 'css'): string;
31
+ /** Returns a duplicate of the color value. */
32
+ clone(): Color;
33
+ /** Converts the color to hex, and returns an integer representation. */
34
+ toHexInt(): number;
35
+ /**
36
+ * Returns the numeric value for a given channel.
37
+ * Throws an error if the channel is unsupported in the current color format.
38
+ */
39
+ getChannelValue(channel: ColorChannel): number;
40
+ /**
41
+ * Sets the numeric value for a given channel, and returns a new Color object.
42
+ * Throws an error if the channel is unsupported in the current color format.
43
+ */
44
+ withChannelValue(channel: ColorChannel, value: number): Color;
45
+ /**
46
+ * Returns the minimum, maximum, and step values for a given channel.
47
+ */
48
+ getChannelRange(channel: ColorChannel): ColorChannelRange;
49
+ /**
50
+ * Returns a localized color channel name for a given channel and locale,
51
+ * for use in visual or accessibility labels.
52
+ */
53
+ getChannelName(channel: ColorChannel, locale: string): string;
54
+ /**
55
+ * Returns the number formatting options for the given channel.
56
+ */
57
+ getChannelFormatOptions(channel: ColorChannel): Intl.NumberFormatOptions;
58
+ /**
59
+ * Formats the numeric value for a given channel for display according to the provided locale.
60
+ */
61
+ formatChannelValue(channel: ColorChannel, locale: string): string;
62
+ /**
63
+ * Returns the color space, 'rgb', 'hsb' or 'hsl', for the current color.
64
+ */
65
+ getColorSpace(): ColorSpace;
66
+ /**
67
+ * Returns the color space axes, xChannel, yChannel, zChannel.
68
+ */
69
+ getColorSpaceAxes(xyChannels: { xChannel?: ColorChannel; yChannel?: ColorChannel }): ColorAxes;
70
+ /**
71
+ * Returns an array of the color channels within the current color space space.
72
+ */
73
+ getColorChannels(): [ColorChannel, ColorChannel, ColorChannel];
74
+ /**
75
+ * Returns a localized name for the color, for use in visual or accessibility labels.
76
+ */
77
+ getColorName(locale: string): string;
78
+ /**
79
+ * Returns a localized name for the hue, for use in visual or accessibility labels.
80
+ */
81
+ getHueName(locale: string): string;
82
+ }
@@ -0,0 +1,30 @@
1
+ /** @jsxImportSource octane */
2
+ // Ported from adobe/react-spectrum@1c84a49a1faf50b571c84e00bcf9c60b22ddd03e (packages/react-stately/src/color/useColor.ts).
3
+ /*
4
+ * Copyright 2020 Adobe. All rights reserved.
5
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License. You may obtain a copy
7
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under
10
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
11
+ * OF ANY KIND, either express or implied. See the License for the specific language
12
+ * governing permissions and limitations under the License.
13
+ */
14
+
15
+ import { Color } from './types';
16
+ import { parseColor } from './Color';
17
+ import { useMemo } from '../../compat/react';
18
+
19
+ export function useColor(value: string | Color | undefined | null): Color | null | undefined {
20
+ return useMemo(() => {
21
+ if (typeof value === 'string') {
22
+ try {
23
+ return parseColor(value);
24
+ } catch {
25
+ return undefined;
26
+ }
27
+ }
28
+ return value;
29
+ }, [value]);
30
+ }
@@ -0,0 +1,255 @@
1
+ /** @jsxImportSource octane */
2
+ // Ported from adobe/react-spectrum@1c84a49a1faf50b571c84e00bcf9c60b22ddd03e (packages/react-stately/src/color/useColorAreaState.ts).
3
+ /*
4
+ * Copyright 2020 Adobe. All rights reserved.
5
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License. You may obtain a copy
7
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under
10
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
11
+ * OF ANY KIND, either express or implied. See the License for the specific language
12
+ * governing permissions and limitations under the License.
13
+ */
14
+
15
+ import { clamp, snapValueToStep } from '../utils/number';
16
+
17
+ import { Color, ColorChannel, ColorSpace } from './types';
18
+ import { normalizeColor, parseColor } from './Color';
19
+ import { useControlledState } from '../utils/useControlledState';
20
+ import { useMemo, useRef, useState } from '../../compat/react';
21
+ import { ValueBase } from '@react-types/shared';
22
+
23
+ export interface ColorAreaProps extends Omit<ValueBase<string | Color>, 'onChange'> {
24
+ /**
25
+ * The color space that the color area operates in. The `xChannel` and `yChannel` must be in this
26
+ * color space. If not provided, this defaults to the color space of the `color` or `defaultColor`
27
+ * value.
28
+ */
29
+ colorSpace?: ColorSpace;
30
+ /** Color channel for the horizontal axis. */
31
+ xChannel?: ColorChannel;
32
+ /** Color channel for the vertical axis. */
33
+ yChannel?: ColorChannel;
34
+ /** Whether the ColorArea is disabled. */
35
+ isDisabled?: boolean;
36
+ /** Handler that is called when the value changes, as the user drags. */
37
+ onChange?: (value: Color) => void;
38
+ /** Handler that is called when the user stops dragging. */
39
+ onChangeEnd?: (value: Color) => void;
40
+ }
41
+
42
+ export interface ColorAreaState {
43
+ /** The current color value displayed by the color area. */
44
+ readonly value: Color;
45
+ /** The default value of the color area. */
46
+ readonly defaultValue: Color;
47
+ /** Sets the current color value. If a string is passed, it will be parsed to a Color. */
48
+ setValue(value: string | Color): void;
49
+
50
+ /** The current value of the horizontal axis channel displayed by the color area. */
51
+ xValue: number;
52
+ /**
53
+ * Sets the value for the horizontal axis channel displayed by the color area, and triggers
54
+ * `onChange`.
55
+ */
56
+ setXValue(value: number): void;
57
+
58
+ /** The current value of the vertical axis channel displayed by the color area. */
59
+ yValue: number;
60
+ /**
61
+ * Sets the value for the vertical axis channel displayed by the color area, and triggers
62
+ * `onChange`.
63
+ */
64
+ setYValue(value: number): void;
65
+
66
+ /**
67
+ * Sets the x and y channels of the current color value based on a percentage of the width and
68
+ * height of the color area, and triggers `onChange`.
69
+ */
70
+ setColorFromPoint(x: number, y: number): void;
71
+ /**
72
+ * Returns the coordinates of the thumb relative to the upper left corner of the color area as a
73
+ * percentage.
74
+ */
75
+ getThumbPosition(): { x: number; y: number };
76
+
77
+ /** Increments the value of the horizontal axis channel by the channel step or page amount. */
78
+ incrementX(stepSize?: number): void;
79
+ /** Decrements the value of the horizontal axis channel by the channel step or page amount. */
80
+ decrementX(stepSize?: number): void;
81
+
82
+ /** Increments the value of the vertical axis channel by the channel step or page amount. */
83
+ incrementY(stepSize?: number): void;
84
+ /** Decrements the value of the vertical axis channel by the channel step or page amount. */
85
+ decrementY(stepSize?: number): void;
86
+
87
+ /** Whether the color area is currently being dragged. */
88
+ readonly isDragging: boolean;
89
+ /** Sets whether the color area is being dragged. */
90
+ setDragging(value: boolean): void;
91
+
92
+ /** Returns the xChannel, yChannel and zChannel names based on the color value. */
93
+ channels: { xChannel: ColorChannel; yChannel: ColorChannel; zChannel: ColorChannel };
94
+ /** The step value of the xChannel, used when incrementing and decrementing. */
95
+ xChannelStep: number;
96
+ /** The step value of the yChannel, used when incrementing and decrementing. */
97
+ yChannelStep: number;
98
+ /** The page step value of the xChannel, used when incrementing and decrementing. */
99
+ xChannelPageStep: number;
100
+ /** The page step value of the yChannel, used when incrementing and decrementing. */
101
+ yChannelPageStep: number;
102
+
103
+ /** Returns the color that should be displayed in the color area thumb instead of `value`. */
104
+ getDisplayColor(): Color;
105
+ }
106
+
107
+ const DEFAULT_COLOR = parseColor('#ffffff');
108
+ /**
109
+ * Provides state management for a color area component. Color area allows users to adjust two
110
+ * channels of an HSL, HSB or RGB color value against a two-dimensional gradient background.
111
+ */
112
+ export function useColorAreaState(props: ColorAreaProps): ColorAreaState {
113
+ let { value, defaultValue, colorSpace, xChannel, yChannel, onChange, onChangeEnd } = props;
114
+
115
+ if (!value && !defaultValue) {
116
+ defaultValue = DEFAULT_COLOR;
117
+ }
118
+ if (value) {
119
+ value = normalizeColor(value);
120
+ }
121
+ if (defaultValue) {
122
+ defaultValue = normalizeColor(defaultValue);
123
+ }
124
+
125
+ // safe to cast value and defaultValue to Color, one of them will always be defined because if neither are, we assign a default
126
+ let [colorValue, setColorState] = useControlledState<Color>(
127
+ value as Color,
128
+ defaultValue as Color,
129
+ onChange,
130
+ );
131
+ let [initialValue] = useState(colorValue);
132
+ let color = useMemo(
133
+ () => (colorSpace && colorValue ? colorValue.toFormat(colorSpace) : colorValue),
134
+ [colorValue, colorSpace],
135
+ );
136
+ let valueRef = useRef(color);
137
+ let setColor = (color: Color) => {
138
+ valueRef.current = color;
139
+ setColorState(color);
140
+ };
141
+
142
+ let channels = useMemo(
143
+ () => color.getColorSpaceAxes({ xChannel, yChannel }),
144
+ [color, xChannel, yChannel],
145
+ );
146
+
147
+ let xChannelRange = color.getChannelRange(channels.xChannel);
148
+ let yChannelRange = color.getChannelRange(channels.yChannel);
149
+ let {
150
+ minValue: minValueX,
151
+ maxValue: maxValueX,
152
+ step: stepX,
153
+ pageSize: pageSizeX,
154
+ } = xChannelRange;
155
+ let {
156
+ minValue: minValueY,
157
+ maxValue: maxValueY,
158
+ step: stepY,
159
+ pageSize: pageSizeY,
160
+ } = yChannelRange;
161
+
162
+ let [isDragging, setDragging] = useState(false);
163
+ let isDraggingRef = useRef(false);
164
+
165
+ let xValue = color.getChannelValue(channels.xChannel);
166
+ let yValue = color.getChannelValue(channels.yChannel);
167
+ let setXValue = (v: number) => {
168
+ if (v === xValue) {
169
+ return;
170
+ }
171
+ let newColor = color.withChannelValue(channels.xChannel, v);
172
+ setColor(newColor);
173
+ };
174
+ let setYValue = (v: number) => {
175
+ if (v === yValue) {
176
+ return;
177
+ }
178
+ let newColor = color.withChannelValue(channels.yChannel, v);
179
+ setColor(newColor);
180
+ };
181
+
182
+ return {
183
+ channels,
184
+ xChannelStep: stepX,
185
+ yChannelStep: stepY,
186
+ xChannelPageStep: pageSizeX,
187
+ yChannelPageStep: pageSizeY,
188
+ value: color,
189
+ defaultValue: value !== undefined ? initialValue : (defaultValue as Color),
190
+ setValue(value) {
191
+ setColor(normalizeColor(value));
192
+ },
193
+ xValue,
194
+ setXValue,
195
+ yValue,
196
+ setYValue,
197
+ setColorFromPoint(x: number, y: number) {
198
+ let newXValue = minValueX + clamp(x, 0, 1) * (maxValueX - minValueX);
199
+ let newYValue = minValueY + (1 - clamp(y, 0, 1)) * (maxValueY - minValueY);
200
+ let newColor: Color | undefined;
201
+ if (newXValue !== xValue) {
202
+ // Round new value to multiple of step, clamp value between min and max
203
+ newXValue = snapValueToStep(newXValue, minValueX, maxValueX, stepX);
204
+ newColor = color.withChannelValue(channels.xChannel, newXValue);
205
+ }
206
+ if (newYValue !== yValue) {
207
+ // Round new value to multiple of step, clamp value between min and max
208
+ newYValue = snapValueToStep(newYValue, minValueY, maxValueY, stepY);
209
+ newColor = (newColor || color).withChannelValue(channels.yChannel, newYValue);
210
+ }
211
+ if (newColor) {
212
+ setColor(newColor);
213
+ }
214
+ },
215
+ getThumbPosition() {
216
+ let x = (xValue - minValueX) / (maxValueX - minValueX);
217
+ let y = 1 - (yValue - minValueY) / (maxValueY - minValueY);
218
+ return { x, y };
219
+ },
220
+ incrementX(stepSize = 1) {
221
+ setXValue(
222
+ xValue + stepSize > maxValueX
223
+ ? maxValueX
224
+ : snapValueToStep(xValue + stepSize, minValueX, maxValueX, stepX),
225
+ );
226
+ },
227
+ incrementY(stepSize = 1) {
228
+ setYValue(
229
+ yValue + stepSize > maxValueY
230
+ ? maxValueY
231
+ : snapValueToStep(yValue + stepSize, minValueY, maxValueY, stepY),
232
+ );
233
+ },
234
+ decrementX(stepSize = 1) {
235
+ setXValue(snapValueToStep(xValue - stepSize, minValueX, maxValueX, stepX));
236
+ },
237
+ decrementY(stepSize = 1) {
238
+ setYValue(snapValueToStep(yValue - stepSize, minValueY, maxValueY, stepY));
239
+ },
240
+ setDragging(isDragging) {
241
+ let wasDragging = isDraggingRef.current;
242
+ isDraggingRef.current = isDragging;
243
+
244
+ if (onChangeEnd && !isDragging && wasDragging) {
245
+ onChangeEnd(valueRef.current);
246
+ }
247
+
248
+ setDragging(isDragging);
249
+ },
250
+ isDragging,
251
+ getDisplayColor() {
252
+ return color.withChannelValue('alpha', 1);
253
+ },
254
+ };
255
+ }
@@ -0,0 +1,86 @@
1
+ /** @jsxImportSource octane */
2
+ // Ported from adobe/react-spectrum@1c84a49a1faf50b571c84e00bcf9c60b22ddd03e (packages/react-stately/src/color/useColorChannelFieldState.ts).
3
+ import { Color, ColorChannel, ColorSpace } from './types';
4
+ import { ColorFieldProps } from './useColorFieldState';
5
+ import { NumberFieldState, useNumberFieldState } from '../numberfield/useNumberFieldState';
6
+ import { useColor } from './useColor';
7
+ import { useControlledState } from '../utils/useControlledState';
8
+ import { useMemo, useState } from '../../compat/react';
9
+
10
+ export interface ColorChannelFieldProps extends ColorFieldProps {
11
+ colorSpace?: ColorSpace;
12
+ channel: ColorChannel;
13
+ }
14
+
15
+ export interface ColorChannelFieldStateOptions extends ColorChannelFieldProps {
16
+ locale: string;
17
+ }
18
+
19
+ export interface ColorChannelFieldState extends NumberFieldState {
20
+ /** The current value of the field. */
21
+ colorValue: Color;
22
+ /** The default value of the field. */
23
+ defaultColorValue: Color | null;
24
+ /** Sets the color value of the field. */
25
+ setColorValue(value: Color | null): void;
26
+ }
27
+
28
+ /**
29
+ * Provides state management for a color channel field, allowing users to edit the
30
+ * value of an individual color channel.
31
+ */
32
+ export function useColorChannelFieldState(
33
+ props: ColorChannelFieldStateOptions,
34
+ ): ColorChannelFieldState {
35
+ let { channel, colorSpace, locale } = props;
36
+ let initialValue = useColor(props.value);
37
+ let initialDefaultValue = useColor(props.defaultValue);
38
+ let [colorValue, setColor] = useControlledState(
39
+ initialValue,
40
+ initialDefaultValue ?? null,
41
+ props.onChange,
42
+ );
43
+ let color = useConvertColor(colorValue, colorSpace);
44
+ let [initialColorValue] = useState(colorValue);
45
+ let defaultColorValue = initialDefaultValue ?? initialColorValue;
46
+ let defaultColor = useConvertColor(defaultColorValue, colorSpace);
47
+ let value = color.getChannelValue(channel);
48
+ let range = color.getChannelRange(channel);
49
+ let formatOptions = useMemo(() => color.getChannelFormatOptions(channel), [color, channel]);
50
+ let multiplier = formatOptions.style === 'percent' && range.maxValue === 100 ? 100 : 1;
51
+
52
+ let numberFieldState = useNumberFieldState({
53
+ locale,
54
+ value: colorValue === null ? NaN : value / multiplier,
55
+ defaultValue:
56
+ defaultColorValue === null ? NaN : defaultColor.getChannelValue(channel) / multiplier,
57
+ onChange: (v) => {
58
+ if (!Number.isNaN(v)) {
59
+ setColor(color.withChannelValue(channel, v * multiplier));
60
+ } else {
61
+ setColor(null);
62
+ }
63
+ },
64
+ minValue: range.minValue / multiplier,
65
+ maxValue: range.maxValue / multiplier,
66
+ step: range.step / multiplier,
67
+ formatOptions,
68
+ });
69
+
70
+ return {
71
+ ...numberFieldState,
72
+ colorValue: color,
73
+ defaultColorValue,
74
+ setColorValue: setColor,
75
+ };
76
+ }
77
+
78
+ function useConvertColor(colorValue: Color | null, colorSpace: ColorSpace | null | undefined) {
79
+ let black = useColor('#000')!;
80
+ return useMemo(() => {
81
+ let nonNullColorValue = colorValue || black;
82
+ return colorSpace && nonNullColorValue
83
+ ? nonNullColorValue.toFormat(colorSpace)
84
+ : nonNullColorValue;
85
+ }, [black, colorValue, colorSpace]);
86
+ }
@@ -0,0 +1,220 @@
1
+ /** @jsxImportSource octane */
2
+ // Ported from adobe/react-spectrum@1c84a49a1faf50b571c84e00bcf9c60b22ddd03e (packages/react-stately/src/color/useColorFieldState.ts).
3
+ /*
4
+ * Copyright 2020 Adobe. All rights reserved.
5
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
6
+ * you may not use this file except in compliance with the License. You may obtain a copy
7
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under
10
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
11
+ * OF ANY KIND, either express or implied. See the License for the specific language
12
+ * governing permissions and limitations under the License.
13
+ */
14
+
15
+ import { Color } from './types';
16
+ import {
17
+ FocusableProps,
18
+ HelpTextProps,
19
+ InputBase,
20
+ LabelableProps,
21
+ TextInputBase,
22
+ Validation,
23
+ ValueBase,
24
+ } from '@react-types/shared';
25
+ import { FormValidationState, useFormValidationState } from '../form/useFormValidationState';
26
+ import { parseColor } from './Color';
27
+ import { useColor } from './useColor';
28
+ import { useControlledState } from '../utils/useControlledState';
29
+ import { useMemo, useState } from '../../compat/react';
30
+
31
+ export interface ColorFieldProps
32
+ extends
33
+ Omit<ValueBase<string | Color | null>, 'onChange'>,
34
+ InputBase,
35
+ Validation<Color | null>,
36
+ FocusableProps,
37
+ TextInputBase,
38
+ LabelableProps,
39
+ HelpTextProps {
40
+ /** Handler that is called when the value changes. */
41
+ onChange?: (color: Color | null) => void;
42
+ }
43
+
44
+ export interface ColorFieldState extends FormValidationState {
45
+ /**
46
+ * The current text value of the input. Updated as the user types,
47
+ * and formatted according to `formatOptions` on blur.
48
+ */
49
+ readonly inputValue: string;
50
+ /**
51
+ * The currently parsed color value, or null if the field is empty.
52
+ * Updated based on the `inputValue` as the user types.
53
+ */
54
+ readonly colorValue: Color | null;
55
+ /** The default value of the color field. */
56
+ readonly defaultColorValue: Color | null;
57
+ /** Sets the color value of the field. */
58
+ setColorValue(value: Color | null): void;
59
+ /** Sets the current text value of the input. */
60
+ setInputValue(value: string): void;
61
+ /**
62
+ * Updates the input value based on the currently parsed color value.
63
+ * Typically this is called when the field is blurred.
64
+ */
65
+ commit(): void;
66
+ /** Increments the current input value to the next step boundary, and fires `onChange`. */
67
+ increment(): void;
68
+ /** Decrements the current input value to the next step boundary, and fires `onChange`. */
69
+ decrement(): void;
70
+ /** Sets the current value to the maximum color value, and fires `onChange`. */
71
+ incrementToMax(): void;
72
+ /** Sets the current value to the minimum color value, and fires `onChange`. */
73
+ decrementToMin(): void;
74
+ /**
75
+ * Validates a user input string. Values can be partially entered, and may be valid even if they
76
+ * cannot currently be parsed to a color. Can be used to implement validation as a user types.
77
+ */
78
+ validate(value: string): boolean;
79
+ }
80
+
81
+ const MIN_COLOR = parseColor('#000000');
82
+ const MAX_COLOR = parseColor('#FFFFFF');
83
+ const MIN_COLOR_INT = MIN_COLOR.toHexInt();
84
+ const MAX_COLOR_INT = MAX_COLOR.toHexInt();
85
+
86
+ /**
87
+ * Provides state management for a color field component. Color fields allow
88
+ * users to enter and adjust a hex color value.
89
+ */
90
+ export function useColorFieldState(props: ColorFieldProps): ColorFieldState {
91
+ let { value, defaultValue, onChange } = props;
92
+ let { step } = MIN_COLOR.getChannelRange('red');
93
+
94
+ let initialDefaultValue = useColor(defaultValue);
95
+ let [colorValue, setColorValue] = useControlledState<Color | null>(
96
+ useColor(value),
97
+ initialDefaultValue!,
98
+ onChange,
99
+ );
100
+ let [initialValue] = useState(colorValue);
101
+ let [inputValue, setInputValue] = useState(() =>
102
+ (value || defaultValue) && colorValue ? colorValue.toString('hex') : '',
103
+ );
104
+
105
+ let validation = useFormValidationState({
106
+ ...props,
107
+ value: colorValue,
108
+ });
109
+
110
+ let safelySetColorValue = (newColor: Color | null) => {
111
+ if (!colorValue || !newColor) {
112
+ setColorValue(newColor);
113
+ return;
114
+ }
115
+ if (newColor.toHexInt() !== colorValue.toHexInt()) {
116
+ setColorValue(newColor);
117
+ return;
118
+ }
119
+ };
120
+
121
+ let [prevValue, setPrevValue] = useState(colorValue);
122
+ if (prevValue !== colorValue) {
123
+ setInputValue(colorValue ? colorValue.toString('hex') : '');
124
+ setPrevValue(colorValue);
125
+ }
126
+
127
+ let parsedValue = useMemo(() => {
128
+ let color;
129
+ try {
130
+ color = parseColor(inputValue.startsWith('#') ? inputValue : `#${inputValue}`);
131
+ } catch {
132
+ color = null;
133
+ }
134
+ return color;
135
+ }, [inputValue]);
136
+
137
+ let commit = () => {
138
+ // Set to empty state if input value is empty
139
+ if (!inputValue.length) {
140
+ safelySetColorValue(null);
141
+ if (value === undefined || colorValue === null) {
142
+ setInputValue('');
143
+ } else {
144
+ setInputValue(colorValue.toString('hex'));
145
+ }
146
+ return;
147
+ }
148
+
149
+ // if it failed to parse, then reset input to formatted version of current number
150
+ if (parsedValue == null) {
151
+ setInputValue(colorValue ? colorValue.toString('hex') : '');
152
+ return;
153
+ }
154
+
155
+ safelySetColorValue(parsedValue);
156
+ // in a controlled state, the numberValue won't change, so we won't go back to our old input without help
157
+ let newColorValue = '';
158
+ if (colorValue) {
159
+ newColorValue = colorValue.toString('hex');
160
+ }
161
+ setInputValue(newColorValue);
162
+ validation.commitValidation();
163
+ };
164
+
165
+ let increment = () => {
166
+ let newValue = addColorValue(parsedValue ?? colorValue ?? MIN_COLOR, step);
167
+ // if we've arrived at the same value that was previously in the state, the
168
+ // input value should be updated to match
169
+ // ex type 4, press increment, highlight the number in the input, type 4 again, press increment
170
+ // you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input
171
+ if (newValue === colorValue) {
172
+ setInputValue(newValue.toString('hex'));
173
+ }
174
+ safelySetColorValue(newValue);
175
+ validation.commitValidation();
176
+ };
177
+ let decrement = () => {
178
+ let newValue = addColorValue(parsedValue ?? colorValue ?? MIN_COLOR, -step);
179
+ // if we've arrived at the same value that was previously in the state, the
180
+ // input value should be updated to match
181
+ // ex type 4, press increment, highlight the number in the input, type 4 again, press increment
182
+ // you'd be at 5, then incrementing to 5 again, so no re-render would happen and 4 would be left in the input
183
+ if (newValue === colorValue) {
184
+ setInputValue(newValue.toString('hex'));
185
+ }
186
+ safelySetColorValue(newValue);
187
+ validation.commitValidation();
188
+ };
189
+ let incrementToMax = () => safelySetColorValue(MAX_COLOR);
190
+ let decrementToMin = () => safelySetColorValue(MIN_COLOR);
191
+
192
+ let validate = (value: string) => value === '' || !!value.match(/^#?[0-9a-f]{0,6}$/i)?.[0];
193
+
194
+ return {
195
+ ...validation,
196
+ validate,
197
+ colorValue,
198
+ defaultColorValue: initialDefaultValue ?? initialValue,
199
+ setColorValue,
200
+ inputValue,
201
+ setInputValue,
202
+ commit,
203
+ increment,
204
+ incrementToMax,
205
+ decrement,
206
+ decrementToMin,
207
+ };
208
+ }
209
+
210
+ function addColorValue(color: Color, step: number) {
211
+ let newColor = color ? color : MIN_COLOR;
212
+ let colorInt = newColor.toHexInt();
213
+
214
+ let clampInt = Math.min(Math.max(colorInt + step, MIN_COLOR_INT), MAX_COLOR_INT);
215
+ if (clampInt !== colorInt) {
216
+ let newColorString = `#${clampInt.toString(16).padStart(6, '0').toUpperCase()}`;
217
+ newColor = parseColor(newColorString);
218
+ }
219
+ return newColor;
220
+ }