@ndla/ui 12.1.0 → 13.1.1

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 (37) hide show
  1. package/es/Breadcrumb/Breadcrumb.js +101 -24
  2. package/es/Breadcrumb/BreadcrumbItem.js +70 -18
  3. package/es/Breadcrumb/HeaderBreadcrumb.js +53 -0
  4. package/es/Breadcrumb/HomeBreadcrumb.js +117 -0
  5. package/es/Breadcrumb/index.js +2 -1
  6. package/es/Breadcrumblist/index.js +1 -2
  7. package/es/all.css +1 -1
  8. package/es/index.js +1 -1
  9. package/lib/Breadcrumb/Breadcrumb.d.ts +10 -12
  10. package/lib/Breadcrumb/Breadcrumb.js +104 -32
  11. package/lib/Breadcrumb/BreadcrumbItem.d.ts +15 -7
  12. package/lib/Breadcrumb/BreadcrumbItem.js +68 -20
  13. package/lib/Breadcrumb/HeaderBreadcrumb.d.ts +15 -0
  14. package/lib/Breadcrumb/HeaderBreadcrumb.js +67 -0
  15. package/lib/Breadcrumb/HomeBreadcrumb.d.ts +15 -0
  16. package/lib/Breadcrumb/HomeBreadcrumb.js +127 -0
  17. package/lib/Breadcrumb/index.d.ts +3 -1
  18. package/lib/Breadcrumb/index.js +11 -3
  19. package/lib/Breadcrumblist/index.js +0 -1
  20. package/lib/all.css +1 -1
  21. package/lib/index.d.ts +2 -1
  22. package/lib/index.js +10 -3
  23. package/package.json +2 -2
  24. package/src/Breadcrumb/Breadcrumb.tsx +76 -40
  25. package/src/Breadcrumb/BreadcrumbItem.tsx +84 -18
  26. package/src/Breadcrumb/HeaderBreadcrumb.tsx +57 -0
  27. package/src/Breadcrumb/HomeBreadcrumb.tsx +88 -0
  28. package/src/Breadcrumb/index.ts +5 -1
  29. package/src/Breadcrumblist/index.tsx +0 -1
  30. package/src/index.ts +2 -1
  31. package/src/main.scss +0 -2
  32. package/es/Breadcrumb/BreadcrumbBlock.js +0 -90
  33. package/lib/Breadcrumb/BreadcrumbBlock.d.ts +0 -15
  34. package/lib/Breadcrumb/BreadcrumbBlock.js +0 -105
  35. package/src/Breadcrumb/BreadcrumbBlock.tsx +0 -80
  36. package/src/Breadcrumb/component.breadcrumb-block.scss +0 -98
  37. package/src/Breadcrumb/component.breadcrumb.scss +0 -104
package/lib/index.d.ts CHANGED
@@ -58,7 +58,8 @@ export { default as Topic } from './Topic';
58
58
  export type { TopicProps } from './Topic';
59
59
  export { default as Aside } from './Aside';
60
60
  export { default as AuthorInfo } from './AuthorInfo';
61
- export { default as Breadcrumb, BreadcrumbBlock } from './Breadcrumb';
61
+ export { default as Breadcrumb, HeaderBreadcrumb, HomeBreadcrumb } from './Breadcrumb';
62
+ export type { SimpleBreadcrumbItem, IndexedBreadcrumbItem } from './Breadcrumb';
62
63
  export type { BreadcrumbItemProps } from './Breadcrumblist/Breadcrumblist';
63
64
  export { i18nInstance, formatNestedMessages, formatMessage } from './i18n';
64
65
  export { default as ResourceGroup } from './ResourceGroup';
package/lib/index.js CHANGED
@@ -119,7 +119,8 @@ var _exportNames = {
119
119
  Aside: true,
120
120
  AuthorInfo: true,
121
121
  Breadcrumb: true,
122
- BreadcrumbBlock: true,
122
+ HeaderBreadcrumb: true,
123
+ HomeBreadcrumb: true,
123
124
  i18nInstance: true,
124
125
  formatNestedMessages: true,
125
126
  formatMessage: true,
@@ -856,10 +857,16 @@ Object.defineProperty(exports, "Breadcrumb", {
856
857
  return _Breadcrumb["default"];
857
858
  }
858
859
  });
859
- Object.defineProperty(exports, "BreadcrumbBlock", {
860
+ Object.defineProperty(exports, "HeaderBreadcrumb", {
860
861
  enumerable: true,
861
862
  get: function get() {
862
- return _Breadcrumb.BreadcrumbBlock;
863
+ return _Breadcrumb.HeaderBreadcrumb;
864
+ }
865
+ });
866
+ Object.defineProperty(exports, "HomeBreadcrumb", {
867
+ enumerable: true,
868
+ get: function get() {
869
+ return _Breadcrumb.HomeBreadcrumb;
863
870
  }
864
871
  });
865
872
  Object.defineProperty(exports, "i18nInstance", {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ndla/ui",
3
- "version": "12.1.0",
3
+ "version": "13.1.1",
4
4
  "description": "UI component library for NDLA.",
5
5
  "license": "GPL-3.0",
6
6
  "main": "lib/index.js",
@@ -81,5 +81,5 @@
81
81
  "publishConfig": {
82
82
  "access": "public"
83
83
  },
84
- "gitHead": "edc7a29956dd163f2e0df15537f4312031946c03"
84
+ "gitHead": "92aedcd209594cb7225467e957c3b86878b5c409"
85
85
  }
@@ -6,47 +6,83 @@
6
6
  *
7
7
  */
8
8
 
9
- import React, { ReactNode } from 'react';
10
- import BEMHelper, { ReturnObject } from 'react-bem-helper';
9
+ import React, { ReactNode, useRef } from 'react';
10
+ import { useComponentSize, useIsomorphicLayoutEffect } from '@ndla/hooks';
11
11
  import { uuid } from '@ndla/util';
12
- import { withTranslation, WithTranslation } from 'react-i18next';
13
- import { Home } from '@ndla/icons/common';
14
- import BreadcrumbItem from './BreadcrumbItem';
15
-
16
- const classes: BEMHelper<ReturnObject> = BEMHelper({
17
- name: 'breadcrumb',
18
- prefix: 'c-',
19
- });
20
-
21
- export interface BreadcrumbItemI {
22
- to: string;
23
- name: string;
24
- }
12
+ import styled from '@emotion/styled';
13
+ import { useTranslation } from 'react-i18next';
14
+ import BreadcrumbItem, { IndexedBreadcrumbItem, SimpleBreadcrumbItem } from './BreadcrumbItem';
25
15
 
26
- interface Props extends WithTranslation {
27
- children?: ReactNode;
28
- items: BreadcrumbItemI[];
29
- invertedStyle: boolean;
16
+ interface Props {
17
+ items: SimpleBreadcrumbItem[];
18
+ autoCollapse?: boolean;
19
+ collapseFirst?: boolean;
20
+ renderItem?: (item: IndexedBreadcrumbItem, totalCount: number) => ReactNode;
21
+ renderSeparator?: (item: IndexedBreadcrumbItem, totalCount: number) => ReactNode;
30
22
  }
31
23
 
32
- const Breadcrumb = ({ children, items, invertedStyle, t }: Props) => (
33
- <nav aria-label={t('breadcrumb.breadcrumb')}>
34
- {children}
35
- <ol {...classes('list')}>
36
- {items.map((item, i) => (
37
- <BreadcrumbItem
38
- invertedStyle={invertedStyle}
39
- classes={classes}
40
- home={i === 0}
41
- key={uuid()}
42
- isCurrent={i === items.length - 1}
43
- to={item.to}
44
- name={item.name}>
45
- {i === 0 ? <Home className="c-icon--20" title={item.name} /> : item.name}
46
- </BreadcrumbItem>
47
- ))}
48
- </ol>
49
- </nav>
50
- );
51
-
52
- export default withTranslation()(Breadcrumb);
24
+ const BreadcrumbNav = styled.nav``;
25
+
26
+ const StyledList = styled.ol`
27
+ display: inline-block;
28
+ padding-left: 0;
29
+ margin-bottom: 0;
30
+ margin-top: 0;
31
+ list-style: none;
32
+ `;
33
+
34
+ const Breadcrumb = ({ items, autoCollapse, renderItem, renderSeparator, collapseFirst }: Props) => {
35
+ const { t } = useTranslation();
36
+ const olRef = useRef<any>();
37
+ const containerRef = useRef<HTMLDivElement>(null);
38
+ // No idiomatic way of dealing with sets of refs yet
39
+ // See: https://github.com/facebook/react/issues/14072#issuecomment-446777406
40
+ const breadcrumbItemRefs = useRef(new Map()).current;
41
+ const size = useComponentSize(containerRef);
42
+
43
+ useIsomorphicLayoutEffect(() => {
44
+ if (!autoCollapse) {
45
+ return;
46
+ }
47
+ // Create an array of all breadcrumb item refs
48
+ const items = Array.from(breadcrumbItemRefs).map(([key, value]) => value);
49
+
50
+ // Clear max width on all items
51
+ items.forEach((el) => {
52
+ el.setMaxWidth('none');
53
+ });
54
+
55
+ // Set maxWidth on breadcrumb text items iteratively until
56
+ // the ordered list fits on a single line. It's on a single line
57
+ // if the height of the list is less then 70.
58
+ items.forEach((el) => {
59
+ if (olRef.current.offsetHeight > 60) {
60
+ el.setMaxWidth('40px');
61
+ }
62
+ });
63
+ }, [size]);
64
+
65
+ return (
66
+ <BreadcrumbNav ref={containerRef} aria-label={t('breadcrumb.breadcrumb')}>
67
+ <StyledList ref={olRef}>
68
+ {items.map((item, index) => (
69
+ <BreadcrumbItem
70
+ autoCollapse={autoCollapse}
71
+ renderItem={renderItem}
72
+ renderSeparator={renderSeparator}
73
+ ref={(element) =>
74
+ element === null || (!collapseFirst && index === 0) // skip first item which is never truncated
75
+ ? breadcrumbItemRefs.delete(item.to)
76
+ : breadcrumbItemRefs.set(item.to, element)
77
+ }
78
+ key={uuid()}
79
+ totalCount={items.length}
80
+ item={{ ...item, index }}
81
+ />
82
+ ))}
83
+ </StyledList>
84
+ </BreadcrumbNav>
85
+ );
86
+ };
87
+
88
+ export default Breadcrumb;
@@ -9,37 +9,103 @@
9
9
  import React, { useRef, useImperativeHandle, ReactNode, forwardRef } from 'react';
10
10
  import { ChevronRight } from '@ndla/icons/common';
11
11
  import SafeLink from '@ndla/safelink';
12
- import BEMHelper, { ReturnObject } from 'react-bem-helper';
12
+ import styled from '@emotion/styled';
13
+ import { mq, spacing, breakpoints } from '@ndla/core';
14
+ import { css } from '@emotion/core';
13
15
 
14
- interface Props {
15
- classes: BEMHelper<ReturnObject>;
16
- isCurrent: boolean;
17
- children: ReactNode;
16
+ export interface SimpleBreadcrumbItem {
18
17
  to: string | Partial<Location>;
19
- home: boolean;
20
18
  name: string;
21
- invertedStyle: boolean;
19
+ }
20
+
21
+ export interface IndexedBreadcrumbItem extends SimpleBreadcrumbItem {
22
+ index: number;
23
+ }
24
+
25
+ export interface BreadcrumbRenderProps {
26
+ item: IndexedBreadcrumbItem;
27
+ totalCount: number;
28
+ }
29
+
30
+ interface AutoCollapseProps {
31
+ autoCollapse?: boolean;
32
+ }
33
+
34
+ const StyledListItem = styled.li<AutoCollapseProps>`
35
+ margin-bottom: 0;
36
+ margin-left: 0;
37
+ display: inline-flex;
38
+ align-items: center;
39
+ :before {
40
+ display: none;
41
+ }
42
+
43
+ ${({ autoCollapse }) =>
44
+ !autoCollapse &&
45
+ css`
46
+ ${mq.range({ until: breakpoints.tablet })} {
47
+ display: block;
48
+ }
49
+ `}
50
+ `;
51
+
52
+ const CollapseContainer = styled.div<AutoCollapseProps>`
53
+ display: inline-block;
54
+ color: inherit;
55
+ ${({ autoCollapse }) =>
56
+ autoCollapse &&
57
+ css`
58
+ text-overflow: ellipsis;
59
+ white-space: nowrap;
60
+ overflow: hidden;
61
+ display: inline-block;
62
+ `}
63
+ `;
64
+
65
+ const StyledChevron = styled(ChevronRight)`
66
+ margin: ${spacing.xxsmall};
67
+ color: inherit;
68
+ `;
69
+
70
+ const StyledSafeLink = styled(SafeLink)`
71
+ color: inherit;
72
+ `;
73
+
74
+ interface Props {
75
+ item: IndexedBreadcrumbItem;
76
+ autoCollapse?: boolean;
77
+ totalCount: number;
78
+ renderItem?: (item: IndexedBreadcrumbItem, totalCount: number) => ReactNode;
79
+ renderSeparator?: (item: IndexedBreadcrumbItem, totalCount: number) => ReactNode;
22
80
  }
23
81
 
24
82
  const BreadcrumbItem = forwardRef<any, Props>(
25
- ({ to, children, classes, isCurrent, home, invertedStyle, name }, ref) => {
83
+ ({ renderItem, renderSeparator, item, totalCount, autoCollapse }, ref) => {
26
84
  const liRef = useRef<any>();
85
+
27
86
  useImperativeHandle(ref, () => ({
28
87
  setMaxWidth: (maxWidth: number) => {
29
88
  liRef.current.children[0].style.maxWidth = maxWidth;
30
89
  },
31
90
  }));
91
+
92
+ const { to, name, index } = item;
93
+ const isLast = index === totalCount - 1;
32
94
  return (
33
- <li {...classes('item', { home, invertedStyle })} ref={liRef}>
34
- {isCurrent ? (
35
- <span>{children}</span>
36
- ) : (
37
- <SafeLink to={to} aria-label={home ? name : undefined}>
38
- {children}
39
- </SafeLink>
40
- )}
41
- {!home && <ChevronRight />}
42
- </li>
95
+ <StyledListItem ref={liRef} autoCollapse={autoCollapse}>
96
+ <CollapseContainer autoCollapse={autoCollapse}>
97
+ {renderItem ? (
98
+ renderItem(item, totalCount)
99
+ ) : isLast ? (
100
+ <span>{name}</span>
101
+ ) : (
102
+ <StyledSafeLink to={to}>
103
+ <span>{name}</span>
104
+ </StyledSafeLink>
105
+ )}
106
+ </CollapseContainer>
107
+ {renderSeparator ? renderSeparator(item, totalCount) : !isLast && <StyledChevron />}
108
+ </StyledListItem>
43
109
  );
44
110
  },
45
111
  );
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Copyright (c) 2022-present, NDLA.
3
+ *
4
+ * This source code is licensed under the GPLv3 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import styled from '@emotion/styled';
10
+ import { colors, fonts, spacing } from '@ndla/core';
11
+ import { ChevronRight } from '@ndla/icons/common';
12
+ import SafeLink from '@ndla/safelink';
13
+ import React from 'react';
14
+ import Breadcrumb from './Breadcrumb';
15
+ import { IndexedBreadcrumbItem, SimpleBreadcrumbItem } from './BreadcrumbItem';
16
+
17
+ interface ThemeProps {
18
+ light: boolean | undefined;
19
+ }
20
+
21
+ const HeaderBreadcrumbWrapper = styled.div<ThemeProps>`
22
+ color: ${({ light }) => (light ? colors.white : colors.brand.primary)};
23
+ `;
24
+
25
+ const StyledHeaderSafeLink = styled(SafeLink)`
26
+ ${fonts.sizes(14)};
27
+ font-weight: ${fonts.weight.bold};
28
+ color: inherit;
29
+ `;
30
+
31
+ const StyledRightChevron = styled(ChevronRight)`
32
+ margin: ${spacing.xxsmall};
33
+ color: inherit;
34
+ `;
35
+
36
+ interface Props {
37
+ items: SimpleBreadcrumbItem[];
38
+ light?: boolean;
39
+ }
40
+
41
+ const HeaderBreadcrumb = ({ items, light }: Props) => {
42
+ const renderSeparator = (item: IndexedBreadcrumbItem, totalCount: number) => {
43
+ if (item.index === totalCount - 1) {
44
+ return null;
45
+ }
46
+ return <StyledRightChevron />;
47
+ };
48
+
49
+ return (
50
+ <HeaderBreadcrumbWrapper light={light}>
51
+ <StyledHeaderSafeLink to={items[0]?.to}>{items[0]?.name}</StyledHeaderSafeLink>
52
+ <Breadcrumb items={items.slice(1)} renderSeparator={renderSeparator} autoCollapse collapseFirst />
53
+ </HeaderBreadcrumbWrapper>
54
+ );
55
+ };
56
+
57
+ export default HeaderBreadcrumb;
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Copyright (c) 2022-present, NDLA.
3
+ *
4
+ * This source code is licensed under the GPLv3 license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ */
8
+
9
+ import styled from '@emotion/styled';
10
+ import { breakpoints, colors, fonts, mq, spacing } from '@ndla/core';
11
+ import { ChevronRight, Home } from '@ndla/icons/common';
12
+ import SafeLink from '@ndla/safelink';
13
+ import React from 'react';
14
+ import Breadcrumb from './Breadcrumb';
15
+ import { IndexedBreadcrumbItem, SimpleBreadcrumbItem } from './BreadcrumbItem';
16
+
17
+ interface ThemeProps {
18
+ light: boolean | undefined;
19
+ }
20
+
21
+ const StyledSeparator = styled.div<ThemeProps>`
22
+ ${fonts.sizes('14px')};
23
+ margin: 0 ${spacing.small};
24
+ user-select: none;
25
+ color: ${({ light }) => (light ? colors.white : colors.text.primary)};
26
+ ${mq.range({ until: breakpoints.tablet })} {
27
+ display: none;
28
+ }
29
+ `;
30
+
31
+ const StyledIconSafeLink = styled(SafeLink)`
32
+ box-shadow: none;
33
+ border-bottom: none;
34
+ `;
35
+ const StyledHome = styled(Home)<ThemeProps>`
36
+ width: 20px;
37
+ height: 20px;
38
+ color: ${({ light }) => (light ? colors.white : colors.text.primary)};
39
+ `;
40
+ const StyledRightChevron = styled(ChevronRight)<ThemeProps>`
41
+ color: ${({ light }) => (light ? colors.white : colors.text.primary)};
42
+ margin: ${spacing.xxsmall};
43
+ `;
44
+ const StyledSpan = styled.span<ThemeProps>`
45
+ color: ${({ light }) => (light ? colors.white : colors.text.primary)};
46
+ `;
47
+ const StyledSafeLink = styled(SafeLink)<ThemeProps>`
48
+ color: ${({ light }) => (light ? colors.white : colors.text.primary)};
49
+ `;
50
+
51
+ interface Props {
52
+ items: SimpleBreadcrumbItem[];
53
+ light?: boolean;
54
+ }
55
+
56
+ const HomeBreadcrumb = ({ items, light }: Props) => {
57
+ const renderItem = (item: IndexedBreadcrumbItem, totalCount: number) => {
58
+ if (item.index === totalCount - 1) {
59
+ return <StyledSpan light={light}>{item.name}</StyledSpan>;
60
+ }
61
+ if (item.index === 0) {
62
+ return (
63
+ <StyledIconSafeLink aria-label={item.name} to={item.to}>
64
+ <StyledHome title={item.name} light={light} />
65
+ </StyledIconSafeLink>
66
+ );
67
+ }
68
+ return (
69
+ <StyledSafeLink light={light} to={item.to}>
70
+ {item.name}
71
+ </StyledSafeLink>
72
+ );
73
+ };
74
+
75
+ const renderSeparator = (item: IndexedBreadcrumbItem, totalCount: number) => {
76
+ if (item.index === totalCount - 1) {
77
+ return null;
78
+ }
79
+ if (item.index === 0) {
80
+ return <StyledSeparator light={light}>|</StyledSeparator>;
81
+ }
82
+ return <StyledRightChevron light={light} />;
83
+ };
84
+
85
+ return <Breadcrumb items={items} renderItem={renderItem} renderSeparator={renderSeparator} />;
86
+ };
87
+
88
+ export default HomeBreadcrumb;
@@ -8,6 +8,10 @@
8
8
 
9
9
  import Breadcrumb from './Breadcrumb';
10
10
 
11
- export { default as BreadcrumbBlock } from './BreadcrumbBlock';
11
+ export type { SimpleBreadcrumbItem, IndexedBreadcrumbItem } from './BreadcrumbItem';
12
+
13
+ export { default as HeaderBreadcrumb } from './HeaderBreadcrumb';
14
+
15
+ export { default as HomeBreadcrumb } from './HomeBreadcrumb';
12
16
 
13
17
  export default Breadcrumb;
@@ -1,4 +1,3 @@
1
1
  import Breadcrumblist from './Breadcrumblist';
2
2
 
3
- // export { default as BreadcrumbBlock } from './BreadcrumbBlock';
4
3
  export default Breadcrumblist;
package/src/index.ts CHANGED
@@ -151,7 +151,8 @@ export type { TopicProps } from './Topic';
151
151
  export { default as Aside } from './Aside';
152
152
  export { default as AuthorInfo } from './AuthorInfo';
153
153
 
154
- export { default as Breadcrumb, BreadcrumbBlock } from './Breadcrumb';
154
+ export { default as Breadcrumb, HeaderBreadcrumb, HomeBreadcrumb } from './Breadcrumb';
155
+ export type { SimpleBreadcrumbItem, IndexedBreadcrumbItem } from './Breadcrumb';
155
156
 
156
157
  export type { BreadcrumbItemProps } from './Breadcrumblist/Breadcrumblist';
157
158
  export { i18nInstance, formatNestedMessages, formatMessage } from './i18n';
package/src/main.scss CHANGED
@@ -8,8 +8,6 @@
8
8
  @import 'Article/component.article-author-popup';
9
9
 
10
10
  @import 'global/components/component.bodybox';
11
- @import 'Breadcrumb/component.breadcrumb';
12
- @import 'Breadcrumb/component.breadcrumb-block';
13
11
  @import 'global/components/component.story-intro';
14
12
  @import 'Figure/component.figure';
15
13
  @import 'Figure/component.figure-license';
@@ -1,90 +0,0 @@
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
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
4
-
5
- function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
6
-
7
- function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
8
-
9
- function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
10
-
11
- function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
12
-
13
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
14
-
15
- /**
16
- * Copyright (c) 2019-present, NDLA.
17
- *
18
- * This source code is licensed under the GPLv3 license found in the
19
- * LICENSE file in the root directory of this source tree.
20
- *
21
- */
22
- import React, { useRef } from 'react';
23
- import BEMHelper from 'react-bem-helper';
24
- import { useComponentSize, useIsomorphicLayoutEffect } from '@ndla/hooks';
25
- import { useTranslation } from 'react-i18next';
26
- import BreadcrumbItem from './BreadcrumbItem';
27
- import { jsx as ___EmotionJSX } from "@emotion/core";
28
- var classes = BEMHelper({
29
- name: 'breadcrumb-block',
30
- prefix: 'c-'
31
- });
32
-
33
- var BreadcrumbBlock = function BreadcrumbBlock(_ref) {
34
- var children = _ref.children,
35
- items = _ref.items;
36
-
37
- var _useTranslation = useTranslation(),
38
- t = _useTranslation.t;
39
-
40
- var olRef = useRef();
41
- var containerRef = useRef(null); // No idiomatic way of dealing with sets of refs yet
42
- // See: https://github.com/facebook/react/issues/14072#issuecomment-446777406
43
-
44
- var breadcrumbItemRefs = useRef(new Map()).current;
45
- var size = useComponentSize(containerRef);
46
- useIsomorphicLayoutEffect(function () {
47
- // Create an array of all breadcrumb item refs
48
- var items = Array.from(breadcrumbItemRefs).map(function (_ref2) {
49
- var _ref3 = _slicedToArray(_ref2, 2),
50
- key = _ref3[0],
51
- value = _ref3[1];
52
-
53
- return value;
54
- }); // Clear max width on all items
55
-
56
- items.forEach(function (el) {
57
- el.setMaxWidth('none');
58
- }); // Set maxWidth on breadcrumb text items iteratively until
59
- // the ordered list fits on a single line. It's on a single line
60
- // if the height of the list is less then 70.
61
-
62
- items.forEach(function (el) {
63
- if (olRef.current.offsetHeight > 70) {
64
- el.setMaxWidth('40px');
65
- }
66
- });
67
- }, [size]);
68
- return ___EmotionJSX("nav", _extends({}, classes(''), {
69
- ref: containerRef,
70
- "aria-label": t('breadcrumb.breadcrumb')
71
- }), children, ___EmotionJSX("ol", _extends({}, classes('list'), {
72
- ref: olRef
73
- }), items.map(function (item, i) {
74
- return ___EmotionJSX(BreadcrumbItem, {
75
- ref: function ref(element) {
76
- return element === null || i === 0 // skip first item which is never truncated
77
- ? breadcrumbItemRefs["delete"](item.to) : breadcrumbItemRefs.set(item.to, element);
78
- },
79
- classes: classes,
80
- key: item.to,
81
- isCurrent: i === items.length - 1,
82
- to: item.to,
83
- name: item.name,
84
- home: false,
85
- invertedStyle: false
86
- }, item.name);
87
- })));
88
- };
89
-
90
- export default BreadcrumbBlock;
@@ -1,15 +0,0 @@
1
- /**
2
- * Copyright (c) 2019-present, NDLA.
3
- *
4
- * This source code is licensed under the GPLv3 license found in the
5
- * LICENSE file in the root directory of this source tree.
6
- *
7
- */
8
- import { ReactNode } from 'react';
9
- import { BreadcrumbItemI } from './Breadcrumb';
10
- interface Props {
11
- children?: ReactNode;
12
- items: BreadcrumbItemI[];
13
- }
14
- declare const BreadcrumbBlock: ({ children, items }: Props) => JSX.Element;
15
- export default BreadcrumbBlock;