@telus-uds/components-base 0.0.2-prerelease.4 → 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.
- package/CHANGELOG.md +17 -0
- package/__fixtures__/testTheme.js +83 -11
- package/__tests__/Divider/Divider.test.jsx +26 -5
- package/__tests__/Feedback/Feedback.test.jsx +42 -0
- package/__tests__/Spacer/Spacer.test.jsx +63 -0
- package/__tests__/StackView/StackView.test.jsx +242 -0
- package/__tests__/StackView/StackWrap.test.jsx +47 -0
- package/__tests__/StackView/getStackedContent.test.jsx +295 -0
- package/__tests__/TextInput/TextInput.test.jsx +146 -0
- package/__tests__/utils/useUniqueId.test.js +31 -0
- package/lib/Box/Box.js +7 -2
- package/lib/Button/ButtonBase.js +6 -16
- package/lib/Button/ButtonGroup.js +13 -22
- package/lib/Divider/Divider.js +40 -2
- package/lib/Feedback/Feedback.js +110 -0
- package/lib/Feedback/index.js +2 -0
- package/lib/Icon/Icon.js +1 -1
- package/lib/InputLabel/InputLabel.js +86 -0
- package/lib/InputLabel/LabelContent.native.js +8 -0
- package/lib/InputLabel/LabelContent.web.js +17 -0
- package/lib/InputLabel/index.js +2 -0
- package/lib/Link/LinkBase.js +9 -3
- package/lib/Spacer/Spacer.js +98 -0
- package/lib/Spacer/index.js +2 -0
- package/lib/StackView/StackView.js +105 -0
- package/lib/StackView/StackWrap.js +32 -0
- package/lib/StackView/StackWrap.native.js +3 -0
- package/lib/StackView/StackWrapBox.js +85 -0
- package/lib/StackView/StackWrapGap.js +45 -0
- package/lib/StackView/common.js +30 -0
- package/lib/StackView/getStackedContent.js +111 -0
- package/lib/StackView/index.js +5 -0
- package/lib/TextInput/TextInput.js +337 -0
- package/lib/TextInput/index.js +2 -0
- package/lib/Typography/Typography.js +0 -4
- package/lib/index.js +6 -1
- package/lib/utils/input.js +3 -1
- package/lib/utils/propTypes.js +9 -1
- package/lib/utils/useUniqueId.js +12 -0
- package/package.json +2 -2
- package/release-context.json +4 -4
- package/src/Box/Box.jsx +4 -2
- package/src/Button/ButtonBase.jsx +6 -18
- package/src/Button/ButtonGroup.jsx +13 -17
- package/src/Divider/Divider.jsx +38 -3
- package/src/Feedback/Feedback.jsx +99 -0
- package/src/Feedback/index.js +3 -0
- package/src/Icon/Icon.jsx +2 -1
- package/src/InputLabel/InputLabel.jsx +99 -0
- package/src/InputLabel/LabelContent.native.jsx +6 -0
- package/src/InputLabel/LabelContent.web.jsx +13 -0
- package/src/InputLabel/index.js +3 -0
- package/src/Link/LinkBase.jsx +9 -3
- package/src/Spacer/Spacer.jsx +91 -0
- package/src/Spacer/index.js +3 -0
- package/src/StackView/StackView.jsx +103 -0
- package/src/StackView/StackWrap.jsx +33 -0
- package/src/StackView/StackWrap.native.jsx +4 -0
- package/src/StackView/StackWrapBox.jsx +82 -0
- package/src/StackView/StackWrapGap.jsx +39 -0
- package/src/StackView/common.jsx +28 -0
- package/src/StackView/getStackedContent.jsx +106 -0
- package/src/StackView/index.js +6 -0
- package/src/TextInput/TextInput.jsx +325 -0
- package/src/TextInput/index.js +3 -0
- package/src/Typography/Typography.jsx +0 -4
- package/src/index.js +6 -1
- package/src/utils/input.js +2 -1
- package/src/utils/propTypes.js +9 -0
- package/src/utils/useUniqueId.js +14 -0
- package/stories/A11yText/A11yText.stories.jsx +11 -5
- package/stories/ActivityIndicator/ActivityIndicator.stories.jsx +11 -2
- package/stories/Box/Box.stories.jsx +29 -2
- package/stories/Button/Button.stories.jsx +17 -21
- package/stories/Button/ButtonGroup.stories.jsx +2 -1
- package/stories/Button/ButtonLink.stories.jsx +6 -4
- package/stories/Card/Card.stories.jsx +13 -1
- package/stories/Divider/Divider.stories.jsx +26 -2
- package/stories/ExpandCollapse/ExpandCollapse.stories.jsx +74 -79
- package/stories/Feedback/Feedback.stories.jsx +97 -0
- package/stories/FlexGrid/01 FlexGrid.stories.jsx +20 -7
- package/stories/Icon/Icon.stories.jsx +11 -3
- package/stories/InputLabel/InputLabel.stories.jsx +37 -0
- package/stories/Link/ChevronLink.stories.jsx +20 -4
- package/stories/Link/Link.stories.jsx +24 -2
- package/stories/Link/TextButton.stories.jsx +24 -2
- package/stories/Pagination/Pagination.stories.jsx +28 -14
- package/stories/SideNav/SideNav.stories.jsx +17 -2
- package/stories/Spacer/Spacer.stories.jsx +33 -0
- package/stories/StackView/StackView.stories.jsx +65 -0
- package/stories/StackView/StackWrap.stories.jsx +52 -0
- package/stories/TextInput/TextInput.stories.jsx +103 -0
- package/stories/ToggleSwitch/ToggleSwitch.stories.jsx +16 -3
- package/stories/Typography/Typography.stories.jsx +12 -3
- package/stories/platform-supports.web.jsx +1 -1
- package/stories/supports.jsx +109 -13
package/lib/Divider/Divider.js
CHANGED
|
@@ -2,7 +2,13 @@ import React from 'react';
|
|
|
2
2
|
import PropTypes from 'prop-types';
|
|
3
3
|
import { View, StyleSheet, Platform } from 'react-native';
|
|
4
4
|
import { useThemeTokens } from '../ThemeProvider';
|
|
5
|
-
import
|
|
5
|
+
import Spacer from '../Spacer';
|
|
6
|
+
import { getTokensPropType, variantProp, spacingProps } from '../utils';
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {import('../utils/propTypes.js').SpacingIndex} SpacingIndex
|
|
9
|
+
* @typedef {import('../utils/propTypes.js').SpacingObject} SpacingObject
|
|
10
|
+
*/
|
|
11
|
+
|
|
6
12
|
/**
|
|
7
13
|
* A basic divider component, horizontal by default. Color and thickness can be controlled by theme.
|
|
8
14
|
* Divider is based on the flexbox model and uses `alignSelf: flex` to span its flex parent.
|
|
@@ -15,6 +21,26 @@ import { getTokensPropType, variantProp } from '../utils/propTypes';
|
|
|
15
21
|
*
|
|
16
22
|
* In a flexbox row, vertical dividers will automatically size to their parent height.
|
|
17
23
|
*
|
|
24
|
+
* ## Space
|
|
25
|
+
*
|
|
26
|
+
* Use this to create **space either side of the divider**. For simple cases, pass a number referring to
|
|
27
|
+
* a position on the theme's spacing scale; for example, this will put a Spacer of the smallest supported
|
|
28
|
+
* size either side of the divider:
|
|
29
|
+
*
|
|
30
|
+
* ```jsx
|
|
31
|
+
* <Divider space={1} />
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* `space` prop uses `useSpacingScale` and may accept a {@link SpacingObject} or a {@link SpacingIndex} number.
|
|
35
|
+
*
|
|
36
|
+
* To **reduce the length of a divider** as well as creating space between it and its neighbours, wrap it in
|
|
37
|
+
* a `Box` component. For example, this will have the second-smallest theme-supported spacing value between
|
|
38
|
+
* the dividing line and its neighbours, and will shorten the line at either end by the same amount:
|
|
39
|
+
*
|
|
40
|
+
* ```jsx
|
|
41
|
+
* <Box space={2}><Divider /></Box>
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
18
44
|
* ## Accessibility
|
|
19
45
|
*
|
|
20
46
|
* For accessibility purposes a divider component will be described with ARIA attributes, i.e. `role="separator"` and `aria-orientation="vertical/horizontal"`.
|
|
@@ -23,6 +49,7 @@ import { getTokensPropType, variantProp } from '../utils/propTypes';
|
|
|
23
49
|
const Divider = ({
|
|
24
50
|
variant,
|
|
25
51
|
vertical = false,
|
|
52
|
+
space,
|
|
26
53
|
tokens,
|
|
27
54
|
testID
|
|
28
55
|
}) => {
|
|
@@ -43,14 +70,25 @@ const Divider = ({
|
|
|
43
70
|
accessibilityRole: 'separator'
|
|
44
71
|
} : // There are no such equivalent attributes for native
|
|
45
72
|
{};
|
|
46
|
-
|
|
73
|
+
const divider = /*#__PURE__*/React.createElement(View, Object.assign({
|
|
47
74
|
style: [styles.divider, borderStyles],
|
|
48
75
|
testID: testID
|
|
49
76
|
}, a11y));
|
|
77
|
+
if (!space) return divider;
|
|
78
|
+
const spacerProps = {
|
|
79
|
+
space,
|
|
80
|
+
direction: vertical ? 'row' : 'column'
|
|
81
|
+
};
|
|
82
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Spacer, Object.assign({}, spacerProps, {
|
|
83
|
+
testID: testID ? `${testID}-Spacer-before` : undefined
|
|
84
|
+
})), divider, /*#__PURE__*/React.createElement(Spacer, Object.assign({}, spacerProps, {
|
|
85
|
+
testID: testID ? `${testID}-Spacer-after` : undefined
|
|
86
|
+
})));
|
|
50
87
|
};
|
|
51
88
|
|
|
52
89
|
Divider.propTypes = {
|
|
53
90
|
tokens: getTokensPropType('Divider'),
|
|
91
|
+
space: spacingProps.types.spacingValue,
|
|
54
92
|
variant: variantProp.propType,
|
|
55
93
|
|
|
56
94
|
/**
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { Text, View, StyleSheet } from 'react-native';
|
|
3
|
+
import PropTypes from 'prop-types';
|
|
4
|
+
import { applyTextStyles, useThemeTokens } from '../ThemeProvider';
|
|
5
|
+
import { a11yProps, getTokensPropType, selectTokens, variantProp } from '../utils';
|
|
6
|
+
|
|
7
|
+
const selectStyles = tokens => selectTokens('Feedback', tokens);
|
|
8
|
+
|
|
9
|
+
const selectTitleTextStyles = ({
|
|
10
|
+
titleFontSize,
|
|
11
|
+
...tokens
|
|
12
|
+
}) => applyTextStyles(selectTokens('Typography', { ...tokens,
|
|
13
|
+
fontSize: titleFontSize
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
const selectContentTextStyles = ({
|
|
17
|
+
contentFontSize,
|
|
18
|
+
...tokens
|
|
19
|
+
}) => applyTextStyles(selectTokens('Typography', { ...tokens,
|
|
20
|
+
fontSize: contentFontSize
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
const selectIconTokens = ({
|
|
24
|
+
iconSize,
|
|
25
|
+
iconColor
|
|
26
|
+
}) => ({
|
|
27
|
+
size: iconSize,
|
|
28
|
+
color: iconColor
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const selectIconContainerStyles = ({
|
|
32
|
+
iconGap
|
|
33
|
+
}) => ({
|
|
34
|
+
paddingRight: iconGap
|
|
35
|
+
});
|
|
36
|
+
/**
|
|
37
|
+
* A feedback box commonly used with form fields.
|
|
38
|
+
*
|
|
39
|
+
* ### Standalone usage
|
|
40
|
+
* While its primary use is to facilitate feedback states for other form components such as `TextInput`,
|
|
41
|
+
* you may use it standalone.
|
|
42
|
+
*
|
|
43
|
+
* ### Complex content
|
|
44
|
+
* You may pass any React tree as the children of this component, bear in mind that a render function
|
|
45
|
+
* is better suited for styling children based on Feedback's variant.
|
|
46
|
+
*
|
|
47
|
+
* ### Using a render function
|
|
48
|
+
* When a function is passed for rendering content, it will receive the feedback text styles and
|
|
49
|
+
* variant as arguments.
|
|
50
|
+
*
|
|
51
|
+
* ### Accessibility
|
|
52
|
+
* All accessibility props set on this component will be applied to the outer container.
|
|
53
|
+
*/
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
const Feedback = ({
|
|
57
|
+
title,
|
|
58
|
+
children,
|
|
59
|
+
tokens,
|
|
60
|
+
variant,
|
|
61
|
+
...rest
|
|
62
|
+
}) => {
|
|
63
|
+
const themeTokens = useThemeTokens('Feedback', tokens, variant);
|
|
64
|
+
const {
|
|
65
|
+
icon: IconComponent
|
|
66
|
+
} = themeTokens;
|
|
67
|
+
const titleTextStyles = selectTitleTextStyles(themeTokens);
|
|
68
|
+
const contentTextStyles = selectContentTextStyles(themeTokens);
|
|
69
|
+
const content = typeof children === 'string' ? /*#__PURE__*/React.createElement(Text, {
|
|
70
|
+
style: contentTextStyles
|
|
71
|
+
}, children) : children;
|
|
72
|
+
const accessibilityProps = a11yProps.select(rest); // TODO: use Stack to separate the title from content (space 2)
|
|
73
|
+
|
|
74
|
+
return /*#__PURE__*/React.createElement(View, Object.assign({
|
|
75
|
+
style: selectStyles(themeTokens)
|
|
76
|
+
}, accessibilityProps), title !== undefined && /*#__PURE__*/React.createElement(View, {
|
|
77
|
+
style: staticStyles.title
|
|
78
|
+
}, IconComponent && /*#__PURE__*/React.createElement(View, {
|
|
79
|
+
style: selectIconContainerStyles(themeTokens)
|
|
80
|
+
}, /*#__PURE__*/React.createElement(IconComponent, {
|
|
81
|
+
tokens: selectIconTokens(themeTokens)
|
|
82
|
+
})), /*#__PURE__*/React.createElement(Text, {
|
|
83
|
+
style: titleTextStyles
|
|
84
|
+
}, title)), content && typeof content === 'function' ? content({
|
|
85
|
+
textStyles: contentTextStyles,
|
|
86
|
+
variant
|
|
87
|
+
}) : /*#__PURE__*/React.createElement(View, null, content));
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
Feedback.propTypes = {
|
|
91
|
+
/**
|
|
92
|
+
* Emphasized summary of the feedback. If an icon is set, it is rendered next to the title.
|
|
93
|
+
*/
|
|
94
|
+
title: PropTypes.string,
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Feedback content rendered below the title. A render function `({textStyles, variant}) => {}` is supported.
|
|
98
|
+
*/
|
|
99
|
+
children: PropTypes.oneOfType([PropTypes.string, PropTypes.node, PropTypes.func]),
|
|
100
|
+
tokens: getTokensPropType('Feedback'),
|
|
101
|
+
variant: variantProp.propType
|
|
102
|
+
};
|
|
103
|
+
export default Feedback;
|
|
104
|
+
const staticStyles = StyleSheet.create({
|
|
105
|
+
title: {
|
|
106
|
+
display: 'flex',
|
|
107
|
+
flexDirection: 'row',
|
|
108
|
+
alignItems: 'center'
|
|
109
|
+
}
|
|
110
|
+
});
|
package/lib/Icon/Icon.js
CHANGED
|
@@ -23,7 +23,7 @@ const Icon = ({
|
|
|
23
23
|
style: {
|
|
24
24
|
// TODO: https://github.com/telus/universal-design-system/issues/487
|
|
25
25
|
transition: 'transform 200ms',
|
|
26
|
-
transform: [themeTokens.scale ? `scale(${themeTokens.scale})` : '', themeTokens.translateX ? `translateX(${themeTokens.translateX}px)` : ''].filter(exists => exists).join(' ')
|
|
26
|
+
transform: [themeTokens.scale ? `scale(${themeTokens.scale})` : '', themeTokens.translateX ? `translateX(${themeTokens.translateX}px)` : '', themeTokens.translateY ? `translateY(${themeTokens.translateY}px)` : ''].filter(exists => exists).join(' ')
|
|
27
27
|
}
|
|
28
28
|
}, iconContent) : iconContent;
|
|
29
29
|
}; // Auto-generated SVG icon components wrap Icon and pass through these props:
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { View, Text, StyleSheet } from 'react-native';
|
|
3
|
+
import PropTypes from 'prop-types';
|
|
4
|
+
import { applyTextStyles, useThemeTokens } from '../ThemeProvider';
|
|
5
|
+
import { getTokensPropType, selectTokens, variantProp } from '../utils';
|
|
6
|
+
import LabelContent from './LabelContent';
|
|
7
|
+
|
|
8
|
+
const selectLabelStyles = tokens => applyTextStyles(selectTokens('Typography', tokens));
|
|
9
|
+
|
|
10
|
+
const selectHintStyles = ({
|
|
11
|
+
hintColor,
|
|
12
|
+
hintFontName,
|
|
13
|
+
hintFontSize,
|
|
14
|
+
hintFontWeight,
|
|
15
|
+
hintLineHeight
|
|
16
|
+
}) => applyTextStyles({
|
|
17
|
+
color: hintColor,
|
|
18
|
+
fontName: hintFontName,
|
|
19
|
+
fontSize: hintFontSize,
|
|
20
|
+
fontWeight: hintFontWeight,
|
|
21
|
+
lineHeight: hintLineHeight
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const selectGapStyles = ({
|
|
25
|
+
gap
|
|
26
|
+
}) => ({
|
|
27
|
+
marginRight: gap
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
function InputLabel({
|
|
31
|
+
label,
|
|
32
|
+
forId,
|
|
33
|
+
hint,
|
|
34
|
+
hintPosition = 'inline',
|
|
35
|
+
hintId,
|
|
36
|
+
tooltip,
|
|
37
|
+
tokens,
|
|
38
|
+
variant
|
|
39
|
+
}) {
|
|
40
|
+
const themeTokens = useThemeTokens('InputLabel', tokens, variant); // TODO: use the actual Tooltip component here
|
|
41
|
+
|
|
42
|
+
const hasTooltip = tooltip !== undefined;
|
|
43
|
+
const isHintInline = hintPosition === 'inline';
|
|
44
|
+
return /*#__PURE__*/React.createElement(View, {
|
|
45
|
+
style: [staticStyles.container, !isHintInline && staticStyles.containerWithHintBelow]
|
|
46
|
+
}, /*#__PURE__*/React.createElement(Text, {
|
|
47
|
+
style: [selectLabelStyles(themeTokens), selectGapStyles(themeTokens), staticStyles.label]
|
|
48
|
+
}, /*#__PURE__*/React.createElement(LabelContent, {
|
|
49
|
+
forId: forId
|
|
50
|
+
}, label)), hint && isHintInline && /*#__PURE__*/React.createElement(Text, {
|
|
51
|
+
style: [selectHintStyles(themeTokens), hasTooltip && selectGapStyles(themeTokens)],
|
|
52
|
+
nativeID: hintId
|
|
53
|
+
}, hint), tooltip && /*#__PURE__*/React.createElement(Text, null, "?"), hint && !isHintInline && /*#__PURE__*/React.createElement(Text, {
|
|
54
|
+
style: [selectHintStyles(themeTokens), staticStyles.hintBelow],
|
|
55
|
+
nativeID: hintId
|
|
56
|
+
}, hint));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
InputLabel.propTypes = {
|
|
60
|
+
label: PropTypes.string.isRequired,
|
|
61
|
+
forId: PropTypes.string,
|
|
62
|
+
hint: PropTypes.string,
|
|
63
|
+
hintPosition: PropTypes.oneOf(['inline', 'below']),
|
|
64
|
+
hintId: PropTypes.string,
|
|
65
|
+
tooltip: PropTypes.string,
|
|
66
|
+
tokens: getTokensPropType('InputLabel'),
|
|
67
|
+
variant: variantProp.propType
|
|
68
|
+
};
|
|
69
|
+
export default InputLabel;
|
|
70
|
+
const staticStyles = StyleSheet.create({
|
|
71
|
+
container: {
|
|
72
|
+
display: 'flex',
|
|
73
|
+
flexDirection: 'row',
|
|
74
|
+
alignItems: 'center'
|
|
75
|
+
},
|
|
76
|
+
containerWithHintBelow: {
|
|
77
|
+
flexWrap: 'wrap'
|
|
78
|
+
},
|
|
79
|
+
label: {
|
|
80
|
+
flexShrink: 0
|
|
81
|
+
},
|
|
82
|
+
hintBelow: {
|
|
83
|
+
flexBasis: '100%',
|
|
84
|
+
flexShrink: 0
|
|
85
|
+
}
|
|
86
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import PropTypes from 'prop-types';
|
|
3
|
+
|
|
4
|
+
function LabelContent({
|
|
5
|
+
children,
|
|
6
|
+
forId
|
|
7
|
+
}) {
|
|
8
|
+
return /*#__PURE__*/React.createElement("label", {
|
|
9
|
+
htmlFor: forId
|
|
10
|
+
}, children);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export default LabelContent;
|
|
14
|
+
LabelContent.propTypes = {
|
|
15
|
+
children: PropTypes.string,
|
|
16
|
+
forId: PropTypes.string
|
|
17
|
+
};
|
package/lib/Link/LinkBase.js
CHANGED
|
@@ -25,7 +25,10 @@ Platform.OS === 'web' ? {
|
|
|
25
25
|
outline: outerBorderOutline,
|
|
26
26
|
borderWidth: outerBorderWidth,
|
|
27
27
|
borderColor: outerBorderColor,
|
|
28
|
-
borderRadius: outerBorderRadius
|
|
28
|
+
borderRadius: outerBorderRadius,
|
|
29
|
+
// Stops focus ring stretching horizontally if parent has display: block
|
|
30
|
+
// width: fit-content isn't supported on Firefox; can't cascade props like CSS `width: fit-content; width: --moz-fit-content;`
|
|
31
|
+
display: 'inline-flex'
|
|
29
32
|
} : {};
|
|
30
33
|
|
|
31
34
|
const selectTextStyles = ({
|
|
@@ -53,10 +56,12 @@ const selectIconStyles = ({
|
|
|
53
56
|
iconGapBefore,
|
|
54
57
|
iconGapAfter,
|
|
55
58
|
iconScale,
|
|
56
|
-
iconTranslateX
|
|
59
|
+
iconTranslateX,
|
|
60
|
+
iconTranslateY
|
|
57
61
|
}) => ({
|
|
58
62
|
scale: iconScale,
|
|
59
63
|
translateX: iconTranslateX,
|
|
64
|
+
translateY: iconTranslateY,
|
|
60
65
|
size: iconSize,
|
|
61
66
|
gapBefore: iconGapBefore,
|
|
62
67
|
gapAfter: iconGapAfter
|
|
@@ -183,7 +188,8 @@ const LinkBase = ({
|
|
|
183
188
|
size: iconStyles.size ? iconStyles.size * iconScale : undefined,
|
|
184
189
|
color: contentStyles.color ?? undefined,
|
|
185
190
|
scale: iconStyles.scale ?? undefined,
|
|
186
|
-
translateX: iconStyles.translateX ?? undefined
|
|
191
|
+
translateX: iconStyles.translateX ?? undefined,
|
|
192
|
+
translateY: iconStyles.translateY ?? undefined
|
|
187
193
|
};
|
|
188
194
|
const iconContent = /*#__PURE__*/React.createElement(IconComponent, {
|
|
189
195
|
tokens: iconTokens,
|
|
@@ -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,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,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;
|