@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
@@ -0,0 +1,151 @@
1
+ import PropTypes from 'prop-types'
2
+ import { Platform } from 'react-native'
3
+ import getPropSelector from './getPropSelector'
4
+
5
+ // React Native exports a11y prop definitions as TypeScript Interfaces, but no longer exports PropTypes
6
+ // so we have to define them ourselves.
7
+ const nativeA11yPropTypes = {
8
+ accessible: PropTypes.bool,
9
+ focusable: PropTypes.bool,
10
+
11
+ accessibilityElementsHidden: PropTypes.bool,
12
+ accessibilityHint: PropTypes.string, // react-native-web ignores `accessibilityHint`
13
+ accessibilityIgnoresInvertColors: PropTypes.bool,
14
+ accessibilityLabel: PropTypes.string,
15
+ accessibilityRole: PropTypes.string,
16
+ accessibilityActions: PropTypes.arrayOf(
17
+ PropTypes.shape({
18
+ name: PropTypes.string.isRequired,
19
+ label: PropTypes.string
20
+ })
21
+ ),
22
+ accessibilityLiveRegion: PropTypes.oneOf(['none', 'polite', 'assertive']),
23
+ accessibilityState: PropTypes.shape({
24
+ disabled: PropTypes.bool,
25
+ selected: PropTypes.bool,
26
+ checked: PropTypes.oneOf([true, false, 'mixed']),
27
+ busy: PropTypes.bool,
28
+ expanded: PropTypes.bool
29
+ }),
30
+ accessibilityValue: PropTypes.shape({
31
+ min: PropTypes.number,
32
+ max: PropTypes.number,
33
+ now: PropTypes.number,
34
+ text: PropTypes.string
35
+ }),
36
+ accessibilityViewIsModal: PropTypes.bool,
37
+ importantForAccessibility: PropTypes.oneOf(['auto', 'yes', 'no', 'no-hide-descendants']),
38
+ onAccessibilityAction: PropTypes.func,
39
+ onAccessibilityEscape: PropTypes.func,
40
+ onAccessibilityTap: PropTypes.func
41
+ }
42
+
43
+ const a11yPropTypes = {
44
+ ...nativeA11yPropTypes,
45
+ // React Native Web adds many a11y props that alias aria-* attributes
46
+ // Types based on https://necolas.github.io/react-native-web/docs/accessibility/
47
+ accessibilityActiveDescendant: PropTypes.string,
48
+ accessibilityAtomic: PropTypes.bool,
49
+ accessibilityAutoComplete: PropTypes.string,
50
+ accessibilityBusy: PropTypes.bool,
51
+ accessibilityChecked: PropTypes.oneOf([true, false, 'mixed']),
52
+ accessibilityColumnCount: PropTypes.number,
53
+ accessibilityColumnIndex: PropTypes.number,
54
+ accessibilityColumnSpan: PropTypes.number,
55
+ accessibilityControls: PropTypes.oneOfType([
56
+ PropTypes.string,
57
+ PropTypes.arrayOf(PropTypes.string)
58
+ ]),
59
+ accessibilityCurrent: PropTypes.oneOf([true, false, 'page', 'step', 'location', 'date', 'time']),
60
+ accessibilityDescribedBy: PropTypes.oneOfType([
61
+ PropTypes.string,
62
+ PropTypes.arrayOf(PropTypes.string)
63
+ ]),
64
+ accessibilityDetails: PropTypes.string,
65
+ accessibilityDisabled: PropTypes.bool,
66
+ accessibilityErrorMessage: PropTypes.string,
67
+ accessibilityExpanded: PropTypes.bool,
68
+ accessibilityFlowTo: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
69
+ accessibilityHasPopup: PropTypes.string,
70
+ accessibilityHidden: PropTypes.bool,
71
+ accessibilityInvalid: PropTypes.bool,
72
+ accessibilityKeyShortcuts: PropTypes.string,
73
+ accessibilityLabelledBy: PropTypes.oneOfType([
74
+ PropTypes.string,
75
+ PropTypes.arrayOf(PropTypes.string)
76
+ ]),
77
+ accessibilityLevel: PropTypes.number,
78
+ accessibilityModal: PropTypes.bool,
79
+ accessibilityMultiline: PropTypes.bool,
80
+ accessibilityMultiSelectable: PropTypes.bool,
81
+ accessibilityOrientation: PropTypes.oneOf(['horizontal', 'vertical']),
82
+ accessibilityOwns: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
83
+ accessibilityPlaceholder: PropTypes.string,
84
+ accessibilityPosInSet: PropTypes.number,
85
+ accessibilityPressed: PropTypes.bool,
86
+ accessibilityReadOnly: PropTypes.bool,
87
+ accessibilityRequired: PropTypes.bool,
88
+ accessibilityRoleDescription: PropTypes.string,
89
+ accessibilityRowCount: PropTypes.number,
90
+ accessibilityRowIndex: PropTypes.number,
91
+ accessibilityRowSpan: PropTypes.number,
92
+ accessibilitySelected: PropTypes.bool,
93
+ accessibilitySetSize: PropTypes.number,
94
+ accessibilitySort: PropTypes.oneOf(['ascending', 'descending', 'none', 'other']),
95
+ accessibilityValueMax: PropTypes.number,
96
+ accessibilityValueMin: PropTypes.number,
97
+ accessibilityValueNow: PropTypes.number,
98
+ accessibilityValueText: PropTypes.string
99
+ }
100
+
101
+ export default {
102
+ /**
103
+ * Proptypes for recognised React Native accessiblity (a11y) props.
104
+ * Spread this in the propTypes of components that accept React Native a11y props.
105
+ */
106
+ types: a11yPropTypes,
107
+ /**
108
+ * Filters a props object, returning only recognised React Native accessiblity (a11y) props.
109
+ *
110
+ * Where components accept React Native a11y props, pass { ...rest } from its props to this,
111
+ * then spread the returned object into the component's props (usually its outer container).
112
+ */
113
+ select: getPropSelector(a11yPropTypes, /^aria-/),
114
+ /**
115
+ * Use this to disable focus for elements which are visually hidden but still rendered.
116
+ */
117
+ nonFocusableProps: {
118
+ focusable: false, // for android, and for keyboard nav in web
119
+ ...Platform.select({
120
+ web: {
121
+ accessibilityHidden: true // web screenreaders
122
+ },
123
+ android: {
124
+ importantForAccessibility: 'no-hide-descendants'
125
+ },
126
+ ios: {
127
+ accessibilityElementsHidden: true
128
+ }
129
+ })
130
+ },
131
+ /**
132
+ * Generates an object of platform-appropriate a11y props describing an item that has an
133
+ * ordered position in a set. Examples of usage by accessibility tools includes screenreaders
134
+ * saying "Item X of Y" when this item is select.
135
+ *
136
+ * @param {number} itemsCount - the number of items in the set
137
+ * @param {number} index - the current item's index in the set
138
+ * @returns {object} - platform-applicable a11y props describing this position (if available)
139
+ */
140
+ getPositionInSet: (itemsCount, index) =>
141
+ Platform.select({
142
+ web: {
143
+ // accessibilityPosInSet etc exists in React Native Web main branch
144
+ // but not in a release compatible with Expo etc; just use `aria-*`
145
+ 'aria-setsize': itemsCount,
146
+ 'aria-posinset': index + 1
147
+ },
148
+ // No equivalents exist on the native side
149
+ default: {}
150
+ })
151
+ }
@@ -0,0 +1,31 @@
1
+ import PropTypes from 'prop-types'
2
+
3
+ const clickHandlerMapping = {
4
+ onClick: 'onPress',
5
+ mouseDown: 'onPressIn',
6
+ mouseUp: 'onPressOut'
7
+ }
8
+
9
+ export default {
10
+ /**
11
+ * Web-oriented HTML click handlers that may be mapped to React Native press handlers
12
+ */
13
+ types: Object.fromEntries(
14
+ Object.keys(clickHandlerMapping).map((mouseName) => [mouseName, PropTypes.func])
15
+ ),
16
+ /**
17
+ * Takes a set of props and converts HTML mouse click oriented event handlers to closest
18
+ * equivalent React Native press event handler.
19
+ *
20
+ * Use this when a component that expects press-oriented props may need to support third-party
21
+ * web-oriented tooling that injects web-oriented event handlers directly. For example, for
22
+ * to support use with NextJS's 'next/link' component, which injects `onClick` prop into its child.
23
+ */
24
+ toPressProps: (props) =>
25
+ Object.fromEntries(
26
+ Object.entries(props).map(([originalName, value]) => {
27
+ const translatedName = clickHandlerMapping[originalName]
28
+ return translatedName ? [translatedName, value] : [originalName, value]
29
+ })
30
+ )
31
+ }
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Returns a prop type validator which checks whether a prop is either a component or an array of
3
+ * components of a given type, based on their `name` or `displayName` properties.
4
+ * Use an array of strings for `passedName` to accept more than one component type.
5
+ * For an array the validation fails on the first occurrence of an invalid element.
6
+ *
7
+ * @param {string|string[]} passedName
8
+ * @return {function}
9
+ */
10
+ export default function componentPropType(passedName) {
11
+ const passedNames = typeof passedName === 'string' ? [passedName] : passedName
12
+
13
+ const checkProp = (props, propName, componentName) => {
14
+ if (props[propName] === undefined || props[propName] === null) {
15
+ return undefined
16
+ }
17
+
18
+ if (Array.isArray(props[propName])) {
19
+ // Iterates through every child and try to find the first element that does not match the passed name
20
+ // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean
21
+ return props[propName]
22
+ .map((_, index) => checkProp(props[propName], index, componentName))
23
+ .find(Boolean)
24
+ }
25
+
26
+ const nameInProp = props[propName].type?.displayName || props[propName].type?.name
27
+ if (!nameInProp || !passedNames.includes(nameInProp)) {
28
+ const propDescription = nameInProp ? `Component ${nameInProp}` : typeof props[propName]
29
+ return new Error(
30
+ `${componentName}: ${propDescription} was passed to \`${propName}\` prop; should be ${passedNames.join(
31
+ ' or '
32
+ )}`
33
+ )
34
+ }
35
+ return undefined
36
+ }
37
+
38
+ const checkRequired = (props, propName, componentName) => {
39
+ if (props[propName] === undefined) {
40
+ return new Error(
41
+ `The prop \`${propName}\` is marked as required in \`${componentName}\`, but its value is ${props[propName]}.`
42
+ )
43
+ }
44
+ return undefined
45
+ }
46
+
47
+ const createValidate = (isRequired) => {
48
+ if (isRequired) {
49
+ return (props, propName, componentName) => {
50
+ const checkForError = checkProp(props, propName, componentName)
51
+
52
+ if (checkForError) {
53
+ return checkForError
54
+ }
55
+
56
+ return checkRequired(props, propName, componentName)
57
+ }
58
+ }
59
+
60
+ return checkProp
61
+ }
62
+
63
+ const validate = createValidate()
64
+ validate.isRequired = createValidate(true)
65
+
66
+ return validate
67
+ }
@@ -0,0 +1,3 @@
1
+ import PropTypes from 'prop-types'
2
+
3
+ export default PropTypes.oneOf(['en', 'fr'])
@@ -0,0 +1,14 @@
1
+ export default function getPropSelector(propTypes, regexp) {
2
+ const keys = Object.keys(propTypes)
3
+ return (props) =>
4
+ Object.entries(props).reduce(
5
+ (items, [key, value]) =>
6
+ keys.includes(key) || (regexp && regexp.test(key))
7
+ ? {
8
+ ...items,
9
+ [key]: value
10
+ }
11
+ : items,
12
+ {}
13
+ )
14
+ }
@@ -0,0 +1,25 @@
1
+ import PropTypes from 'prop-types'
2
+
3
+ // Props related to HTML <a> anchor link attributes.
4
+ const targetValues = ['_self', '_blank', '_parent', '_top']
5
+ export default {
6
+ types: {
7
+ // React Native Web >= 0.15.0 supports hrefAttrs prop with only these properties
8
+ // and passes them to <a> if an element has a href prop or accessibilityRole "link"
9
+ download: PropTypes.string,
10
+ rel: PropTypes.string,
11
+ target: PropTypes.oneOf(targetValues)
12
+ },
13
+ /**
14
+ * Takes a props object, bundles any hrefAttrs props present into one object
15
+ * and returns that keyed as `hrefAttrs` and non-hrefAttrs props keyed as `rest`
16
+ */
17
+ bundle: ({ download, rel, target, ...rest }) => ({
18
+ hrefAttrs: {
19
+ download,
20
+ rel,
21
+ target
22
+ },
23
+ rest
24
+ })
25
+ }
@@ -0,0 +1,15 @@
1
+ export * from './tokens'
2
+ export { default as a11yProps } from './a11yProps'
3
+ export { default as clickProps } from './clickProps'
4
+ export { default as copyPropTypes } from './copyPropTypes'
5
+ export { default as componentPropType } from './componentPropType'
6
+ export { default as hrefAttrsProp } from './hrefAttrsProp'
7
+ export { default as linkProps } from './linkProps'
8
+ export { default as pressProps } from './pressProps'
9
+ export { default as paddingProp } from './paddingProp'
10
+ export { default as rectProp } from './rectProp'
11
+ export { default as responsiveProps } from './responsiveProps'
12
+ export { default as spacingProps } from './spacingProps'
13
+ export { default as selectSystemProps } from './selectSystemProps'
14
+ export { default as variantProp } from './variantProp'
15
+ export { default as viewProps } from './viewProps'
@@ -0,0 +1,43 @@
1
+ import { Linking, Platform } from 'react-native'
2
+ import PropTypes from 'prop-types'
3
+ import { pressPropTypes } from './pressProps'
4
+ import a11yProps from './a11yProps'
5
+ import hrefAttrsProp from './hrefAttrsProp'
6
+ import getPropSelector from './getPropSelector'
7
+
8
+ const linkPropTypes = {
9
+ ...pressPropTypes,
10
+ href: PropTypes.string,
11
+ hrefAttrs: PropTypes.shape(hrefAttrsProp.types),
12
+ ...a11yProps.types
13
+ }
14
+
15
+ export default {
16
+ /**
17
+ * Proptypes for components that, on Web, can output <a href="..."> link elements
18
+ */
19
+ types: linkPropTypes,
20
+ /**
21
+ * Filters a props object, returning only the platform-relevant link props defined above
22
+ */
23
+ select: getPropSelector(linkPropTypes),
24
+ /**
25
+ * Turn hrefs into press handlers on Native and throw if not given `onPress` or `href`.
26
+ *
27
+ * @param {({ onPress?: () => void, href?: string })}
28
+ * @returns {(() => void)|undefined} Returns a press handler, or undefined on web if href
29
+ * string is provided. Expects calling component to use href string on web (e.g. in `<a>`).
30
+ */
31
+ handleHref: ({ onPress, href }) => {
32
+ if (!href && !onPress) {
33
+ throw new Error('handleHref requires either href or onPress')
34
+ }
35
+ return Platform.select({
36
+ web: onPress,
37
+ default: (...args) => {
38
+ if (onPress) onPress(...args)
39
+ if (href) Linking.openURL(href)
40
+ }
41
+ })
42
+ }
43
+ }
@@ -0,0 +1,10 @@
1
+ import PropTypes from 'prop-types'
2
+
3
+ export default {
4
+ propType: PropTypes.shape({
5
+ paddingBottom: PropTypes.number,
6
+ paddingLeft: PropTypes.number,
7
+ paddingRight: PropTypes.number,
8
+ paddingTop: PropTypes.number
9
+ })
10
+ }
@@ -0,0 +1,45 @@
1
+ import { Platform } from 'react-native'
2
+ import PropTypes from 'prop-types'
3
+ import rectProp from './rectProp'
4
+ import getPropSelector from './getPropSelector'
5
+
6
+ const pressHandlerPropTypes = {
7
+ onPress: PropTypes.func,
8
+ onPressIn: PropTypes.func,
9
+ onPressOut: PropTypes.func,
10
+ ...Platform.select({
11
+ web: {
12
+ onMouseEnter: PropTypes.func,
13
+ onMouseLeave: PropTypes.func,
14
+ onFocus: PropTypes.func,
15
+ onBlur: PropTypes.func
16
+ },
17
+ default: {
18
+ onLongPress: PropTypes.func
19
+ }
20
+ })
21
+ }
22
+
23
+ export const pressPropTypes = {
24
+ ...pressHandlerPropTypes,
25
+ disabled: PropTypes.bool,
26
+ ...Platform.select({
27
+ web: {},
28
+ default: {
29
+ hitSlop: PropTypes.number,
30
+ pressRetentionOffset: PropTypes.oneOfType([PropTypes.number, rectProp.propType])
31
+ }
32
+ })
33
+ }
34
+
35
+ export default {
36
+ /**
37
+ * Proptypes for clickable or pressable components, including web-only props
38
+ */
39
+ types: pressPropTypes,
40
+ /**
41
+ * Filters a props object, returning only the platform-relevant press props defined above
42
+ */
43
+ select: getPropSelector(pressPropTypes),
44
+ selectHandlers: getPropSelector(pressHandlerPropTypes)
45
+ }
@@ -0,0 +1,10 @@
1
+ import PropTypes from 'prop-types'
2
+
3
+ export default {
4
+ propType: PropTypes.shape({
5
+ bottom: PropTypes.number,
6
+ left: PropTypes.number,
7
+ right: PropTypes.number,
8
+ top: PropTypes.number
9
+ })
10
+ }
@@ -0,0 +1,30 @@
1
+ import PropTypes from 'prop-types'
2
+
3
+ const getByViewport = (propType) => ({
4
+ xs: propType,
5
+ sm: propType,
6
+ md: propType,
7
+ lg: propType,
8
+ xl: propType
9
+ })
10
+
11
+ /**
12
+ * Utilities for props that allow different values to be applied at different viewports.
13
+ *
14
+ * These should apply viewport inheritance such that if a viewport is undefined, the value is
15
+ * taken from the next smallest viewport for which a value is available. For example, a
16
+ * responsive prop { xs: 2, lg: 3 } should apply 2 at sizes sm and md, and 3 at size xl.
17
+ *
18
+ * @property {Function} getByViewport - returns an object where each each viewport has a key
19
+ * containing the provided argument.
20
+ * @property {Function} getTypeByViewport - returns a PropTypes shape validator for an object where
21
+ * each viewport has a key containing the provided proptype.
22
+ * @property {Function} getTypeOptionallyByViewport - returns a PropTypes validator that accepts
23
+ * either the provided proptype on its own, or the output of getTypeByViewport
24
+ */
25
+ export default {
26
+ getByViewport,
27
+ getTypeByViewport: (propType) => PropTypes.shape(getByViewport(propType)),
28
+ getTypeOptionallyByViewport: (propType) =>
29
+ PropTypes.oneOfType([propType, PropTypes.shape(getByViewport(propType))])
30
+ }
@@ -0,0 +1,25 @@
1
+ // Returns a selector for requested prop kinds as well as the propTypes for
2
+ // those props merged into one object
3
+ export default function selectSystemProps(systemPropHelpers) {
4
+ const selectProps = (props) =>
5
+ systemPropHelpers.reduce((acc, propHelper) => {
6
+ if (typeof propHelper?.select !== 'function') {
7
+ throw new Error(
8
+ `An invalid system prop helper has been passed to 'selectSystemProps': prop selector ('.select') is missing in ${propHelper}`
9
+ )
10
+ }
11
+
12
+ return { ...acc, ...propHelper.select(props) }
13
+ }, {})
14
+ const selectedPropTypes = systemPropHelpers.reduce((acc, propHelper) => {
15
+ if (!propHelper?.types) {
16
+ throw new Error(
17
+ `An invalid system prop helper has been passed to 'selectSystemProps': types selector ('.types') is missing in ${propHelper}`
18
+ )
19
+ }
20
+
21
+ return { ...acc, ...propHelper.types }
22
+ }, {})
23
+
24
+ return [selectProps, selectedPropTypes]
25
+ }
@@ -0,0 +1,58 @@
1
+ import PropTypes from 'prop-types'
2
+ import responsiveProps from './responsiveProps'
3
+ import variantProp from './variantProp'
4
+
5
+ /**
6
+ * @typedef {0|1|2|3|4|5|6|7|8|9|10|11} SpacingIndex - value used to select a size on the spacing scale
7
+ *
8
+ * @typedef SpacingOptions
9
+ * @property {VariantProp} [SpacingOptions.variant] - optional theme scale variants e.g. compact, wide
10
+ * @property {number} [SpacingOptions.size] - optional override to force a particular pixel size
11
+ * @property {number} [SpacingOptions.subtract] - optional number to subtract from final pixel size
12
+ *
13
+ * @typedef SpacingObject
14
+ * @property {SpacingIndex} [SpacingObject.xs] - space scale index to use above xs viewport
15
+ * @property {SpacingIndex} [SpacingObject.sm] - space scale index to use above sm viewport
16
+ * @property {SpacingIndex} [SpacingObject.md] - space scale index to use above md viewport
17
+ * @property {SpacingIndex} [SpacingObject.lg] - space scale index to use above lg viewport
18
+ * @property {SpacingIndex} [SpacingObject.xl] - space scale index to use above xl viewport
19
+ * @property {SpacingIndex} [SpacingObject.space] - space scale index to use at all viewports
20
+ * @property {SpacingOptions} [SpacingObject.options] - optional options for this spacing
21
+ *
22
+ * @typedef {SpacingIndex|SpacingObject} SpacingValue
23
+ */
24
+ const spacingScale = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
25
+ const spacingIndexPropType = PropTypes.oneOf(spacingScale)
26
+ const spacingObjectPropType = PropTypes.shape({
27
+ ...responsiveProps.getByViewport(spacingIndexPropType),
28
+ space: spacingIndexPropType,
29
+ options: PropTypes.shape({
30
+ variant: variantProp.propType,
31
+ size: PropTypes.number
32
+ })
33
+ })
34
+
35
+ /**
36
+ * Components and utilities that assign fixed space between components use a theme-defined spacing scale.
37
+ *
38
+ * They typically take one or more props of the {@link SpacingValue} type and turn it into a pixel size value
39
+ * using the hook `useSpacingScale`, which resolves any options or responsive behaviour and returns the
40
+ * appropriate value from the theme spacing scale.
41
+ *
42
+ * - see /ADRs/inter-component-spacing/README.md - ADR on this structure
43
+ * - see /src/utils/spacing/useSpacingScale.js - hook that processes spacing values
44
+ * - @see {@link SpacingIndex} - themes provide spacing scales of up to 12 sizes with optional theme rules.
45
+ * - @see {@link SpacingValue} - either a simple number referencing an index position on the spacing
46
+ * scale, or an object with an optional `options` key and one or more spacing indexes keyed either by
47
+ * viewports or `space` to apply at all viewports.
48
+ */
49
+ export default {
50
+ scale: spacingScale,
51
+ types: {
52
+ spacingIndex: spacingIndexPropType,
53
+ spacingObject: spacingObjectPropType,
54
+
55
+ // Most spacing components and utilities take this prop / arg type:
56
+ spacingValue: PropTypes.oneOfType([spacingIndexPropType, spacingObjectPropType])
57
+ }
58
+ }