@telus-uds/components-base 0.0.2-prerelease.1 → 0.0.2-prerelease.5

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 (161) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/__fixtures__/testTheme.js +264 -84
  3. package/__tests__/Box/Box.test.jsx +81 -58
  4. package/__tests__/Card/Card.test.jsx +63 -0
  5. package/__tests__/Divider/Divider.test.jsx +26 -5
  6. package/__tests__/Feedback/Feedback.test.jsx +42 -0
  7. package/__tests__/FlexGrid/Col.test.jsx +5 -0
  8. package/__tests__/Pagination/Pagination.test.jsx +160 -0
  9. package/__tests__/Spacer/Spacer.test.jsx +63 -0
  10. package/__tests__/StackView/StackView.test.jsx +242 -0
  11. package/__tests__/StackView/StackWrap.test.jsx +47 -0
  12. package/__tests__/StackView/getStackedContent.test.jsx +295 -0
  13. package/__tests__/TextInput/TextInput.test.jsx +146 -0
  14. package/__tests__/ThemeProvider/useThemeTokens.test.jsx +5 -3
  15. package/__tests__/utils/spacing.test.jsx +273 -0
  16. package/__tests__/utils/useUniqueId.test.js +31 -0
  17. package/babel.config.json +8 -0
  18. package/jest.config.js +7 -6
  19. package/lib/A11yInfoProvider/index.js +2 -2
  20. package/lib/A11yText/index.js +1 -3
  21. package/lib/ActivityIndicator/Spinner.web.js +3 -5
  22. package/lib/Box/Box.js +117 -82
  23. package/lib/Button/Button.js +1 -3
  24. package/lib/Button/ButtonBase.js +9 -21
  25. package/lib/Button/ButtonGroup.js +14 -25
  26. package/lib/Button/ButtonLink.js +1 -3
  27. package/lib/Card/Card.js +103 -0
  28. package/lib/Card/index.js +2 -0
  29. package/lib/Divider/Divider.js +40 -4
  30. package/lib/ExpandCollapse/Accordion.js +1 -3
  31. package/lib/ExpandCollapse/Control.js +3 -5
  32. package/lib/ExpandCollapse/Panel.js +2 -4
  33. package/lib/Feedback/Feedback.js +110 -0
  34. package/lib/Feedback/index.js +2 -0
  35. package/lib/FlexGrid/Col/Col.js +3 -5
  36. package/lib/FlexGrid/FlexGrid.js +1 -3
  37. package/lib/FlexGrid/Row/Row.js +1 -3
  38. package/lib/FlexGrid/providers/GutterContext.js +1 -1
  39. package/lib/Icon/Icon.js +1 -1
  40. package/lib/InputLabel/InputLabel.js +86 -0
  41. package/lib/InputLabel/LabelContent.native.js +8 -0
  42. package/lib/InputLabel/LabelContent.web.js +17 -0
  43. package/lib/InputLabel/index.js +2 -0
  44. package/lib/Link/ChevronLink.js +1 -3
  45. package/lib/Link/Link.js +1 -3
  46. package/lib/Link/LinkBase.js +11 -7
  47. package/lib/Link/TextButton.js +1 -3
  48. package/lib/Pagination/PageButton.js +85 -0
  49. package/lib/Pagination/Pagination.js +118 -0
  50. package/lib/Pagination/SideButton.js +108 -0
  51. package/lib/Pagination/dictionary.js +18 -0
  52. package/lib/Pagination/index.js +2 -0
  53. package/lib/Pagination/useCopy.js +10 -0
  54. package/lib/Pagination/usePagination.js +70 -0
  55. package/lib/SideNav/Item.js +4 -6
  56. package/lib/SideNav/ItemsGroup.js +11 -11
  57. package/lib/SideNav/SideNav.js +2 -4
  58. package/lib/Spacer/Spacer.js +98 -0
  59. package/lib/Spacer/index.js +2 -0
  60. package/lib/StackView/StackView.js +105 -0
  61. package/lib/StackView/StackWrap.js +32 -0
  62. package/lib/StackView/StackWrap.native.js +3 -0
  63. package/lib/StackView/StackWrapBox.js +85 -0
  64. package/lib/StackView/StackWrapGap.js +45 -0
  65. package/lib/StackView/common.js +30 -0
  66. package/lib/StackView/getStackedContent.js +111 -0
  67. package/lib/StackView/index.js +5 -0
  68. package/lib/TextInput/TextInput.js +337 -0
  69. package/lib/TextInput/index.js +2 -0
  70. package/lib/ThemeProvider/ThemeProvider.js +2 -2
  71. package/lib/ThemeProvider/useThemeTokens.js +34 -6
  72. package/lib/ThemeProvider/utils/theme-tokens.js +37 -9
  73. package/lib/ToggleSwitch/ToggleSwitch.js +17 -47
  74. package/lib/Typography/Typography.js +1 -7
  75. package/lib/ViewportProvider/index.js +1 -1
  76. package/lib/index.js +8 -1
  77. package/lib/utils/index.js +2 -1
  78. package/lib/utils/input.js +3 -1
  79. package/lib/utils/propTypes.js +103 -8
  80. package/lib/utils/spacing/index.js +2 -0
  81. package/lib/utils/spacing/useSpacingScale.js +102 -0
  82. package/lib/utils/spacing/utils.js +32 -0
  83. package/lib/utils/useUniqueId.js +12 -0
  84. package/package.json +6 -9
  85. package/release-context.json +4 -4
  86. package/src/Box/Box.jsx +117 -80
  87. package/src/Button/ButtonBase.jsx +8 -21
  88. package/src/Button/ButtonGroup.jsx +13 -17
  89. package/src/Card/Card.jsx +101 -0
  90. package/src/Card/index.js +3 -0
  91. package/src/Divider/Divider.jsx +38 -3
  92. package/src/ExpandCollapse/Control.jsx +2 -3
  93. package/src/Feedback/Feedback.jsx +99 -0
  94. package/src/Feedback/index.js +3 -0
  95. package/src/FlexGrid/Col/Col.jsx +4 -2
  96. package/src/Icon/Icon.jsx +2 -1
  97. package/src/InputLabel/InputLabel.jsx +99 -0
  98. package/src/InputLabel/LabelContent.native.jsx +6 -0
  99. package/src/InputLabel/LabelContent.web.jsx +13 -0
  100. package/src/InputLabel/index.js +3 -0
  101. package/src/Link/LinkBase.jsx +9 -3
  102. package/src/Pagination/PageButton.jsx +80 -0
  103. package/src/Pagination/Pagination.jsx +135 -0
  104. package/src/Pagination/SideButton.jsx +93 -0
  105. package/src/Pagination/dictionary.js +18 -0
  106. package/src/Pagination/index.js +3 -0
  107. package/src/Pagination/useCopy.js +7 -0
  108. package/src/Pagination/usePagination.js +69 -0
  109. package/src/SideNav/Item.jsx +3 -3
  110. package/src/SideNav/ItemsGroup.jsx +11 -13
  111. package/src/Spacer/Spacer.jsx +91 -0
  112. package/src/Spacer/index.js +3 -0
  113. package/src/StackView/StackView.jsx +103 -0
  114. package/src/StackView/StackWrap.jsx +33 -0
  115. package/src/StackView/StackWrap.native.jsx +4 -0
  116. package/src/StackView/StackWrapBox.jsx +82 -0
  117. package/src/StackView/StackWrapGap.jsx +39 -0
  118. package/src/StackView/common.jsx +28 -0
  119. package/src/StackView/getStackedContent.jsx +106 -0
  120. package/src/StackView/index.js +6 -0
  121. package/src/TextInput/TextInput.jsx +325 -0
  122. package/src/TextInput/index.js +3 -0
  123. package/src/ThemeProvider/useThemeTokens.js +34 -7
  124. package/src/ThemeProvider/utils/theme-tokens.js +37 -8
  125. package/src/ToggleSwitch/ToggleSwitch.jsx +23 -43
  126. package/src/Typography/Typography.jsx +0 -4
  127. package/src/index.js +8 -1
  128. package/src/utils/index.js +1 -0
  129. package/src/utils/input.js +2 -1
  130. package/src/utils/propTypes.js +105 -16
  131. package/src/utils/spacing/index.js +3 -0
  132. package/src/utils/spacing/useSpacingScale.js +93 -0
  133. package/src/utils/spacing/utils.js +28 -0
  134. package/src/utils/useUniqueId.js +14 -0
  135. package/stories/A11yText/A11yText.stories.jsx +11 -5
  136. package/stories/ActivityIndicator/ActivityIndicator.stories.jsx +11 -2
  137. package/stories/Box/Box.stories.jsx +46 -17
  138. package/stories/Button/Button.stories.jsx +17 -21
  139. package/stories/Button/ButtonGroup.stories.jsx +2 -1
  140. package/stories/Button/ButtonLink.stories.jsx +6 -4
  141. package/stories/Card/Card.stories.jsx +62 -0
  142. package/stories/Divider/Divider.stories.jsx +26 -2
  143. package/stories/ExpandCollapse/ExpandCollapse.stories.jsx +74 -79
  144. package/stories/Feedback/Feedback.stories.jsx +97 -0
  145. package/stories/FlexGrid/01 FlexGrid.stories.jsx +20 -7
  146. package/stories/Icon/Icon.stories.jsx +11 -3
  147. package/stories/InputLabel/InputLabel.stories.jsx +37 -0
  148. package/stories/Link/ChevronLink.stories.jsx +20 -4
  149. package/stories/Link/Link.stories.jsx +24 -3
  150. package/stories/Link/TextButton.stories.jsx +24 -3
  151. package/stories/Pagination/Pagination.stories.jsx +64 -0
  152. package/stories/SideNav/SideNav.stories.jsx +17 -2
  153. package/stories/Spacer/Spacer.stories.jsx +33 -0
  154. package/stories/StackView/StackView.stories.jsx +65 -0
  155. package/stories/StackView/StackWrap.stories.jsx +52 -0
  156. package/stories/TextInput/TextInput.stories.jsx +103 -0
  157. package/stories/ToggleSwitch/ToggleSwitch.stories.jsx +16 -3
  158. package/stories/Typography/Typography.stories.jsx +12 -3
  159. package/stories/platform-supports.web.jsx +1 -1
  160. package/stories/supports.jsx +113 -13
  161. package/babel.config.js +0 -3
@@ -0,0 +1,337 @@
1
+ import React, { useState } from 'react';
2
+ import { View, TextInput as NativeTextInput, Platform, StyleSheet } from 'react-native';
3
+ import PropTypes from 'prop-types';
4
+ import { useThemeTokens, applyTextStyles } from '../ThemeProvider';
5
+ import { getTokensPropType, useInputValue, variantProp } from '../utils';
6
+ import InputLabel from '../InputLabel';
7
+ import Feedback from '../Feedback';
8
+ import Box from '../Box';
9
+ import useUniqueId from '../utils/useUniqueId';
10
+
11
+ const selectInputStyles = ({
12
+ backgroundColor,
13
+ color,
14
+ borderWidth,
15
+ borderColor,
16
+ borderRadius,
17
+ paddingTop,
18
+ paddingBottom,
19
+ paddingLeft,
20
+ paddingRight = 0,
21
+ fontName,
22
+ fontSize,
23
+ fontWeight,
24
+ lineHeight,
25
+ icon,
26
+ iconSize = 0
27
+ }, inactive) => {
28
+ // Subtract border width from padding so overall input width/height doesn't
29
+ // jump around if the border width changes (avoiding NaN and negative padding)
30
+ const offsetBorder = value => typeof value === 'number' && typeof borderWidth === 'number' ? Math.max(0, value - borderWidth) : value;
31
+
32
+ const textStyles = applyTextStyles({
33
+ fontName,
34
+ fontSize,
35
+ lineHeight,
36
+ fontWeight
37
+ });
38
+ const webStyles = Platform.select({
39
+ web: {
40
+ outline: 'none',
41
+ cursor: inactive ? 'not-allowed' : undefined
42
+ }
43
+ });
44
+ const paddingWithIcon = iconSize + paddingRight;
45
+ return {
46
+ backgroundColor,
47
+ color,
48
+ borderWidth,
49
+ borderColor,
50
+ borderRadius,
51
+ paddingLeft: offsetBorder(paddingLeft),
52
+ paddingRight: icon ? offsetBorder(paddingWithIcon) : offsetBorder(paddingRight),
53
+ paddingTop: offsetBorder(paddingTop),
54
+ paddingBottom: offsetBorder(paddingBottom),
55
+ ...textStyles,
56
+ ...webStyles
57
+ };
58
+ };
59
+
60
+ const selectOuterBorderStyles = ({
61
+ outerBackgroundColor,
62
+ outerBorderWidth = 0,
63
+ outerBorderColor,
64
+ outerBorderRadius = 0
65
+ }) => {
66
+ // Use negative margins so that the outer border doesn't expand the input's bounding box
67
+ const margin = -1 * outerBorderWidth; // Account for the border width since we style it as an outline
68
+
69
+ const borderRadius = outerBorderRadius + outerBorderWidth;
70
+ return {
71
+ background: outerBackgroundColor,
72
+ borderWidth: outerBorderWidth,
73
+ borderColor: outerBorderColor,
74
+ borderRadius,
75
+ marginTop: margin,
76
+ marginBottom: margin,
77
+ marginLeft: margin,
78
+ marginRight: margin
79
+ };
80
+ };
81
+
82
+ const selectIconTokens = ({
83
+ iconSize,
84
+ iconColor
85
+ }) => ({
86
+ size: iconSize,
87
+ color: iconColor
88
+ });
89
+
90
+ const joinDefined = array => array.filter(item => item !== undefined).join(' ');
91
+ /**
92
+ * A basic text input component. Use in forms or individually to receive user's input.
93
+ * Due to React Native's implementation of `TextInput` it's not possible to access the current value by passing a ref.
94
+ *
95
+ * ## Controlled component
96
+ * If it is required that the state of the `TextInput` be controlled by the application or other external methods,
97
+ * `value` and `onChange` props must be passed to the `TextInput`.
98
+ * If the `TextInput` value should not be changed by user input, a `readOnly` prop must be provided.
99
+ *
100
+ * ## Uncontrolled component
101
+ * If it is not necessary to control the `TextInput` state, you can create one without a value prop.
102
+ * In this case its value can be accessed within the `onChange` callback.
103
+ *
104
+ * NOTE: In order to support more advanced usages all additional props will be passed down to the React Native's
105
+ * `TextInput` component. See <a href="https://reactnative.dev/docs/textinput" target="_blank">React Native documentation</a> for
106
+ * supported props and <a href="https://reactnative.dev/docs/textinput" target="_blank">React Native Web documentation</a> for
107
+ * their implementation on the web.
108
+ */
109
+
110
+
111
+ function TextInput({
112
+ value,
113
+ initialValue,
114
+ label,
115
+ hint,
116
+ hintPosition = 'inline',
117
+ feedback,
118
+ tooltip,
119
+ validation,
120
+ inactive,
121
+ readOnly,
122
+ onChange,
123
+ onChangeText,
124
+ onFocus,
125
+ onBlur,
126
+ onMouseOver,
127
+ onMouseOut,
128
+ tokens,
129
+ variant = {},
130
+ ...remainingProps
131
+ }) {
132
+ const inputId = useUniqueId('text-input');
133
+ const hintId = useUniqueId('text-input-hint');
134
+ const feedbackId = useUniqueId('text-input-feedback');
135
+ const [isFocused, setIsFocused] = useState(false);
136
+
137
+ const handleFocus = event => {
138
+ setIsFocused(true);
139
+ if (typeof onFocus === 'function') onFocus(event);
140
+ };
141
+
142
+ const handleBlur = event => {
143
+ setIsFocused(false);
144
+ if (typeof onBlur === 'function') onBlur(event);
145
+ };
146
+
147
+ const [isHovered, setIsHovered] = useState(false);
148
+
149
+ const handleMouseOver = event => {
150
+ setIsHovered(true);
151
+ if (typeof onMouseOver === 'function') onMouseOver(event);
152
+ };
153
+
154
+ const handleMouseOut = event => {
155
+ setIsHovered(false);
156
+ if (typeof onMouseOut === 'function') onMouseOut(event);
157
+ };
158
+
159
+ const {
160
+ currentValue,
161
+ setValue,
162
+ isControlled
163
+ } = useInputValue({
164
+ value,
165
+ initialValue,
166
+ onChange,
167
+ readOnly
168
+ });
169
+
170
+ const handleChangeText = text => {
171
+ setValue(text);
172
+ if (typeof onChangeText === 'function') onChangeText(text);
173
+ };
174
+
175
+ const states = {
176
+ focus: isFocused,
177
+ hover: isHovered,
178
+ inactive
179
+ };
180
+ const themeTokens = useThemeTokens('TextInput', tokens, { ...variant,
181
+ validation
182
+ }, states);
183
+ const {
184
+ icon: IconComponent
185
+ } = themeTokens;
186
+ const hasValidationError = validation === 'error';
187
+ const inputProps = { ...remainingProps,
188
+ editable: !inactive,
189
+ onFocus: handleFocus,
190
+ onBlur: handleBlur,
191
+ onMouseOver: handleMouseOver,
192
+ onMouseOut: handleMouseOut,
193
+ onChangeText: handleChangeText,
194
+ accessibilityLabel: label,
195
+ accessibilityHint: joinDefined([!hasValidationError && feedback, hint]),
196
+ // native only -> replaced with describedBy on web
197
+ accessibilityDescribedBy: joinDefined([!hasValidationError && feedback && feedbackId, // feedback receives a11yRole=alert, so there's no need to include it here
198
+ hint && hintId]),
199
+ // introduced in RNW 0.15.0
200
+ accessibilityInvalid: hasValidationError,
201
+ // introduced in RNW 0.15.0
202
+ nativeID: inputId,
203
+ defaultValue: initialValue,
204
+ // currentValue is being updated even if the input is not controlled, passing it down to the
205
+ // Input could lead to changing its state from uncontrolled to controlled
206
+ value: isControlled ? currentValue : undefined
207
+ }; // TODO: use Stack instead of the Boxes
208
+
209
+ const feedbackVariant = {};
210
+
211
+ if (hasValidationError) {
212
+ feedbackVariant.state = 'error';
213
+ } else if (validation === 'success') {
214
+ feedbackVariant.state = 'success';
215
+ }
216
+
217
+ return /*#__PURE__*/React.createElement(View, null, label && /*#__PURE__*/React.createElement(Box, {
218
+ bottom: 1
219
+ }, /*#__PURE__*/React.createElement(InputLabel, {
220
+ label: label,
221
+ hint: hint,
222
+ hintPosition: hintPosition,
223
+ hintId: hintId,
224
+ tooltip: tooltip,
225
+ forId: inputId
226
+ })), /*#__PURE__*/React.createElement(View, {
227
+ style: selectOuterBorderStyles(themeTokens)
228
+ }, /*#__PURE__*/React.createElement(NativeTextInput, Object.assign({
229
+ style: selectInputStyles(themeTokens, inactive)
230
+ }, inputProps)), IconComponent && /*#__PURE__*/React.createElement(View, {
231
+ pointerEvents: "none" // avoid hijacking input press events
232
+ ,
233
+ style: [staticStyles.iconContainer, selectIconContainerStyles(themeTokens)]
234
+ }, /*#__PURE__*/React.createElement(IconComponent, {
235
+ tokens: selectIconTokens(themeTokens)
236
+ }))), feedback && /*#__PURE__*/React.createElement(Box, {
237
+ top: 1
238
+ }, /*#__PURE__*/React.createElement(Feedback, {
239
+ title: feedback,
240
+ variant: feedbackVariant,
241
+ accessibilityRole: hasValidationError ? 'alert' : undefined
242
+ })));
243
+ }
244
+
245
+ const selectIconContainerStyles = ({
246
+ paddingRight
247
+ }) => ({
248
+ paddingRight
249
+ });
250
+
251
+ TextInput.propTypes = {
252
+ /**
253
+ * The input label.
254
+ */
255
+ label: PropTypes.string,
256
+
257
+ /**
258
+ * If the `TextInput's` state is to be controlled by a parent component, use this prop
259
+ * together with the `onChange` to pass down and update the lifted state.
260
+ */
261
+ value: PropTypes.string,
262
+
263
+ /**
264
+ * Use this to set the initial value of an uncontrolled `TextInput`.
265
+ * Updating `initialValue` will **not** update the actual value.
266
+ */
267
+ initialValue: PropTypes.string,
268
+
269
+ /**
270
+ * A short description of the expected input.
271
+ */
272
+ hint: PropTypes.string,
273
+
274
+ /**
275
+ * Position of the hint relative to label.
276
+ */
277
+ hintPosition: PropTypes.oneOf(['inline', 'below']),
278
+
279
+ /**
280
+ * A detailed description of validation error/success or additional instructions.
281
+ * Visual variant is determined based on the `validation` prop.
282
+ */
283
+ feedback: PropTypes.string,
284
+
285
+ /**
286
+ * Content of an optional `Tooltip`. If set, a tooltip button will be shown next to the label.
287
+ */
288
+ tooltip: PropTypes.string,
289
+
290
+ /**
291
+ * Use to visually mark an input as valid or invalid.
292
+ */
293
+ validation: PropTypes.oneOf(['error', 'success']),
294
+
295
+ /**
296
+ * Disables all user interactions with the input.
297
+ */
298
+ inactive: PropTypes.bool,
299
+
300
+ /**
301
+ * Makes it impossible to change the input's value.
302
+ */
303
+ readOnly: PropTypes.bool,
304
+
305
+ /**
306
+ * Use to react upon input's value changes. Required when the `value` prop is set.
307
+ * Will receive the input's value as an argument.
308
+ */
309
+ onChange: PropTypes.func,
310
+
311
+ /** @ignore */
312
+ onChangeText: PropTypes.func,
313
+
314
+ /** @ignore */
315
+ onFocus: PropTypes.func,
316
+
317
+ /** @ignore */
318
+ onBlur: PropTypes.func,
319
+
320
+ /** @ignore */
321
+ onMouseOver: PropTypes.func,
322
+
323
+ /** @ignore */
324
+ onMouseOut: PropTypes.func,
325
+ tokens: getTokensPropType('TextInput'),
326
+ variant: variantProp.propType
327
+ };
328
+ export default TextInput;
329
+ const staticStyles = StyleSheet.create({
330
+ iconContainer: {
331
+ position: 'absolute',
332
+ right: 0,
333
+ top: 0,
334
+ bottom: 0,
335
+ justifyContent: 'center'
336
+ }
337
+ });
@@ -0,0 +1,2 @@
1
+ import TextInput from './TextInput';
2
+ export default TextInput;
@@ -1,8 +1,8 @@
1
1
  import React, { createContext, useState } from 'react';
2
2
  import PropTypes from 'prop-types';
3
3
  export const uninitialisedError = new Error('Theme context used outside of ThemeProvider');
4
- export const ThemeContext = createContext(uninitialisedError);
5
- export const ThemeSetterContext = createContext(uninitialisedError); // TODO: develop this as theme metadata is added
4
+ export const ThemeContext = /*#__PURE__*/createContext(uninitialisedError);
5
+ export const ThemeSetterContext = /*#__PURE__*/createContext(uninitialisedError); // TODO: develop this as theme metadata is added
6
6
  // https://github.com/telus/universal-design-system/issues/92
7
7
 
8
8
  export const themeType = PropTypes.object;
@@ -1,9 +1,21 @@
1
1
  import { useCallback } from 'react';
2
2
  import useTheme from './useTheme';
3
- import { getComponentTheme, getThemeTokens } from './utils';
3
+ import { getComponentTheme, getThemeTokens, resolveTokens, mergeAppearances } from './utils';
4
+ /**
5
+ * @typedef {import('../utils/propTypes.js').AppearanceSet} AppearanceSet
6
+ * @typedef {import('../utils/propTypes.js').TokensProp} TokensProp
7
+ * @typedef {import('../utils/propTypes.js').TokensSet} TokensSet
8
+ */
9
+
4
10
  /**
5
11
  * Returns a complete set of theme tokens for a component based on which of the
6
12
  * component's theme rules apply to the current set of theme appearances.
13
+ *
14
+ * @param {string} componentName - the name as defined in the theme schema of the component whose theme is to be used
15
+ * @param {TokensProp} [tokens] - every themed component should accept an optional `tokens` prop allowing theme tokens to be overridden
16
+ * @param {AppearanceSet} [variants] - every themed component should accept an optional `variants` prop specifying theme variants
17
+ * @param {AppearanceSet} [states] - optional object containing current theme appearances dictated by user action or context
18
+ * @returns {TokensSet} - the currently-applicable resolved set of theme tokens to apply
7
19
  */
8
20
 
9
21
  export const useThemeTokens = (componentName, tokens = {}, variants = {}, states = {}) => {
@@ -13,14 +25,30 @@ export const useThemeTokens = (componentName, tokens = {}, variants = {}, states
13
25
  return themeTokens;
14
26
  };
15
27
  /**
16
- * Returns a memoised function that behaves the same as useThemeTokens hook,
17
- * allowing tokens to be obtained inside callbacks and such other places where
18
- * calling useThemeTokens directly would be disallowed by React's hook rules.
28
+ * Returns a memoised tokens getter function that gets tokens similar to calling useThemeTokens.
29
+ * Scenarios where useThemeTokensCallback should be used instead of useThemeTokens include:
30
+ *
31
+ * - Where tokens to be obtained from state accessible only in scopes like callbacks and render functions,
32
+ * where calling useThemeTokens directly would be disallowed by React's hook rules.
33
+ * - Passing a tokens getter down via a child component's `tokens` prop, applying rules using the
34
+ * child component's current state. Consider wrapping the returned tokens in `selectTokens()`.
35
+ *
36
+ * @param {string} componentName - the name as defined in the theme schema of the component whose theme is to be used
37
+ * @param {TokensProp} [tokens] - every themed component should accept a `tokens` prop allowing theme tokens to be overridden
38
+ * @param {AppearanceSet} [variants] - variants passed in as props that don't change dynamically
39
+ * @returns {(states: AppearanceSet, tokenOverrides?: TokensProp) => TokensSet}
40
+ * - callback function that returning an overridable tokens set for current state. Only pass
41
+ * tokenOverrides in rare cases where tokens overrides are also generated outside hook scope,
42
+ * e.g. if one theme tokens callback needs to pass certain token overrides to another.
19
43
  */
20
44
 
21
- export const useThemeTokensCallback = componentName => {
45
+ export const useThemeTokensCallback = (componentName, tokens = {}, variants = {}) => {
22
46
  const theme = useTheme();
23
47
  const componentTheme = getComponentTheme(theme, componentName);
24
- const getThemeTokensCallback = useCallback((tokens, variants, states) => getThemeTokens(componentTheme, tokens, variants, states), [componentTheme]);
48
+ const getThemeTokensCallback = useCallback((states, tokenOverrides) => {
49
+ const appearances = mergeAppearances(variants, states);
50
+ const resolvedTokens = resolveTokens(tokens, tokenOverrides, appearances);
51
+ return getThemeTokens(componentTheme, resolvedTokens, appearances);
52
+ }, [componentTheme, tokens, variants]);
25
53
  return getThemeTokensCallback;
26
54
  };
@@ -1,3 +1,9 @@
1
+ /**
2
+ * @typedef {import('../../utils/propTypes.js').AppearanceSet} AppearanceSet
3
+ * @typedef {import('../../utils/propTypes.js').TokensProp} TokensProp
4
+ * @typedef {import('../../utils/propTypes.js').TokensSet} TokensSet
5
+ */
6
+
1
7
  /**
2
8
  * General utilities around working with theme tokens
3
9
  */
@@ -28,25 +34,47 @@ export const doesThemeConditionApply = ([key, value], appearances) => {
28
34
  return Array.isArray(value) ? value.includes(appearanceValue) : value === appearanceValue;
29
35
  };
30
36
  export const doesThemeRuleApply = (rule, appearances) => Object.entries(rule.if).every(condition => doesThemeConditionApply(condition, appearances));
37
+ /**
38
+ * Turns a tokens prop and an optional tokens override prop (either or both of which may be a tokens getter function)
39
+ * into one tokens set object where overrides are applied over the resolved default tokens.
40
+ *
41
+ * @param {TokensProp} defaultTokens - a set of tokens or tokens getter function which may be overridden
42
+ * @param {TokensProp} [tokenOverrides] - optional set of tokens or tokens getter function to override the default
43
+ * @param {AppearanceSet} [appearances] - optional appearance set to pass to tokens getter functions
44
+ * @returns {TokensSet} - object containing resolved tokens with overrides applied
45
+ */
46
+
47
+ export const resolveTokens = (defaultTokens, tokenOverrides, appearances = {}) => {
48
+ const resolve = tokens => typeof tokens === 'function' ? tokens(appearances) : tokens;
31
49
 
32
- const mergeTokens = (tokensProp, themeTokens, appearances) => {
33
- const overrideTokens = typeof tokensProp === 'function' ? tokensProp(appearances) : tokensProp;
34
- return Object.entries(overrideTokens).reduce((mergedTokens, [tokenName, tokenValue]) => tokenValue === undefined ? mergedTokens : { ...mergedTokens,
50
+ if (!tokenOverrides) return resolve(defaultTokens);
51
+ return Object.entries(resolve(tokenOverrides)).reduce((mergedTokens, [tokenName, tokenValue]) => tokenValue === undefined ? mergedTokens : { ...mergedTokens,
35
52
  [tokenName]: tokenValue
36
- }, themeTokens);
53
+ }, resolve(defaultTokens));
37
54
  };
55
+ /**
56
+ * Merges variants over states. Must be merged in that order to allow static showcases of a state,
57
+ * e.g. `<Button variant={{ pressed: true }} />` where button's pressed state is `false` by default.
58
+ * Returns an empty object if both variants and states are undefined.
59
+ *
60
+ * @param {AppearanceSet} [variants]
61
+ * @param {AppearanceSet} [states]
62
+ * @returns {AppearanceSet}
63
+ */
38
64
 
65
+ export const mergeAppearances = (variants = {}, states) => states ? { ...states,
66
+ ...variants
67
+ } : variants;
39
68
  export const getThemeTokens = ({
40
69
  rules = [],
41
70
  tokens: defaultThemeTokens = {}
42
- }, tokensProp, variants = {}, states = {}) => {
43
- const appearances = { ...states,
44
- ...variants
45
- }; // TODO: if in dev mode, validate the appearances and provided propTokens
71
+ }, tokensProp, variants = {}, states) => {
72
+ const appearances = mergeAppearances(variants, states); // TODO: if in dev mode, validate the appearances and provided propTokens
73
+ // Get the theme's default tokens set and merge tokens from applicable theme rules over it
46
74
 
47
75
  const themeTokens = rules.reduce((mergedTokens, rule) => doesThemeRuleApply(rule, appearances) ? { ...mergedTokens,
48
76
  ...rule.tokens
49
77
  } : mergedTokens, defaultThemeTokens);
50
- return tokensProp ? mergeTokens(tokensProp, themeTokens, appearances) : themeTokens;
78
+ return resolveTokens(themeTokens, tokensProp, appearances);
51
79
  };
52
80
  export const toArray = strOrArr => Array.isArray(strOrArr) ? strOrArr : [strOrArr];
@@ -3,43 +3,23 @@ import PropTypes from 'prop-types';
3
3
  import { Platform, View, StyleSheet } from 'react-native';
4
4
  import ButtonBase from '../Button/ButtonBase';
5
5
  import { useThemeTokensCallback, applyShadowToken } from '../ThemeProvider';
6
- import { a11yProps, pressProps, variantProp, getTokensPropType } from '../utils/propTypes';
6
+ import { a11yProps, pressProps, variantProp, getTokensPropType, selectTokens } from '../utils/propTypes';
7
7
  import { useInputValue } from '../utils/input';
8
8
 
9
- const selectButtonTokens = ({
10
- borderColor,
11
- borderWidth,
12
- borderRadius,
13
- outerBorderColor,
14
- outerBorderWidth,
15
- outerBorderGap,
16
- outerBorderRadius,
17
- outerBackgroundColor,
18
- backgroundColor,
19
- opacity,
20
- paddingLeft,
21
- paddingRight,
22
- paddingTop,
23
- paddingBottom,
24
- shadow
25
- }) => ({
26
- borderColor,
27
- borderWidth,
28
- borderRadius,
29
- outerBorderColor,
30
- outerBorderWidth,
31
- outerBorderGap,
32
- outerBorderRadius,
33
- outerBackgroundColor,
34
- backgroundColor,
35
- opacity,
36
- paddingLeft,
37
- paddingRight,
38
- paddingTop,
39
- paddingBottom,
40
- shadow,
41
- width: null // make it wrap around our track width
9
+ const selectButtonTokens = tokens => selectTokens('Button', { ...tokens,
10
+ // Width tokens are applied to our inner track. Disable Button width token so it wraps our track width.
11
+ width: null
12
+ }); // Map and rename icon-specific tokens to name used within Icon
13
+
42
14
 
15
+ const selectIconTokens = ({
16
+ iconSize,
17
+ iconColor,
18
+ iconOpacity
19
+ }) => ({
20
+ opacity: iconOpacity,
21
+ size: iconSize,
22
+ color: iconColor
43
23
  });
44
24
 
45
25
  const selectTrackStyles = ({
@@ -77,16 +57,6 @@ const selectSwitchStyles = ({
77
57
  })
78
58
  });
79
59
 
80
- const selectIconTokens = ({
81
- iconSize,
82
- iconColor,
83
- iconOpacity
84
- }) => ({
85
- opacity: iconOpacity,
86
- size: iconSize,
87
- color: iconColor
88
- });
89
-
90
60
  const ToggleSwitch = ({
91
61
  value,
92
62
  initialValue,
@@ -96,7 +66,7 @@ const ToggleSwitch = ({
96
66
  variant,
97
67
  accessibilityRole = 'switch'
98
68
  }) => {
99
- const getTokens = useThemeTokensCallback('ToggleSwitch');
69
+ const getTokens = useThemeTokensCallback('ToggleSwitch', tokens, variant);
100
70
  const {
101
71
  currentValue,
102
72
  setValue
@@ -108,7 +78,7 @@ const ToggleSwitch = ({
108
78
 
109
79
  const handlePress = () => setValue(!currentValue);
110
80
 
111
- const getButtonTokens = buttonState => selectButtonTokens(getTokens(tokens, variant, buttonState));
81
+ const getButtonTokens = buttonState => selectButtonTokens(getTokens(buttonState));
112
82
 
113
83
  return /*#__PURE__*/React.createElement(ButtonBase, {
114
84
  selected: currentValue,
@@ -120,7 +90,7 @@ const ToggleSwitch = ({
120
90
  },
121
91
  onPress: handlePress
122
92
  }, buttonState => {
123
- const themeTokens = getTokens(tokens, variant, buttonState);
93
+ const themeTokens = getTokens(buttonState);
124
94
  const IconComponent = themeTokens.icon;
125
95
  const switchStyles = selectSwitchStyles(themeTokens);
126
96
  const trackStyles = selectTrackStyles(themeTokens);
@@ -1,5 +1,3 @@
1
- function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
-
3
1
  import React from 'react';
4
2
  import PropTypes from 'prop-types';
5
3
  import { Platform, Text, View } from 'react-native';
@@ -38,8 +36,6 @@ const selectTextStyles = ({
38
36
  color,
39
37
  lineHeight,
40
38
  fontName,
41
- marginTop,
42
- marginBottom,
43
39
  textAlign,
44
40
  textTransform
45
41
  }) => applyTextStyles({
@@ -48,8 +44,6 @@ const selectTextStyles = ({
48
44
  color,
49
45
  lineHeight,
50
46
  fontName,
51
- marginTop,
52
- marginBottom,
53
47
  textAlign,
54
48
  textTransform
55
49
  }); // General-purpose flexible theme-neutral base component for text
@@ -82,7 +76,7 @@ const Typography = ({
82
76
  // Must be hyphenated, not camel case; `accessibilityLevel` only works on RNW >=0.15.1
83
77
  [headingLevel && 'aria-level']: headingLevel
84
78
  };
85
- return block ? /*#__PURE__*/React.createElement(View, a11y, /*#__PURE__*/React.createElement(Text, textProps, children)) : /*#__PURE__*/React.createElement(Text, _extends({}, textProps, a11y), children);
79
+ return block ? /*#__PURE__*/React.createElement(View, a11y, /*#__PURE__*/React.createElement(Text, textProps, children)) : /*#__PURE__*/React.createElement(Text, Object.assign({}, textProps, a11y), children);
86
80
  };
87
81
 
88
82
  Typography.propTypes = { ...a11yProps.types,
@@ -8,7 +8,7 @@ import { viewports } from '@telus-uds/system-constants'; // we are using Dimensi
8
8
 
9
9
  const lookupViewport = () => viewports.select(Dimensions.get('window').width);
10
10
 
11
- export const ViewportContext = createContext({});
11
+ export const ViewportContext = /*#__PURE__*/createContext({});
12
12
  /**
13
13
  * Provides an up-to-date viewport value from system-constants, available via the `useViewport` hook
14
14
  */
package/lib/index.js CHANGED
@@ -1,17 +1,24 @@
1
1
  export { default as ActivityIndicator } from './ActivityIndicator';
2
2
  export { default as Box } from './Box';
3
3
  export * from './Button';
4
+ export { default as Card } from './Card';
4
5
  export { default as Divider } from './Divider';
5
6
  export { default as ExpandCollapse, Accordion } from './ExpandCollapse';
7
+ export { default as Feedback } from './Feedback';
6
8
  export { default as FlexGrid } from './FlexGrid';
7
9
  export { default as Icon } from './Icon';
8
10
  export * from './Icon';
9
11
  export * from './Link';
12
+ export { default as Pagination } from './Pagination';
10
13
  export { default as SideNav } from './SideNav';
14
+ export { default as Spacer } from './Spacer';
15
+ export { default as StackView } from './StackView';
16
+ export * from './StackView';
17
+ export { default as TextInput } from './TextInput';
11
18
  export { default as ToggleSwitch } from './ToggleSwitch';
12
19
  export { default as Typography } from './Typography';
13
20
  export { default as A11yInfoProvider, useA11yInfo } from './A11yInfoProvider';
14
21
  export { default as BaseProvider } from './BaseProvider';
15
22
  export { default as ViewportProvider, useViewport } from './ViewportProvider';
16
- export { default as ThemeProvider, useTheme, useSetTheme } from './ThemeProvider';
23
+ export { default as ThemeProvider, useTheme, useSetTheme, useThemeTokens } from './ThemeProvider';
17
24
  export * from './utils';
@@ -1,3 +1,4 @@
1
1
  export * from './animation';
2
2
  export * from './input';
3
- export * from './propTypes';
3
+ export * from './propTypes';
4
+ export * from './spacing';
@@ -60,6 +60,7 @@ Consumers of this hook must be one of:
60
60
  * currentValue: any
61
61
  * setValue: (value: any) => void
62
62
  * resetValue: () => void
63
+ * isControlled: bool
63
64
  * }}
64
65
  */
65
66
 
@@ -91,7 +92,8 @@ export const useInputValue = (props = {}, hookName = 'useInputValue') => {
91
92
  return {
92
93
  currentValue,
93
94
  setValue,
94
- resetValue
95
+ resetValue,
96
+ isControlled
95
97
  };
96
98
  };
97
99
  /**