@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
@@ -27,7 +27,7 @@ const ItemsGroup = ({
27
27
  onToggle
28
28
  }) => {
29
29
  // A SideNav control uses the same style and theme as SideNavItem, with a 'parent' variant,
30
- // plus control-specific tokens from the SideNavItemsGroup theme.
30
+ // plus control-specific tokens from the SideNavItemsGroup theme (e.g. open/close icon, etc).
31
31
  const getAppearance = appearance => ({ ...variant,
32
32
  ...appearance,
33
33
  active: isActive
@@ -35,22 +35,22 @@ const ItemsGroup = ({
35
35
 
36
36
  const getItemAppearance = appearance => ({ ...getAppearance(appearance),
37
37
  type: 'parent'
38
- }); // TODO: refactor useThemeTokensCallback return so it has the same shape as tokens prop functions
39
- // https://github.com/telus/universal-design-system/issues/588
38
+ });
40
39
 
40
+ const getGroupTokens = useThemeTokensCallback('SideNavItemsGroup', tokens, variant);
41
41
 
42
- const getGroupTokens = useThemeTokensCallback('SideNavItemsGroup');
42
+ const getPanelTokens = appearance => selectTokens('ExpandCollapsePanel', getGroupTokens(getAppearance(appearance)));
43
43
 
44
- const panelTokensGetterProp = appearance => selectTokens('ExpandCollapsePanel', getGroupTokens(tokens, getAppearance(appearance)));
44
+ const getItemTokens = useThemeTokensCallback('SideNavItem', itemTokens, variant);
45
45
 
46
- const getItemTokens = useThemeTokensCallback('SideNavItem');
46
+ const getControlTokens = appearance => selectTokens('ExpandCollapseControl', { ...getItemTokens(getItemAppearance(appearance)),
47
+ // main style from SideNavItem
48
+ ...getGroupTokens(getAppearance(appearance)) // control-specific tokens like icon etc
47
49
 
48
- const controlTokensGetterProp = appearance => selectTokens('ExpandCollapseControl', { ...getItemTokens(itemTokens, getItemAppearance(appearance)),
49
- ...getGroupTokens(tokens, getAppearance(appearance))
50
50
  });
51
51
 
52
52
  const controlContent = controlState => {
53
- const currentItemTokens = getItemTokens(itemTokens, getItemAppearance(controlState));
53
+ const currentItemTokens = getItemTokens(getItemAppearance(controlState));
54
54
  return /*#__PURE__*/React.createElement(ItemContent, {
55
55
  tokens: currentItemTokens
56
56
  }, label);
@@ -60,8 +60,8 @@ const ItemsGroup = ({
60
60
  openIds: openGroups,
61
61
  panelId: groupId,
62
62
  onToggle: onToggle,
63
- tokens: panelTokensGetterProp,
64
- controlTokens: controlTokensGetterProp,
63
+ tokens: getPanelTokens,
64
+ controlTokens: getControlTokens,
65
65
  control: controlContent,
66
66
  accessibilityState: {
67
67
  active: isActive
@@ -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, { useState } from 'react';
4
2
  import PropTypes from 'prop-types';
5
3
  import ExpandCollapse from '../ExpandCollapse';
@@ -65,7 +63,7 @@ const SideNav = ({
65
63
  if (onPress) onPress(...args);
66
64
  };
67
65
 
68
- return /*#__PURE__*/React.createElement(Item, _extends({}, item.props, {
66
+ return /*#__PURE__*/React.createElement(Item, Object.assign({}, item.props, {
69
67
  key: itemId,
70
68
  itemId: itemId,
71
69
  groupId: groupId,
@@ -87,7 +85,7 @@ const SideNav = ({
87
85
  groupId = `group-${index}`
88
86
  } = child.props;
89
87
  const isGroupActive = active.itemId !== undefined && active.groupId === groupId;
90
- return /*#__PURE__*/React.createElement(ItemsGroup, _extends({}, child.props, {
88
+ return /*#__PURE__*/React.createElement(ItemsGroup, Object.assign({}, child.props, {
91
89
  key: groupId,
92
90
  groupId: groupId,
93
91
  variant: variant,
@@ -0,0 +1,98 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { StyleSheet, View } from 'react-native';
4
+ import { useSpacingScale, spacingProps } from '../utils';
5
+ /**
6
+ * @typedef {import('../utils/propTypes.js').SpacingValue} SpacingValue
7
+ * @typedef {import('../utils/propTypes.js').SpacingObject} SpacingObject
8
+ */
9
+
10
+ const selectSizeStyle = (size, direction) => ({
11
+ // Only apply space in one direction at a time, else large spacers will increase the
12
+ // 'across' size of the parent and mess up siblings with styles like `alignItems: stretch`.
13
+ [direction === 'row' ? 'width' : 'height']: size
14
+ });
15
+ /**
16
+ * An empty element used to create a gap between components that is sized according to the theme spacing scale,
17
+ * passing its `space` prop ({@link SpacingValue}) to `useSpacingScale`.
18
+ *
19
+ * ## Simple spacing
20
+ *
21
+ * For most simple uses, pass a number to Spacer's `space` prop referring to an index in the theme's
22
+ * spacing scale. For example, for a spacer of the theme's smallest non-zero spacing size:
23
+ *
24
+ * ```jsx
25
+ * <Spacer space={1} />
26
+ * ```
27
+ *
28
+ * ## Responsive spacing
29
+ *
30
+ * Different spacing scale sizes may be chosen for different viewports by passing `space` a {@link SpacingObject}
31
+ * using keys for the viewports at and above which certain spacing indexes should apply.
32
+ *
33
+ * This can be useful when a Spacer is inside a layout with a shape that changes between viewports.
34
+ *
35
+ * For example, a spacer might need no width below 'md' viewport because the items it separates will be on separate lines,
36
+ * but may need a visible size above that viewport, and a wider size at the highest viewport:
37
+ *
38
+ * ```jsx
39
+ * <Spacer space={{ xs: 0, md: 2, xl: 3 }} />
40
+ * ```
41
+ *
42
+ * Note that themes may also define viewport-specific behaviour for indexes on the spacing scale. These viewport
43
+ * props should only be used where necessary to achieve a certain responsive layoutm and shouldn't be used to replace
44
+ * or deviate from responsive spacing behaviour in a theme that is intended to apply universally.
45
+ *
46
+ * ## More options
47
+ *
48
+ * See `useSpacingScale` hook for more options that may be used with the `space` prop.
49
+ *
50
+ * ## Accessibility
51
+ *
52
+ * Spacer has no content and is ignored by tools such as screen readers. Use `Divider` for
53
+ * separations between elements that may be treated as semantically meaningful on web.
54
+ */
55
+
56
+
57
+ const Spacer = ({
58
+ space = 1,
59
+ direction = 'column',
60
+ testID
61
+ }) => {
62
+ const size = useSpacingScale(space);
63
+ const sizeStyle = selectSizeStyle(size, direction);
64
+ return /*#__PURE__*/React.createElement(View, {
65
+ testID: testID,
66
+ style: [staticStyles.stretch, sizeStyle]
67
+ });
68
+ };
69
+
70
+ Spacer.propTypes = {
71
+ /**
72
+ * The size of the spacer according to the theme's spacing scale.
73
+ * Either a number corresponding to a position on the theme's spacing scale (1 is smallest, 2 is second smallest, etc),
74
+ * or, a SpacingObject with viewport keys and options (see `useSpacingScale` for details).
75
+ */
76
+ space: spacingProps.types.spacingValue,
77
+
78
+ /**
79
+ * The spacer applies space in only one direction, which is controlled by the `direction` prop:
80
+ *
81
+ * - `'column'` (default) applies space vertically; has a fixed height and not width.
82
+ * - `'row'` applies space horizontally; has a fixed width and not height.
83
+ */
84
+ direction: PropTypes.oneOf(['column', 'row']),
85
+
86
+ /**
87
+ * @ignore
88
+ * In tests, the only way to select Spacers is with testID prop and getByTestId, since they have no content,
89
+ * no accessibility role, and there is no equivalent of nextSibling in React Native Testing Library.
90
+ */
91
+ testID: PropTypes.string
92
+ };
93
+ const staticStyles = StyleSheet.create({
94
+ stretch: {
95
+ alignSelf: 'stretch'
96
+ }
97
+ });
98
+ export default Spacer;
@@ -0,0 +1,2 @@
1
+ import Spacer from './Spacer';
2
+ export default Spacer;
@@ -0,0 +1,105 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { View } from 'react-native';
4
+ import Divider from '../Divider';
5
+ import { spacingProps, a11yProps } from '../utils';
6
+ import { useThemeTokens } from '../ThemeProvider';
7
+ import getStackedContent from './getStackedContent';
8
+ import { staticStyles, selectFlexStyles } from './common';
9
+ /**
10
+ * StackView evenly spaces its children, by default placing `Spacer` components between them.
11
+ *
12
+ * ## Spacing
13
+ *
14
+ * StackView takes the same spacing props as `Spacer` and passes them directly to the layout components
15
+ * it uses for spacing.
16
+ *
17
+ * ## Direction
18
+ *
19
+ * Use `direction` to control how the children are stacked:
20
+ *
21
+ * - `'column'` (default) stacks and spaces the children vertically.
22
+ * - `'row'` stacks and spaces the children horizontally.
23
+ * - `'column-reverse'` acts the same as `'column'`, but reverses the direction.
24
+ * - `'row-reverse'` acts the same as `'row'`, but reverses the direction.
25
+ *
26
+ * ## Theming, alignment and justification
27
+ *
28
+ * `StackView` is a pure layout component and has no theme styles beyond a small set of layout styles
29
+ * (`alignItems`, `justifyContent`, `flexGrow`) which may be set using the `tokens` prop.
30
+ *
31
+ * All other styling such as borders, background colours, padding etc should be applied by wrapping
32
+ * the `StackView` in other components. For example, a `StackView` may be the child of a `Card`.
33
+ *
34
+ * ## Separator options
35
+ *
36
+ * By default, StackView inserts a `<Spacer>` component between each rendered child.
37
+ *
38
+ * With the `divider` prop, it instead renders a `Divider` instead of a `Spacer`. The gap between
39
+ * items will be double what it would be without this prop plus the width of the divider.
40
+ *
41
+ * ## Accessibility
42
+ *
43
+ * `StackView` may take any React Native accessibility props and applies them to the outer `View`.
44
+ * It applies no accessibility props of its own, unless `divider` prop is passed (`Divider` has a
45
+ * semantic role but only on web, not within native apps).
46
+ */
47
+
48
+ const StackView = ({
49
+ space = 1,
50
+ divider,
51
+ direction = 'column',
52
+ inherit,
53
+ children,
54
+ variant,
55
+ tokens,
56
+ ...rest
57
+ }) => {
58
+ const a11y = a11yProps.select({ ...rest
59
+ });
60
+ const content = getStackedContent(children, {
61
+ direction,
62
+ divider,
63
+ space
64
+ });
65
+ const themeTokens = useThemeTokens('StackView', tokens, variant);
66
+ const flexStyles = selectFlexStyles(themeTokens);
67
+ return inherit ? content : /*#__PURE__*/React.createElement(View, Object.assign({}, a11y, {
68
+ style: [flexStyles, staticStyles[direction]]
69
+ }), content);
70
+ };
71
+
72
+ StackView.propTypes = { ...a11yProps.propTypes,
73
+
74
+ /**
75
+ * Sets the `flexDirection` of the container and, if `divider` is used, gives the Divider the appropriate direction.
76
+ */
77
+ direction: PropTypes.oneOf(['column', 'row', 'column-reverse', 'row-reverse']),
78
+
79
+ /**
80
+ * If true, renders a UDS `Divider` component between each item. If an object is passed,
81
+ * this object is passes as props to each Divider.
82
+ */
83
+ divider: PropTypes.oneOfType([PropTypes.bool, PropTypes.shape(Divider.propTypes)]),
84
+
85
+ /**
86
+ * If true, does not render any container and returns an array of spaced children, inheriting
87
+ * flex styles from the StackView's parent. That parent must have a `flexDirection` style corresponding
88
+ * to the StackView's `direction` prop for `divider` prop to behave correctly.
89
+ */
90
+ inherit: PropTypes.bool,
91
+
92
+ /**
93
+ * The size of the spacer according to the theme's spacing scale.
94
+ * Either a number corresponding to a position on the theme's spacing scale (1 is smallest, 2 is second smallest, etc),
95
+ * or, a SpacingObject with viewport keys and options (see `useSpacingScale` for details).
96
+ */
97
+ space: spacingProps.types.spacingValue,
98
+
99
+ /**
100
+ * A StackView may take any children, but will have no effect if it is only passed one child or is passed children
101
+ * wrapped in a component. If necessary, children may be wrapped in one React Fragment.
102
+ */
103
+ children: PropTypes.node
104
+ };
105
+ export default StackView;
@@ -0,0 +1,32 @@
1
+ import React from 'react';
2
+ import StackWrapBox from './StackWrapBox';
3
+ import StackWrapGap from './StackWrapGap'; // CSS.supports needs an example of the type of value you intend to use.
4
+ // Will be an integer appended `px` after hooks and JSX styles are resolved.
5
+
6
+ const exampleGapValue = '1px';
7
+ /**
8
+ * StackWrap is an alternative to StackView where the spaced items are allowed to wrap.
9
+ *
10
+ * As well as providing space between the items, StackWrap provides `gap` space between
11
+ * wrapped lines of items. By default, this `gap` is the same as the gap between spaced items.
12
+ * If a different spacing is desired between wrapped lines, pass a spacing value to the `gap` prop.
13
+ */
14
+
15
+ const StackWrap = props => {
16
+ const {
17
+ space
18
+ } = props; // Don't apply separate gap if `null` or `undefined`, so can be unset in Storybook etc
19
+ // eslint-disable-next-line react/destructuring-assignment
20
+
21
+ const gap = props.gap ?? space;
22
+ return gap === space && CSS?.supports('gap', exampleGapValue) ?
23
+ /*#__PURE__*/
24
+ // If possible, use the cleaner implementation that applies CSS `gap` styles to the container.
25
+ React.createElement(StackWrapGap, props) :
26
+ /*#__PURE__*/
27
+ // Else, use the fallback implementation which renders a `Box` component around each child.
28
+ React.createElement(StackWrapBox, props);
29
+ };
30
+
31
+ StackWrap.propTypes = StackWrapBox.propTypes;
32
+ export default StackWrap;
@@ -0,0 +1,3 @@
1
+ import StackWrapBox from './StackWrapBox'; // No support for CSS `gap` on React Native, so always use the fallback "box" implementation.
2
+
3
+ export default StackWrapBox;
@@ -0,0 +1,85 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { View } from 'react-native';
4
+ import { spacingProps, a11yProps, useSpacingScale } from '../utils';
5
+ import { useThemeTokens } from '../ThemeProvider';
6
+ import getStackedContent from './getStackedContent';
7
+ import { staticStyles, selectFlexStyles } from './common';
8
+ const offsetSides = {
9
+ row: 'marginBottom',
10
+ column: 'marginRight'
11
+ };
12
+ const gapSides = {
13
+ row: 'bottom',
14
+ column: 'right'
15
+ };
16
+ const spaceSides = {
17
+ row: 'right',
18
+ column: 'bottom'
19
+ };
20
+ /**
21
+ * An alternative fallback implementation of `StackWrap` that doesn't use CSS `gap`.
22
+ *
23
+ * This is not intended to be used directly. It will automatically be used instead of StackWrap in:
24
+ *
25
+ * - Native apps (no `gap` style prop in React Native).
26
+ * - Older (~<=2020) browsers which don't support CSS `gap`.
27
+ * - Cases where the `space` between items should be different to the `gap` between wrapped rows .
28
+ *
29
+ * Unlike the cleaner and more side-effect-free CSS gap StackWrap implementation, this renders a Box (View)
30
+ * between the container and the stacked children, and requires a negative margin on the container.
31
+ */
32
+
33
+ const StackWrapBox = ({
34
+ space = 1,
35
+ gap = space,
36
+ direction = 'row',
37
+ children,
38
+ tokens,
39
+ variant,
40
+ ...rest
41
+ }) => {
42
+ const themeTokens = useThemeTokens('StackView', tokens, variant);
43
+ const flexStyles = selectFlexStyles(themeTokens);
44
+ const a11y = a11yProps.select({ ...rest
45
+ }); // Mimic CSS `gap` using box spacing on the side after a wrapped row then offsetting it on the last row.
46
+
47
+ const gapSize = useSpacingScale(gap);
48
+ const offsetStyle = {
49
+ [offsetSides[direction]]: -1 * gapSize
50
+ };
51
+ const boxProps = {
52
+ [gapSides[direction]]: gap,
53
+ [spaceSides[direction]]: space
54
+ };
55
+ const content = getStackedContent(children, {
56
+ direction,
57
+ space: 0,
58
+ box: boxProps
59
+ });
60
+ return /*#__PURE__*/React.createElement(View, Object.assign({}, a11y, {
61
+ style: [flexStyles, staticStyles.wrap, staticStyles[direction], offsetStyle]
62
+ }), content);
63
+ };
64
+
65
+ StackWrapBox.propTypes = { ...a11yProps.propTypes,
66
+
67
+ /**
68
+ * Sets the `flexDirection` of the container, or if 'inherit' returns spaced children as an array.
69
+ */
70
+ direction: PropTypes.oneOf(['column', 'row']),
71
+
72
+ /**
73
+ * The size of the spacer according to the theme's spacing scale.
74
+ * Either a number corresponding to a position on the theme's spacing scale (1 is smallest, 2 is second smallest, etc),
75
+ * or, a SpacingObject with viewport keys and options (see `useSpacingScale` for details).
76
+ */
77
+ space: spacingProps.types.spacingValue,
78
+
79
+ /**
80
+ * A StackWrap may take any children, but will have no effect if it is only passed one child or is passed children
81
+ * wrapped in a component. If necessary, children may be wrapped in one React Fragment.
82
+ */
83
+ children: PropTypes.node
84
+ };
85
+ export default StackWrapBox;
@@ -0,0 +1,45 @@
1
+ import React from 'react';
2
+ import { View } from 'react-native';
3
+ import StackWrapBox from './StackWrapBox';
4
+ import { a11yProps, useSpacingScale } from '../utils';
5
+ import { useThemeTokens } from '../ThemeProvider';
6
+ import getStackedContent from './getStackedContent';
7
+ import { staticStyles, selectFlexStyles } from './common';
8
+ /**
9
+ * The primary implementation of StackWrap.
10
+ *
11
+ * This is not intended to be used directly. It will automatically be used instead of StackWrap if:
12
+ *
13
+ * - On web, and,
14
+ * - On a browser that supports CSS `gap` (most browser versions from 2020 onwards), and,
15
+ * - The `space` between items is the same as the `gap` between wrapped rows (this is the
16
+ * default if `gap` prop is undefined)
17
+ */
18
+
19
+ const StackWrapGap = ({
20
+ space = 1,
21
+ tokens,
22
+ variant,
23
+ direction = 'row',
24
+ children,
25
+ ...rest
26
+ }) => {
27
+ const themeTokens = useThemeTokens('StackView', tokens, variant);
28
+ const flexStyles = selectFlexStyles(themeTokens);
29
+ const a11y = a11yProps.select({ ...rest
30
+ });
31
+ const size = useSpacingScale(space);
32
+ const gapStyle = {
33
+ gap: size
34
+ };
35
+ const content = getStackedContent(children, {
36
+ direction,
37
+ space: 0
38
+ });
39
+ return /*#__PURE__*/React.createElement(View, Object.assign({}, a11y, {
40
+ style: [flexStyles, staticStyles.wrap, staticStyles[direction], gapStyle]
41
+ }), content);
42
+ };
43
+
44
+ StackWrapGap.propTypes = StackWrapBox.propTypes;
45
+ export default StackWrapGap;
@@ -0,0 +1,30 @@
1
+ import { StyleSheet } from 'react-native';
2
+ export const selectFlexStyles = ({
3
+ alignItems,
4
+ justifyContent,
5
+ flexGrow
6
+ }) => ({
7
+ alignItems,
8
+ justifyContent,
9
+ flexGrow
10
+ });
11
+ export const staticStyles = StyleSheet.create({
12
+ wrap: {
13
+ flexShrink: 1,
14
+ flexWrap: 'wrap'
15
+ },
16
+ row: {
17
+ flexDirection: 'row'
18
+ },
19
+ 'row-reverse': {
20
+ flexDirection: 'row-reverse'
21
+ },
22
+ column: {
23
+ // This is the React Native View default, but also it's very fundamental to the behaviour,
24
+ // so make it explicit and allow it to be tested for.
25
+ flexDirection: 'column'
26
+ },
27
+ 'column-reverse': {
28
+ flexDirection: 'column-reverse'
29
+ }
30
+ });
@@ -0,0 +1,111 @@
1
+ import React, { Children, Fragment } from 'react';
2
+ import Box from '../Box';
3
+ import Divider from '../Divider';
4
+ import Spacer from '../Spacer';
5
+ /**
6
+ * @typedef {import('react').ReactChildren} ReactChildren
7
+ * @typedef {import('react').ReactElement} ReactElement
8
+ * @typedef {import('../utils/propTypes.js').SpacingValue} SpacingValue
9
+ */
10
+
11
+ /**
12
+ * Takes valid React Children and inserts a specified amount of space between each valid (not nullish) top
13
+ * level element, according to the provided `options`.
14
+ *
15
+ * Returns an array of the original elements plus inserted spacing elements.
16
+ * This array of elements is keyed and may be used as a component's `children` without mapping.
17
+ *
18
+ * @param {ReactChildren} children
19
+ * @param {object} [options] -
20
+ * - `space`: amount of space to insert using the theme spacing scale (see `useSpacingScale`)
21
+ * - `direction`: if 'row' or 'row-reverse', applies space horizontally, if 'column' or 'column-reverse', applies space vertically
22
+ * - `box`: if truthy, wraps each valid child in a Box component; if an object, passes it to Box as props
23
+ * - `divider`: if truthy, inserts Divider components; if an object, passes it to Divider as props
24
+ * @param {SpacingValue} [options.space]
25
+ * @param {boolean | object} [options.divider]
26
+ * @param {"row" | "column" | "row-reverse" | "column-reverse"} [options.direction]
27
+ * @returns {ReactElement[]}
28
+ */
29
+
30
+ const getStackedContent = (children, {
31
+ divider,
32
+ space,
33
+ direction = 'column',
34
+ box
35
+ }) => {
36
+ const boxProps = box && typeof box === 'object' ? box : {
37
+ space
38
+ };
39
+ const dividerProps = divider && typeof divider === 'object' ? divider : {}; // Avoid surprises if children contains comments, nulls, or is a variable wrapped as a fragment
40
+
41
+ const validChildren = Children.toArray(unpackFragment(children)).filter(Boolean);
42
+ const content = validChildren.reduce((newChildren, child, index) => {
43
+ const boxID = `Stack-Box-${index}`;
44
+ const item = box ?
45
+ /*#__PURE__*/
46
+ // If wrapped in Box, that Box needs a key.
47
+ // If possible, use an existing content key; use an index-based key only if necessary.
48
+ React.createElement(Box, Object.assign({}, boxProps, {
49
+ key: child.key || boxID,
50
+ testID: boxID
51
+ }), child) : child;
52
+ if (!index || !space && !divider) return [...newChildren, item];
53
+ const testID = `Stack-${divider ? 'Divider' : 'Spacer'}-${index}`;
54
+ const commonProps = {
55
+ testID,
56
+ key: testID,
57
+ space
58
+ };
59
+ const separator = divider ? /*#__PURE__*/React.createElement(Divider, Object.assign({
60
+ vertical: direction.startsWith('row')
61
+ }, dividerProps, commonProps)) : /*#__PURE__*/React.createElement(Spacer, Object.assign({
62
+ direction: direction.startsWith('row') ? 'row' : 'column'
63
+ }, commonProps));
64
+ return [...newChildren, separator, item];
65
+ }, []);
66
+ return content;
67
+ };
68
+ /**
69
+ * Unpacks top-level fragments, so that common compositional patterns such as the following examples
70
+ * can be iterated as flat siblings (as if they were `<Child1 /><Child2 /><Child3 />`):
71
+ *
72
+ * - Setting `children` as a property wrapped in a fragment:
73
+ *
74
+ * ```jsx
75
+ * args.children = <><Child1 /><Child2 /><Child3 /></>
76
+ * ```
77
+ *
78
+ * - Defining `children` as a variable wrapped in a fragment:
79
+ *
80
+ * ```jsx
81
+ * const content = <><Child1 /><Child2 /><Child3 /></>
82
+ * if (someCondition) return <SomeWrapper>{content}</SomeWrapper>
83
+ * ```
84
+ *
85
+ * - Using fragments at the top level of a JSX block for conditional rendering:
86
+ *
87
+ * ```jsx
88
+ * <Child1 />
89
+ * {someCondition && (
90
+ * <>
91
+ * <Child2 />
92
+ * <Child3 />
93
+ * </>
94
+ * )}
95
+ * ```
96
+ *
97
+ * @param {ReactChildren} child
98
+ * @returns {ReactChildren}
99
+ */
100
+
101
+
102
+ const unpackFragment = child => {
103
+ // If this level is a set of top-level siblings rather than one child, check each in turn
104
+ if (Children.count(child) > 1) return Children.map(child, unpackFragment); // When a fragment is found, unpack its children to the top level and check them
105
+
106
+ if (child?.type === Fragment) return unpackFragment(child.props?.children); // Stop unpacking as soon as any non-fragment child is found
107
+
108
+ return child;
109
+ };
110
+
111
+ export default getStackedContent;
@@ -0,0 +1,5 @@
1
+ import StackView from './StackView';
2
+ import StackWrap from './StackWrap';
3
+ export default StackView;
4
+ export { StackWrap };
5
+ export { default as getStackedContent } from './getStackedContent';