@telus-uds/components-base 1.8.2 → 1.8.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 (278) hide show
  1. package/CHANGELOG.md +33 -2
  2. package/component-docs.json +36 -1
  3. package/lib/ActivityIndicator/Spinner.js +7 -7
  4. package/lib/ActivityIndicator/Spinner.native.js +2 -2
  5. package/lib/Button/ButtonBase.js +2 -2
  6. package/lib/Button/ButtonGroup.js +1 -1
  7. package/lib/Checkbox/Checkbox.js +5 -3
  8. package/lib/Checkbox/CheckboxGroup.js +2 -2
  9. package/lib/Divider/Divider.js +2 -2
  10. package/lib/ExpandCollapse/Panel.js +2 -17
  11. package/lib/FlexGrid/Col/Col.js +1 -1
  12. package/lib/Icon/Icon.js +1 -1
  13. package/lib/Link/InlinePressable.js +5 -1
  14. package/lib/Link/LinkBase.js +5 -2
  15. package/lib/List/List.js +12 -7
  16. package/lib/List/ListItem.js +4 -5
  17. package/lib/Modal/Modal.js +2 -1
  18. package/lib/Pagination/PageButton.js +2 -2
  19. package/lib/Pagination/Pagination.js +5 -3
  20. package/lib/Pagination/usePagination.js +2 -2
  21. package/lib/Progress/ProgressBar.js +3 -3
  22. package/lib/Progress/ProgressBarBackground.js +3 -3
  23. package/lib/Radio/Radio.js +2 -2
  24. package/lib/Radio/RadioGroup.js +2 -2
  25. package/lib/RadioCard/RadioCard.js +1 -1
  26. package/lib/RadioCard/RadioCardGroup.js +2 -2
  27. package/lib/Search/Search.js +1 -1
  28. package/lib/SideNav/SideNav.js +2 -2
  29. package/lib/Skeleton/Skeleton.js +1 -1
  30. package/lib/Skeleton/skeletonWebAnimation.js +1 -1
  31. package/lib/StackView/StackWrap.js +3 -1
  32. package/lib/StackView/getStackedContent.js +2 -2
  33. package/lib/Tabs/Tabs.js +28 -3
  34. package/lib/Tabs/TabsItem.js +5 -6
  35. package/lib/Tags/Tags.js +1 -1
  36. package/lib/ThemeProvider/utils/styles.js +2 -2
  37. package/lib/ThemeProvider/utils/theme-tokens.js +7 -9
  38. package/lib/ToggleSwitch/ToggleSwitch.js +1 -1
  39. package/lib/ToggleSwitch/ToggleSwitchGroup.js +1 -1
  40. package/lib/Tooltip/Backdrop.js +2 -10
  41. package/lib/utils/animation/useVerticalExpandAnimation.js +40 -14
  42. package/lib/utils/children.js +2 -2
  43. package/lib/utils/input.js +6 -12
  44. package/lib/utils/props/componentPropType.js +3 -3
  45. package/lib/utils/props/selectSystemProps.js +2 -2
  46. package/lib/utils/props/tokens.js +2 -2
  47. package/lib/utils/useSpacingScale.js +3 -1
  48. package/lib/utils/useUniqueId.js +1 -1
  49. package/lib-module/A11yInfoProvider/index.js +63 -0
  50. package/lib-module/A11yText/index.js +56 -0
  51. package/lib-module/ActivityIndicator/Spinner.js +77 -0
  52. package/lib-module/ActivityIndicator/Spinner.native.js +144 -0
  53. package/lib-module/ActivityIndicator/index.js +41 -0
  54. package/lib-module/ActivityIndicator/shared.js +12 -0
  55. package/lib-module/BaseProvider/index.js +29 -0
  56. package/lib-module/Box/Box.js +244 -0
  57. package/lib-module/Box/index.js +2 -0
  58. package/lib-module/Button/Button.js +26 -0
  59. package/lib-module/Button/ButtonBase.js +271 -0
  60. package/lib-module/Button/ButtonGroup.js +247 -0
  61. package/lib-module/Button/ButtonLink.js +40 -0
  62. package/lib-module/Button/index.js +4 -0
  63. package/lib-module/Button/propTypes.js +36 -0
  64. package/lib-module/Card/Card.js +84 -0
  65. package/lib-module/Card/CardBase.js +66 -0
  66. package/lib-module/Card/PressableCardBase.js +114 -0
  67. package/lib-module/Card/index.js +4 -0
  68. package/lib-module/Checkbox/Checkbox.js +348 -0
  69. package/lib-module/Checkbox/CheckboxGroup.js +233 -0
  70. package/lib-module/Checkbox/CheckboxInput.js +60 -0
  71. package/lib-module/Checkbox/CheckboxInput.native.js +6 -0
  72. package/lib-module/Checkbox/index.js +3 -0
  73. package/lib-module/Divider/Divider.js +124 -0
  74. package/lib-module/Divider/index.js +2 -0
  75. package/lib-module/ExpandCollapse/Accordion.js +15 -0
  76. package/lib-module/ExpandCollapse/Control.js +136 -0
  77. package/lib-module/ExpandCollapse/ExpandCollapse.js +95 -0
  78. package/lib-module/ExpandCollapse/Panel.js +151 -0
  79. package/lib-module/ExpandCollapse/index.js +7 -0
  80. package/lib-module/Feedback/Feedback.js +157 -0
  81. package/lib-module/Feedback/index.js +2 -0
  82. package/lib-module/Fieldset/Fieldset.js +153 -0
  83. package/lib-module/Fieldset/FieldsetContainer.js +32 -0
  84. package/lib-module/Fieldset/FieldsetContainer.native.js +23 -0
  85. package/lib-module/Fieldset/Legend.js +24 -0
  86. package/lib-module/Fieldset/Legend.native.js +31 -0
  87. package/lib-module/Fieldset/cssReset.js +14 -0
  88. package/lib-module/Fieldset/index.js +2 -0
  89. package/lib-module/FlexGrid/Col/Col.js +276 -0
  90. package/lib-module/FlexGrid/Col/index.js +2 -0
  91. package/lib-module/FlexGrid/FlexGrid.js +148 -0
  92. package/lib-module/FlexGrid/Row/Row.js +184 -0
  93. package/lib-module/FlexGrid/Row/index.js +2 -0
  94. package/lib-module/FlexGrid/helpers/index.js +18 -0
  95. package/lib-module/FlexGrid/index.js +2 -0
  96. package/lib-module/FlexGrid/providers/GutterContext.js +3 -0
  97. package/lib-module/HorizontalScroll/HorizontalScroll.js +175 -0
  98. package/lib-module/HorizontalScroll/HorizontalScrollButton.js +81 -0
  99. package/lib-module/HorizontalScroll/ScrollViewEnd.js +48 -0
  100. package/lib-module/HorizontalScroll/ScrollViewEnd.native.js +27 -0
  101. package/lib-module/HorizontalScroll/dictionary.js +11 -0
  102. package/lib-module/HorizontalScroll/index.js +11 -0
  103. package/lib-module/HorizontalScroll/itemPositions.js +112 -0
  104. package/lib-module/Icon/Icon.js +62 -0
  105. package/lib-module/Icon/IconText.js +83 -0
  106. package/lib-module/Icon/index.js +4 -0
  107. package/lib-module/IconButton/IconButton.js +122 -0
  108. package/lib-module/IconButton/index.js +2 -0
  109. package/lib-module/InputLabel/InputLabel.js +148 -0
  110. package/lib-module/InputLabel/LabelContent.js +27 -0
  111. package/lib-module/InputLabel/LabelContent.native.js +19 -0
  112. package/lib-module/InputLabel/index.js +2 -0
  113. package/lib-module/InputSupports/InputSupports.js +96 -0
  114. package/lib-module/InputSupports/index.js +2 -0
  115. package/lib-module/InputSupports/useInputSupports.js +32 -0
  116. package/lib-module/Link/ChevronLink.js +52 -0
  117. package/lib-module/Link/InlinePressable.js +44 -0
  118. package/lib-module/Link/InlinePressable.native.js +89 -0
  119. package/lib-module/Link/Link.js +28 -0
  120. package/lib-module/Link/LinkBase.js +243 -0
  121. package/lib-module/Link/TextButton.js +35 -0
  122. package/lib-module/Link/index.js +5 -0
  123. package/lib-module/List/List.js +62 -0
  124. package/lib-module/List/ListItem.js +248 -0
  125. package/lib-module/List/index.js +5 -0
  126. package/lib-module/Modal/Modal.js +222 -0
  127. package/lib-module/Modal/dictionary.js +9 -0
  128. package/lib-module/Modal/index.js +2 -0
  129. package/lib-module/Notification/Notification.js +209 -0
  130. package/lib-module/Notification/dictionary.js +8 -0
  131. package/lib-module/Notification/index.js +2 -0
  132. package/lib-module/Pagination/PageButton.js +66 -0
  133. package/lib-module/Pagination/Pagination.js +153 -0
  134. package/lib-module/Pagination/SideButton.js +106 -0
  135. package/lib-module/Pagination/dictionary.js +18 -0
  136. package/lib-module/Pagination/index.js +2 -0
  137. package/lib-module/Pagination/usePagination.js +73 -0
  138. package/lib-module/Progress/Progress.js +89 -0
  139. package/lib-module/Progress/ProgressBar.js +138 -0
  140. package/lib-module/Progress/ProgressBarBackground.js +42 -0
  141. package/lib-module/Progress/index.js +4 -0
  142. package/lib-module/Radio/Radio.js +285 -0
  143. package/lib-module/Radio/RadioButton.js +138 -0
  144. package/lib-module/Radio/RadioGroup.js +243 -0
  145. package/lib-module/Radio/RadioInput.js +62 -0
  146. package/lib-module/Radio/RadioInput.native.js +6 -0
  147. package/lib-module/Radio/index.js +3 -0
  148. package/lib-module/RadioCard/RadioCard.js +219 -0
  149. package/lib-module/RadioCard/RadioCardGroup.js +250 -0
  150. package/lib-module/RadioCard/index.js +3 -0
  151. package/lib-module/Search/Search.js +261 -0
  152. package/lib-module/Search/dictionary.js +12 -0
  153. package/lib-module/Search/index.js +2 -0
  154. package/lib-module/Select/Group.js +21 -0
  155. package/lib-module/Select/Group.native.js +15 -0
  156. package/lib-module/Select/Item.js +20 -0
  157. package/lib-module/Select/Item.native.js +3 -0
  158. package/lib-module/Select/Picker.js +68 -0
  159. package/lib-module/Select/Picker.native.js +120 -0
  160. package/lib-module/Select/Select.js +337 -0
  161. package/lib-module/Select/index.js +6 -0
  162. package/lib-module/SideNav/Item.js +145 -0
  163. package/lib-module/SideNav/ItemContent.js +48 -0
  164. package/lib-module/SideNav/ItemsGroup.js +117 -0
  165. package/lib-module/SideNav/SideNav.js +136 -0
  166. package/lib-module/SideNav/index.js +1 -0
  167. package/lib-module/Skeleton/Skeleton.js +179 -0
  168. package/lib-module/Skeleton/index.js +2 -0
  169. package/lib-module/Skeleton/skeleton.constant.js +3 -0
  170. package/lib-module/Skeleton/skeletonWebAnimation.js +18 -0
  171. package/lib-module/Skeleton/useSkeletonNativeAnimation.js +24 -0
  172. package/lib-module/Spacer/Spacer.js +98 -0
  173. package/lib-module/Spacer/index.js +2 -0
  174. package/lib-module/StackView/StackView.js +125 -0
  175. package/lib-module/StackView/StackWrap.js +50 -0
  176. package/lib-module/StackView/StackWrap.native.js +3 -0
  177. package/lib-module/StackView/StackWrapBox.js +115 -0
  178. package/lib-module/StackView/StackWrapGap.js +59 -0
  179. package/lib-module/StackView/common.js +35 -0
  180. package/lib-module/StackView/getStackedContent.js +124 -0
  181. package/lib-module/StackView/index.js +5 -0
  182. package/lib-module/StepTracker/Step.js +248 -0
  183. package/lib-module/StepTracker/StepTracker.js +185 -0
  184. package/lib-module/StepTracker/dictionary.js +10 -0
  185. package/lib-module/StepTracker/index.js +2 -0
  186. package/lib-module/Tabs/Tabs.js +143 -0
  187. package/lib-module/Tabs/TabsItem.js +224 -0
  188. package/lib-module/Tabs/index.js +2 -0
  189. package/lib-module/Tags/Tags.js +251 -0
  190. package/lib-module/Tags/index.js +2 -0
  191. package/lib-module/TextInput/TextArea.js +94 -0
  192. package/lib-module/TextInput/TextInput.js +72 -0
  193. package/lib-module/TextInput/TextInputBase.js +245 -0
  194. package/lib-module/TextInput/index.js +3 -0
  195. package/lib-module/TextInput/propTypes.js +37 -0
  196. package/lib-module/ThemeProvider/ThemeProvider.js +36 -0
  197. package/lib-module/ThemeProvider/index.js +6 -0
  198. package/lib-module/ThemeProvider/useSetTheme.js +25 -0
  199. package/lib-module/ThemeProvider/useTheme.js +14 -0
  200. package/lib-module/ThemeProvider/useThemeTokens.js +110 -0
  201. package/lib-module/ThemeProvider/utils/index.js +2 -0
  202. package/lib-module/ThemeProvider/utils/styles.js +181 -0
  203. package/lib-module/ThemeProvider/utils/theme-tokens.js +163 -0
  204. package/lib-module/ToggleSwitch/ToggleSwitch.js +250 -0
  205. package/lib-module/ToggleSwitch/ToggleSwitchGroup.js +220 -0
  206. package/lib-module/ToggleSwitch/index.js +3 -0
  207. package/lib-module/Tooltip/Backdrop.js +45 -0
  208. package/lib-module/Tooltip/Backdrop.native.js +44 -0
  209. package/lib-module/Tooltip/Tooltip.js +348 -0
  210. package/lib-module/Tooltip/dictionary.js +8 -0
  211. package/lib-module/Tooltip/getTooltipPosition.js +175 -0
  212. package/lib-module/Tooltip/index.js +2 -0
  213. package/lib-module/TooltipButton/TooltipButton.js +78 -0
  214. package/lib-module/TooltipButton/index.js +2 -0
  215. package/lib-module/Typography/Typography.js +124 -0
  216. package/lib-module/Typography/index.js +2 -0
  217. package/lib-module/ViewportProvider/ViewportProvider.js +29 -0
  218. package/lib-module/ViewportProvider/index.js +3 -0
  219. package/lib-module/ViewportProvider/useViewport.js +3 -0
  220. package/lib-module/ViewportProvider/useViewportListener.js +46 -0
  221. package/lib-module/index.js +48 -0
  222. package/lib-module/utils/a11y/index.js +2 -0
  223. package/lib-module/utils/a11y/semantics.js +157 -0
  224. package/lib-module/utils/a11y/textSize.js +36 -0
  225. package/lib-module/utils/animation/index.js +2 -0
  226. package/lib-module/utils/animation/useVerticalExpandAnimation.js +88 -0
  227. package/lib-module/utils/children.js +119 -0
  228. package/lib-module/utils/containUniqueFields.js +26 -0
  229. package/lib-module/utils/index.js +16 -0
  230. package/lib-module/utils/info/index.js +7 -0
  231. package/lib-module/utils/info/platform/index.js +11 -0
  232. package/lib-module/utils/info/platform/platform.android.js +1 -0
  233. package/lib-module/utils/info/platform/platform.ios.js +1 -0
  234. package/lib-module/utils/info/platform/platform.js +1 -0
  235. package/lib-module/utils/info/platform/platform.native.js +4 -0
  236. package/lib-module/utils/info/versions.js +5 -0
  237. package/lib-module/utils/input.js +179 -0
  238. package/lib-module/utils/pressability.js +111 -0
  239. package/lib-module/utils/props/a11yProps.js +140 -0
  240. package/lib-module/utils/props/clickProps.js +26 -0
  241. package/lib-module/utils/props/componentPropType.js +63 -0
  242. package/lib-module/utils/props/copyPropTypes.js +2 -0
  243. package/lib-module/utils/props/getPropSelector.js +9 -0
  244. package/lib-module/utils/props/handlerProps.js +65 -0
  245. package/lib-module/utils/props/hrefAttrsProp.js +33 -0
  246. package/lib-module/utils/props/index.js +19 -0
  247. package/lib-module/utils/props/inputSupportsProps.js +62 -0
  248. package/lib-module/utils/props/linkProps.js +44 -0
  249. package/lib-module/utils/props/paddingProp.js +9 -0
  250. package/lib-module/utils/props/pressProps.js +42 -0
  251. package/lib-module/utils/props/rectProp.js +9 -0
  252. package/lib-module/utils/props/responsiveProps.js +30 -0
  253. package/lib-module/utils/props/selectSystemProps.js +24 -0
  254. package/lib-module/utils/props/spacingProps.js +56 -0
  255. package/lib-module/utils/props/textInputProps.js +194 -0
  256. package/lib-module/utils/props/textProps.js +59 -0
  257. package/lib-module/utils/props/tokens.js +133 -0
  258. package/lib-module/utils/props/variantProp.js +18 -0
  259. package/lib-module/utils/props/viewProps.js +22 -0
  260. package/lib-module/utils/ssr.js +38 -0
  261. package/lib-module/utils/useCopy.js +44 -0
  262. package/lib-module/utils/useHash.js +45 -0
  263. package/lib-module/utils/useHash.native.js +7 -0
  264. package/lib-module/utils/useResponsiveProp.js +47 -0
  265. package/lib-module/utils/useSpacingScale.js +125 -0
  266. package/lib-module/utils/useUniqueId.js +13 -0
  267. package/lib-module/utils/withLinkRouter.js +83 -0
  268. package/package.json +3 -2
  269. package/src/Checkbox/Checkbox.jsx +3 -1
  270. package/src/ExpandCollapse/Panel.jsx +2 -19
  271. package/src/Link/InlinePressable.jsx +5 -2
  272. package/src/Link/LinkBase.jsx +4 -1
  273. package/src/List/List.jsx +34 -19
  274. package/src/List/ListItem.jsx +12 -2
  275. package/src/Modal/Modal.jsx +2 -1
  276. package/src/Tabs/Tabs.jsx +24 -2
  277. package/src/Tabs/TabsItem.jsx +6 -5
  278. package/src/utils/animation/useVerticalExpandAnimation.js +47 -13
@@ -0,0 +1,36 @@
1
+ import React, { createContext, useState } from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { validateThemeTokensVersion } from './utils';
4
+ import { jsx as _jsx } from "react/jsx-runtime";
5
+ export const uninitialisedError = new Error('Theme context used outside of ThemeProvider');
6
+ export const ThemeContext = /*#__PURE__*/createContext(uninitialisedError);
7
+ export const ThemeSetterContext = /*#__PURE__*/createContext(uninitialisedError);
8
+
9
+ const ThemeProvider = _ref => {
10
+ let {
11
+ children,
12
+ defaultTheme
13
+ } = _ref;
14
+ const [theme, setTheme] = useState(defaultTheme); // Validate the theme tokens version on every render.
15
+ // This will intentionally break the application when attempting to use an invalid theme.
16
+ // This will surface an incompatibility quickly rather than allowing the potential for strange bugs due to missing or incompatible tokens.
17
+
18
+ validateThemeTokensVersion(theme);
19
+ return /*#__PURE__*/_jsx(ThemeSetterContext.Provider, {
20
+ value: setTheme,
21
+ children: /*#__PURE__*/_jsx(ThemeContext.Provider, {
22
+ value: theme,
23
+ children: children
24
+ })
25
+ });
26
+ };
27
+
28
+ ThemeProvider.propTypes = {
29
+ children: PropTypes.node.isRequired,
30
+ defaultTheme: PropTypes.shape({
31
+ metadata: PropTypes.shape({
32
+ themeTokensVersion: PropTypes.string.isRequired
33
+ }).isRequired
34
+ }).isRequired
35
+ };
36
+ export default ThemeProvider;
@@ -0,0 +1,6 @@
1
+ import ThemeProvider from './ThemeProvider';
2
+ export { default as useTheme } from './useTheme';
3
+ export { default as useSetTheme } from './useSetTheme';
4
+ export * from './useThemeTokens';
5
+ export * from './utils';
6
+ export default ThemeProvider;
@@ -0,0 +1,25 @@
1
+ import { useCallback, useContext } from 'react';
2
+ import { ThemeSetterContext } from './ThemeProvider';
3
+
4
+ const useSetTheme = () => {
5
+ // Replace current theme with provided object
6
+ const setTheme = useContext(ThemeSetterContext); // Fail fast if dev uses useSetTheme outside of ThemeProvider
7
+
8
+ if (setTheme instanceof Error) {
9
+ throw setTheme;
10
+ } // Merge provided object into current theme
11
+
12
+
13
+ const editTheme = useCallback(newTheme => setTheme(function () {
14
+ let oldTheme = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
15
+ return { ...oldTheme,
16
+ ...newTheme
17
+ };
18
+ }), [setTheme]);
19
+ return {
20
+ setTheme,
21
+ editTheme
22
+ };
23
+ };
24
+
25
+ export default useSetTheme;
@@ -0,0 +1,14 @@
1
+ import { useContext } from 'react';
2
+ import { ThemeContext } from './ThemeProvider';
3
+
4
+ const useTheme = () => {
5
+ const theme = useContext(ThemeContext); // Fail fast if dev uses useTheme outside of ThemeProvider
6
+
7
+ if (theme instanceof Error) {
8
+ throw theme;
9
+ }
10
+
11
+ return theme;
12
+ };
13
+
14
+ export default useTheme;
@@ -0,0 +1,110 @@
1
+ import { useCallback } from 'react';
2
+ import useTheme from './useTheme';
3
+ import { getComponentTheme, getThemeTokens, resolveThemeTokens, mergeAppearances } from './utils';
4
+ /**
5
+ * @typedef {import('../utils/props/variantProp.js').AppearanceSet} AppearanceSet
6
+ * @typedef {import('../utils/props/tokens.js').TokensProp} TokensProp
7
+ * @typedef {import('../utils/props/tokens.js').TokensSet} TokensSet
8
+ */
9
+
10
+ /**
11
+ * Returns a complete set of theme tokens for a component based on which of the
12
+ * component's theme rules apply to the current set of theme appearances.
13
+ *
14
+ * When applying theme tokens as styles to a component, don't spread the theme tokens
15
+ * object, explicitly select each property. This way, if new theme tokens are added to the
16
+ * component's theme in a minor or patch release of the theme package, the appearance
17
+ * of the component in sites and apps will only change when the component itself is updated.
18
+ *
19
+ * @example
20
+ * const seletContainerTokens = ({ color, width, height }) => ({
21
+ * backgroundColor: color, width, height
22
+ * })
23
+ * const MyComponent = ({ tokens, variant, children }) => {
24
+ * const viewport = useViewport()
25
+ * const themeTokens = useThemeTokens('MyComponent', tokens, variant, { viewport })
26
+ * const style = seletContainerTokens(themeTokens)
27
+ * return <View style={style}>{children}</View>
28
+ * }
29
+ *
30
+ * @param {string} componentName - the name as defined in the theme schema of the component whose theme is to be used
31
+ * @param {TokensProp} [tokens] - every themed component should accept an optional `tokens` prop allowing theme tokens to be overridden
32
+ * @param {AppearanceSet} [variants] - every themed component should accept an optional `variants` prop specifying theme variants
33
+ * @param {AppearanceSet} [states] - optional object containing current theme appearances dictated by user action or context
34
+ * @returns {TokensSet} - the currently-applicable resolved set of theme tokens to apply
35
+ */
36
+
37
+ export const useThemeTokens = function (componentName) {
38
+ let tokens = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
39
+ let variants = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
40
+ let states = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
41
+ const theme = useTheme();
42
+ const componentTheme = getComponentTheme(theme, componentName);
43
+ const themeTokens = getThemeTokens(componentTheme, tokens, variants, states);
44
+ return themeTokens;
45
+ };
46
+ /**
47
+ * Returns a memoised tokens getter function that gets tokens similar to calling useThemeTokens.
48
+ * Scenarios where `useThemeTokensCallback` should be used instead of `useThemeTokens` include:
49
+ *
50
+ * - Where tokens are to be obtained from state that is accessible only in scopes like callbacks
51
+ * and render functions, where calling useThemeTokens directly would be disallowed by React's hook rules.
52
+ * - Passing a tokens getter down via a child component's `tokens` prop, applying rules using the
53
+ * child component's current state. Consider wrapping the returned tokens in `selectTokens()`.
54
+ *
55
+ * The function returned by `useThemeTokens` may be called with an object of state appearances to get an object
56
+ * of tokens, or may be passed as a `tokens` prop to other themed components or `useThemeTokens`/`useThemeTokensCallback`
57
+ * hooks.
58
+ *
59
+ * @example
60
+ * // Resolving tokens inside Pressable's style function, based on Pressable state
61
+ * const PressMe = ({ tokens, variant, children }) => {
62
+ * const getTokens = useThemeTokensCallback('PressMe', tokens, variant)
63
+ * const getPressableStyle = ({ pressed }) => {
64
+ * const { color, width, height } = getTokens({ pressed })
65
+ * return { backgroundColor: color, width, height }
66
+ * }
67
+ * return <Pressable style={getPressableStyle}>{children}</Pressable>
68
+ * }
69
+ *
70
+ * @example
71
+ * // Setting the theme in a parent and resolving it in a child based on child's state
72
+ * const MenuButton = ({ tokens, variant, ...buttonProps }) => {
73
+ * // Define what theme, variant etc we want in this component...
74
+ * const getTokens = useThemeTokensCallback('Button', tokens, variant)
75
+ * // ...resolve them in another component based on its state (e.g. press, hover...)
76
+ * return <ButtonBase tokens={getTokens} accessibilityRole="menuitem" {...buttonProps} />
77
+ * }
78
+ *
79
+ * @example
80
+ * // Chaining multiple themes together, sharing the same variants and state
81
+ * const GlowingLink = ({ tokens, variant, children }) => {
82
+ * // applies a small theme for a glowing effect, using same variants and states as "link"
83
+ * const getGlowTokens = useThemeCallback('GlowingLink', tokens, variant)
84
+ * // applies link state to get "Link" theme tokens and merge "GlowingLink" tokens on top
85
+ * const getTokens = useThemeCallback('Link', getGlowTokens, variant)
86
+ * // renders a link with both themes applied, both based on current link state
87
+ * return <LinkBase tokens={getTokens}>{children}</LinkBase>
88
+ * }
89
+ *
90
+ * @param {string} componentName - the name as defined in the theme schema of the component whose theme is to be used
91
+ * @param {TokensProp} [tokens] - every themed component should accept a `tokens` prop allowing theme tokens to be overridden
92
+ * @param {AppearanceSet} [variants] - variants passed in as props that don't change dynamically
93
+ * @returns {(states: AppearanceSet, tokenOverrides?: TokensProp) => TokensSet}
94
+ * - callback function that returning an overridable tokens set for current state. Only pass
95
+ * tokenOverrides in rare cases where tokens overrides are also generated outside hook scope,
96
+ * e.g. if one theme tokens callback needs to pass certain token overrides to another.
97
+ */
98
+
99
+ export const useThemeTokensCallback = function (componentName) {
100
+ let tokens = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
101
+ let variants = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
102
+ const theme = useTheme();
103
+ const componentTheme = getComponentTheme(theme, componentName);
104
+ const getThemeTokensCallback = useCallback((states, tokenOverrides) => {
105
+ const appearances = mergeAppearances(variants, states);
106
+ const resolvedTokens = resolveThemeTokens(tokens, appearances, tokenOverrides);
107
+ return getThemeTokens(componentTheme, resolvedTokens, appearances);
108
+ }, [componentTheme, tokens, variants]);
109
+ return getThemeTokensCallback;
110
+ };
@@ -0,0 +1,2 @@
1
+ export * from './styles';
2
+ export * from './theme-tokens';
@@ -0,0 +1,181 @@
1
+ /**
2
+ * React-Native specific implementations of style patterns that
3
+ * don't map directly to cross-platform design token values.
4
+ */
5
+ import Platform from "react-native-web/dist/exports/Platform";
6
+ import { fontBasePixels } from '@telus-uds/system-constants'; // Make design tokens fit React Native's text styles' specific requirements and quirks
7
+
8
+ export function applyTextStyles(_ref) {
9
+ let {
10
+ fontSize,
11
+ fontScaleCap,
12
+ lineHeight,
13
+ letterSpacing,
14
+ fontWeight = '400',
15
+ fontName,
16
+ fontStyle = 'normal',
17
+ ...rest
18
+ } = _ref;
19
+ const styles = { ...rest
20
+ };
21
+
22
+ if (fontSize) {
23
+ // If relative font sizes are needed, catch and calculate them here
24
+ styles.fontSize = fontSize;
25
+ }
26
+
27
+ if (typeof lineHeight === 'number') {
28
+ // React Native expects absolute line heights but multipliers are better as design tokens
29
+ styles.lineHeight = lineHeight * (fontSize || fontBasePixels);
30
+ }
31
+
32
+ if (typeof letterSpacing === 'number' && letterSpacing) {
33
+ // Same as for line heights - React Native expects absolute letter spacing value
34
+ styles.letterSpacing = letterSpacing * (fontSize || fontBasePixels);
35
+ }
36
+
37
+ if (fontName) {
38
+ // Don't set undefined font families. May need some validation here that the font is available.
39
+ // Android doesn't recognise font weights natively so apply custom font weights via `fontFamily`.
40
+ styles.fontFamily = "".concat(fontName).concat(fontWeight).concat(fontStyle);
41
+ } else if (fontWeight) {
42
+ // If using system default font, apply the font weight directly.
43
+ // Font weight support in Android is limited to 'bold' or anything else === 'normal'.
44
+ styles.fontWeight = Platform.OS === 'android' && Number(fontWeight) > 400 ? 'bold' : fontWeight;
45
+ } // TODO: look into calculating typographic spacing with PixelRatio.getFontScale()
46
+ // Do while implementing advanced typography e.g. paragraph etc
47
+ // https://github.com/telus/universal-design-system/issues/89
48
+
49
+
50
+ return styles;
51
+ }
52
+ /**
53
+ * React Native's in-built shadow support is limited. Take an object describing an ideal shadow and
54
+ * return a platform-appropriate implementation that gets close to the described effect.
55
+ *
56
+ * @param {object} shadowToken
57
+ * @param {boolean} shadowToken.inset
58
+ * @param {color} shadowToken.string
59
+ * @param {number} shadowToken.offsetX
60
+ * @param {number} shadowToken.offsetY
61
+ * @param {number} shadowToken.blur
62
+ * @param {number} shadowToken.spread
63
+ * @return {object}
64
+ */
65
+
66
+ export function applyShadowToken(shadowToken) {
67
+ if (!shadowToken) return {};
68
+ const applyShadow = Platform.select({
69
+ web: () => applyWebShadow(shadowToken),
70
+ ios: () => applyIosShadow(shadowToken),
71
+ android: () => applyAndroidShadow(shadowToken)
72
+ });
73
+ return applyShadow();
74
+ }
75
+
76
+ function applyWebShadow(_ref2) {
77
+ let {
78
+ inset,
79
+ color = '',
80
+ offsetX = 0,
81
+ offsetY = 0,
82
+ blur = 0,
83
+ spread = 0
84
+ } = _ref2;
85
+ const insetString = inset ? 'inset ' : '';
86
+ const boxShadow = {
87
+ boxShadow: "".concat(insetString).concat(offsetX, "px ").concat(offsetY, "px ").concat(blur, "px ").concat(spread, "px ").concat(color)
88
+ };
89
+ return boxShadow;
90
+ }
91
+
92
+ function applyAndroidShadow(shadowToken) {
93
+ const {
94
+ color,
95
+ inset,
96
+ spread,
97
+ offsetY,
98
+ offsetX
99
+ } = shadowToken; // Android shadows are very limited and workarounds are hacky. Use the built-in `elevation` API unless
100
+ // shadow design is radically different to a simple top-down shadow (e.g. horizontal, upward, or inset)
101
+
102
+ if (!inset && offsetY >= 0 && offsetY >= offsetX) {
103
+ return {
104
+ // Can't match other platforms, but can give longer shadows to elements designed for longer shadows
105
+ elevation: spread + offsetY,
106
+ shadowColor: color // Only applies on Android >= 9 and React Native >= 0.64.0
107
+
108
+ };
109
+ } // TODO: maybe use a library or workaround to increase capabilities.
110
+ // https://github.com/telus/universal-design-system/issues/535
111
+
112
+
113
+ return {};
114
+ }
115
+
116
+ function applyIosShadow(_ref3) {
117
+ let {
118
+ inset,
119
+ color,
120
+ offsetX,
121
+ offsetY,
122
+ spread,
123
+ blur
124
+ } = _ref3;
125
+ // React Native iOS doesn't support inset shadows.
126
+ // TODO: maybe use a library or workaround for inset shadows. This is a sizable task and would
127
+ // involve changing component implementations to use a custom view, can't be done with styles alone.
128
+ // https://github.com/telus/universal-design-system/issues/535
129
+ if (inset) return {}; // React Native iOS doesn't support spread. Where there's an offset, can approximate positive spread by
130
+ // increasing the (positive or negative) offset by the spread. This makes the shadow reach the point it
131
+ // would if spread was supported (with a side effect that its start is further "under" the element).
132
+
133
+ const nudgeBySpread = offset => {
134
+ if (!offset || spread <= 0) return 0;
135
+ return offset > 0 ? spread : spread * -1;
136
+ };
137
+
138
+ return {
139
+ shadowColor: color,
140
+ shadowOffset: {
141
+ width: offsetX + nudgeBySpread(offsetX),
142
+ height: offsetY + nudgeBySpread(offsetY)
143
+ },
144
+ shadowRadius: blur,
145
+ shadowOpacity: 1 // opacity is applied via RGBA in the `color` token
146
+
147
+ };
148
+ }
149
+
150
+ const verticalAlignToFlexRowAlign = {
151
+ top: 'flex-start',
152
+ middle: 'center',
153
+ bottom: 'flex-end'
154
+ };
155
+ export function verticalAlignRow(verticalAlign) {
156
+ let reverse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
157
+ return {
158
+ flexDirection: reverse ? 'row-reverse' : 'row',
159
+ alignItems: verticalAlignToFlexRowAlign[verticalAlign]
160
+ };
161
+ }
162
+ /**
163
+ * Use on an outer container to create an outer border with an optional gap around it
164
+ * that matches the border radius of any inner border.
165
+ */
166
+
167
+ export const applyOuterBorder = _ref4 => {
168
+ let {
169
+ outerBorderColor,
170
+ outerBorderWidth = 0,
171
+ outerBorderGap = 0,
172
+ borderRadius = 0
173
+ } = _ref4;
174
+ return {
175
+ margin: 0 - outerBorderWidth - outerBorderGap,
176
+ padding: outerBorderGap,
177
+ borderRadius: borderRadius + outerBorderGap + outerBorderWidth,
178
+ borderWidth: outerBorderWidth,
179
+ borderColor: outerBorderColor
180
+ };
181
+ };
@@ -0,0 +1,163 @@
1
+ import PropTypes from 'prop-types';
2
+ import semVerSatisfies from 'semver/functions/satisfies';
3
+ import pkg from '../../../package.json';
4
+ /**
5
+ * @typedef {import('../../utils/props/variantProp.js').AppearanceSet} AppearanceSet
6
+ * @typedef {import('../../utils/props/tokens.js').TokensProp} TokensProp
7
+ * @typedef {import('../../utils/props/tokens.js').TokensSet} TokensSet
8
+ */
9
+
10
+ /**
11
+ * General utilities around working with theme tokens
12
+ */
13
+
14
+ export const getComponentTheme = (theme, componentName) => {
15
+ var _theme$metadata;
16
+
17
+ // Give clear and understandable error messages for common dev errors, for example,
18
+ // typo in component name, missing export or accessing old version of theme
19
+ if (!theme) {
20
+ throw new Error("Called useTheme's getStyle on \"".concat(componentName, "\" with no theme provided"));
21
+ }
22
+
23
+ const themeName = ((_theme$metadata = theme.metadata) === null || _theme$metadata === void 0 ? void 0 : _theme$metadata.name) || '';
24
+
25
+ if (!theme.components) {
26
+ throw new Error("Theme \"".concat(themeName, "\" has no components defined (looking for \"").concat(componentName, "\")"));
27
+ }
28
+
29
+ const componentTheme = theme.components[componentName];
30
+
31
+ if (!componentTheme) {
32
+ throw new Error("Theme \"".concat(themeName, "\" does not have styles for component \"").concat(componentName, "\""));
33
+ }
34
+
35
+ return componentTheme;
36
+ };
37
+ export const doesThemeConditionApply = (_ref, appearances) => {
38
+ var _appearances$key;
39
+
40
+ let [key, value] = _ref;
41
+ // use null rather than undefined so we can serialise the value in themes
42
+ const appearanceValue = (_appearances$key = appearances[key]) !== null && _appearances$key !== void 0 ? _appearances$key : null;
43
+ return Array.isArray(value) ? value.includes(appearanceValue) : value === appearanceValue;
44
+ };
45
+ export const doesThemeRuleApply = (rule, appearances) => Object.entries(rule.if).every(condition => doesThemeConditionApply(condition, appearances));
46
+ /**
47
+ * Turns a tokens prop (which may be either a tokens object or a tokens getter function)
48
+ * into one resolved tokens object, based on current appearances state.
49
+ *
50
+ * This is used inside `useThemeTokens` and `useThemeTokensCallback` for handling the `tokens`
51
+ * props of themed components. It may also be used directly in cases where a non-themed component
52
+ * needs to resolve a tokens prop that may be a tokens getter function or a resolved tokens object.
53
+ *
54
+ * A second tokens prop of tokens overrides may optionally be passed; it will be resolved to an object
55
+ * based on provided appearance state and then shallow-merged over the first set of tokens.
56
+ *
57
+ * @param {TokensProp} defaultTokens - a set of tokens or tokens getter function which may be overridden
58
+ * @param {AppearanceSet} [appearances] - optional appearance set to pass to tokens getter functions
59
+ * @param {TokensProp} [tokenOverrides] - optional set of tokens or tokens getter function to override the default
60
+ * @returns {TokensSet} - object containing resolved tokens with overrides applied
61
+ */
62
+
63
+ export const resolveThemeTokens = function (defaultTokens) {
64
+ let appearances = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
65
+ let tokenOverrides = arguments.length > 2 ? arguments[2] : undefined;
66
+
67
+ const resolve = tokens => typeof tokens === 'function' ? tokens(appearances) : tokens;
68
+
69
+ if (!tokenOverrides) return resolve(defaultTokens);
70
+ return Object.entries(resolve(tokenOverrides)).reduce((mergedTokens, _ref2) => {
71
+ let [tokenName, tokenValue] = _ref2;
72
+ return tokenValue === undefined ? mergedTokens : { ...mergedTokens,
73
+ [tokenName]: tokenValue
74
+ };
75
+ }, resolve(defaultTokens));
76
+ };
77
+ /**
78
+ * Gives a prop types error if a set of resolved tokens doesn't match a provided prop
79
+ * types validator.
80
+ *
81
+ * In production, this is a no-op (PropTypes.checkPropTypes is automatically replaced
82
+ * with a no-op function).
83
+ *
84
+ * @param {TokensSet} themeTokens - object containing resolved theme tokens
85
+ * @param {function} validator - PropTypes validator function
86
+ * @param {string} componentName - identifier to display in PropType error messages
87
+ */
88
+
89
+ export const validateThemeTokens = (themeTokens, validator, componentName) => {
90
+ PropTypes.checkPropTypes({
91
+ tokens: validator
92
+ }, {
93
+ tokens: themeTokens
94
+ }, 'resolved token', componentName);
95
+ return themeTokens;
96
+ };
97
+ /**
98
+ * Merges variants over states. Must be merged in that order to allow static showcases of a state,
99
+ * e.g. `<Button variant={{ pressed: true }} />` where button's pressed state is `false` by default.
100
+ * Returns an empty object if both variants and states are undefined.
101
+ *
102
+ * @param {AppearanceSet} [variants]
103
+ * @param {AppearanceSet} [states]
104
+ * @returns {AppearanceSet}
105
+ */
106
+
107
+ export const mergeAppearances = function () {
108
+ let variants = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
109
+ let states = arguments.length > 1 ? arguments[1] : undefined;
110
+ return states ? { ...states,
111
+ ...variants
112
+ } : variants;
113
+ };
114
+ /**
115
+ * Get a set of theme tokens by applying a theme's component rules based on a component's variants and state.
116
+ *
117
+ * This is used internally in `useThemeTokens` and `useThemeTokensCallback` and should not normally be called directly.
118
+ *
119
+ * @param {object} componentTheme - object containing a theme's `tokens` and `rules` for a component
120
+ * @param {object|function} [tokensProp] - UDS tokens prop containing theming overrides
121
+ * @param {object} [variants] - UDS variant prop containing the variant options for this component instance
122
+ * @param {object} [states] - object containing current state options for this component instance
123
+ * @returns {object} Set of resolved theme tokens
124
+ */
125
+
126
+ export const getThemeTokens = function (_ref3, tokensProp) {
127
+ let {
128
+ rules = [],
129
+ tokens: defaultThemeTokens = {}
130
+ } = _ref3;
131
+ let variants = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
132
+ let states = arguments.length > 3 ? arguments[3] : undefined;
133
+ const appearances = mergeAppearances(variants, states); // TODO: if in dev mode, validate the appearances and provided propTokens
134
+ // Get the theme's default tokens set and merge tokens from applicable theme rules over it
135
+
136
+ const themeTokens = rules.reduce((mergedTokens, rule) => doesThemeRuleApply(rule, appearances) ? { ...mergedTokens,
137
+ ...rule.tokens
138
+ } : mergedTokens, defaultThemeTokens);
139
+ return resolveThemeTokens(themeTokens, appearances, tokensProp);
140
+ };
141
+ export const toArray = strOrArr => Array.isArray(strOrArr) ? strOrArr : [strOrArr];
142
+ /**
143
+ * Throws an error if the theme was built with an incompatible version of @telus-uds/system-theme-tokens
144
+ *
145
+ * This is used internally by the ThemeProvider to force a fast failure when an incompatible version is detected.
146
+ *
147
+ * Version compatibility is calculated with semver.satisfies.
148
+ * Refer to https://github.com/npm/node-semver and https://semver.npmjs.com/ for details about how
149
+ * semver compatibility.
150
+ *
151
+ * @param {object} theme - UDS theme built for react-native
152
+ */
153
+
154
+ export const validateThemeTokensVersion = theme => {
155
+ var _theme$metadata2;
156
+
157
+ const expectedThemeTokensVersion = pkg.dependencies['@telus-uds/system-theme-tokens'];
158
+ const actualThemeTokensVersion = theme === null || theme === void 0 ? void 0 : (_theme$metadata2 = theme.metadata) === null || _theme$metadata2 === void 0 ? void 0 : _theme$metadata2.themeTokensVersion;
159
+
160
+ if (!semVerSatisfies(actualThemeTokensVersion, expectedThemeTokensVersion)) {
161
+ throw new Error("Invalid UDS token schema version detected.\n\nThe UDS base components ".concat(pkg.name, " v").concat(pkg.version, " are only compatible with UDS themes that are built with @telus-uds/system-theme-tokens version that is semver compatible with ").concat(expectedThemeTokensVersion, ". The current theme was built with @telus-uds/system-theme-tokens v").concat(actualThemeTokensVersion, ".\n\nIf you see this error than most likely you have attempted to install ").concat(pkg.name, " and a UDS theme manually because you are building a multi-brand application. If you are building a single brand application, consider installing the brand specific design system package such as @telus-uds/ds-allium. For more information, see https://github.com/telus/universal-design-system/blob/main/packages/docs-uds/docs/multi-brand-usage.md"));
162
+ }
163
+ };