@atlaskit/primitives 3.0.0 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @atlaskit/primitives
2
2
 
3
+ ## 3.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#76431](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/pull-requests/76431) [`c6819de73d02`](https://stash.atlassian.com/projects/CONFCLOUD/repos/confluence-frontend/commits/c6819de73d02) - Default text color + allow color inheritance if explicitly defined
8
+
3
9
  ## 3.0.0
4
10
 
5
11
  ### Major Changes
@@ -4,9 +4,10 @@ description: Text is a token-backed typography component to display body and UI
4
4
  order: 0
5
5
  ---
6
6
 
7
- import TextDefault from '../../examples/constellation/text/basic';
8
- import TextAsElement from '../../examples/constellation/text/as-element';
7
+ import Basic from '../../examples/constellation/text/basic';
9
8
  import ColorInverse from '../../examples/constellation/text/color-inverse';
9
+ import Weight from '../../examples/constellation/text/weight';
10
+ import AsElement from '../../examples/constellation/text/as-element';
10
11
 
11
12
  ## Basic
12
13
 
@@ -14,16 +15,22 @@ Implements the [Atlassian typography system](/foundations/typography/) as a comp
14
15
 
15
16
  The `variant` prop expresses the visual appearance of the text element.
16
17
 
17
- <Example Component={TextDefault} pacckageName="@atlaskit/primitives" />
18
+ <Example Component={Basic} pacckageName="@atlaskit/primitives" />
18
19
 
19
- ## Rendered HTML tag
20
+ ## Color
20
21
 
21
- By default, `body` variants are rendered as `<p>` tags and `ui` variants are rendered as `<span>` tags. This can be ovrridden by the `as` prop.
22
+ Set `color` to change the text color, defaults to `text.color` if not nested in other Text components. When used within a [box component](/components/primitives/box) that has a bold background color, the text color will automatically be inverted.
22
23
 
23
- <Example Component={TextAsElement} packageName="@atlaskit/primitives" />
24
+ <Example Component={ColorInverse} pacckageName="@atlaskit/primitives" />
24
25
 
25
- ## Color
26
+ ## Weight
26
27
 
27
- Set `color` to change the text color. When used within a [box component](/components/primitives/box) that has a background color set, the text color can be automatically inverted without setting the `color` prop depending on the background color of the box.
28
+ Set `weight` to change the front weight.
28
29
 
29
- <Example Component={ColorInverse} pacckageName="@atlaskit/primitives" />
30
+ <Example Component={Weight} pacckageName="@atlaskit/primitives" />
31
+
32
+ ## Rendered HTML tag
33
+
34
+ Set `as` to change the rendered HTML element, defaults to `span`.
35
+
36
+ <Example Component={AsElement} packageName="@atlaskit/primitives" />
@@ -76,7 +76,7 @@ var Anchor = function Anchor(_ref, ref) {
76
76
  action: 'clicked',
77
77
  componentName: componentName || 'Anchor',
78
78
  packageName: "@atlaskit/primitives",
79
- packageVersion: "3.0.0",
79
+ packageVersion: "3.1.0",
80
80
  analyticsData: analyticsContext,
81
81
  actionSubject: 'link'
82
82
  });
@@ -80,7 +80,7 @@ var UNSAFE_PRESSABLE = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref)
80
80
  action: 'clicked',
81
81
  componentName: componentName || 'Pressable',
82
82
  packageName: "@atlaskit/primitives",
83
- packageVersion: "3.0.0",
83
+ packageVersion: "3.1.0",
84
84
  analyticsData: analyticsContext,
85
85
  actionSubject: 'button'
86
86
  });
@@ -7,7 +7,8 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.default = void 0;
8
8
  var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
9
9
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
10
- var _react = require("@emotion/react");
10
+ var _react = require("react");
11
+ var _react2 = require("@emotion/react");
11
12
  var _tinyInvariant = _interopRequireDefault(require("tiny-invariant"));
12
13
  var _styleMaps = require("../xcss/style-maps.partial");
13
14
  var _surfaceProvider = require("./internal/surface-provider");
@@ -18,50 +19,62 @@ var asAllowlist = ['span', 'p', 'strong', 'em'];
18
19
  // We're doing this because our CSS reset can add top margins to elements such as `p` which is totally insane.
19
20
  // Long term we should remove those instances from the reset - it should be a reset to 0.
20
21
  // For now, at least we know <Text> will be unaffected by this.
21
- var resetStyles = (0, _react.css)({
22
+ var resetStyles = (0, _react2.css)({
22
23
  margin: 0
23
24
  });
24
25
  var variantStyles = _objectSpread(_objectSpread({}, _styleMaps.bodyTextStylesMap), _styleMaps.uiTextStylesMap);
25
- var strongStyles = (0, _react.css)({
26
+ var strongStyles = (0, _react2.css)({
26
27
  fontWeight: 'bold'
27
28
  });
28
- var emStyles = (0, _react.css)({
29
+ var emStyles = (0, _react2.css)({
29
30
  fontStyle: 'italic'
30
31
  });
31
32
  var textAlignMap = {
32
- center: (0, _react.css)({
33
+ center: (0, _react2.css)({
33
34
  textAlign: 'center'
34
35
  }),
35
- end: (0, _react.css)({
36
+ end: (0, _react2.css)({
36
37
  textAlign: 'end'
37
38
  }),
38
- start: (0, _react.css)({
39
+ start: (0, _react2.css)({
39
40
  textAlign: 'start'
40
41
  })
41
42
  };
42
- var truncationStyles = (0, _react.css)({
43
+ var truncationStyles = (0, _react2.css)({
43
44
  display: '-webkit-box',
44
45
  overflow: 'hidden',
45
46
  WebkitBoxOrient: 'vertical'
46
47
  });
47
48
  var wordBreakMap = {
48
- breakAll: (0, _react.css)({
49
+ breakAll: (0, _react2.css)({
49
50
  wordBreak: 'break-all'
50
51
  })
51
52
  };
53
+ var HasTextAncestorContext = /*#__PURE__*/(0, _react.createContext)(false);
54
+ var useHasTextAncestor = function useHasTextAncestor() {
55
+ return (0, _react.useContext)(HasTextAncestorContext);
56
+ };
52
57
 
53
58
  /**
54
59
  * Custom hook designed to abstract the parsing of the color props and make it clearer in the future how color is reconciled between themes and tokens.
55
60
  */
56
- var useColor = function useColor(colorProp) {
61
+ var useColor = function useColor(colorProp, hasTextAncestor) {
57
62
  var surface = (0, _surfaceProvider.useSurface)();
58
63
 
59
64
  /**
60
- * Where the color of the surface is inverted we override the user choice
65
+ * Where the color of the surface is inverted we always override the color
61
66
  * as there is no valid choice that is not covered by the override.
62
67
  */
63
- var color = _styleMaps.inverseColorMap.hasOwnProperty(surface) ? _styleMaps.inverseColorMap[surface] : colorProp;
64
- return color;
68
+ if (_styleMaps.inverseColorMap.hasOwnProperty(surface)) {
69
+ return _styleMaps.inverseColorMap[surface];
70
+ }
71
+ if (colorProp === 'inherit') {
72
+ return undefined;
73
+ }
74
+ if (!colorProp && hasTextAncestor) {
75
+ return undefined;
76
+ }
77
+ return colorProp || 'color.text';
65
78
  };
66
79
 
67
80
  /**
@@ -76,7 +89,8 @@ var useColor = function useColor(colorProp) {
76
89
  var Text = function Text(_ref) {
77
90
  var children = _ref.children,
78
91
  props = (0, _objectWithoutProperties2.default)(_ref, _excluded);
79
- var asElement = props.as,
92
+ var _props$as = props.as,
93
+ Component = _props$as === void 0 ? 'span' : _props$as,
80
94
  colorProp = props.color,
81
95
  textAlign = props.textAlign,
82
96
  testId = props.testId,
@@ -84,10 +98,6 @@ var Text = function Text(_ref) {
84
98
  _props$variant = props.variant,
85
99
  variant = _props$variant === void 0 ? 'body' : _props$variant,
86
100
  weight = props.weight;
87
-
88
- // body variants -> p
89
- // ui variants -> span
90
- var Component = asElement || (variant.includes('body') ? 'p' : 'span');
91
101
  (0, _tinyInvariant.default)(asAllowlist.includes(Component), "@atlaskit/primitives: Text received an invalid \"as\" value of \"".concat(Component, "\""));
92
102
 
93
103
  // Remove the ability to bypass typescript errors for maxLines
@@ -95,14 +105,20 @@ var Text = function Text(_ref) {
95
105
  if ('maxLines' in props && variant.includes('body')) {
96
106
  maxLines = props.maxLines;
97
107
  }
98
- var color = useColor(colorProp);
99
- return (0, _react.jsx)(Component, {
100
- css: [resetStyles, variant && variantStyles[variant], color && _styleMaps.textColorStylesMap[color], maxLines && truncationStyles, maxLines === 1 && wordBreakMap.breakAll, textAlign && textAlignMap[textAlign], weight && _styleMaps.fontWeightStylesMap[weight], asElement === 'em' && emStyles, asElement === 'strong' && strongStyles],
108
+ var hasTextAncestor = useHasTextAncestor();
109
+ var color = useColor(colorProp, hasTextAncestor);
110
+ var component = (0, _react2.jsx)(Component, {
111
+ css: [resetStyles, variantStyles[variant], color && _styleMaps.textColorStylesMap[color], maxLines && truncationStyles, maxLines === 1 && wordBreakMap.breakAll, textAlign && textAlignMap[textAlign], weight && _styleMaps.fontWeightStylesMap[weight], Component === 'em' && emStyles, Component === 'strong' && strongStyles],
101
112
  style: {
102
113
  WebkitLineClamp: maxLines
103
114
  },
104
115
  "data-testid": testId,
105
116
  id: id
106
117
  }, children);
118
+ return hasTextAncestor ?
119
+ // no need to re-apply context if the text is already wrapped
120
+ component : (0, _react2.jsx)(HasTextAncestorContext.Provider, {
121
+ value: true
122
+ }, component);
107
123
  };
108
124
  var _default = exports.default = Text;
@@ -62,7 +62,7 @@ const Anchor = ({
62
62
  action: 'clicked',
63
63
  componentName: componentName || 'Anchor',
64
64
  packageName: "@atlaskit/primitives",
65
- packageVersion: "3.0.0",
65
+ packageVersion: "3.1.0",
66
66
  analyticsData: analyticsContext,
67
67
  actionSubject: 'link'
68
68
  });
@@ -66,7 +66,7 @@ const UNSAFE_PRESSABLE = /*#__PURE__*/forwardRef(({
66
66
  action: 'clicked',
67
67
  componentName: componentName || 'Pressable',
68
68
  packageName: "@atlaskit/primitives",
69
- packageVersion: "3.0.0",
69
+ packageVersion: "3.1.0",
70
70
  analyticsData: analyticsContext,
71
71
  actionSubject: 'button'
72
72
  });
@@ -1,5 +1,5 @@
1
1
  /** @jsx jsx */
2
-
2
+ import { createContext, useContext } from 'react';
3
3
  import { css, jsx } from '@emotion/react';
4
4
  import invariant from 'tiny-invariant';
5
5
  import { bodyTextStylesMap, fontWeightStylesMap, inverseColorMap, textColorStylesMap, uiTextStylesMap } from '../xcss/style-maps.partial';
@@ -42,19 +42,29 @@ const wordBreakMap = {
42
42
  wordBreak: 'break-all'
43
43
  })
44
44
  };
45
+ const HasTextAncestorContext = /*#__PURE__*/createContext(false);
46
+ const useHasTextAncestor = () => useContext(HasTextAncestorContext);
45
47
 
46
48
  /**
47
49
  * Custom hook designed to abstract the parsing of the color props and make it clearer in the future how color is reconciled between themes and tokens.
48
50
  */
49
- const useColor = colorProp => {
51
+ const useColor = (colorProp, hasTextAncestor) => {
50
52
  const surface = useSurface();
51
53
 
52
54
  /**
53
- * Where the color of the surface is inverted we override the user choice
55
+ * Where the color of the surface is inverted we always override the color
54
56
  * as there is no valid choice that is not covered by the override.
55
57
  */
56
- const color = inverseColorMap.hasOwnProperty(surface) ? inverseColorMap[surface] : colorProp;
57
- return color;
58
+ if (inverseColorMap.hasOwnProperty(surface)) {
59
+ return inverseColorMap[surface];
60
+ }
61
+ if (colorProp === 'inherit') {
62
+ return undefined;
63
+ }
64
+ if (!colorProp && hasTextAncestor) {
65
+ return undefined;
66
+ }
67
+ return colorProp || 'color.text';
58
68
  };
59
69
 
60
70
  /**
@@ -71,7 +81,7 @@ const Text = ({
71
81
  ...props
72
82
  }) => {
73
83
  const {
74
- as: asElement,
84
+ as: Component = 'span',
75
85
  color: colorProp,
76
86
  textAlign,
77
87
  testId,
@@ -79,10 +89,6 @@ const Text = ({
79
89
  variant = 'body',
80
90
  weight
81
91
  } = props;
82
-
83
- // body variants -> p
84
- // ui variants -> span
85
- const Component = asElement || (variant.includes('body') ? 'p' : 'span');
86
92
  invariant(asAllowlist.includes(Component), `@atlaskit/primitives: Text received an invalid "as" value of "${Component}"`);
87
93
 
88
94
  // Remove the ability to bypass typescript errors for maxLines
@@ -90,14 +96,20 @@ const Text = ({
90
96
  if ('maxLines' in props && variant.includes('body')) {
91
97
  maxLines = props.maxLines;
92
98
  }
93
- const color = useColor(colorProp);
94
- return jsx(Component, {
95
- css: [resetStyles, variant && variantStyles[variant], color && textColorStylesMap[color], maxLines && truncationStyles, maxLines === 1 && wordBreakMap.breakAll, textAlign && textAlignMap[textAlign], weight && fontWeightStylesMap[weight], asElement === 'em' && emStyles, asElement === 'strong' && strongStyles],
99
+ const hasTextAncestor = useHasTextAncestor();
100
+ const color = useColor(colorProp, hasTextAncestor);
101
+ const component = jsx(Component, {
102
+ css: [resetStyles, variantStyles[variant], color && textColorStylesMap[color], maxLines && truncationStyles, maxLines === 1 && wordBreakMap.breakAll, textAlign && textAlignMap[textAlign], weight && fontWeightStylesMap[weight], Component === 'em' && emStyles, Component === 'strong' && strongStyles],
96
103
  style: {
97
104
  WebkitLineClamp: maxLines
98
105
  },
99
106
  "data-testid": testId,
100
107
  id: id
101
108
  }, children);
109
+ return hasTextAncestor ?
110
+ // no need to re-apply context if the text is already wrapped
111
+ component : jsx(HasTextAncestorContext.Provider, {
112
+ value: true
113
+ }, component);
102
114
  };
103
115
  export default Text;
@@ -66,7 +66,7 @@ var Anchor = function Anchor(_ref, ref) {
66
66
  action: 'clicked',
67
67
  componentName: componentName || 'Anchor',
68
68
  packageName: "@atlaskit/primitives",
69
- packageVersion: "3.0.0",
69
+ packageVersion: "3.1.0",
70
70
  analyticsData: analyticsContext,
71
71
  actionSubject: 'link'
72
72
  });
@@ -70,7 +70,7 @@ var UNSAFE_PRESSABLE = /*#__PURE__*/forwardRef(function (_ref, ref) {
70
70
  action: 'clicked',
71
71
  componentName: componentName || 'Pressable',
72
72
  packageName: "@atlaskit/primitives",
73
- packageVersion: "3.0.0",
73
+ packageVersion: "3.1.0",
74
74
  analyticsData: analyticsContext,
75
75
  actionSubject: 'button'
76
76
  });
@@ -4,7 +4,7 @@ var _excluded = ["children"];
4
4
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
5
5
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
6
6
  /** @jsx jsx */
7
-
7
+ import { createContext, useContext } from 'react';
8
8
  import { css, jsx } from '@emotion/react';
9
9
  import invariant from 'tiny-invariant';
10
10
  import { bodyTextStylesMap, fontWeightStylesMap, inverseColorMap, textColorStylesMap, uiTextStylesMap } from '../xcss/style-maps.partial';
@@ -44,19 +44,31 @@ var wordBreakMap = {
44
44
  wordBreak: 'break-all'
45
45
  })
46
46
  };
47
+ var HasTextAncestorContext = /*#__PURE__*/createContext(false);
48
+ var useHasTextAncestor = function useHasTextAncestor() {
49
+ return useContext(HasTextAncestorContext);
50
+ };
47
51
 
48
52
  /**
49
53
  * Custom hook designed to abstract the parsing of the color props and make it clearer in the future how color is reconciled between themes and tokens.
50
54
  */
51
- var useColor = function useColor(colorProp) {
55
+ var useColor = function useColor(colorProp, hasTextAncestor) {
52
56
  var surface = useSurface();
53
57
 
54
58
  /**
55
- * Where the color of the surface is inverted we override the user choice
59
+ * Where the color of the surface is inverted we always override the color
56
60
  * as there is no valid choice that is not covered by the override.
57
61
  */
58
- var color = inverseColorMap.hasOwnProperty(surface) ? inverseColorMap[surface] : colorProp;
59
- return color;
62
+ if (inverseColorMap.hasOwnProperty(surface)) {
63
+ return inverseColorMap[surface];
64
+ }
65
+ if (colorProp === 'inherit') {
66
+ return undefined;
67
+ }
68
+ if (!colorProp && hasTextAncestor) {
69
+ return undefined;
70
+ }
71
+ return colorProp || 'color.text';
60
72
  };
61
73
 
62
74
  /**
@@ -71,7 +83,8 @@ var useColor = function useColor(colorProp) {
71
83
  var Text = function Text(_ref) {
72
84
  var children = _ref.children,
73
85
  props = _objectWithoutProperties(_ref, _excluded);
74
- var asElement = props.as,
86
+ var _props$as = props.as,
87
+ Component = _props$as === void 0 ? 'span' : _props$as,
75
88
  colorProp = props.color,
76
89
  textAlign = props.textAlign,
77
90
  testId = props.testId,
@@ -79,10 +92,6 @@ var Text = function Text(_ref) {
79
92
  _props$variant = props.variant,
80
93
  variant = _props$variant === void 0 ? 'body' : _props$variant,
81
94
  weight = props.weight;
82
-
83
- // body variants -> p
84
- // ui variants -> span
85
- var Component = asElement || (variant.includes('body') ? 'p' : 'span');
86
95
  invariant(asAllowlist.includes(Component), "@atlaskit/primitives: Text received an invalid \"as\" value of \"".concat(Component, "\""));
87
96
 
88
97
  // Remove the ability to bypass typescript errors for maxLines
@@ -90,14 +99,20 @@ var Text = function Text(_ref) {
90
99
  if ('maxLines' in props && variant.includes('body')) {
91
100
  maxLines = props.maxLines;
92
101
  }
93
- var color = useColor(colorProp);
94
- return jsx(Component, {
95
- css: [resetStyles, variant && variantStyles[variant], color && textColorStylesMap[color], maxLines && truncationStyles, maxLines === 1 && wordBreakMap.breakAll, textAlign && textAlignMap[textAlign], weight && fontWeightStylesMap[weight], asElement === 'em' && emStyles, asElement === 'strong' && strongStyles],
102
+ var hasTextAncestor = useHasTextAncestor();
103
+ var color = useColor(colorProp, hasTextAncestor);
104
+ var component = jsx(Component, {
105
+ css: [resetStyles, variantStyles[variant], color && textColorStylesMap[color], maxLines && truncationStyles, maxLines === 1 && wordBreakMap.breakAll, textAlign && textAlignMap[textAlign], weight && fontWeightStylesMap[weight], Component === 'em' && emStyles, Component === 'strong' && strongStyles],
96
106
  style: {
97
107
  WebkitLineClamp: maxLines
98
108
  },
99
109
  "data-testid": testId,
100
110
  id: id
101
111
  }, children);
112
+ return hasTextAncestor ?
113
+ // no need to re-apply context if the text is already wrapped
114
+ component : jsx(HasTextAncestorContext.Provider, {
115
+ value: true
116
+ }, component);
102
117
  };
103
118
  export default Text;
@@ -36,10 +36,10 @@ type TextPropsBase = {
36
36
  children: ReactNode;
37
37
  /**
38
38
  * Token representing text color with a built-in fallback value.
39
- * Will apply inverse text color automatically if placed within a Box with backgroundColor.
40
- *
39
+ * Will apply inverse text color automatically if placed within a Box with bold background color.
40
+ * Defaults to `text.color` if not nested in other Text components.
41
41
  */
42
- color?: TextColor;
42
+ color?: TextColor | 'inherit';
43
43
  /**
44
44
  * The [HTML `id` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).
45
45
  */
@@ -41,10 +41,10 @@ type TextPropsBase = {
41
41
  children: ReactNode;
42
42
  /**
43
43
  * Token representing text color with a built-in fallback value.
44
- * Will apply inverse text color automatically if placed within a Box with backgroundColor.
45
- *
44
+ * Will apply inverse text color automatically if placed within a Box with bold background color.
45
+ * Defaults to `text.color` if not nested in other Text components.
46
46
  */
47
- color?: TextColor;
47
+ color?: TextColor | 'inherit';
48
48
  /**
49
49
  * The [HTML `id` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id).
50
50
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/primitives",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Primitives are token-backed low-level building blocks.",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"