@telus-uds/components-base 1.0.1 → 1.3.0

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 (193) hide show
  1. package/.storybook/main.js +4 -0
  2. package/.storybook/preview.js +37 -0
  3. package/.turbo/turbo-build.log +5 -0
  4. package/CHANGELOG.md +32 -0
  5. package/README.md +1 -1
  6. package/__fixtures__/Accessible.js +4 -2
  7. package/__fixtures__/Accessible.native.js +5 -2
  8. package/__fixtures__/testTheme.js +9 -0
  9. package/__tests__/FlexGrid/Col.test.jsx +6 -10
  10. package/__tests__/HorizontalScroll/HorizontalScroll.test.jsx +1 -0
  11. package/__tests__/ToggleSwitch/ToggleSwitch.test.jsx +10 -0
  12. package/__tests__/ToggleSwitch/ToggleSwitchGroup.test.jsx +192 -0
  13. package/__tests__/utils/props.test.js +36 -0
  14. package/__tests__/utils/semantics.test.jsx +1 -10
  15. package/babel.config.js +9 -16
  16. package/component-docs.json +10142 -0
  17. package/generate-component-docs.js +59 -0
  18. package/lib/A11yText/index.js +2 -2
  19. package/lib/ActivityIndicator/index.js +3 -3
  20. package/lib/Box/Box.js +4 -4
  21. package/lib/Button/Button.js +2 -2
  22. package/lib/Button/ButtonBase.js +20 -6
  23. package/lib/Button/ButtonGroup.js +10 -12
  24. package/lib/Button/ButtonLink.js +4 -4
  25. package/lib/Button/propTypes.js +4 -2
  26. package/lib/Card/Card.js +2 -2
  27. package/lib/Card/CardBase.js +6 -9
  28. package/lib/Card/PressableCardBase.js +12 -6
  29. package/lib/Checkbox/Checkbox.js +3 -5
  30. package/lib/Divider/Divider.js +2 -2
  31. package/lib/FlexGrid/FlexGrid.js +4 -4
  32. package/lib/FlexGrid/helpers/index.js +1 -4
  33. package/lib/IconButton/IconButton.js +11 -6
  34. package/lib/Link/LinkBase.js +19 -12
  35. package/lib/List/List.js +4 -5
  36. package/lib/List/ListItem.js +14 -27
  37. package/lib/List/index.js +15 -0
  38. package/lib/Pagination/PageButton.js +4 -8
  39. package/lib/Pagination/Pagination.js +16 -4
  40. package/lib/Pagination/SideButton.js +3 -1
  41. package/lib/Progress/Progress.js +2 -2
  42. package/lib/Progress/ProgressBar.js +2 -2
  43. package/lib/Radio/Radio.js +3 -5
  44. package/lib/Spacer/Spacer.js +2 -2
  45. package/lib/StackView/StackWrap.js +9 -5
  46. package/lib/StackView/getStackedContent.js +1 -1
  47. package/lib/StepTracker/StepTracker.js +2 -2
  48. package/lib/Tabs/Tabs.js +12 -4
  49. package/lib/Tabs/TabsItem.js +12 -6
  50. package/lib/Tags/Tags.js +9 -9
  51. package/lib/ThemeProvider/useThemeTokens.js +3 -3
  52. package/lib/ThemeProvider/utils/theme-tokens.js +3 -3
  53. package/lib/ToggleSwitch/ToggleSwitch.js +105 -43
  54. package/lib/ToggleSwitch/ToggleSwitchGroup.js +230 -0
  55. package/lib/ToggleSwitch/index.js +14 -4
  56. package/lib/index.js +28 -9
  57. package/lib/utils/a11y/semantics.js +4 -3
  58. package/lib/utils/index.js +14 -5
  59. package/lib/utils/pressability.js +2 -2
  60. package/lib/utils/props/a11yProps.js +153 -0
  61. package/lib/utils/props/clickProps.js +36 -0
  62. package/lib/utils/props/componentPropType.js +70 -0
  63. package/lib/utils/props/copyPropTypes.js +14 -0
  64. package/lib/utils/props/getPropSelector.js +13 -0
  65. package/lib/utils/props/hrefAttrsProp.js +41 -0
  66. package/lib/utils/props/index.js +149 -0
  67. package/lib/utils/props/linkProps.js +64 -0
  68. package/lib/utils/props/paddingProp.js +20 -0
  69. package/lib/utils/props/pressProps.js +57 -0
  70. package/lib/utils/props/rectProp.js +20 -0
  71. package/lib/utils/props/responsiveProps.js +40 -0
  72. package/lib/utils/props/selectSystemProps.js +31 -0
  73. package/lib/utils/props/spacingProps.js +71 -0
  74. package/lib/utils/props/tokens.js +145 -0
  75. package/lib/utils/props/variantProp.js +28 -0
  76. package/lib/utils/props/viewProps.js +34 -0
  77. package/lib/utils/useResponsiveProp.js +1 -1
  78. package/lib/utils/useSpacingScale.js +4 -4
  79. package/lib/utils/withLinkRouter.js +98 -0
  80. package/package.json +9 -5
  81. package/release-context.json +4 -4
  82. package/src/A11yText/index.jsx +1 -1
  83. package/src/ActivityIndicator/index.jsx +1 -1
  84. package/src/Box/Box.jsx +5 -4
  85. package/src/Button/Button.jsx +1 -1
  86. package/src/Button/ButtonBase.jsx +11 -4
  87. package/src/Button/ButtonGroup.jsx +17 -8
  88. package/src/Button/ButtonLink.jsx +1 -1
  89. package/src/Button/propTypes.js +2 -1
  90. package/src/Card/Card.jsx +1 -1
  91. package/src/Card/CardBase.jsx +6 -5
  92. package/src/Card/PressableCardBase.jsx +7 -5
  93. package/src/Checkbox/Checkbox.jsx +1 -3
  94. package/src/Divider/Divider.jsx +2 -2
  95. package/src/FlexGrid/FlexGrid.jsx +11 -5
  96. package/src/FlexGrid/helpers/index.js +1 -3
  97. package/src/IconButton/IconButton.jsx +7 -5
  98. package/src/Link/LinkBase.jsx +7 -5
  99. package/src/List/List.jsx +2 -4
  100. package/src/List/ListItem.jsx +11 -26
  101. package/src/List/index.js +5 -0
  102. package/src/Pagination/PageButton.jsx +5 -8
  103. package/src/Pagination/Pagination.jsx +29 -2
  104. package/src/Pagination/SideButton.jsx +2 -2
  105. package/src/Progress/Progress.jsx +1 -1
  106. package/src/Progress/ProgressBar.jsx +1 -1
  107. package/src/Radio/Radio.jsx +1 -3
  108. package/src/Spacer/Spacer.jsx +2 -2
  109. package/src/StackView/StackWrap.jsx +7 -6
  110. package/src/StackView/getStackedContent.jsx +1 -1
  111. package/src/StepTracker/StepTracker.jsx +1 -1
  112. package/src/Tabs/Tabs.jsx +49 -22
  113. package/src/Tabs/TabsItem.jsx +11 -7
  114. package/src/Tags/Tags.jsx +1 -7
  115. package/src/ThemeProvider/useThemeTokens.js +3 -3
  116. package/src/ThemeProvider/utils/theme-tokens.js +3 -3
  117. package/src/ToggleSwitch/ToggleSwitch.jsx +93 -41
  118. package/src/ToggleSwitch/ToggleSwitchGroup.jsx +203 -0
  119. package/src/ToggleSwitch/index.js +2 -1
  120. package/src/index.js +2 -2
  121. package/src/utils/a11y/semantics.js +3 -2
  122. package/src/utils/index.js +2 -1
  123. package/src/utils/pressability.js +1 -1
  124. package/src/utils/props/a11yProps.js +151 -0
  125. package/src/utils/props/clickProps.js +31 -0
  126. package/src/utils/props/componentPropType.js +67 -0
  127. package/src/utils/props/copyPropTypes.js +3 -0
  128. package/src/utils/props/getPropSelector.js +14 -0
  129. package/src/utils/props/hrefAttrsProp.js +25 -0
  130. package/src/utils/props/index.js +15 -0
  131. package/src/utils/props/linkProps.js +43 -0
  132. package/src/utils/props/paddingProp.js +10 -0
  133. package/src/utils/props/pressProps.js +45 -0
  134. package/src/utils/props/rectProp.js +10 -0
  135. package/src/utils/props/responsiveProps.js +30 -0
  136. package/src/utils/props/selectSystemProps.js +25 -0
  137. package/src/utils/props/spacingProps.js +58 -0
  138. package/src/utils/props/tokens.js +150 -0
  139. package/src/utils/props/variantProp.js +20 -0
  140. package/src/utils/props/viewProps.js +23 -0
  141. package/src/utils/useResponsiveProp.js +1 -1
  142. package/src/utils/useSpacingScale.js +4 -4
  143. package/src/utils/withLinkRouter.jsx +68 -0
  144. package/stories/A11yText/A11yText.stories.jsx +1 -1
  145. package/stories/ActivityIndicator/ActivityIndicator.stories.jsx +1 -1
  146. package/stories/Box/Box.stories.jsx +1 -1
  147. package/stories/Button/Button.stories.jsx +1 -1
  148. package/stories/Button/ButtonGroup.stories.jsx +1 -1
  149. package/stories/Button/ButtonLink.stories.jsx +1 -1
  150. package/stories/Card/Card.stories.jsx +1 -1
  151. package/stories/Checkbox/Checkbox.stories.jsx +1 -1
  152. package/stories/Divider/Divider.stories.jsx +1 -1
  153. package/stories/ExpandCollapse/ExpandCollapse.stories.jsx +1 -1
  154. package/stories/Feedback/Feedback.stories.jsx +1 -1
  155. package/stories/FlexGrid/01 FlexGrid.stories.jsx +1 -1
  156. package/stories/FlexGrid/02 Row.stories.jsx +1 -1
  157. package/stories/FlexGrid/03 Col.stories.jsx +1 -1
  158. package/stories/Icon/Icon.stories.jsx +1 -1
  159. package/stories/IconButton/IconButton.stories.jsx +1 -1
  160. package/stories/InputLabel/InputLabel.stories.jsx +1 -1
  161. package/stories/Link/ChevronLink.stories.jsx +1 -1
  162. package/stories/Link/Link.stories.jsx +1 -1
  163. package/stories/Link/TextButton.stories.jsx +1 -1
  164. package/stories/List/List.stories.jsx +1 -1
  165. package/stories/Modal/Modal.stories.jsx +1 -1
  166. package/stories/Notification/Notification.stories.jsx +1 -1
  167. package/stories/Pagination/Pagination.stories.jsx +1 -1
  168. package/stories/Progress/Progress.stories.jsx +1 -1
  169. package/stories/Radio/Radio.stories.jsx +1 -1
  170. package/stories/RadioCard/RadioCard.stories.jsx +1 -1
  171. package/stories/Search/Search.stories.jsx +1 -1
  172. package/stories/Select/Select.stories.jsx +1 -1
  173. package/stories/SideNav/SideNav.stories.jsx +1 -1
  174. package/stories/SideNav/SideNavItem.stories.jsx +1 -1
  175. package/stories/SideNav/SideNavItemsGroup.stories.jsx +1 -1
  176. package/stories/Skeleton/Skeleton.stories.jsx +1 -1
  177. package/stories/Spacer/Spacer.stories.jsx +1 -1
  178. package/stories/StackView/StackView.stories.jsx +1 -1
  179. package/stories/StackView/StackWrap.stories.jsx +1 -1
  180. package/stories/StepTracker/StepTracker.stories.jsx +1 -1
  181. package/stories/Tabs/Tabs.stories.jsx +1 -1
  182. package/stories/Tags/Tags.stories.jsx +1 -1
  183. package/stories/TextInput/TextArea.stories.jsx +2 -1
  184. package/stories/TextInput/TextInput.stories.jsx +1 -1
  185. package/stories/ToggleSwitch/ToggleSwitch.stories.jsx +6 -2
  186. package/stories/ToggleSwitch/ToggleSwitchGroup.stories.jsx +81 -0
  187. package/stories/Tooltip/Tooltip.stories.jsx +1 -1
  188. package/stories/TooltipButton/TooltipButton.stories.jsx +1 -1
  189. package/stories/Typography/Typography.stories.jsx +1 -1
  190. package/stories/supports.jsx +2 -3
  191. package/.ultra.cache.json +0 -1
  192. package/lib/utils/propTypes.js +0 -623
  193. package/src/utils/propTypes.js +0 -640
@@ -1,640 +0,0 @@
1
- import PropTypes from 'prop-types'
2
- import { Linking, Platform } from 'react-native'
3
- import { components as tokenKeys } from '@telus-uds/system-theme-tokens'
4
-
5
- export const paddingProp = {
6
- propType: PropTypes.shape({
7
- paddingBottom: PropTypes.number,
8
- paddingLeft: PropTypes.number,
9
- paddingRight: PropTypes.number,
10
- paddingTop: PropTypes.number
11
- })
12
- }
13
-
14
- export const rectProp = {
15
- propType: PropTypes.shape({
16
- bottom: PropTypes.number,
17
- left: PropTypes.number,
18
- right: PropTypes.number,
19
- top: PropTypes.number
20
- })
21
- }
22
-
23
- /**
24
- * @typedef {{[key: string]: string|number|boolean}} AppearanceSet
25
- * @typedef {AppearanceSet} VariantProp
26
- */
27
- export const variantProp = {
28
- /**
29
- * 'variant' is an optional object prop on all themable components.
30
- *
31
- * Contains an object with keys that correspond to the current component theme's allowed
32
- * appearances and values that correspond to the allowed values of that component.
33
- *
34
- * Since the particular keys and values depend on which theme is currently active,
35
- * the exact shape of variant props is not enforced using PropTypes.
36
- */
37
- propType: PropTypes.objectOf(
38
- PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.bool])
39
- )
40
- }
41
-
42
- // Tokens can be primitive values (e.g. `'rgba(0,0,0,0'`, `12`), or objects of such values,
43
- // such as tokens that describe shadow (e.g. shadow: { inset: true, color: 'rgba(...)' ... }),
44
- // or components (e.g. Icon tokens)
45
- const tokenValue = PropTypes.oneOfType([
46
- PropTypes.string,
47
- PropTypes.number,
48
- PropTypes.bool,
49
- PropTypes.elementType
50
- ])
51
- const tokenValueType = PropTypes.oneOfType([tokenValue, PropTypes.objectOf(tokenValue)])
52
-
53
- export const getTokenNames = (componentName) => {
54
- const componentTokenSchema = tokenKeys[componentName]
55
- if (!componentTokenSchema) {
56
- throw new Error(`No "${componentName}" tokenKeys in @telus-uds/system-theme-tokens`)
57
- }
58
- return Object.keys(componentTokenSchema)
59
- }
60
-
61
- /**
62
- * Returns the subset of a set of tokens that may be accepted by the `tokens` prop of a named component
63
- * or by a provided array of tokens. A prefix may be provided, for example:
64
- *
65
- * @example
66
- * ```jsx
67
- * // returns tokens from `themeTokens` that exist in the theme schema for `Button`.
68
- * selectTokens('Button', themeTokens)
69
- * ```
70
- *
71
- * @example
72
- * ```jsx
73
- * // returns `{ backgroundColor, width }` where the values of those keys come from
74
- * // the source object's `{ containerBackgroundColor, containerWidth }` properties.
75
- * selectTokens(['backgroundColor', 'width'], themeTokens, 'container')
76
- * ```
77
- *
78
- * @example
79
- * ```jsx
80
- * // returns tokens that are defined in the `Button` theme schema, from tokens with
81
- * // a prefix `'button'` e.g. a token `buttonBorderWidth` outputs as `borderWidth`.
82
- * selectTokens('Button', themeTokens, 'button')
83
- * ```
84
- *
85
- * @param {string[]|string} specifier - a name of a component in the theme schema, or an array of token names
86
- * @param {object} tokens - a source object of theme tokens
87
- * @param {string} [prefix] - if provided, matches keys in the source object with this as the first camelCase item
88
- * @returns {object} - subset of theme tokens
89
- */
90
- export const selectTokens = (specifier, tokens, prefix) => {
91
- const tokenNames = typeof specifier === 'string' ? getTokenNames(specifier) : specifier
92
- const filteredTokens = tokenNames.reduce((validTokens, key) => {
93
- const prefixedKey = prefix ? `${prefix}${key[0].toUpperCase()}${key.slice(1)}` : key
94
- const token = tokens[prefixedKey]
95
- return token !== undefined ? { ...validTokens, [key]: token } : validTokens
96
- }, {})
97
- return filteredTokens
98
- }
99
-
100
- /**
101
- * @typedef {string|number|boolean|{[key: string]:string|number|boolean}} TokenValue
102
- * @typedef {{[key: string]: TokenValue}} TokensSet
103
- * @typedef {(AppearanceSet) => TokensSet} TokensGetter
104
- * @typedef {TokensSet|TokensGetter} TokensProp
105
- */
106
- /**
107
- * 'tokens' is an optional object prop on all themable components. Its keys must match the
108
- * token keys in the component's theme schema.
109
- *
110
- * This prop is intended to be used as an 'escape hatch' for difficult or exceptional cases
111
- * where the main theming system doesn't apply. It is intentionally permissive about values.
112
- *
113
- * @param {...string} componentsNames - one or more ComponentName, which tokens keys are accepted
114
- * @return {function} - a custom PropTypes validator
115
- *
116
- * @example
117
- * Component.propTypes = {
118
- * // accepts all tokens keys defined in Component schema
119
- * tokens: getTokensPropType('Component')
120
- * }
121
- *
122
- * Component.propTypes = {
123
- * // accepts all tokens keys defined in schemas for Component1 and Component2
124
- * tokens: getTokensPropType('Component1', 'Component2')
125
- * }
126
- */
127
- export const getTokensPropType = (...componentsNames) => (props, propName, componentName) => {
128
- PropTypes.checkPropTypes(
129
- {
130
- [propName]: PropTypes.oneOfType([PropTypes.object, PropTypes.func])
131
- },
132
- props,
133
- propName,
134
- componentName
135
- )
136
-
137
- if (typeof props[propName] !== 'function') {
138
- PropTypes.checkPropTypes(
139
- {
140
- [propName]: PropTypes.exact(
141
- Object.fromEntries(
142
- componentsNames.flatMap((component) =>
143
- getTokenNames(component).map((key) => [key, tokenValueType])
144
- )
145
- )
146
- )
147
- },
148
- props,
149
- propName,
150
- componentName
151
- )
152
- }
153
- }
154
-
155
- /**
156
- * Get a proptypes validator for a set of tokens that is not based on a component in the theme schema.
157
- *
158
- * For example, for a set of tokens used in a common style, or for a set of tokens required by
159
- * a themeless component whose tokens are set by a parent but requires tokens of a certain shape.
160
- *
161
- * By default, requires an object with a complete set of tokens (allowing `null`, but not `undefined`).
162
- *
163
- * @param {string[]} componentTokenKeys - array of strings of token names
164
- * @param {object} [options]
165
- * @param {boolean} [options.partial] - if true, allows tokens to be undefined
166
- * @param {boolean} [options.allowFunction] - if true, allows functions as well as tokens objects
167
- * @returns
168
- */
169
- export const getTokensSetPropType = (
170
- componentTokenKeys,
171
- { partial = false, allowFunction = false } = {}
172
- ) => {
173
- const tokensObjectValidator = PropTypes.exact(
174
- Object.fromEntries(
175
- componentTokenKeys.map((key) => [
176
- key,
177
- partial
178
- ? tokenValueType
179
- : // Some theme tokens can validly resolve to `null`, but .isRequired treats null as undefined
180
- (props, propName, ...args) =>
181
- props[propName] !== null && tokenValueType.isRequired(props, propName, ...args)
182
- ])
183
- )
184
- )
185
- return allowFunction
186
- ? PropTypes.oneOfType([tokensObjectValidator, PropTypes.func])
187
- : tokensObjectValidator
188
- }
189
-
190
- function getPropSelector(propTypes, regexp) {
191
- const keys = Object.keys(propTypes)
192
- return (props) =>
193
- Object.entries(props).reduce(
194
- (items, [key, value]) =>
195
- keys.includes(key) || (regexp && regexp.test(key))
196
- ? {
197
- ...items,
198
- [key]: value
199
- }
200
- : items,
201
- {}
202
- )
203
- }
204
-
205
- // React Native exports a11y prop definitions as TypeScript Interfaces, but no longer exports PropTypes
206
- // so we have to define them ourselves.
207
- const a11yPropTypes = {
208
- accessible: PropTypes.bool,
209
- focusable: PropTypes.bool,
210
-
211
- accessibilityElementsHidden: PropTypes.bool,
212
- accessibilityHint: PropTypes.string, // react-native-web ignores `accessibilityHint`
213
- accessibilityIgnoresInvertColors: PropTypes.bool,
214
- accessibilityLabel: PropTypes.string,
215
- accessibilityRole: PropTypes.string,
216
- accessibilityActions: PropTypes.arrayOf(
217
- PropTypes.shape({
218
- name: PropTypes.string.isRequired,
219
- label: PropTypes.string
220
- })
221
- ),
222
- accessibilityLiveRegion: PropTypes.oneOf(['none', 'polite', 'assertive']),
223
- accessibilityState: PropTypes.shape({
224
- disabled: PropTypes.bool,
225
- selected: PropTypes.bool,
226
- checked: PropTypes.oneOf([true, false, 'mixed']),
227
- busy: PropTypes.bool,
228
- expanded: PropTypes.bool
229
- }),
230
- accessibilityValue: PropTypes.shape({
231
- min: PropTypes.number,
232
- max: PropTypes.number,
233
- now: PropTypes.number,
234
- text: PropTypes.string
235
- }),
236
- accessibilityViewIsModal: PropTypes.bool,
237
- importantForAccessibility: PropTypes.oneOf(['auto', 'yes', 'no', 'no-hide-descendants']),
238
- onAccessibilityAction: PropTypes.func,
239
- onAccessibilityEscape: PropTypes.func,
240
- onAccessibilityTap: PropTypes.func,
241
- ...Platform.select({
242
- web: {
243
- // React Native Web adds many a11y props that alias aria-* attributes
244
- // Types based on https://necolas.github.io/react-native-web/docs/accessibility/
245
- accessibilityActiveDescendant: PropTypes.string,
246
- accessibilityAtomic: PropTypes.bool,
247
- accessibilityAutoComplete: PropTypes.string,
248
- accessibilityBusy: PropTypes.bool,
249
- accessibilityChecked: PropTypes.oneOf([true, false, 'mixed']),
250
- accessibilityColumnCount: PropTypes.number,
251
- accessibilityColumnIndex: PropTypes.number,
252
- accessibilityColumnSpan: PropTypes.number,
253
- accessibilityControls: PropTypes.oneOfType([
254
- PropTypes.string,
255
- PropTypes.arrayOf(PropTypes.string)
256
- ]),
257
- accessibilityCurrent: PropTypes.oneOf([
258
- true,
259
- false,
260
- 'page',
261
- 'step',
262
- 'location',
263
- 'date',
264
- 'time'
265
- ]),
266
- accessibilityDescribedBy: PropTypes.oneOfType([
267
- PropTypes.string,
268
- PropTypes.arrayOf(PropTypes.string)
269
- ]),
270
- accessibilityDetails: PropTypes.string,
271
- accessibilityDisabled: PropTypes.bool,
272
- accessibilityErrorMessage: PropTypes.string,
273
- accessibilityExpanded: PropTypes.bool,
274
- accessibilityFlowTo: PropTypes.oneOfType([
275
- PropTypes.string,
276
- PropTypes.arrayOf(PropTypes.string)
277
- ]),
278
- accessibilityHasPopup: PropTypes.string,
279
- accessibilityHidden: PropTypes.bool,
280
- accessibilityInvalid: PropTypes.bool,
281
- accessibilityKeyShortcuts: PropTypes.string,
282
- accessibilityLabelledBy: PropTypes.oneOfType([
283
- PropTypes.string,
284
- PropTypes.arrayOf(PropTypes.string)
285
- ]),
286
- accessibilityLevel: PropTypes.number,
287
- accessibilityModal: PropTypes.bool,
288
- accessibilityMultiline: PropTypes.bool,
289
- accessibilityMultiSelectable: PropTypes.bool,
290
- accessibilityOrientation: PropTypes.oneOf(['horizontal', 'vertical']),
291
- accessibilityOwns: PropTypes.oneOfType([
292
- PropTypes.string,
293
- PropTypes.arrayOf(PropTypes.string)
294
- ]),
295
- accessibilityPlaceholder: PropTypes.string,
296
- accessibilityPosInSet: PropTypes.number,
297
- accessibilityPressed: PropTypes.bool,
298
- accessibilityReadOnly: PropTypes.bool,
299
- accessibilityRequired: PropTypes.bool,
300
- accessibilityRoleDescription: PropTypes.string,
301
- accessibilityRowCount: PropTypes.number,
302
- accessibilityRowIndex: PropTypes.number,
303
- accessibilityRowSpan: PropTypes.number,
304
- accessibilitySelected: PropTypes.bool,
305
- accessibilitySetSize: PropTypes.number,
306
- accessibilitySort: PropTypes.oneOf(['ascending', 'descending', 'none', 'other']),
307
- accessibilityValueMax: PropTypes.number,
308
- accessibilityValueMin: PropTypes.number,
309
- accessibilityValueNow: PropTypes.number,
310
- accessibilityValueText: PropTypes.string
311
- }
312
- })
313
- }
314
-
315
- export const a11yProps = {
316
- /**
317
- * Proptypes for recognised React Native accessiblity (a11y) props.
318
- * Spread this in the propTypes of components that accept React Native a11y props.
319
- */
320
- types: a11yPropTypes,
321
- /**
322
- * Filters a props object, returning only recognised React Native accessiblity (a11y) props.
323
- *
324
- * Where components accept React Native a11y props, pass { ...rest } from its props to this,
325
- * then spread the returned object into the component's props (usually its outer container).
326
- */
327
- select: getPropSelector(a11yPropTypes, /^aria-/),
328
- /**
329
- * Use this to disable focus for elements which are visually hidden but still rendered.
330
- */
331
- nonFocusableProps: {
332
- focusable: false, // for android, and for keyboard nav in web
333
- ...Platform.select({
334
- web: {
335
- accessibilityHidden: true // web screenreaders
336
- },
337
- android: {
338
- importantForAccessibility: 'no-hide-descendants'
339
- },
340
- ios: {
341
- accessibilityElementsHidden: true
342
- }
343
- })
344
- },
345
- /**
346
- * Generates an object of platform-appropriate a11y props describing an item that has an
347
- * ordered position in a set. Examples of usage by accessibility tools includes screenreaders
348
- * saying "Item X of Y" when this item is select.
349
- *
350
- * @param {number} itemsCount - the number of items in the set
351
- * @param {number} index - the current item's index in the set
352
- * @returns {object} - platform-applicable a11y props describing this position (if available)
353
- */
354
- getPositionInSet: (itemsCount, index) =>
355
- Platform.select({
356
- web: {
357
- // accessibilityPosInSet etc exists in React Native Web main branch
358
- // but not in a release compatible with Expo etc; just use `aria-*`
359
- 'aria-setsize': itemsCount,
360
- 'aria-posinset': index + 1
361
- },
362
- // No equivalents exist on the native side
363
- default: {}
364
- })
365
- }
366
-
367
- // Props related to HTML <a> anchor link attributes.
368
- const targetValues = ['_self', '_blank', '_parent', '_top']
369
- export const hrefAttrsProp = {
370
- types: {
371
- // React Native Web >= 0.15.0 supports hrefAttrs prop with only these properties
372
- // and passes them to <a> if an element has a href prop or accessibilityRole "link"
373
- download: PropTypes.string,
374
- rel: PropTypes.string,
375
- target: PropTypes.oneOf(targetValues)
376
- },
377
- /**
378
- * Takes a props object, bundles any hrefAttrs props present into one object
379
- * and returns that keyed as `hrefAttrs` and non-hrefAttrs props keyed as `rest`
380
- */
381
- bundle: ({ download, rel, target, ...rest }) => ({
382
- hrefAttrs: {
383
- download,
384
- rel,
385
- target
386
- },
387
- rest
388
- })
389
- }
390
-
391
- const pressHandlerPropTypes = {
392
- onPress: PropTypes.func,
393
- onPressIn: PropTypes.func,
394
- onPressOut: PropTypes.func,
395
- ...Platform.select({
396
- web: {
397
- onMouseEnter: PropTypes.func,
398
- onMouseLeave: PropTypes.func,
399
- onFocus: PropTypes.func,
400
- onBlur: PropTypes.func
401
- },
402
- default: {
403
- onLongPress: PropTypes.func
404
- }
405
- })
406
- }
407
-
408
- const pressPropTypes = {
409
- ...pressHandlerPropTypes,
410
- disabled: PropTypes.bool,
411
- ...Platform.select({
412
- web: {},
413
- default: {
414
- hitSlop: PropTypes.number,
415
- pressRetentionOffset: PropTypes.oneOfType([PropTypes.number, rectProp.propType])
416
- }
417
- })
418
- }
419
-
420
- export const pressProps = {
421
- /**
422
- * Proptypes for clickable or pressable components, including web-only props
423
- */
424
- types: pressPropTypes,
425
- /**
426
- * Filters a props object, returning only the platform-relevant press props defined above
427
- */
428
- select: getPropSelector(pressPropTypes),
429
- selectHandlers: getPropSelector(pressHandlerPropTypes)
430
- }
431
-
432
- const linkPropTypes = {
433
- ...pressPropTypes,
434
- href: PropTypes.string,
435
- hrefAttrs: PropTypes.shape(hrefAttrsProp.types),
436
- ...a11yPropTypes
437
- }
438
-
439
- export const linkProps = {
440
- /**
441
- * Proptypes for components that, on Web, can output <a href="..."> link elements
442
- */
443
- types: linkPropTypes,
444
- /**
445
- * Filters a props object, returning only the platform-relevant link props defined above
446
- */
447
- select: getPropSelector(linkPropTypes),
448
- /**
449
- * Turn hrefs into press handlers on Native and throw if not given `onPress` or `href`.
450
- *
451
- * @param {({ onPress?: () => void, href?: string })}
452
- * @returns {(() => void)|undefined} Returns a press handler, or undefined on web if href
453
- * string is provided. Expects calling component to use href string on web (e.g. in `<a>`).
454
- */
455
- handleHref: ({ onPress, href }) => {
456
- if (!href && !onPress) {
457
- throw new Error('handleHref requires either href or onPress')
458
- }
459
- return Platform.select({
460
- web: onPress,
461
- default: (...args) => {
462
- if (onPress) onPress(...args)
463
- if (href) Linking.openURL(href)
464
- }
465
- })
466
- }
467
- }
468
-
469
- const viewPropTypes = {
470
- pointerEvents: PropTypes.oneOf(['all', 'none', 'box-only', 'box-none']),
471
- onLayout: PropTypes.func,
472
- nativeID: PropTypes.string,
473
- testID: PropTypes.string,
474
- dataSet: PropTypes.object
475
- }
476
-
477
- export const viewProps = {
478
- /**
479
- * Subset of `View` proptypes that could conceivably be needed on any component
480
- * that renders a single View.
481
- */
482
- types: viewPropTypes,
483
- /**
484
- * Filters a props object, returning only cross-platform View props that are potentially
485
- * relevant to any basic layout component that renders one View.
486
- */
487
- select: getPropSelector(viewPropTypes)
488
- }
489
-
490
- const getByViewport = (propType) => ({
491
- xs: propType,
492
- sm: propType,
493
- md: propType,
494
- lg: propType,
495
- xl: propType
496
- })
497
- /**
498
- * Utilities for props that allow different values to be applied at different viewports.
499
- *
500
- * These should apply viewport inheritance such that if a viewport is undefined, the value is
501
- * taken from the next smallest viewport for which a value is available. For example, a
502
- * responsive prop { xs: 2, lg: 3 } should apply 2 at sizes sm and md, and 3 at size xl.
503
- *
504
- * @property {Function} getByViewport - returns an object where each each viewport has a key
505
- * containing the provided argument.
506
- * @property {Function} getTypeByViewport - returns a PropTypes shape validator for an object where
507
- * each viewport has a key containing the provided proptype.
508
- * @property {Function} getTypeOptionallyByViewport - returns a PropTypes validator that accepts
509
- * either the provided proptype on its own, or the output of getTypeByViewport
510
- */
511
- export const responsiveProps = {
512
- getByViewport,
513
- getTypeByViewport: (propType) => PropTypes.shape(getByViewport(propType)),
514
- getTypeOptionallyByViewport: (propType) =>
515
- PropTypes.oneOfType([propType, PropTypes.shape(getByViewport(propType))])
516
- }
517
-
518
- /**
519
- * @typedef {0|1|2|3|4|5|6|7|8|9|10|11} SpacingIndex - value used to select a size on the spacing scale
520
- *
521
- * @typedef SpacingOptions
522
- * @property {VariantProp} [SpacingOptions.variant] - optional theme scale variants e.g. compact, wide
523
- * @property {number} [SpacingOptions.size] - optional override to force a particular pixel size
524
- * @property {number} [SpacingOptions.subtract] - optional number to subtract from final pixel size
525
- *
526
- * @typedef SpacingObject
527
- * @property {SpacingIndex} [SpacingObject.xs] - space scale index to use above xs viewport
528
- * @property {SpacingIndex} [SpacingObject.sm] - space scale index to use above sm viewport
529
- * @property {SpacingIndex} [SpacingObject.md] - space scale index to use above md viewport
530
- * @property {SpacingIndex} [SpacingObject.lg] - space scale index to use above lg viewport
531
- * @property {SpacingIndex} [SpacingObject.xl] - space scale index to use above xl viewport
532
- * @property {SpacingIndex} [SpacingObject.space] - space scale index to use at all viewports
533
- * @property {SpacingOptions} [SpacingObject.options] - optional options for this spacing
534
- *
535
- * @typedef {SpacingIndex|SpacingObject} SpacingValue
536
- */
537
- const spacingScale = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
538
- const spacingIndexPropType = PropTypes.oneOf(spacingScale)
539
- const spacingObjectPropType = PropTypes.shape({
540
- ...responsiveProps.getByViewport(spacingIndexPropType),
541
- space: spacingIndexPropType,
542
- options: PropTypes.shape({
543
- variant: variantProp.propType,
544
- size: PropTypes.number
545
- })
546
- })
547
- /**
548
- * Components and utilities that assign fixed space between components use a theme-defined spacing scale.
549
- *
550
- * They typically take one or more props of the {@link SpacingValue} type and turn it into a pixel size value
551
- * using the hook `useSpacingScale`, which resolves any options or responsive behaviour and returns the
552
- * appropriate value from the theme spacing scale.
553
- *
554
- * - see /ADRs/inter-component-spacing/README.md - ADR on this structure
555
- * - see /src/utils/spacing/useSpacingScale.js - hook that processes spacing values
556
- * - @see {@link SpacingIndex} - themes provide spacing scales of up to 12 sizes with optional theme rules.
557
- * - @see {@link SpacingValue} - either a simple number referencing an index position on the spacing
558
- * scale, or an object with an optional `options` key and one or more spacing indexes keyed either by
559
- * viewports or `space` to apply at all viewports.
560
- */
561
- export const spacingProps = {
562
- scale: spacingScale,
563
- types: {
564
- spacingIndex: spacingIndexPropType,
565
- spacingObject: spacingObjectPropType,
566
-
567
- // Most spacing components and utilities take this prop / arg type:
568
- spacingValue: PropTypes.oneOfType([spacingIndexPropType, spacingObjectPropType])
569
- }
570
- }
571
-
572
- /**
573
- * Returns a prop type validator which checks whether a prop is either a component or an array of
574
- * components of a given type, based on their `name` or `displayName` properties.
575
- * Use an array of strings for `passedName` to accept more than one component type.
576
- * For an array the validation fails on the first occurrence of an invalid element.
577
- *
578
- * @param {string|string[]} passedName
579
- * @return {function}
580
- */
581
- export const componentPropType = (passedName) => {
582
- const passedNames = typeof passedName === 'string' ? [passedName] : passedName
583
-
584
- const checkProp = (props, propName, componentName) => {
585
- if (props[propName] === undefined || props[propName] === null) {
586
- return undefined
587
- }
588
-
589
- if (Array.isArray(props[propName])) {
590
- // Iterates through every child and try to find the first element that does not match the passed name
591
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
592
- return props[propName]
593
- .map((_, index) => checkProp(props[propName], index, componentName))
594
- .find(Boolean)
595
- }
596
-
597
- const nameInProp = props[propName].type?.displayName || props[propName].type?.name
598
- if (!nameInProp || !passedNames.includes(nameInProp)) {
599
- const propDescription = nameInProp ? `Component ${nameInProp}` : typeof props[propName]
600
- return new Error(
601
- `${componentName}: ${propDescription} was passed to \`${propName}\` prop; should be ${passedNames.join(
602
- ' or '
603
- )}`
604
- )
605
- }
606
- return undefined
607
- }
608
-
609
- const checkRequired = (props, propName, componentName) => {
610
- if (props[propName] === undefined) {
611
- return new Error(
612
- `The prop \`${propName}\` is marked as required in \`${componentName}\`, but its value is ${props[propName]}.`
613
- )
614
- }
615
- return undefined
616
- }
617
-
618
- const createValidate = (isRequired) => {
619
- if (isRequired) {
620
- return (props, propName, componentName) => {
621
- const checkForError = checkProp(props, propName, componentName)
622
-
623
- if (checkForError) {
624
- return checkForError
625
- }
626
-
627
- return checkRequired(props, propName, componentName)
628
- }
629
- }
630
-
631
- return checkProp
632
- }
633
-
634
- const validate = createValidate()
635
- validate.isRequired = createValidate(true)
636
-
637
- return validate
638
- }
639
-
640
- export const copyPropTypes = PropTypes.oneOf(['en', 'fr'])