victory-core 36.5.0 → 36.5.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 (364) hide show
  1. package/dist/victory-core.js +13817 -10827
  2. package/dist/victory-core.min.js +1 -1
  3. package/es/index.d.ts +7 -27
  4. package/es/index.js +7 -53
  5. package/es/{types.d.ts → types/callbacks.d.ts} +12 -8
  6. package/es/types/callbacks.js +2 -0
  7. package/es/types/prop-types.d.ts +130 -0
  8. package/es/types/prop-types.js +2 -0
  9. package/es/victory-accessible-group/victory-accessible-group.d.ts +1 -0
  10. package/es/victory-accessible-group/victory-accessible-group.js +2 -1
  11. package/es/victory-animation/util.d.ts +2 -1
  12. package/es/victory-animation/util.js +6 -4
  13. package/es/victory-animation/victory-animation.d.ts +1 -0
  14. package/es/victory-animation/victory-animation.js +2 -1
  15. package/es/victory-clip-container/victory-clip-container.d.ts +6 -5
  16. package/es/victory-clip-container/victory-clip-container.js +2 -1
  17. package/es/victory-container/victory-container.d.ts +8 -7
  18. package/es/victory-container/victory-container.js +2 -1
  19. package/es/victory-label/victory-label.d.ts +3 -2
  20. package/es/victory-label/victory-label.js +4 -4
  21. package/es/victory-portal/portal-context.d.ts +1 -0
  22. package/es/victory-portal/portal-context.js +2 -1
  23. package/es/victory-portal/portal.d.ts +3 -2
  24. package/es/victory-portal/portal.js +2 -1
  25. package/es/victory-portal/victory-portal.d.ts +1 -0
  26. package/es/victory-portal/victory-portal.js +2 -1
  27. package/es/victory-primitives/arc.d.ts +31 -2
  28. package/es/victory-primitives/arc.js +3 -2
  29. package/es/victory-primitives/background.d.ts +31 -2
  30. package/es/victory-primitives/background.js +3 -2
  31. package/es/victory-primitives/border.d.ts +28 -2
  32. package/es/victory-primitives/border.js +3 -2
  33. package/es/victory-primitives/circle.d.ts +1 -0
  34. package/es/victory-primitives/circle.js +2 -1
  35. package/es/victory-primitives/clip-path.d.ts +2 -1
  36. package/es/victory-primitives/clip-path.js +2 -1
  37. package/es/victory-primitives/index.d.ts +1 -0
  38. package/es/victory-primitives/index.js +2 -1
  39. package/es/victory-primitives/line-segment.d.ts +29 -2
  40. package/es/victory-primitives/line-segment.js +3 -2
  41. package/es/victory-primitives/line.d.ts +1 -0
  42. package/es/victory-primitives/line.js +2 -1
  43. package/es/victory-primitives/path.d.ts +1 -0
  44. package/es/victory-primitives/path.js +2 -1
  45. package/es/victory-primitives/point.d.ts +30 -2
  46. package/es/victory-primitives/point.js +3 -2
  47. package/es/victory-primitives/rect.d.ts +1 -0
  48. package/es/victory-primitives/rect.js +2 -1
  49. package/es/victory-primitives/text.d.ts +2 -1
  50. package/es/victory-primitives/text.js +2 -1
  51. package/es/victory-primitives/tspan.d.ts +2 -1
  52. package/es/victory-primitives/tspan.js +2 -1
  53. package/es/victory-primitives/types.d.ts +2 -1
  54. package/es/victory-primitives/types.js +2 -1
  55. package/es/victory-primitives/whisker.d.ts +37 -2
  56. package/es/victory-primitives/whisker.js +3 -2
  57. package/es/victory-state/defaults.d.ts +3 -0
  58. package/es/victory-state/defaults.js +3 -0
  59. package/es/victory-state/helpers/get-axis-data.d.ts +3 -0
  60. package/es/victory-state/helpers/get-axis-data.js +15 -0
  61. package/es/victory-state/helpers/get-data.d.ts +15 -0
  62. package/es/victory-state/helpers/get-data.js +88 -0
  63. package/es/victory-state/helpers/get-domain.d.ts +6 -0
  64. package/es/victory-state/helpers/get-domain.js +68 -0
  65. package/es/victory-state/helpers/get-range.d.ts +6 -0
  66. package/es/victory-state/helpers/get-range.js +37 -0
  67. package/es/victory-state/helpers/get-scale.d.ts +6 -0
  68. package/es/victory-state/helpers/get-scale.js +59 -0
  69. package/es/victory-state/index.d.ts +3 -0
  70. package/es/victory-state/index.js +3 -0
  71. package/es/victory-state/types.d.ts +24 -0
  72. package/es/victory-state/types.js +2 -0
  73. package/es/victory-state/victory-provider.d.ts +19 -0
  74. package/es/victory-state/victory-provider.js +118 -0
  75. package/es/victory-theme/grayscale.d.ts +2 -1
  76. package/es/victory-theme/grayscale.js +2 -1
  77. package/es/victory-theme/material.d.ts +2 -1
  78. package/es/victory-theme/material.js +2 -1
  79. package/{lib/victory-theme/victory-theme-definition.d.ts → es/victory-theme/types.d.ts} +3 -8
  80. package/es/victory-theme/types.js +2 -0
  81. package/es/victory-theme/victory-theme.d.ts +4 -3
  82. package/es/victory-theme/victory-theme.js +3 -2
  83. package/es/victory-transition/victory-transition.d.ts +1 -0
  84. package/es/victory-transition/victory-transition.js +2 -1
  85. package/es/victory-util/add-events.d.ts +106 -0
  86. package/es/victory-util/add-events.js +40 -33
  87. package/es/victory-util/axis.d.ts +51 -0
  88. package/es/victory-util/axis.js +9 -7
  89. package/es/victory-util/collection.d.ts +9 -0
  90. package/es/victory-util/collection.js +6 -4
  91. package/es/victory-util/common-props.d.ts +84 -0
  92. package/es/victory-util/common-props.js +29 -12
  93. package/es/victory-util/data.d.ts +75 -0
  94. package/es/victory-util/data.js +2 -1
  95. package/es/victory-util/default-transitions.d.ts +5 -0
  96. package/es/victory-util/default-transitions.js +2 -1
  97. package/es/victory-util/domain.d.ts +91 -0
  98. package/es/victory-util/domain.js +6 -2
  99. package/es/victory-util/events.d.ts +65 -0
  100. package/es/victory-util/events.js +13 -8
  101. package/es/victory-util/helpers.d.ts +61 -0
  102. package/es/victory-util/helpers.js +8 -4
  103. package/es/victory-util/hooks/index.d.ts +3 -0
  104. package/es/victory-util/hooks/index.js +2 -1
  105. package/es/victory-util/hooks/use-animation-state.js +4 -3
  106. package/es/victory-util/hooks/use-previous-props.d.ts +2 -0
  107. package/es/victory-util/hooks/use-previous-props.js +4 -3
  108. package/es/victory-util/immutable.d.ts +12 -0
  109. package/es/victory-util/immutable.js +5 -4
  110. package/es/victory-util/index.d.ts +29 -0
  111. package/es/victory-util/index.js +48 -0
  112. package/es/victory-util/label-helpers.d.ts +27 -0
  113. package/es/victory-util/label-helpers.js +2 -1
  114. package/es/victory-util/line-helpers.d.ts +5 -0
  115. package/es/victory-util/line-helpers.js +24 -6
  116. package/es/victory-util/log.d.ts +2 -0
  117. package/es/victory-util/log.js +2 -1
  118. package/es/victory-util/point-path-helpers.d.ts +1 -0
  119. package/es/victory-util/point-path-helpers.js +2 -1
  120. package/es/victory-util/prop-types.d.ts +51 -0
  121. package/es/victory-util/prop-types.js +35 -44
  122. package/es/victory-util/scale.d.ts +10 -0
  123. package/es/victory-util/scale.js +14 -6
  124. package/es/victory-util/selection.d.ts +25 -23
  125. package/es/victory-util/selection.js +25 -14
  126. package/es/victory-util/style.d.ts +19 -0
  127. package/es/victory-util/style.js +2 -1
  128. package/es/victory-util/textsize.d.ts +37 -8
  129. package/es/victory-util/textsize.js +9 -6
  130. package/es/victory-util/timer-context.d.ts +1 -0
  131. package/es/victory-util/timer-context.js +2 -1
  132. package/es/victory-util/timer.d.ts +3 -2
  133. package/es/victory-util/timer.js +2 -1
  134. package/es/victory-util/transitions.d.ts +49 -0
  135. package/es/victory-util/transitions.js +5 -4
  136. package/es/victory-util/type-helpers.d.ts +10 -0
  137. package/es/victory-util/type-helpers.js +28 -0
  138. package/es/victory-util/user-props.d.ts +21 -0
  139. package/es/victory-util/user-props.js +6 -5
  140. package/es/victory-util/with-container.d.ts +5 -0
  141. package/es/victory-util/with-container.js +32 -0
  142. package/es/victory-util/wrapper.d.ts +38 -0
  143. package/es/victory-util/wrapper.js +4 -3
  144. package/lib/index.d.ts +7 -27
  145. package/lib/index.js +58 -147
  146. package/lib/{types.d.ts → types/callbacks.d.ts} +12 -8
  147. package/lib/types/callbacks.js +2 -0
  148. package/lib/types/prop-types.d.ts +130 -0
  149. package/lib/types/prop-types.js +2 -0
  150. package/lib/victory-accessible-group/victory-accessible-group.d.ts +1 -0
  151. package/lib/victory-accessible-group/victory-accessible-group.js +2 -1
  152. package/lib/victory-animation/util.d.ts +2 -1
  153. package/lib/victory-animation/util.js +6 -4
  154. package/lib/victory-animation/victory-animation.d.ts +1 -0
  155. package/lib/victory-animation/victory-animation.js +2 -1
  156. package/lib/victory-clip-container/victory-clip-container.d.ts +6 -5
  157. package/lib/victory-clip-container/victory-clip-container.js +2 -1
  158. package/lib/victory-container/victory-container.d.ts +8 -7
  159. package/lib/victory-container/victory-container.js +2 -1
  160. package/lib/victory-label/victory-label.d.ts +3 -2
  161. package/lib/victory-label/victory-label.js +4 -4
  162. package/lib/victory-portal/portal-context.d.ts +1 -0
  163. package/lib/victory-portal/portal-context.js +2 -1
  164. package/lib/victory-portal/portal.d.ts +3 -2
  165. package/lib/victory-portal/portal.js +2 -1
  166. package/lib/victory-portal/victory-portal.d.ts +1 -0
  167. package/lib/victory-portal/victory-portal.js +2 -1
  168. package/lib/victory-primitives/arc.d.ts +31 -2
  169. package/lib/victory-primitives/arc.js +4 -3
  170. package/lib/victory-primitives/background.d.ts +31 -2
  171. package/lib/victory-primitives/background.js +4 -3
  172. package/lib/victory-primitives/border.d.ts +28 -2
  173. package/lib/victory-primitives/border.js +4 -3
  174. package/lib/victory-primitives/circle.d.ts +1 -0
  175. package/lib/victory-primitives/circle.js +2 -1
  176. package/lib/victory-primitives/clip-path.d.ts +2 -1
  177. package/lib/victory-primitives/clip-path.js +2 -1
  178. package/lib/victory-primitives/index.d.ts +1 -0
  179. package/lib/victory-primitives/index.js +2 -1
  180. package/lib/victory-primitives/line-segment.d.ts +29 -2
  181. package/lib/victory-primitives/line-segment.js +4 -3
  182. package/lib/victory-primitives/line.d.ts +1 -0
  183. package/lib/victory-primitives/line.js +2 -1
  184. package/lib/victory-primitives/path.d.ts +1 -0
  185. package/lib/victory-primitives/path.js +2 -1
  186. package/lib/victory-primitives/point.d.ts +30 -2
  187. package/lib/victory-primitives/point.js +4 -3
  188. package/lib/victory-primitives/rect.d.ts +1 -0
  189. package/lib/victory-primitives/rect.js +2 -1
  190. package/lib/victory-primitives/text.d.ts +2 -1
  191. package/lib/victory-primitives/text.js +2 -1
  192. package/lib/victory-primitives/tspan.d.ts +2 -1
  193. package/lib/victory-primitives/tspan.js +2 -1
  194. package/lib/victory-primitives/types.d.ts +2 -1
  195. package/lib/victory-primitives/types.js +2 -1
  196. package/lib/victory-primitives/whisker.d.ts +37 -2
  197. package/lib/victory-primitives/whisker.js +4 -3
  198. package/lib/victory-state/defaults.d.ts +3 -0
  199. package/lib/victory-state/defaults.js +11 -0
  200. package/lib/victory-state/helpers/get-axis-data.d.ts +3 -0
  201. package/lib/victory-state/helpers/get-axis-data.js +23 -0
  202. package/lib/victory-state/helpers/get-data.d.ts +15 -0
  203. package/lib/victory-state/helpers/get-data.js +99 -0
  204. package/lib/victory-state/helpers/get-domain.d.ts +6 -0
  205. package/lib/victory-state/helpers/get-domain.js +82 -0
  206. package/lib/victory-state/helpers/get-range.d.ts +6 -0
  207. package/lib/victory-state/helpers/get-range.js +46 -0
  208. package/lib/victory-state/helpers/get-scale.d.ts +6 -0
  209. package/lib/victory-state/helpers/get-scale.js +74 -0
  210. package/lib/victory-state/index.d.ts +3 -0
  211. package/lib/victory-state/index.js +32 -0
  212. package/lib/victory-state/types.d.ts +24 -0
  213. package/lib/victory-state/types.js +2 -0
  214. package/lib/victory-state/victory-provider.d.ts +19 -0
  215. package/lib/victory-state/victory-provider.js +144 -0
  216. package/lib/victory-theme/grayscale.d.ts +2 -1
  217. package/lib/victory-theme/grayscale.js +2 -1
  218. package/lib/victory-theme/material.d.ts +2 -1
  219. package/lib/victory-theme/material.js +2 -1
  220. package/{es/victory-theme/victory-theme-definition.d.ts → lib/victory-theme/types.d.ts} +3 -8
  221. package/lib/victory-theme/types.js +2 -0
  222. package/lib/victory-theme/victory-theme.d.ts +4 -3
  223. package/lib/victory-theme/victory-theme.js +6 -5
  224. package/lib/victory-transition/victory-transition.d.ts +1 -0
  225. package/lib/victory-transition/victory-transition.js +2 -1
  226. package/lib/victory-util/add-events.d.ts +106 -0
  227. package/lib/victory-util/add-events.js +40 -36
  228. package/lib/victory-util/axis.d.ts +51 -0
  229. package/lib/victory-util/axis.js +8 -6
  230. package/lib/victory-util/collection.d.ts +9 -0
  231. package/lib/victory-util/collection.js +6 -4
  232. package/lib/victory-util/common-props.d.ts +84 -0
  233. package/lib/victory-util/common-props.js +27 -12
  234. package/lib/victory-util/data.d.ts +75 -0
  235. package/lib/victory-util/data.js +2 -1
  236. package/lib/victory-util/default-transitions.d.ts +5 -0
  237. package/lib/victory-util/default-transitions.js +2 -1
  238. package/lib/victory-util/domain.d.ts +91 -0
  239. package/lib/victory-util/domain.js +6 -2
  240. package/lib/victory-util/events.d.ts +65 -0
  241. package/lib/victory-util/events.js +13 -9
  242. package/lib/victory-util/helpers.d.ts +61 -0
  243. package/lib/victory-util/helpers.js +8 -4
  244. package/lib/victory-util/hooks/index.d.ts +3 -0
  245. package/lib/victory-util/hooks/index.js +2 -1
  246. package/lib/victory-util/hooks/use-animation-state.js +4 -3
  247. package/lib/victory-util/hooks/use-previous-props.d.ts +2 -0
  248. package/lib/victory-util/hooks/use-previous-props.js +5 -6
  249. package/lib/victory-util/immutable.d.ts +12 -0
  250. package/lib/victory-util/immutable.js +5 -4
  251. package/lib/victory-util/index.d.ts +29 -0
  252. package/lib/victory-util/index.js +181 -0
  253. package/lib/victory-util/label-helpers.d.ts +27 -0
  254. package/lib/victory-util/label-helpers.js +2 -1
  255. package/lib/victory-util/line-helpers.d.ts +5 -0
  256. package/lib/victory-util/line-helpers.js +28 -7
  257. package/lib/victory-util/log.d.ts +2 -0
  258. package/lib/victory-util/log.js +2 -1
  259. package/lib/victory-util/point-path-helpers.d.ts +1 -0
  260. package/lib/victory-util/point-path-helpers.js +2 -1
  261. package/lib/victory-util/prop-types.d.ts +51 -0
  262. package/lib/victory-util/prop-types.js +36 -44
  263. package/lib/victory-util/scale.d.ts +10 -0
  264. package/lib/victory-util/scale.js +14 -5
  265. package/lib/victory-util/selection.d.ts +25 -23
  266. package/lib/victory-util/selection.js +25 -19
  267. package/lib/victory-util/style.d.ts +19 -0
  268. package/lib/victory-util/style.js +2 -1
  269. package/lib/victory-util/textsize.d.ts +37 -8
  270. package/lib/victory-util/textsize.js +9 -6
  271. package/lib/victory-util/timer-context.d.ts +1 -0
  272. package/lib/victory-util/timer-context.js +2 -1
  273. package/lib/victory-util/timer.d.ts +3 -2
  274. package/lib/victory-util/timer.js +2 -1
  275. package/lib/victory-util/transitions.d.ts +49 -0
  276. package/lib/victory-util/transitions.js +5 -4
  277. package/lib/victory-util/type-helpers.d.ts +10 -0
  278. package/lib/victory-util/type-helpers.js +45 -0
  279. package/lib/victory-util/user-props.d.ts +21 -0
  280. package/lib/victory-util/user-props.js +6 -5
  281. package/lib/victory-util/with-container.d.ts +5 -0
  282. package/lib/victory-util/with-container.js +44 -0
  283. package/lib/victory-util/wrapper.d.ts +38 -0
  284. package/lib/victory-util/wrapper.js +4 -3
  285. package/package.json +3 -3
  286. package/src/index.ts +9 -31
  287. package/src/{types.ts → types/callbacks.ts} +21 -13
  288. package/src/types/prop-types.ts +195 -0
  289. package/src/victory-accessible-group/victory-accessible-group.tsx +3 -3
  290. package/src/victory-animation/util.ts +3 -2
  291. package/src/victory-animation/victory-animation.tsx +13 -13
  292. package/src/victory-clip-container/victory-clip-container.tsx +23 -23
  293. package/src/victory-container/victory-container.tsx +19 -19
  294. package/src/victory-label/victory-label.tsx +41 -39
  295. package/src/victory-portal/portal.tsx +1 -1
  296. package/src/victory-portal/victory-portal.tsx +3 -3
  297. package/src/victory-primitives/arc.tsx +8 -6
  298. package/src/victory-primitives/background.tsx +8 -6
  299. package/src/victory-primitives/border.tsx +8 -6
  300. package/src/victory-primitives/clip-path.tsx +3 -3
  301. package/src/victory-primitives/line-segment.tsx +8 -6
  302. package/src/victory-primitives/point.tsx +10 -8
  303. package/src/victory-primitives/text.tsx +2 -2
  304. package/src/victory-primitives/tspan.tsx +1 -1
  305. package/src/victory-primitives/types.ts +1 -1
  306. package/src/victory-primitives/whisker.tsx +15 -13
  307. package/src/victory-state/defaults.ts +2 -0
  308. package/src/victory-state/helpers/get-axis-data.ts +14 -0
  309. package/src/victory-state/helpers/get-data.ts +79 -0
  310. package/src/victory-state/helpers/get-domain.ts +62 -0
  311. package/src/victory-state/helpers/get-range.ts +39 -0
  312. package/src/victory-state/helpers/get-scale.ts +64 -0
  313. package/src/victory-state/index.ts +2 -0
  314. package/src/victory-state/types.ts +32 -0
  315. package/src/victory-state/victory-provider.tsx +116 -0
  316. package/src/victory-theme/grayscale.tsx +62 -62
  317. package/src/victory-theme/material.tsx +67 -67
  318. package/src/victory-theme/{victory-theme-definition.ts → types.ts} +10 -14
  319. package/src/victory-theme/victory-theme.tsx +1 -1
  320. package/src/victory-transition/victory-transition.tsx +19 -19
  321. package/src/victory-util/{add-events.js → add-events.tsx} +136 -59
  322. package/src/victory-util/{axis.js → axis.tsx} +17 -18
  323. package/src/victory-util/{collection.js → collection.tsx} +14 -10
  324. package/src/victory-util/common-props.tsx +307 -0
  325. package/src/victory-util/{data.js → data.ts} +14 -10
  326. package/src/victory-util/{default-transitions.js → default-transitions.ts} +20 -18
  327. package/src/victory-util/{domain.js → domain.ts} +26 -23
  328. package/src/victory-util/{events.js → events.ts} +82 -31
  329. package/src/victory-util/{helpers.js → helpers.ts} +41 -22
  330. package/src/victory-util/hooks/{index.js → index.ts} +0 -0
  331. package/src/victory-util/hooks/use-animation-state.js +12 -12
  332. package/src/victory-util/hooks/use-previous-props.ts +9 -0
  333. package/src/victory-util/{immutable.js → immutable.ts} +14 -10
  334. package/src/victory-util/index.ts +28 -0
  335. package/src/victory-util/{label-helpers.js → label-helpers.ts} +7 -7
  336. package/src/victory-util/line-helpers.ts +104 -0
  337. package/src/victory-util/{log.js → log.ts} +1 -1
  338. package/src/victory-util/{prop-types.js → prop-types.ts} +103 -101
  339. package/src/victory-util/{scale.js → scale.ts} +29 -16
  340. package/src/victory-util/selection.ts +158 -0
  341. package/src/victory-util/{style.js → style.ts} +3 -3
  342. package/src/victory-util/{textsize.js → textsize.ts} +32 -17
  343. package/src/victory-util/timer-context.ts +1 -1
  344. package/src/victory-util/timer.ts +5 -10
  345. package/src/victory-util/{transitions.js → transitions.ts} +50 -22
  346. package/src/victory-util/type-helpers.ts +50 -0
  347. package/src/victory-util/{user-props.js → user-props.ts} +16 -11
  348. package/src/victory-util/with-container.tsx +41 -0
  349. package/src/victory-util/{wrapper.js → wrapper.tsx} +38 -33
  350. package/es/types.js +0 -1
  351. package/es/victory-theme/victory-theme-definition.js +0 -1
  352. package/es/victory-util/types.d.ts +0 -188
  353. package/es/victory-util/types.js +0 -1
  354. package/lib/types.js +0 -1
  355. package/lib/victory-theme/victory-theme-definition.js +0 -1
  356. package/lib/victory-util/types.d.ts +0 -188
  357. package/lib/victory-util/types.js +0 -1
  358. package/src/victory-util/common-props.js +0 -197
  359. package/src/victory-util/hooks/use-previous-props.js +0 -9
  360. package/src/victory-util/line-helpers.js +0 -54
  361. package/src/victory-util/selection.d.ts +0 -23
  362. package/src/victory-util/selection.js +0 -98
  363. package/src/victory-util/textsize.d.ts +0 -8
  364. package/src/victory-util/types.ts +0 -247
@@ -0,0 +1,158 @@
1
+ import React from "react";
2
+ import { Collection, VictoryCommonProps } from "./index";
3
+ import type { DomainPropType, ScaleXYPropType } from "../types/prop-types";
4
+
5
+ // Private Functions
6
+
7
+ function transformTarget(
8
+ target: number,
9
+ matrix: DOMMatrix,
10
+ dimension: "x" | "y",
11
+ ) {
12
+ const { a, d, e, f } = matrix;
13
+ return dimension === "y" ? d * target + f : a * target + e;
14
+ }
15
+
16
+ function getTransformationMatrix(svg: SVGGraphicsElement): DOMMatrix {
17
+ return svg.getScreenCTM()!.inverse();
18
+ }
19
+
20
+ interface ReactNativeTouchEvent extends Event {
21
+ identifier?: number;
22
+ locationX: number;
23
+ locationY: number;
24
+ }
25
+ function isNativeTouchEvent(
26
+ nativeEvent: Event | undefined,
27
+ ): nativeEvent is ReactNativeTouchEvent {
28
+ return !!(
29
+ nativeEvent &&
30
+ (nativeEvent as ReactNativeTouchEvent).identifier !== undefined
31
+ );
32
+ }
33
+ function isReactTouchEvent(evt: React.SyntheticEvent): evt is React.TouchEvent {
34
+ return (
35
+ (evt as React.TouchEvent).changedTouches &&
36
+ (evt as React.TouchEvent).changedTouches.length > 0
37
+ );
38
+ }
39
+
40
+ // Exported Functions
41
+
42
+ export function getParentSVG(
43
+ evt: Pick<React.SyntheticEvent, "target"> &
44
+ Partial<Pick<React.SyntheticEvent, "nativeEvent">>,
45
+ ): SVGElement {
46
+ if (isNativeTouchEvent(evt.nativeEvent)) {
47
+ // @ts-expect-error Seems like a superfluous check.
48
+ return undefined;
49
+ }
50
+ const getParent = (target) => {
51
+ if (target.nodeName === "svg") {
52
+ return target;
53
+ } else {
54
+ return target.parentNode ? getParent(target.parentNode) : target;
55
+ }
56
+ };
57
+ return getParent(evt.target);
58
+ }
59
+
60
+ export function getSVGEventCoordinates(
61
+ evt: React.SyntheticEvent,
62
+ svg?: SVGElement,
63
+ ): SVGCoordinateType {
64
+ if (isNativeTouchEvent(evt.nativeEvent)) {
65
+ // react-native override. relies on the RN.View being the _exact_ same size as its child SVG.
66
+ // this should be fine: the svg is the only child of View and the View shirks to its children
67
+ return {
68
+ x: evt.nativeEvent.locationX,
69
+ y: evt.nativeEvent.locationY,
70
+ };
71
+ }
72
+ const location = isReactTouchEvent(evt)
73
+ ? evt.changedTouches[0]
74
+ : (evt as React.MouseEvent);
75
+ svg = svg || getParentSVG(location);
76
+ const matrix = getTransformationMatrix(svg as SVGGraphicsElement);
77
+ return {
78
+ x: transformTarget(location.clientX, matrix, "x"),
79
+ y: transformTarget(location.clientY, matrix, "y"),
80
+ };
81
+ }
82
+
83
+ export function getDomainCoordinates(
84
+ props: Pick<VictoryCommonProps, "scale" | "horizontal">,
85
+ domain?: DomainPropType,
86
+ ): DomainPropType {
87
+ const { horizontal } = props;
88
+ const scale = props.scale as ScaleXYPropType;
89
+ // FIXME: add support for DomainTuple: [number, number]
90
+ const domainObj: any = domain || {
91
+ x: scale.x.domain(),
92
+ y: scale.y.domain(),
93
+ };
94
+ return {
95
+ x: horizontal
96
+ ? [scale.y(domainObj.y[0]), scale.y(domainObj.y[1])]
97
+ : [scale.x(domainObj.x[0]), scale.x(domainObj.x[1])],
98
+ y: horizontal
99
+ ? [scale.x(domainObj.x[0]), scale.x(domainObj.x[1])]
100
+ : [scale.y(domainObj.y[0]), scale.y(domainObj.y[1])],
101
+ };
102
+ }
103
+
104
+ // eslint-disable-next-line max-params
105
+ export function getDataCoordinates(
106
+ props: any,
107
+ scale: ScaleXYPropType,
108
+ x: number,
109
+ y: number,
110
+ ): SVGCoordinateType {
111
+ const { polar, horizontal } = props;
112
+ if (!polar) {
113
+ return {
114
+ x: horizontal ? scale.x.invert(y) : scale.x.invert(x),
115
+ y: horizontal ? scale.y.invert(x) : scale.y.invert(y),
116
+ };
117
+ } else {
118
+ const origin = props.origin || { x: 0, y: 0 };
119
+ const baseX = x - origin.x;
120
+ const baseY = y - origin.y;
121
+ const radius = Math.abs(baseX * Math.sqrt(1 + Math.pow(-baseY / baseX, 2)));
122
+ const angle = (-Math.atan2(baseY, baseX) + Math.PI * 2) % (Math.PI * 2);
123
+ return {
124
+ x: scale.x.invert(angle),
125
+ y: scale.y.invert(radius),
126
+ };
127
+ }
128
+ }
129
+
130
+ export function getBounds(props: ComputedCommonProps): SVGCoordinateBounds {
131
+ const { x1, x2, y1, y2, scale } = props;
132
+ const point1 = getDataCoordinates(props, scale, x1, y1);
133
+ const point2 = getDataCoordinates(props, scale, x2, y2);
134
+ const makeBound = (a: number, b: number) => {
135
+ return [
136
+ Collection.getMinValue([a, b]) as number,
137
+ Collection.getMaxValue([a, b]) as number,
138
+ ] as const;
139
+ };
140
+ return {
141
+ x: makeBound(point1.x, point2.x),
142
+ y: makeBound(point1.y, point2.y),
143
+ };
144
+ }
145
+
146
+ export type SVGCoordinateType = { x: number; y: number };
147
+ export type SVGCoordinateBounds = {
148
+ x: readonly [number, number];
149
+ y: readonly [number, number];
150
+ };
151
+
152
+ type ComputedCommonProps = {
153
+ scale: ScaleXYPropType;
154
+ x1: number;
155
+ x2: number;
156
+ y1: number;
157
+ y2: number;
158
+ };
@@ -18,7 +18,7 @@ export const toTransformString = function (obj, ...more) {
18
18
  if (obj === undefined || obj === null || typeof obj === "string") {
19
19
  return obj;
20
20
  }
21
- const transforms = [];
21
+ const transforms = [] as string[];
22
22
  for (const key in obj) {
23
23
  if (obj.hasOwnProperty(key)) {
24
24
  const value = obj[key];
@@ -49,14 +49,14 @@ export function getColorScale(name) {
49
49
  "#55DBC1",
50
50
  "#EFDA97",
51
51
  "#E2A37F",
52
- "#DF948A"
52
+ "#DF948A",
53
53
  ],
54
54
  heatmap: ["#428517", "#77D200", "#D6D305", "#EC8E19", "#C92B05"],
55
55
  warm: ["#940031", "#C43343", "#DC5429", "#FF821D", "#FFAF55"],
56
56
  cool: ["#2746B9", "#0B69D4", "#2794DB", "#31BB76", "#60E83B"],
57
57
  red: ["#FCAE91", "#FB6A4A", "#DE2D26", "#A50F15", "#750B0E"],
58
58
  blue: ["#002C61", "#004B8F", "#006BC9", "#3795E5", "#65B4F4"],
59
- green: ["#354722", "#466631", "#649146", "#8AB25C", "#A9C97E"]
59
+ green: ["#354722", "#466631", "#649146", "#8AB25C", "#A9C97E"],
60
60
  };
61
61
  return name ? scales[name] : scales.grayscale;
62
62
  }
@@ -120,23 +120,23 @@ const absoluteMeasurementUnitsToPixels = {
120
120
  pt: 1.33,
121
121
  pc: 16,
122
122
  in: 96,
123
- px: 1
123
+ px: 1,
124
124
  };
125
125
  const relativeMeasurementUnitsCoef = {
126
126
  em: 1,
127
- ex: 0.5
127
+ ex: 0.5,
128
128
  };
129
129
 
130
130
  const coefficients = {
131
131
  heightOverlapCoef: 1.05, // Coefficient for height value to prevent overlap.
132
- lineCapitalCoef: 1.15 // Coefficient for height value. Reserve space for capital chars.
132
+ lineCapitalCoef: 1.15, // Coefficient for height value. Reserve space for capital chars.
133
133
  };
134
134
  const defaultStyle = {
135
135
  lineHeight: 1,
136
136
  letterSpacing: "0px",
137
137
  fontSize: 0,
138
138
  angle: 0,
139
- fontFamily: ""
139
+ fontFamily: "",
140
140
  };
141
141
 
142
142
  const _degreeToRadian = (angle) => (angle * Math.PI) / 180;
@@ -147,7 +147,7 @@ const _getFontData = (fontFamily) => {
147
147
  return fonts[fontMatch];
148
148
  };
149
149
 
150
- const _splitToLines = (text) => {
150
+ const _splitToLines = (text: string | string[]) => {
151
151
  return Array.isArray(text) ? text : text.toString().split(/\r\n|\r|\n/g);
152
152
  };
153
153
 
@@ -165,9 +165,12 @@ const _getSizeWithRotate = (axisSize, dependentSize, angle) => {
165
165
  * @param {number} fontSize Current text font-size.
166
166
  * @returns {number} Approximate Css length in pixels.
167
167
  */
168
- export const convertLengthToPixels = (length, fontSize) => {
169
- const attribute = length.match(/[a-zA-Z%]+/) && length.match(/[a-zA-Z%]+/)[0];
170
- const value = length.match(/[0-9.,]+/);
168
+ export const convertLengthToPixels = (
169
+ length: string,
170
+ fontSize?: number,
171
+ ): number => {
172
+ const attribute = length.match(/[a-zA-Z%]+/)?.[0];
173
+ const value = Number(length.match(/[0-9.,]+/));
171
174
  let result;
172
175
  if (!attribute) {
173
176
  result = value || 0;
@@ -195,11 +198,11 @@ const _prepareParams = (inputStyle, index) => {
195
198
  fontSize:
196
199
  typeof style.fontSize === "number"
197
200
  ? style.fontSize
198
- : convertLengthToPixels(String(style.fontSize))
201
+ : convertLengthToPixels(String(style.fontSize)),
199
202
  });
200
203
  };
201
204
 
202
- const _approximateTextWidthInternal = (text, style) => {
205
+ const _approximateTextWidthInternal = (text: string | string[], style) => {
203
206
  if (text === undefined || text === "" || text === null) {
204
207
  return 0;
205
208
  }
@@ -208,7 +211,7 @@ const _approximateTextWidthInternal = (text, style) => {
208
211
  const len = line.toString().length;
209
212
  const { fontSize, letterSpacing, fontFamily } = _prepareParams(
210
213
  style,
211
- index
214
+ index,
212
215
  );
213
216
  const fontData = _getFontData(fontFamily);
214
217
  const width =
@@ -226,7 +229,7 @@ const _approximateTextWidthInternal = (text, style) => {
226
229
  return Math.max(...widths);
227
230
  };
228
231
 
229
- const _approximateTextHeightInternal = (text, style) => {
232
+ const _approximateTextHeightInternal = (text: string | string[], style) => {
230
233
  if (text === undefined || text === "" || text === null) {
231
234
  return 0;
232
235
  }
@@ -240,9 +243,18 @@ const _approximateTextHeightInternal = (text, style) => {
240
243
  }, 0);
241
244
  };
242
245
 
246
+ export interface TextSizeStyleInterface {
247
+ angle?: number;
248
+ characterConstant?: string;
249
+ fontFamily?: string;
250
+ fontSize?: number | string;
251
+ letterSpacing?: string;
252
+ lineHeight?: number;
253
+ }
254
+
243
255
  // Stubbable implementation.
244
256
  export const _approximateTextSizeInternal = {
245
- impl: (text, style) => {
257
+ impl: (text: string | string[], style?: TextSizeStyleInterface) => {
246
258
  const angle = Array.isArray(style)
247
259
  ? style[0] && style[0].angle
248
260
  : style && style.angle;
@@ -256,14 +268,14 @@ export const _approximateTextSizeInternal = {
256
268
  : height;
257
269
  return {
258
270
  width: widthWithRotate,
259
- height: heightWithRotate * coefficients.heightOverlapCoef
271
+ height: heightWithRotate * coefficients.heightOverlapCoef,
260
272
  };
261
- }
273
+ },
262
274
  };
263
275
 
264
276
  /**
265
277
  * Predict text size by font params.
266
- * @param {string} text Content for width calculation.
278
+ * @param {string|string[]} text Content for width calculation.
267
279
  * @param {Object} style Text styles, ,fontFamily, fontSize, etc.
268
280
  * @param {string} style.fontFamily Text fontFamily.
269
281
  * @param {(number|string)} style.fontSize Text fontSize.
@@ -272,5 +284,8 @@ export const _approximateTextSizeInternal = {
272
284
  * @param {number} style.lineHeight Line height coefficient.
273
285
  * @returns {number} Approximate text label height.
274
286
  */
275
- export const approximateTextSize = (text, style) =>
287
+ export const approximateTextSize = (
288
+ text: string | string[],
289
+ style?: TextSizeStyleInterface,
290
+ ): { width: number; height: number } =>
276
291
  _approximateTextSizeInternal.impl(text, style);
@@ -7,7 +7,7 @@ import Timer from "./timer";
7
7
  */
8
8
  const TimerContext = React.createContext({
9
9
  transitionTimer: new Timer(),
10
- animationTimer: new Timer()
10
+ animationTimer: new Timer(),
11
11
  });
12
12
  TimerContext.displayName = "TimerContext";
13
13
 
@@ -1,16 +1,11 @@
1
- import { timer, now } from "victory-vendor/d3-timer";
1
+ import { timer, now, Timer as D3Timer } from "victory-vendor/d3-timer";
2
2
 
3
- interface D3Timer {
4
- new (callback): void;
5
- stop(): void;
6
- }
7
-
8
- type Callback = (elapsed: number, duration: number) => void;
3
+ type TimerCallback = (elapsed: number, duration: number) => void;
9
4
 
10
5
  export default class Timer {
11
6
  private shouldAnimate: boolean;
12
7
  private readonly subscribers: Array<{
13
- callback: Callback;
8
+ callback: TimerCallback;
14
9
  startTime: number;
15
10
  duration: number;
16
11
  }>;
@@ -51,12 +46,12 @@ export default class Timer {
51
46
  }
52
47
  }
53
48
 
54
- subscribe(callback: Callback, duration: number) {
49
+ subscribe(callback: TimerCallback, duration: number) {
55
50
  duration = this.shouldAnimate ? duration : 0;
56
51
  const subscriptionID = this.subscribers.push({
57
52
  startTime: now(),
58
53
  callback,
59
- duration
54
+ duration,
60
55
  });
61
56
  this.activeSubscriptions++;
62
57
  this.start();
@@ -1,5 +1,6 @@
1
1
  import { assign, defaults, identity, keys } from "lodash";
2
2
  import React from "react";
3
+ import { AnimatePropTypeInterface } from "../types/prop-types";
3
4
 
4
5
  function getDatumKey(datum, idx) {
5
6
  return (datum.key || idx).toString();
@@ -46,7 +47,7 @@ function getNodeTransitions(oldData, nextData) {
46
47
  entering:
47
48
  oldDataKeyed && getKeyedDataDifference(nextDataKeyed, oldDataKeyed),
48
49
  exiting:
49
- nextDataKeyed && getKeyedDataDifference(oldDataKeyed, nextDataKeyed)
50
+ nextDataKeyed && getKeyedDataDifference(oldDataKeyed, nextDataKeyed),
50
51
  };
51
52
  }
52
53
 
@@ -59,7 +60,7 @@ function getChildData(child) {
59
60
 
60
61
  /**
61
62
  * If a parent component has animation enabled, calculate the transitions
62
- * for any data of any child component that supports data transitions
63
+ * for any data of any child component that supports data transitions.
63
64
  * Data transitions are defined as any two datasets where data nodes exist
64
65
  * in the first set and not the second, in the second and not the first,
65
66
  * or both.
@@ -95,7 +96,7 @@ export function getInitialTransitionState(oldChildren, nextChildren) {
95
96
  if (child && child.props && child.props.children && next[idx]) {
96
97
  return getTransitionsFromChildren(
97
98
  React.Children.toArray(old[idx].props.children),
98
- React.Children.toArray(next[idx].props.children)
99
+ React.Children.toArray(next[idx].props.children),
99
100
  );
100
101
  }
101
102
  // get Transition entering and exiting nodes
@@ -105,7 +106,7 @@ export function getInitialTransitionState(oldChildren, nextChildren) {
105
106
 
106
107
  const childrenTransitions = getTransitionsFromChildren(
107
108
  React.Children.toArray(oldChildren),
108
- React.Children.toArray(nextChildren)
109
+ React.Children.toArray(nextChildren),
109
110
  );
110
111
  return {
111
112
  nodesWillExit,
@@ -116,20 +117,26 @@ export function getInitialTransitionState(oldChildren, nextChildren) {
116
117
  // is a perfect match for the previous data and domain except
117
118
  // for new nodes. In this case, we wouldn't want a delay before
118
119
  // the new nodes appear.
119
- nodesShouldEnter: false
120
+ nodesShouldEnter: false,
120
121
  };
121
122
  }
122
123
 
123
- function getInitialChildProps(animate, data) {
124
+ type TransitionProps = {
125
+ data;
126
+ animate?: AnimatePropTypeInterface;
127
+ clipWidth?: number;
128
+ };
129
+
130
+ function getInitialChildProps(animate, data): TransitionProps {
124
131
  const after =
125
132
  animate.onEnter && animate.onEnter.after ? animate.onEnter.after : identity;
126
133
  return {
127
- data: data.map((datum, idx) => assign({}, datum, after(datum, idx, data)))
134
+ data: data.map((datum, idx) => assign({}, datum, after(datum, idx, data))),
128
135
  };
129
136
  }
130
137
 
131
138
  // eslint-disable-next-line max-params
132
- function getChildBeforeLoad(animate, child, data, cb) {
139
+ function getChildBeforeLoad(animate, child, data, cb): TransitionProps {
133
140
  animate = assign({}, animate, { onEnd: cb });
134
141
  if (animate && animate.onLoad && !animate.onLoad.duration) {
135
142
  return { animate, data };
@@ -145,7 +152,7 @@ function getChildBeforeLoad(animate, child, data, cb) {
145
152
  }
146
153
 
147
154
  // eslint-disable-next-line max-params
148
- function getChildOnLoad(animate, data, cb) {
155
+ function getChildOnLoad(animate, data, cb): TransitionProps {
149
156
  animate = assign({}, animate, { onEnd: cb });
150
157
  if (animate && animate.onLoad && !animate.onLoad.duration) {
151
158
  return { animate, data };
@@ -161,7 +168,13 @@ function getChildOnLoad(animate, data, cb) {
161
168
  }
162
169
 
163
170
  // eslint-disable-next-line max-params, max-len
164
- function getChildPropsOnExit(animate, child, data, exitingNodes, cb) {
171
+ function getChildPropsOnExit(
172
+ animate,
173
+ child,
174
+ data,
175
+ exitingNodes,
176
+ cb,
177
+ ): TransitionProps {
165
178
  // Whether or not _this_ child has exiting nodes, we want the exit-
166
179
  // transition for all children to have the same duration, delay, etc.
167
180
  const onExit = animate && animate.onExit;
@@ -169,7 +182,7 @@ function getChildPropsOnExit(animate, child, data, exitingNodes, cb) {
169
182
 
170
183
  if (exitingNodes) {
171
184
  // After the exit transition occurs, trigger the animations for
172
- // nodes that are neither exiting or entering.
185
+ // nodes that are neither exiting nor entering.
173
186
  animate.onEnd = cb;
174
187
  const before =
175
188
  animate.onExit && animate.onExit.before
@@ -188,7 +201,13 @@ function getChildPropsOnExit(animate, child, data, exitingNodes, cb) {
188
201
  }
189
202
 
190
203
  // eslint-disable-next-line max-params,max-len
191
- function getChildPropsBeforeEnter(animate, child, data, enteringNodes, cb) {
204
+ function getChildPropsBeforeEnter(
205
+ animate,
206
+ child,
207
+ data,
208
+ enteringNodes,
209
+ cb,
210
+ ): TransitionProps {
192
211
  if (enteringNodes) {
193
212
  // Perform a normal animation here, except - when it finishes - trigger
194
213
  // the transition for entering nodes.
@@ -212,7 +231,12 @@ function getChildPropsBeforeEnter(animate, child, data, enteringNodes, cb) {
212
231
  }
213
232
 
214
233
  // eslint-disable-next-line max-params, max-len
215
- function getChildPropsOnEnter(animate, data, enteringNodes, cb) {
234
+ function getChildPropsOnEnter(
235
+ animate,
236
+ data,
237
+ enteringNodes,
238
+ cb,
239
+ ): TransitionProps {
216
240
  // Whether or not _this_ child has entering nodes, we want the entering-
217
241
  // transition for all children to have the same duration, delay, etc.
218
242
  const onEnter = animate && animate.onEnter;
@@ -243,7 +267,7 @@ function getChildPropsOnEnter(animate, data, enteringNodes, cb) {
243
267
  * and its index in the parent's children array.
244
268
  *
245
269
  * In particular, this will include an `animate` object that is set appropriately
246
- * so that each child will be synchoronized for each stage of a transition
270
+ * so that each child will be synchronized for each stage of a transition
247
271
  * animation. It will also include a transformed `data` object, where each datum
248
272
  * is transformed by `animate.onExit` and `animate.onEnter` `before` and `after`
249
273
  * functions.
@@ -269,7 +293,7 @@ export function getTransitionPropsFactory(props, state, setState) {
269
293
  props.animate && props.animate.onExit && props.animate.onExit.duration,
270
294
  load:
271
295
  props.animate && props.animate.onLoad && props.animate.onLoad.duration,
272
- move: props.animate && props.animate.duration
296
+ move: props.animate && props.animate.duration,
273
297
  };
274
298
 
275
299
  const onLoad = (child, data, animate) => {
@@ -324,9 +348,13 @@ export function getTransitionPropsFactory(props, state, setState) {
324
348
  };
325
349
 
326
350
  // eslint-disable-next-line max-statements, complexity, max-len
327
- return function getTransitionProps(child, index) {
351
+ return function getTransitionProps(child, index): TransitionProps {
328
352
  const data = getChildData(child) || [];
329
- const animate = defaults({}, props.animate, child.props.animate);
353
+ const animate: AnimatePropTypeInterface = defaults(
354
+ {},
355
+ props.animate,
356
+ child.props.animate,
357
+ );
330
358
  const defaultTransitions = child.props.polar
331
359
  ? child.type.defaultPolarTransitions || child.type.defaultTransitions
332
360
  : child.type.defaultTransitions;
@@ -334,17 +362,17 @@ export function getTransitionPropsFactory(props, state, setState) {
334
362
  animate.onExit = defaults(
335
363
  {},
336
364
  animate.onExit,
337
- defaultTransitions && defaultTransitions.onExit
365
+ defaultTransitions && defaultTransitions.onExit,
338
366
  );
339
367
  animate.onEnter = defaults(
340
368
  {},
341
369
  animate.onEnter,
342
- defaultTransitions && defaultTransitions.onEnter
370
+ defaultTransitions && defaultTransitions.onEnter,
343
371
  );
344
372
  animate.onLoad = defaults(
345
373
  {},
346
374
  animate.onLoad,
347
- defaultTransitions && defaultTransitions.onLoad
375
+ defaultTransitions && defaultTransitions.onLoad,
348
376
  );
349
377
 
350
378
  const childTransitions =
@@ -377,13 +405,13 @@ export function getTransitionPropsFactory(props, state, setState) {
377
405
  ? transitionDurations.move
378
406
  : child.props.animate && child.props.animate.duration;
379
407
  const animation = {
380
- duration: nodesShouldEnter && enteringNodes ? enter : move
408
+ duration: nodesShouldEnter && enteringNodes ? enter : move,
381
409
  };
382
410
  return onEnter(
383
411
  enteringNodes,
384
412
  child,
385
413
  data,
386
- assign({}, animate, animation)
414
+ assign({}, animate, animation),
387
415
  );
388
416
  } else if (!state && animate && animate.onExit) {
389
417
  // This is the initial render, and nodes may enter when props change. Because
@@ -0,0 +1,50 @@
1
+ import {
2
+ ForAxes,
3
+ AxisType,
4
+ Tuple,
5
+ Datum,
6
+ DatumValue,
7
+ } from "../types/prop-types";
8
+
9
+ export function hasValueForAxis<T = unknown>(
10
+ value: unknown | ForAxes<T>,
11
+ axis: AxisType,
12
+ ): value is ForAxes<T> {
13
+ if (typeof value === "object" && value !== null) {
14
+ return axis in value;
15
+ }
16
+ return false;
17
+ }
18
+
19
+ export function isTuple<T = unknown>(value: unknown): value is Tuple<T> {
20
+ return Array.isArray(value) && value.length === 2;
21
+ }
22
+
23
+ export function getValueForAxis<T = unknown>(
24
+ value: T | ForAxes<T> | undefined,
25
+ axis: AxisType,
26
+ ): T | undefined {
27
+ if (hasValueForAxis<T>(value, axis)) {
28
+ return value[axis] as T;
29
+ }
30
+ return value;
31
+ }
32
+
33
+ // eslint-disable-next-line @typescript-eslint/ban-types
34
+ export function isFunction<T = Function>(func?: unknown): func is T {
35
+ return typeof func === "function";
36
+ }
37
+
38
+ export function isDate(value: unknown): value is Date {
39
+ return value instanceof Date;
40
+ }
41
+
42
+ export function isKeyValueObject(
43
+ datum: Datum,
44
+ ): datum is { [key: string]: DatumValue } {
45
+ return (
46
+ typeof datum === "object" &&
47
+ !Array.isArray(datum) &&
48
+ !(datum instanceof Date)
49
+ );
50
+ }
@@ -2,15 +2,17 @@ import * as React from "react";
2
2
 
3
3
  /*
4
4
  USER_PROPS_SAFELIST is to contain any string deemed safe for user props.
5
- The startsWidth array will contain the start of any accepted user-prop that
5
+ The startsWidth array will contain the start of any accepted user-prop that
6
6
  starts with these characters.
7
7
  The exactMatch will contain a list of exact prop names that are accepted.
8
8
  */
9
9
  const USER_PROPS_SAFELIST = {
10
- startsWith: ["data-", "aria-"],
11
- exactMatch: []
10
+ startsWith: ["data-", "aria-"] as const,
11
+ exactMatch: [] as string[],
12
12
  };
13
13
 
14
+ type SafeAttribute = `data-${string}` | `aria-${string}`;
15
+
14
16
  /**
15
17
  * doesPropStartWith: Function that takes a prop's key and runs it against all
16
18
  * options in the USER_PROPS_SAFELIST and checks to see if it starts with any
@@ -19,7 +21,7 @@ const USER_PROPS_SAFELIST = {
19
21
  * @returns {Boolean}: returns true if the key starts with an option or false if
20
22
  * otherwise
21
23
  */
22
- const doesPropStartWith = (key) => {
24
+ const doesPropStartWith = (key: string): key is SafeAttribute => {
23
25
  let startsWith = false;
24
26
 
25
27
  USER_PROPS_SAFELIST.startsWith.forEach((starterString) => {
@@ -38,14 +40,15 @@ const doesPropStartWith = (key) => {
38
40
  * @returns {Boolean}: return true if whitelist contains that key, otherwise
39
41
  * returns false.
40
42
  */
41
- const isExactMatch = (key) => USER_PROPS_SAFELIST.exactMatch.includes(key);
43
+ const isExactMatch = (key: string): key is SafeAttribute =>
44
+ USER_PROPS_SAFELIST.exactMatch.includes(key);
42
45
 
43
46
  /**
44
47
  * testIfSafeProp: tests prop's key against both startsWith and exactMatch values
45
48
  * @param {String} key: prop key to be tested against the whitelist
46
49
  * @returns {Boolean}: returns true if found in whitelist, otherwise returns false
47
50
  */
48
- const testIfSafeProp = (key) => {
51
+ const testIfSafeProp = (key: string): key is SafeAttribute => {
49
52
  if (doesPropStartWith(key) || isExactMatch(key)) return true;
50
53
  return false;
51
54
  };
@@ -71,24 +74,26 @@ const getValue = (value, props) => {
71
74
  * @param {Object} props: props to be filtered against USER_PROPS_SAFELIST
72
75
  * @returns {Object}: object containing remaining acceptable props
73
76
  */
74
- export const getSafeUserProps = (props) => {
77
+ export const getSafeUserProps = <T>(
78
+ props: T,
79
+ ): Record<SafeAttribute, string> => {
75
80
  const propsToFilter = { ...props };
76
81
  return Object.fromEntries(
77
82
  Object.entries(propsToFilter)
78
83
  .filter(([key]) => testIfSafeProp(key))
79
84
  .map(([key, value]) => {
80
85
  return [key, getValue(value, props)];
81
- })
86
+ }),
82
87
  );
83
88
  };
84
89
 
85
90
  /**
86
91
  * Wraps a component and adds safe user props
87
92
  *
88
- * @param {ReactNode} component: parent component
93
+ * @param {ReactElement} component: parent component
89
94
  * @param {Object} props: props to be filtered
90
- * @returns {ReactNode} modified component
95
+ * @returns {ReactElement} modified component
91
96
  */
92
- export const withSafeUserProps = (component, props) => {
97
+ export const withSafeUserProps = (component: React.ReactElement, props) => {
93
98
  return React.cloneElement(component, getSafeUserProps(props));
94
99
  };