@atlaskit/code 17.1.2 → 17.2.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,13 @@
1
1
  # @atlaskit/code
2
2
 
3
+ ## 17.2.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#153642](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/153642)
8
+ [`dca85557e83b9`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/dca85557e83b9) -
9
+ Adds a label prop for when code block is scrollable. Fixes the code block scrollablity bug.
10
+
3
11
  ## 17.1.2
4
12
 
5
13
  ### Patch Changes
@@ -1,6 +1,4 @@
1
1
  /* code-block.tsx generated by @compiled/babel-plugin v0.36.1 */
2
- /* eslint-disable @atlaskit/ui-styling-standard/no-unsafe-selectors */
3
- /* eslint-disable @atlaskit/ui-styling-standard/no-nested-selectors */
4
2
  "use strict";
5
3
 
6
4
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
@@ -13,12 +11,17 @@ require("./code-block.compiled.css");
13
11
  var _react = _interopRequireWildcard(require("react"));
14
12
  var React = _react;
15
13
  var _runtime = require("@compiled/react/runtime");
14
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
15
+ var _bindEventListener = require("bind-event-listener");
16
+ var _rafSchd = _interopRequireDefault(require("raf-schd"));
16
17
  var _useHighlight = require("./internal/hooks/use-highlight");
17
18
  var _styles = require("./internal/theme/styles");
18
19
  var _getNormalizedLanguage = require("./internal/utils/get-normalized-language");
19
20
  var _syntaxHighlighter = _interopRequireDefault(require("./syntax-highlighter"));
20
21
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
21
22
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
23
+ /* eslint-disable @atlaskit/ui-styling-standard/no-unsafe-selectors */
24
+ /* eslint-disable @atlaskit/ui-styling-standard/no-nested-selectors */
22
25
  var getCodeBlockStyles = {
23
26
  root: "_2rkoglpi _1dqoglyw _1wyb1crf _k48pi7a9 _1e0c1txw _vwz4gktf _1reo1wug _o572qvpr _1eimjvyg _bfhktkvp _syaz1fxt _ect41odn _1ozdn7od _7xinn7od _t7aun7od _1v15u2gc _ilvcu2gc _m29uu2gc _17wyu2gc _pn28u2gc _gz9fu2gc _1hmyegat _vblregat _vbulegat _wozj1dyw _1agb1dyw _lkm81dyw _tv41hkgb _1hmimyb0 _1ra01n1a _1d4j1y44 _1f8gstnw _1pzyb3bt _j1w0ww7y _13cdh2mm _15ba126e _zvy9f705 _qcxof705 _14y71a66 _j0l11wug _1weckb7n _1na21hna _1xx2grf3 _x7c815vq _lh0y15vq _1m3815vq _qk1e15vq _12l6ysn8 _uga3ysn8 _mx8b7mnp _1kr87mnp _xo19t94y _1bemt94y _nalpstnw _151dstnw _1exb1q9c _1hgu1q9c _1mgnt94y _nhket94y _h909m7j4 _scgayz1z _ipl81e17 _jeky1l04 _1gec1a66 _1gx21e5h _1ls0df4r _vm2c1rh5 _12ok1rh5 _1ls01ule _rude1ule _1q16glyw _1io6glyw _juomusic _lcwuusic _1552u2gc _12afu2gc _28ddu2gc _1i8zu2gc _12tu1a66 _zu0j1a66 _euyxusvi _cahfusvi _zhnuidpf _1amdidpf _mbgcpf9b _bu7zpf9b _131n1giz _gy101giz _1wfuwrk5 _16kzwrk5 _9kk3moej _cjus1w1g _9k2r1m30 _nhmw1m30 _yl021m30 _eiht5x2v _t9zb5x2v _mqok1w1g _3hsg1w1g _i7ngn7od _9wu1fb2s _1xcoh55r _1t361fxt _137bh55r _1k7d1fxt _97lipnps _12nh9lu1 _1g0517qg _i2ig10m5 _326z1fxt _113p131l _1n6tpnps _tgu817qg _1k47pnps _g0lx1fxt _ys4e131l _7gp8h55r _1yvq10m5 _1vww10m5 _1rju10m5 _1v0lh55r _wmyy17qg _748n17qg _1mfn17qg _1d7e17qg _p2vr17qg _19o610m5 _kxov17qg _1np517qg _m2f517qg _1b9tpnps _1tq6pnps _1rd2pnps _1pbkpnps _k3lipnps _13zt131l _2g12fb2s _k86b10m5 _b5iy131l _gti3131l _1f0gpnps _9d3e17qg _qdiapnps _72uvpnps",
24
27
  showLineNumbers: "_17071olh _1i3h1txw _1huoidpf _1a9lidpf _20bqidpf _1oggidpf _bympidpf _9nnjidpf",
@@ -57,9 +60,31 @@ var CodeBlock = /*#__PURE__*/(0, _react.memo)(function CodeBlock(_ref) {
57
60
  _ref$codeBidiWarningT = _ref.codeBidiWarningTooltipEnabled,
58
61
  codeBidiWarningTooltipEnabled = _ref$codeBidiWarningT === void 0 ? true : _ref$codeBidiWarningT,
59
62
  _ref$shouldWrapLongLi = _ref.shouldWrapLongLines,
60
- shouldWrapLongLines = _ref$shouldWrapLongLi === void 0 ? false : _ref$shouldWrapLongLi;
63
+ shouldWrapLongLines = _ref$shouldWrapLongLi === void 0 ? false : _ref$shouldWrapLongLi,
64
+ _ref$label = _ref.label,
65
+ label = _ref$label === void 0 ? 'Scrollable content' : _ref$label;
66
+ var scrollableRef = (0, _react.useRef)(null);
67
+ var _useState = (0, _react.useState)(false),
68
+ _useState2 = (0, _slicedToArray2.default)(_useState, 2),
69
+ showContentFocus = _useState2[0],
70
+ setShowContentFocus = _useState2[1];
61
71
  var numLines = (text || '').split('\n').length + (firstLineNumber > 0 ? firstLineNumber : 1) - 1;
62
72
  var lineNumberWidth = numLines ? (0, _styles.getLineNumWidth)(numLines) : 0;
73
+
74
+ // Schedule a content focus on the target element
75
+ // WARNING: In theory, `target` may not be available when `rafSchedule` hits in concurrent rendering
76
+ (0, _react.useEffect)(function () {
77
+ var schedule = (0, _rafSchd.default)(function () {
78
+ var target = scrollableRef.current;
79
+ target && setShowContentFocus(target.scrollWidth > target.clientWidth);
80
+ });
81
+ schedule();
82
+ var unbindWindowEvent = (0, _bindEventListener.bind)(window, {
83
+ type: 'resize',
84
+ listener: schedule
85
+ });
86
+ return unbindWindowEvent;
87
+ }, [scrollableRef]);
63
88
  var _useHighlightLines = (0, _useHighlight.useHighlightLines)({
64
89
  highlight: highlight,
65
90
  testId: testId
@@ -98,8 +123,11 @@ var CodeBlock = /*#__PURE__*/(0, _react.memo)(function CodeBlock(_ref) {
98
123
  codeBidiWarningLabel: codeBidiWarningLabel,
99
124
  codeBidiWarningTooltipEnabled: codeBidiWarningTooltipEnabled,
100
125
  text: text,
126
+ tabIndex: showContentFocus ? '0' : undefined,
127
+ "aria-label": showContentFocus ? label : undefined,
128
+ role: showContentFocus ? 'region' : undefined,
129
+ scrollRef: scrollableRef,
101
130
  className: (0, _runtime.ax)([getCodeBlockStyles.root, shouldWrapLongLines ? getCodeBlockStyles.shouldWrapLongLines : getCodeBlockStyles.dontWrapLongLines, showLineNumbers ? getCodeBlockStyles.showLineNumbers : getCodeBlockStyles.dontShowLineNumbers])
102
131
  });
103
132
  });
104
- CodeBlock.displayName = 'CodeBlock';
105
133
  var _default = exports.default = CodeBlock;
@@ -52,9 +52,13 @@ var Highlight = /*#__PURE__*/(0, _react.memo)(function Highlight(_ref) {
52
52
  var code = text;
53
53
  var generatorClassName = 'prismjs';
54
54
  var containerProps = _objectSpread(_objectSpread({}, rest), {}, {
55
+ ref: rest.scrollRef,
55
56
  'data-testid': testId,
56
57
  className: rest.className ? "".concat(generatorClassName, " ").concat(rest.className) : generatorClassName
57
58
  });
59
+ if (containerProps.hasOwnProperty('scrollRef')) {
60
+ delete containerProps['scrollRef'];
61
+ }
58
62
  if (shouldWrapLongLines) {
59
63
  codeTagProps.style = {
60
64
  whiteSpace: 'pre-wrap',
@@ -4,7 +4,9 @@
4
4
  import "./code-block.compiled.css";
5
5
  import * as React from 'react';
6
6
  import { ax, ix } from "@compiled/react/runtime";
7
- import { memo, useCallback, useMemo } from 'react';
7
+ import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
8
+ import { bind } from 'bind-event-listener';
9
+ import rafSchedule from 'raf-schd';
8
10
  import { useHighlightLines } from './internal/hooks/use-highlight';
9
11
  import { getLineNumWidth } from './internal/theme/styles';
10
12
  import { normalizeLanguage } from './internal/utils/get-normalized-language';
@@ -38,10 +40,28 @@ const CodeBlock = /*#__PURE__*/memo(function CodeBlock({
38
40
  codeBidiWarnings = true,
39
41
  codeBidiWarningLabel,
40
42
  codeBidiWarningTooltipEnabled = true,
41
- shouldWrapLongLines = false
43
+ shouldWrapLongLines = false,
44
+ label = 'Scrollable content'
42
45
  }) {
46
+ const scrollableRef = useRef(null);
47
+ const [showContentFocus, setShowContentFocus] = useState(false);
43
48
  const numLines = (text || '').split('\n').length + (firstLineNumber > 0 ? firstLineNumber : 1) - 1;
44
49
  const lineNumberWidth = numLines ? getLineNumWidth(numLines) : 0;
50
+
51
+ // Schedule a content focus on the target element
52
+ // WARNING: In theory, `target` may not be available when `rafSchedule` hits in concurrent rendering
53
+ useEffect(() => {
54
+ const schedule = rafSchedule(() => {
55
+ const target = scrollableRef.current;
56
+ target && setShowContentFocus(target.scrollWidth > target.clientWidth);
57
+ });
58
+ schedule();
59
+ const unbindWindowEvent = bind(window, {
60
+ type: 'resize',
61
+ listener: schedule
62
+ });
63
+ return unbindWindowEvent;
64
+ }, [scrollableRef]);
45
65
  const {
46
66
  getHighlightStyles,
47
67
  highlightedLines
@@ -77,8 +97,11 @@ const CodeBlock = /*#__PURE__*/memo(function CodeBlock({
77
97
  codeBidiWarningLabel: codeBidiWarningLabel,
78
98
  codeBidiWarningTooltipEnabled: codeBidiWarningTooltipEnabled,
79
99
  text: text,
100
+ tabIndex: showContentFocus ? '0' : undefined,
101
+ "aria-label": showContentFocus ? label : undefined,
102
+ role: showContentFocus ? 'region' : undefined,
103
+ scrollRef: scrollableRef,
80
104
  className: ax([getCodeBlockStyles.root, shouldWrapLongLines ? getCodeBlockStyles.shouldWrapLongLines : getCodeBlockStyles.dontWrapLongLines, showLineNumbers ? getCodeBlockStyles.showLineNumbers : getCodeBlockStyles.dontShowLineNumbers])
81
105
  });
82
106
  });
83
- CodeBlock.displayName = 'CodeBlock';
84
107
  export default CodeBlock;
@@ -34,9 +34,13 @@ const Highlight = /*#__PURE__*/memo(function Highlight({
34
34
  const generatorClassName = 'prismjs';
35
35
  const containerProps = {
36
36
  ...rest,
37
+ ref: rest.scrollRef,
37
38
  'data-testid': testId,
38
39
  className: rest.className ? `${generatorClassName} ${rest.className}` : generatorClassName
39
40
  };
41
+ if (containerProps.hasOwnProperty('scrollRef')) {
42
+ delete containerProps['scrollRef'];
43
+ }
40
44
  if (shouldWrapLongLines) {
41
45
  codeTagProps.style = {
42
46
  whiteSpace: 'pre-wrap',
@@ -1,10 +1,14 @@
1
1
  /* code-block.tsx generated by @compiled/babel-plugin v0.36.1 */
2
- /* eslint-disable @atlaskit/ui-styling-standard/no-unsafe-selectors */
3
- /* eslint-disable @atlaskit/ui-styling-standard/no-nested-selectors */
2
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
4
3
  import "./code-block.compiled.css";
5
4
  import * as React from 'react';
6
5
  import { ax, ix } from "@compiled/react/runtime";
7
- import { memo, useCallback, useMemo } from 'react';
6
+ /* eslint-disable @atlaskit/ui-styling-standard/no-unsafe-selectors */
7
+ /* eslint-disable @atlaskit/ui-styling-standard/no-nested-selectors */
8
+
9
+ import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
10
+ import { bind } from 'bind-event-listener';
11
+ import rafSchedule from 'raf-schd';
8
12
  import { useHighlightLines } from './internal/hooks/use-highlight';
9
13
  import { getLineNumWidth } from './internal/theme/styles';
10
14
  import { normalizeLanguage } from './internal/utils/get-normalized-language';
@@ -47,9 +51,31 @@ var CodeBlock = /*#__PURE__*/memo(function CodeBlock(_ref) {
47
51
  _ref$codeBidiWarningT = _ref.codeBidiWarningTooltipEnabled,
48
52
  codeBidiWarningTooltipEnabled = _ref$codeBidiWarningT === void 0 ? true : _ref$codeBidiWarningT,
49
53
  _ref$shouldWrapLongLi = _ref.shouldWrapLongLines,
50
- shouldWrapLongLines = _ref$shouldWrapLongLi === void 0 ? false : _ref$shouldWrapLongLi;
54
+ shouldWrapLongLines = _ref$shouldWrapLongLi === void 0 ? false : _ref$shouldWrapLongLi,
55
+ _ref$label = _ref.label,
56
+ label = _ref$label === void 0 ? 'Scrollable content' : _ref$label;
57
+ var scrollableRef = useRef(null);
58
+ var _useState = useState(false),
59
+ _useState2 = _slicedToArray(_useState, 2),
60
+ showContentFocus = _useState2[0],
61
+ setShowContentFocus = _useState2[1];
51
62
  var numLines = (text || '').split('\n').length + (firstLineNumber > 0 ? firstLineNumber : 1) - 1;
52
63
  var lineNumberWidth = numLines ? getLineNumWidth(numLines) : 0;
64
+
65
+ // Schedule a content focus on the target element
66
+ // WARNING: In theory, `target` may not be available when `rafSchedule` hits in concurrent rendering
67
+ useEffect(function () {
68
+ var schedule = rafSchedule(function () {
69
+ var target = scrollableRef.current;
70
+ target && setShowContentFocus(target.scrollWidth > target.clientWidth);
71
+ });
72
+ schedule();
73
+ var unbindWindowEvent = bind(window, {
74
+ type: 'resize',
75
+ listener: schedule
76
+ });
77
+ return unbindWindowEvent;
78
+ }, [scrollableRef]);
53
79
  var _useHighlightLines = useHighlightLines({
54
80
  highlight: highlight,
55
81
  testId: testId
@@ -88,8 +114,11 @@ var CodeBlock = /*#__PURE__*/memo(function CodeBlock(_ref) {
88
114
  codeBidiWarningLabel: codeBidiWarningLabel,
89
115
  codeBidiWarningTooltipEnabled: codeBidiWarningTooltipEnabled,
90
116
  text: text,
117
+ tabIndex: showContentFocus ? '0' : undefined,
118
+ "aria-label": showContentFocus ? label : undefined,
119
+ role: showContentFocus ? 'region' : undefined,
120
+ scrollRef: scrollableRef,
91
121
  className: ax([getCodeBlockStyles.root, shouldWrapLongLines ? getCodeBlockStyles.shouldWrapLongLines : getCodeBlockStyles.dontWrapLongLines, showLineNumbers ? getCodeBlockStyles.showLineNumbers : getCodeBlockStyles.dontShowLineNumbers])
92
122
  });
93
123
  });
94
- CodeBlock.displayName = 'CodeBlock';
95
124
  export default CodeBlock;
@@ -46,9 +46,13 @@ var Highlight = /*#__PURE__*/memo(function Highlight(_ref) {
46
46
  var code = text;
47
47
  var generatorClassName = 'prismjs';
48
48
  var containerProps = _objectSpread(_objectSpread({}, rest), {}, {
49
+ ref: rest.scrollRef,
49
50
  'data-testid': testId,
50
51
  className: rest.className ? "".concat(generatorClassName, " ").concat(rest.className) : generatorClassName
51
52
  });
53
+ if (containerProps.hasOwnProperty('scrollRef')) {
54
+ delete containerProps['scrollRef'];
55
+ }
52
56
  if (shouldWrapLongLines) {
53
57
  codeTagProps.style = {
54
58
  whiteSpace: 'pre-wrap',
@@ -70,6 +70,11 @@ export interface CodeBlockProps {
70
70
  * @default false
71
71
  */
72
72
  shouldWrapLongLines?: boolean;
73
+ /**
74
+ * Text used to describe that the content of the code block is scrollable.
75
+ * Set only if the code block is scrollable. Defaults to 'Scrollable content'.
76
+ */
77
+ label?: string;
73
78
  }
74
79
  export type { SupportedLanguages, LanguageAlias, Language } from '../constants';
75
80
  export type { CodeBlockTheme, CodeTheme } from './theme/types';
@@ -70,6 +70,11 @@ export interface CodeBlockProps {
70
70
  * @default false
71
71
  */
72
72
  shouldWrapLongLines?: boolean;
73
+ /**
74
+ * Text used to describe that the content of the code block is scrollable.
75
+ * Set only if the code block is scrollable. Defaults to 'Scrollable content'.
76
+ */
77
+ label?: string;
73
78
  }
74
79
  export type { SupportedLanguages, LanguageAlias, Language } from '../constants';
75
80
  export type { CodeBlockTheme, CodeTheme } from './theme/types';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/code",
3
- "version": "17.1.2",
3
+ "version": "17.2.0",
4
4
  "description": "Code highlights short strings of code snippets inline with body text.",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/"
@@ -35,7 +35,9 @@
35
35
  "@atlaskit/visually-hidden": "^3.0.0",
36
36
  "@babel/runtime": "^7.0.0",
37
37
  "@compiled/react": "^0.18.3",
38
+ "bind-event-listener": "^3.0.0",
38
39
  "memoize-one": "^6.0.0",
40
+ "raf-schd": "^4.0.3",
39
41
  "refractor": "^3.6.0"
40
42
  },
41
43
  "peerDependencies": {