@skyscanner/backpack-web 42.15.0 → 42.17.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.
Files changed (50) hide show
  1. package/bpk-component-accordion/src/BpkAccordionItem.module.css +1 -1
  2. package/bpk-component-autosuggest/index.d.ts +6 -0
  3. package/bpk-component-autosuggest/index.js +3 -1
  4. package/bpk-component-autosuggest/src/BpkAutosuggest.d.ts +2 -0
  5. package/bpk-component-autosuggest/src/BpkAutosuggest.js +14 -15
  6. package/bpk-component-autosuggest/src/BpkAutosuggest.module.css +1 -1
  7. package/bpk-component-autosuggest/src/BpkAutosuggestSuggestion.d.ts +11 -0
  8. package/bpk-component-autosuggest/src/BpkAutosuggestSuggestion.js +35 -55
  9. package/bpk-component-autosuggest/src/BpkAutosuggestV2/BpkAutosuggest.module.css +1 -1
  10. package/bpk-component-collapsible/index.d.ts +11 -0
  11. package/bpk-component-collapsible/index.js +22 -0
  12. package/bpk-component-collapsible/src/BpkCollapsible.d.ts +8 -0
  13. package/bpk-component-collapsible/src/BpkCollapsible.js +31 -0
  14. package/bpk-component-collapsible/src/BpkCollapsible.module.css +18 -0
  15. package/bpk-component-collapsible/src/BpkCollapsibleContent.d.ts +6 -0
  16. package/bpk-component-collapsible/src/BpkCollapsibleContent.js +36 -0
  17. package/bpk-component-collapsible/src/BpkCollapsibleIndicator.d.ts +6 -0
  18. package/bpk-component-collapsible/src/BpkCollapsibleIndicator.js +34 -0
  19. package/bpk-component-collapsible/src/BpkCollapsibleRoot.d.ts +22 -0
  20. package/bpk-component-collapsible/src/BpkCollapsibleRoot.js +49 -0
  21. package/bpk-component-collapsible/src/BpkCollapsibleRootProvider.d.ts +10 -0
  22. package/bpk-component-collapsible/src/BpkCollapsibleRootProvider.js +33 -0
  23. package/bpk-component-collapsible/src/BpkCollapsibleTrigger.d.ts +6 -0
  24. package/bpk-component-collapsible/src/BpkCollapsibleTrigger.js +43 -0
  25. package/bpk-component-collapsible/src/common-types.d.ts +9 -0
  26. package/bpk-component-collapsible/src/common-types.js +29 -0
  27. package/bpk-component-collapsible/src/useBpkCollapsible.d.ts +5 -0
  28. package/bpk-component-collapsible/src/useBpkCollapsible.js +21 -0
  29. package/bpk-component-comparison-table/src/BpkComparisonTable/BpkComparisonTable.module.css +1 -1
  30. package/bpk-component-comparison-table/src/BpkComparisonTable/BpkComparisonTableColumnHeader.d.ts +2 -4
  31. package/bpk-component-comparison-table/src/BpkComparisonTable/BpkComparisonTableColumnHeader.js +1 -15
  32. package/bpk-component-comparison-table/src/BpkComparisonTable/BpkComparisonTableContent.js +20 -4
  33. package/bpk-component-comparison-table/src/BpkComparisonTable/BpkComparisonTableHeaderRow.d.ts +1 -2
  34. package/bpk-component-comparison-table/src/BpkComparisonTable/BpkComparisonTableHeaderRow.js +1 -5
  35. package/bpk-component-comparison-table/src/BpkComparisonTable/BpkComparisonTableRoot.d.ts +1 -1
  36. package/bpk-component-comparison-table/src/BpkComparisonTable/BpkComparisonTableRoot.js +25 -13
  37. package/bpk-component-comparison-table/src/BpkComparisonTable/common-types.d.ts +2 -0
  38. package/bpk-component-comparison-table/src/BpkComparisonTray/BpkComparisonTray.module.css +1 -1
  39. package/bpk-component-horizontal-nav/index.d.ts +6 -0
  40. package/bpk-component-horizontal-nav/index.js +3 -1
  41. package/bpk-component-horizontal-nav/src/BpkHorizontalNav.d.ts +27 -0
  42. package/bpk-component-horizontal-nav/src/BpkHorizontalNav.js +79 -114
  43. package/bpk-component-horizontal-nav/src/BpkHorizontalNavItem.d.ts +25 -0
  44. package/bpk-component-horizontal-nav/src/BpkHorizontalNavItem.js +38 -73
  45. package/bpk-component-switch/index.d.ts +3 -3
  46. package/bpk-component-switch/index.js +2 -2
  47. package/bpk-component-switch/src/BpkSwitch.d.ts +7 -1
  48. package/bpk-component-switch/src/BpkSwitch.js +10 -1
  49. package/bpk-component-switch/src/BpkSwitch.module.css +1 -1
  50. package/package.json +2 -2
@@ -0,0 +1,21 @@
1
+ /*
2
+ * Backpack - Skyscanner's Design System
3
+ *
4
+ * Copyright 2016 Skyscanner Ltd
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ import { useCollapsible } from '@ark-ui/react';
20
+ const useBpkCollapsible = props => useCollapsible(props);
21
+ export default useBpkCollapsible;
@@ -15,4 +15,4 @@
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
17
  */
18
- .bpk-comparison-table__scroll-container{flex:1;overflow:auto;min-block-size:0;overscroll-behavior:contain}.bpk-comparison-table__scroll-container tbody{transform:translateY(var(--bpk-rows-offset, 0))}.bpk-comparison-table__scroll-container td:not(:last-child){border-inline-end:1px solid #c1c7cf}.bpk-comparison-table__scroll-container th:not(:last-child)::after{position:absolute;content:"";background-color:#c1c7cf;inline-size:1px;inset-block-end:0;inset-block-start:1rem;inset-inline-end:0}.bpk-comparison-table__scroll-container table{border-collapse:separate;border-spacing:0;min-inline-size:33.75rem}.bpk-comparison-table__scroll-container th{position:sticky;z-index:1;padding:0;background-color:#fff;vertical-align:top;inset-block-start:0}.bpk-comparison-table__header-content{display:flex;padding:1rem;flex-direction:column}.bpk-comparison-table__header-image-wrapper{position:relative;opacity:var(--bpk-image-opacity, 1);margin-block-end:1rem}.bpk-comparison-table__header-image-area{display:flex;justify-content:center;align-items:center;border-radius:.25rem;background-color:#e0e4e9;overflow:hidden;block-size:5.1875rem}.bpk-comparison-table__header-image-area img{max-block-size:100%;max-inline-size:100%;object-fit:contain}.bpk-comparison-table__header-id-section{margin-block-start:calc(6.1875rem*(var(--bpk-image-opacity, 1) - 1))}.bpk-comparison-table__best-tag{position:absolute;z-index:1;transform:translateY(-50%);inset-block-start:0}.bpk-comparison-table__remove-button{opacity:var(--bpk-image-opacity, 1);overflow:clip;margin-block-start:calc(.5rem*var(--bpk-image-opacity, 1));max-block-size:calc(5rem*var(--bpk-image-opacity, 1));overflow-clip-margin:.25rem}.bpk-comparison-table__placeholder-cell{background-color:#fff}
18
+ .bpk-comparison-table__scroll-container{flex:1;overflow:auto;min-block-size:0;overscroll-behavior:contain}.bpk-comparison-table__scroll-container tbody{transform:translateY(var(--bpk-rows-offset, 0))}.bpk-comparison-table__scroll-container td:not(:last-child){border-inline-end:1px solid #c1c7cf}.bpk-comparison-table__scroll-container th:not(:last-child)::after{position:absolute;content:"";background-color:#c1c7cf;inline-size:1px;inset-block-end:0;inset-block-start:1rem;inset-inline-end:0}.bpk-comparison-table__scroll-container table{border-collapse:separate;border-spacing:0;box-shadow:none;min-inline-size:33.75rem}.bpk-comparison-table__scroll-container th{position:sticky;z-index:1;padding:0;background-color:#fff;vertical-align:top;inset-block-start:0}.bpk-comparison-table__header-content{display:flex;padding:1rem;flex-direction:column}.bpk-comparison-table__header-image-wrapper{position:relative;opacity:var(--bpk-image-opacity, 1);margin-block-end:1rem}.bpk-comparison-table__header-image-area{display:flex;justify-content:center;align-items:center;border-radius:.25rem;background-color:#e0e4e9;overflow:hidden;block-size:5.1875rem}.bpk-comparison-table__header-image-area img{max-block-size:100%;max-inline-size:100%;object-fit:contain}.bpk-comparison-table__header-id-section{margin-block-start:calc(6.1875rem*(var(--bpk-image-opacity, 1) - 1))}.bpk-comparison-table__best-tag{position:absolute;z-index:1;transform:translateY(-50%);inset-block-start:0}.bpk-comparison-table__remove-cell{padding:0}.bpk-comparison-table__remove-cell-inner{padding:0 1rem calc(1rem*var(--bpk-image-opacity, 1));opacity:var(--bpk-image-opacity, 1);overflow:clip;max-block-size:calc(3.5rem*var(--bpk-image-opacity, 1));overflow-clip-margin:.25rem}.bpk-comparison-table__placeholder-cell{background-color:#fff}
@@ -1,8 +1,6 @@
1
1
  import type { BpkCompareColumn } from './common-types';
2
- type BpkComparisonTableColumnHeaderProps = Pick<BpkCompareColumn, 'imageSrc' | 'imageAlt' | 'headerContent' | 'itemId' | 'bestTag' | 'removeA11yLabel'> & {
3
- removeLabel: string;
2
+ type BpkComparisonTableColumnHeaderProps = Pick<BpkCompareColumn, 'imageSrc' | 'imageAlt' | 'headerContent' | 'bestTag'> & {
4
3
  bestTagLabel: string;
5
- onRemove: (itemId: string) => void;
6
4
  };
7
- declare const BpkComparisonTableColumnHeader: ({ bestTag, bestTagLabel, headerContent, imageAlt, imageSrc, itemId, onRemove, removeA11yLabel, removeLabel, }: BpkComparisonTableColumnHeaderProps) => import("react/jsx-runtime").JSX.Element;
5
+ declare const BpkComparisonTableColumnHeader: ({ bestTag, bestTagLabel, headerContent, imageAlt, imageSrc, }: BpkComparisonTableColumnHeaderProps) => import("react/jsx-runtime").JSX.Element;
8
6
  export default BpkComparisonTableColumnHeader;
@@ -17,7 +17,6 @@
17
17
  */
18
18
 
19
19
  import BpkBadge, { BADGE_TYPES } from "../../../bpk-component-badge";
20
- import BpkButton, { BUTTON_TYPES, SIZE_TYPES } from "../../../bpk-component-button";
21
20
  import { cssModules } from "../../../bpk-react-utils";
22
21
  import STYLES from "./BpkComparisonTable.module.css";
23
22
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
@@ -27,11 +26,7 @@ const BpkComparisonTableColumnHeader = ({
27
26
  bestTagLabel,
28
27
  headerContent,
29
28
  imageAlt = '',
30
- imageSrc,
31
- itemId,
32
- onRemove,
33
- removeA11yLabel,
34
- removeLabel
29
+ imageSrc
35
30
  }) => /*#__PURE__*/_jsxs("div", {
36
31
  className: getClassName('bpk-comparison-table__header-content'),
37
32
  children: [/*#__PURE__*/_jsxs("div", {
@@ -52,15 +47,6 @@ const BpkComparisonTableColumnHeader = ({
52
47
  }), /*#__PURE__*/_jsx("div", {
53
48
  className: getClassName('bpk-comparison-table__header-id-section'),
54
49
  children: headerContent
55
- }), /*#__PURE__*/_jsx("div", {
56
- className: getClassName('bpk-comparison-table__remove-button'),
57
- children: /*#__PURE__*/_jsx(BpkButton, {
58
- type: BUTTON_TYPES.link,
59
- size: SIZE_TYPES.small,
60
- onClick: () => onRemove(itemId),
61
- "aria-label": removeA11yLabel,
62
- children: removeLabel
63
- })
64
50
  })]
65
51
  });
66
52
  export default BpkComparisonTableColumnHeader;
@@ -17,6 +17,7 @@
17
17
  */
18
18
 
19
19
  import { useEffect, useRef } from 'react';
20
+ import BpkButton, { BUTTON_TYPES, SIZE_TYPES } from "../../../bpk-component-button";
20
21
  import { BpkTable, BpkTableBody, BpkTableCell, BpkTableRow, TABLE_BODY_TYPES } from "../../../bpk-component-table";
21
22
  import { cssModules } from "../../../bpk-react-utils";
22
23
  import BpkComparisonTableHeaderRow from "./BpkComparisonTableHeaderRow";
@@ -74,19 +75,34 @@ const BpkComparisonTableContent = ({
74
75
  children: /*#__PURE__*/_jsxs(BpkTable, {
75
76
  children: [/*#__PURE__*/_jsx(BpkComparisonTableHeaderRow, {
76
77
  displayColumns: displayColumns,
77
- onRemove: onRemove,
78
78
  onAddMoreClick: onAddMoreClick,
79
79
  strings: strings
80
- }), /*#__PURE__*/_jsx(BpkTableBody, {
80
+ }), /*#__PURE__*/_jsxs(BpkTableBody, {
81
81
  type: TABLE_BODY_TYPES.striped,
82
- children: rowIds.map(rowId => /*#__PURE__*/_jsx(BpkTableRow, {
82
+ children: [columns.length > 0 && /*#__PURE__*/_jsx(BpkTableRow, {
83
+ children: displayColumns.map((column, index) => /*#__PURE__*/_jsx(BpkTableCell, {
84
+ // Zero padding needed for row collapse animation — no BpkTableCell prop.
85
+ // eslint-disable-next-line @skyscanner/rules/forbid-component-props
86
+ className: getClassName('bpk-comparison-table__remove-cell'),
87
+ children: column && /*#__PURE__*/_jsx("div", {
88
+ className: getClassName('bpk-comparison-table__remove-cell-inner'),
89
+ children: /*#__PURE__*/_jsx(BpkButton, {
90
+ type: BUTTON_TYPES.link,
91
+ size: SIZE_TYPES.small,
92
+ onClick: () => onRemove(column.itemId),
93
+ "aria-label": column.removeA11yLabel,
94
+ children: strings.removeLabel
95
+ })
96
+ })
97
+ }, column ? `remove-${column.itemId}` : `remove-placeholder-${index}`))
98
+ }), rowIds.map(rowId => /*#__PURE__*/_jsx(BpkTableRow, {
83
99
  children: displayColumns.map((column, index) => /*#__PURE__*/_jsx(BpkTableCell, {
84
100
  // Placeholder need a distinct background colour not available via BpkTableCell props
85
101
  // eslint-disable-next-line @skyscanner/rules/forbid-component-props
86
102
  className: column ? undefined : getClassName('bpk-comparison-table__placeholder-cell'),
87
103
  children: column ? cellsByItemAndRow.get(column.itemId)?.get(rowId) ?? null : null
88
104
  }, column ? `${rowId}-${column.itemId}` : `${rowId}-placeholder-${index}`))
89
- }, rowId))
105
+ }, rowId))]
90
106
  })]
91
107
  })
92
108
  });
@@ -1,9 +1,8 @@
1
1
  import type { BpkComparisonTableStrings, BpkCompareColumn } from './common-types';
2
2
  type BpkComparisonTableHeaderRowProps = {
3
3
  displayColumns: Array<BpkCompareColumn | null>;
4
- onRemove: (itemId: string) => void;
5
4
  onAddMoreClick: () => void;
6
5
  strings: BpkComparisonTableStrings;
7
6
  };
8
- declare const BpkComparisonTableHeaderRow: ({ displayColumns, onAddMoreClick, onRemove, strings, }: BpkComparisonTableHeaderRowProps) => import("react/jsx-runtime").JSX.Element;
7
+ declare const BpkComparisonTableHeaderRow: ({ displayColumns, onAddMoreClick, strings, }: BpkComparisonTableHeaderRowProps) => import("react/jsx-runtime").JSX.Element;
9
8
  export default BpkComparisonTableHeaderRow;
@@ -23,22 +23,18 @@ import { jsx as _jsx } from "react/jsx-runtime";
23
23
  const BpkComparisonTableHeaderRow = ({
24
24
  displayColumns,
25
25
  onAddMoreClick,
26
- onRemove,
27
26
  strings
28
27
  }) => {
29
28
  const {
30
29
  addMoreDescription,
31
30
  addMoreLinkText,
32
- bestTagLabel,
33
- removeLabel
31
+ bestTagLabel
34
32
  } = strings;
35
33
  return /*#__PURE__*/_jsx(BpkTableHead, {
36
34
  children: /*#__PURE__*/_jsx(BpkTableRow, {
37
35
  children: displayColumns.map((column, index) => /*#__PURE__*/_jsx(BpkTableHeadCell, {
38
36
  children: column ? /*#__PURE__*/_jsx(BpkComparisonTableColumnHeader, {
39
37
  ...column,
40
- removeLabel: removeLabel,
41
- onRemove: onRemove,
42
38
  bestTagLabel: bestTagLabel
43
39
  }) : /*#__PURE__*/_jsx(BpkComparisonTableColumnHeaderPlaceholder, {
44
40
  addMoreDescription: addMoreDescription,
@@ -1,3 +1,3 @@
1
1
  import type { BpkComparisonTableRootProps } from './common-types';
2
- declare const BpkComparisonTableRoot: ({ children, isOpen, onClose }: BpkComparisonTableRootProps) => import("react/jsx-runtime").JSX.Element;
2
+ declare const BpkComparisonTableRoot: ({ children, isOpen, onClose, onOpen }: BpkComparisonTableRootProps) => import("react/jsx-runtime").JSX.Element;
3
3
  export default BpkComparisonTableRoot;
@@ -16,23 +16,35 @@
16
16
  * limitations under the License.
17
17
  */
18
18
 
19
+ import { useEffect, useRef } from 'react';
19
20
  import { BpkModalV3 } from "../../../bpk-component-modal";
20
21
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
21
22
  const BpkComparisonTableRoot = ({
22
23
  children,
23
24
  isOpen,
24
- onClose
25
- }) => /*#__PURE__*/_jsx(BpkModalV3.Root, {
26
- open: isOpen,
27
- onOpenChange: details => {
28
- if (!details.open) {
29
- onClose();
25
+ onClose,
26
+ onOpen
27
+ }) => {
28
+ // Hold onOpen in a ref so inline callbacks (new identity each render) don't re-trigger the effect and double-fire while the modal stays open.
29
+ const onOpenRef = useRef(onOpen);
30
+ onOpenRef.current = onOpen;
31
+ useEffect(() => {
32
+ if (isOpen) {
33
+ onOpenRef.current?.();
30
34
  }
31
- },
32
- children: /*#__PURE__*/_jsxs(BpkModalV3.Portal, {
33
- children: [/*#__PURE__*/_jsx(BpkModalV3.Scrim, {}), /*#__PURE__*/_jsx(BpkModalV3.Content, {
34
- children: children
35
- })]
36
- })
37
- });
35
+ }, [isOpen]);
36
+ return /*#__PURE__*/_jsx(BpkModalV3.Root, {
37
+ open: isOpen,
38
+ onOpenChange: details => {
39
+ if (!details.open) {
40
+ onClose();
41
+ }
42
+ },
43
+ children: /*#__PURE__*/_jsxs(BpkModalV3.Portal, {
44
+ children: [/*#__PURE__*/_jsx(BpkModalV3.Scrim, {}), /*#__PURE__*/_jsx(BpkModalV3.Content, {
45
+ children: children
46
+ })]
47
+ })
48
+ });
49
+ };
38
50
  export default BpkComparisonTableRoot;
@@ -44,6 +44,8 @@ export type BpkComparisonTableRootProps = {
44
44
  isOpen: boolean;
45
45
  /** Called when the modal close trigger is activated or Escape is pressed. */
46
46
  onClose: () => void;
47
+ /** Optional — called each time the modal opens: on mount if `isOpen` is `true`, and whenever `isOpen` transitions from `false` to `true`. Useful for analytics, logging, or fetching data on open. */
48
+ onOpen?: () => void;
47
49
  /** BpkComparisonTable.Header and BpkComparisonTable.Content. */
48
50
  children: ReactNode;
49
51
  };
@@ -15,4 +15,4 @@
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
17
  */
18
- .bpk-comparison-tray{width:22.1875rem;max-width:100%}@media(max-width: 32rem){.bpk-comparison-tray{width:auto;min-width:0;flex:1}}.bpk-comparison-tray__item{position:relative;width:3.875rem;min-width:0;flex-shrink:1;aspect-ratio:62/28}.bpk-comparison-tray__item-image-container{width:100%;height:100%;background-color:#eff3f8;overflow:hidden;border-radius:.25rem}.bpk-comparison-tray__item-image-container img{height:100%;object-fit:contain}.bpk-comparison-tray__item-close{position:relative;position:absolute;top:0;inset-inline-end:calc(-8*0.0625rem);display:flex;width:1rem;height:1rem;justify-content:center;align-items:center;transform:translateY(-50%);border:.125rem solid #fff;border-radius:50%;background-color:#161616;color:#fff;cursor:pointer;box-shadow:0px 4px 14px 0px rgba(37,32,31,.25)}.bpk-comparison-tray__item-close::before{position:absolute;top:calc(-2.75rem/2 + 50%);left:calc(-2.75rem/2 + 50%);content:"";width:2.75rem;height:2.75rem}.bpk-comparison-tray__item-close svg{width:.5rem;height:.5rem;flex-shrink:0;fill:#fff}.bpk-comparison-tray__item-close:focus-visible{outline:.125rem solid #0062e3;outline-offset:.125rem}.bpk-comparison-tray__item-placeholder{width:3.875rem;min-width:0;flex-shrink:1;border:.0625rem dashed #c1c7cf;aspect-ratio:62/28;border-radius:.25rem}
18
+ .bpk-comparison-tray{width:22.1875rem;max-width:100%}@media(max-width: 32rem){.bpk-comparison-tray{width:auto;min-width:0;flex:1}}.bpk-comparison-tray button{min-height:0;padding-block:0}.bpk-comparison-tray__item{position:relative;width:3.875rem;min-width:0;flex-shrink:1;aspect-ratio:62/28}.bpk-comparison-tray__item-image-container{width:100%;height:100%;background-color:#eff3f8;overflow:hidden;border-radius:.25rem}.bpk-comparison-tray__item-image-container img{height:100%;object-fit:contain}.bpk-comparison-tray__item-close{position:relative;position:absolute;top:0;inset-inline-end:calc(-8*0.0625rem);display:flex;width:1rem;height:1rem;justify-content:center;align-items:center;transform:translateY(-50%);border:.125rem solid #fff;border-radius:50%;background-color:#161616;color:#fff;cursor:pointer;box-shadow:0px 4px 14px 0px rgba(37,32,31,.25)}.bpk-comparison-tray__item-close::before{position:absolute;top:calc(-2.75rem/2 + 50%);left:calc(-2.75rem/2 + 50%);content:"";width:2.75rem;height:2.75rem}.bpk-comparison-tray__item-close svg{width:.5rem;height:.5rem;flex-shrink:0;fill:#fff}.bpk-comparison-tray__item-close:focus-visible{outline:.125rem solid #0062e3;outline-offset:.125rem}.bpk-comparison-tray__item-placeholder{width:3.875rem;min-width:0;flex-shrink:1;border:.0625rem dashed #c1c7cf;aspect-ratio:62/28;border-radius:.25rem}
@@ -0,0 +1,6 @@
1
+ import BpkHorizontalNav, { HORIZONTAL_NAV_TYPES, type Props as NavProps } from './src/BpkHorizontalNav';
2
+ import BpkHorizontalNavItem, { type Props as ItemProps, themeAttributes } from './src/BpkHorizontalNavItem';
3
+ export type BpkHorizontalNavProps = NavProps;
4
+ export type BpkHorizontalNavItemProps = ItemProps;
5
+ export { HORIZONTAL_NAV_TYPES, BpkHorizontalNavItem, themeAttributes };
6
+ export default BpkHorizontalNav;
@@ -14,7 +14,9 @@
14
14
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
- */import BpkHorizontalNav, { HORIZONTAL_NAV_TYPES } from "./src/BpkHorizontalNav";
17
+ */
18
+
19
+ import BpkHorizontalNav, { HORIZONTAL_NAV_TYPES } from "./src/BpkHorizontalNav";
18
20
  import BpkHorizontalNavItem, { themeAttributes } from "./src/BpkHorizontalNavItem";
19
21
  export { HORIZONTAL_NAV_TYPES, BpkHorizontalNavItem, themeAttributes };
20
22
  export default BpkHorizontalNav;
@@ -0,0 +1,27 @@
1
+ import { type ReactNode } from 'react';
2
+ declare const HORIZONTAL_NAV_TYPES: {
3
+ readonly default: "default";
4
+ readonly light: "light";
5
+ };
6
+ type Props = {
7
+ children: ReactNode;
8
+ ariaLabel?: string | null;
9
+ /**
10
+ * Ensures that the selected item is within view when loaded on narrow-screened devices.
11
+ */
12
+ autoScrollToSelected?: boolean;
13
+ className?: string | null;
14
+ leadingScrollIndicatorClassName?: string | null;
15
+ /**
16
+ * When set to "false", the bottom border on the component isn't included. This refers
17
+ * to the underline on the whole "BpkHorizontalNav", not the line that appears under the selected item.
18
+ */
19
+ showUnderline?: boolean;
20
+ trailingScrollIndicatorClassName?: string | null;
21
+ type?: keyof typeof HORIZONTAL_NAV_TYPES;
22
+ [rest: string]: any;
23
+ };
24
+ declare const BpkHorizontalNav: ({ ariaLabel, autoScrollToSelected, children: rawChildren, className, leadingScrollIndicatorClassName, showUnderline, trailingScrollIndicatorClassName, type, ...rest }: Props) => import("react/jsx-runtime").JSX.Element;
25
+ export type { Props };
26
+ export default BpkHorizontalNav;
27
+ export { HORIZONTAL_NAV_TYPES };
@@ -14,8 +14,9 @@
14
14
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
15
  * See the License for the specific language governing permissions and
16
16
  * limitations under the License.
17
- */import PropTypes from 'prop-types';
18
- import { Component, Children, cloneElement } from 'react';
17
+ */
18
+
19
+ import { Children, cloneElement, useCallback, useEffect, useRef } from 'react';
19
20
  import BpkMobileScrollContainer from "../../bpk-component-mobile-scroll-container";
20
21
  import { cssModules } from "../../bpk-react-utils";
21
22
  import STYLES from "./BpkHorizontalNav.module.css";
@@ -29,138 +30,102 @@ const getPos = ref => {
29
30
  if (!ref) {
30
31
  return null;
31
32
  }
32
- const pos = ref.getBoundingClientRect();
33
- return pos;
33
+ return ref.getBoundingClientRect();
34
34
  };
35
- class BpkHorizontalNav extends Component {
36
- constructor(props) {
37
- super(props);
38
- this.scrollRef = null;
39
- this.selectedItemRef = null;
40
- }
41
- componentDidMount() {
42
- requestAnimationFrame(() => {
43
- this.scrollSelectedIntoView(false);
44
- });
45
- }
46
- componentDidUpdate() {
47
- this.scrollSelectedIntoView(true);
48
- }
49
- scrollSelectedIntoView = useSmoothScroll => {
50
- if (!this.props.autoScrollToSelected || !this.scrollRef || !this.selectedItemRef) {
51
- return;
52
- }
53
- if (!this.scrollRef) {
35
+ const BpkHorizontalNav = ({
36
+ ariaLabel = null,
37
+ autoScrollToSelected = false,
38
+ children: rawChildren,
39
+ className = null,
40
+ leadingScrollIndicatorClassName = null,
41
+ showUnderline = true,
42
+ trailingScrollIndicatorClassName = null,
43
+ type = HORIZONTAL_NAV_TYPES.default,
44
+ ...rest
45
+ }) => {
46
+ const scrollRef = useRef(null);
47
+ const selectedItemRef = useRef(null);
48
+ const isMounted = useRef(false);
49
+ const scrollSelectedIntoView = useCallback(useSmoothScroll => {
50
+ if (!autoScrollToSelected || !scrollRef.current || !selectedItemRef.current) {
54
51
  return;
55
52
  }
56
- const selectedItemPos = getPos(this.selectedItemRef);
53
+ const selectedItemPos = getPos(selectedItemRef.current);
57
54
  if (!selectedItemPos) {
58
55
  return;
59
56
  }
60
- const scrollPos = getPos(this.scrollRef);
57
+ const scrollPos = getPos(scrollRef.current);
61
58
  if (!scrollPos) {
62
59
  return;
63
60
  }
64
-
65
- // We only need to carry out a scroll if the element is not already fully within the scroll view
66
61
  const needsScroll = selectedItemPos.left < scrollPos.left || selectedItemPos.right > scrollPos.right;
67
62
  if (!needsScroll) {
68
63
  return;
69
64
  }
70
65
  const scrollAdjustment = selectedItemPos.left - scrollPos.left;
71
- // Some browsers don't support smooth scrolling, so in those cases we must fall back to simply setting `scrollLeft`
72
- if (this.scrollRef && this.scrollRef.scroll && typeof this.scrollRef.scroll === 'function' && useSmoothScroll) {
73
- this.scrollRef.scroll({
66
+ if (scrollRef.current.scroll && typeof scrollRef.current.scroll === 'function' && useSmoothScroll) {
67
+ scrollRef.current.scroll({
74
68
  left: scrollAdjustment,
75
69
  behavior: 'smooth'
76
70
  });
77
- } else if (this.scrollRef) {
78
- this.scrollRef.scrollLeft = scrollAdjustment;
71
+ } else {
72
+ scrollRef.current.scrollLeft = scrollAdjustment;
79
73
  }
80
- };
81
- render() {
82
- const {
83
- ariaLabel,
84
- autoScrollToSelected,
85
- children: rawChildren,
86
- className,
87
- leadingScrollIndicatorClassName,
88
- showUnderline,
89
- trailingScrollIndicatorClassName,
90
- type,
91
- ...rest
92
- } = this.props;
93
- const classNames = getClassName('bpk-horizontal-nav', showUnderline && `bpk-horizontal-nav--show-${type}-underline`, className);
94
- let children = rawChildren;
95
- if (autoScrollToSelected || type === HORIZONTAL_NAV_TYPES.light) {
96
- children = Children.map(rawChildren, child => {
97
- const childProps = {};
98
- let childRef;
99
- if (autoScrollToSelected) {
100
- if (child && child.props && child.props.selected) {
101
- childRef = ref => {
102
- this.selectedItemRef = ref;
103
- };
104
- }
105
- }
106
- if (type === HORIZONTAL_NAV_TYPES.light) {
107
- childProps.type = HORIZONTAL_NAV_TYPES.light;
108
- }
109
- return child ? /*#__PURE__*/_jsx("div", {
110
- ref: childRef,
111
- children: /*#__PURE__*/cloneElement(child, childProps)
112
- }) : null;
113
- });
74
+ }, [autoScrollToSelected]);
75
+ useEffect(() => {
76
+ requestAnimationFrame(() => {
77
+ scrollSelectedIntoView(false);
78
+ });
79
+ // eslint-disable-next-line react-hooks/exhaustive-deps
80
+ }, []);
81
+ useEffect(() => {
82
+ if (isMounted.current) {
83
+ scrollSelectedIntoView(true);
84
+ } else {
85
+ isMounted.current = true;
114
86
  }
115
- return (
116
- /*#__PURE__*/
117
- // $FlowFixMe[cannot-spread-inexact] - inexact rest. See 'decisions/flowfixme.md'.
118
- _jsx("div", {
119
- className: classNames,
120
- children: /*#__PURE__*/_jsx(BpkMobileScrollContainer, {
121
- ariaLabel: ariaLabel,
122
- innerContainerTagName: "nav",
123
- leadingIndicatorClassName: leadingScrollIndicatorClassName,
124
- trailingIndicatorClassName: trailingScrollIndicatorClassName,
125
- scrollerRef: ref => {
126
- this.scrollRef = ref;
127
- },
128
- ...rest,
129
- children: /*#__PURE__*/_jsx("div", {
130
- className: getClassName('bpk-horizontal-nav__list'),
131
- role: "tablist",
132
- children: children
133
- })
134
- })
135
- })
136
- );
87
+ });
88
+ const classNames = getClassName('bpk-horizontal-nav', showUnderline && `bpk-horizontal-nav--show-${type}-underline`, className);
89
+ let children = rawChildren;
90
+ if (autoScrollToSelected || type === HORIZONTAL_NAV_TYPES.light) {
91
+ children = Children.map(rawChildren, child => {
92
+ const isElement = typeof child === 'object' && child !== null && 'props' in child;
93
+ const childProps = {};
94
+ let childRef;
95
+ if (autoScrollToSelected) {
96
+ if (isElement && child.props.selected) {
97
+ childRef = ref => {
98
+ selectedItemRef.current = ref;
99
+ };
100
+ }
101
+ }
102
+ if (type === HORIZONTAL_NAV_TYPES.light) {
103
+ childProps.type = HORIZONTAL_NAV_TYPES.light;
104
+ }
105
+ return isElement ? /*#__PURE__*/_jsx("div", {
106
+ ref: childRef,
107
+ children: /*#__PURE__*/cloneElement(child, childProps)
108
+ }) : null;
109
+ });
137
110
  }
138
- }
139
- BpkHorizontalNav.propTypes = {
140
- ariaLabel: PropTypes.string,
141
- children: PropTypes.node.isRequired,
142
- /**
143
- * Ensures that the selected item is within view when loaded on narrow-screened devices.
144
- */
145
- autoScrollToSelected: PropTypes.bool,
146
- className: PropTypes.string,
147
- leadingScrollIndicatorClassName: PropTypes.string,
148
- /**
149
- * When set to "false", the bottom border on the component isn't included. This refers
150
- * to the underline on the whole "BpkHorizontalNav", not the line that appears under the selected item.
151
- */
152
- showUnderline: PropTypes.bool,
153
- trailingScrollIndicatorClassName: PropTypes.string,
154
- type: PropTypes.oneOf(Object.keys(HORIZONTAL_NAV_TYPES))
155
- };
156
- BpkHorizontalNav.defaultProps = {
157
- ariaLabel: null,
158
- autoScrollToSelected: false,
159
- className: null,
160
- leadingScrollIndicatorClassName: null,
161
- showUnderline: true,
162
- trailingScrollIndicatorClassName: null,
163
- type: HORIZONTAL_NAV_TYPES.default
111
+ return /*#__PURE__*/_jsx("div", {
112
+ className: classNames,
113
+ children: /*#__PURE__*/_jsx(BpkMobileScrollContainer, {
114
+ ariaLabel: ariaLabel ?? undefined,
115
+ innerContainerTagName: "nav",
116
+ leadingIndicatorClassName: leadingScrollIndicatorClassName ?? undefined,
117
+ trailingIndicatorClassName: trailingScrollIndicatorClassName ?? undefined,
118
+ scrollerRef: ref => {
119
+ scrollRef.current = ref;
120
+ },
121
+ ...rest,
122
+ children: /*#__PURE__*/_jsx("div", {
123
+ className: getClassName('bpk-horizontal-nav__list'),
124
+ role: "tablist",
125
+ children: children
126
+ })
127
+ })
128
+ });
164
129
  };
165
130
  export default BpkHorizontalNav;
166
131
  export { HORIZONTAL_NAV_TYPES };
@@ -0,0 +1,25 @@
1
+ import { type ReactNode } from 'react';
2
+ import { HORIZONTAL_NAV_TYPES } from './BpkHorizontalNav';
3
+ type Props = {
4
+ children: ReactNode;
5
+ className?: string | null;
6
+ /**
7
+ * **Note:**
8
+ * "disabled" and "selected" are mutually exclusive for true values, i.e. only one of them can be true for a given nav item.
9
+ */
10
+ disabled?: boolean;
11
+ href?: string | null;
12
+ /**
13
+ * **Note:**
14
+ * "disabled" and "selected" are mutually exclusive for true values, i.e. only one of them can be true for a given nav item.
15
+ */
16
+ selected?: boolean;
17
+ spaceAround?: boolean;
18
+ type?: keyof typeof HORIZONTAL_NAV_TYPES;
19
+ [rest: string]: any;
20
+ };
21
+ declare const BpkHorizontalNavItem: import("react").ForwardRefExoticComponent<Omit<Props, "ref"> & import("react").RefAttributes<HTMLDivElement>>;
22
+ declare const themeAttributes: string[];
23
+ export type { Props };
24
+ export { themeAttributes };
25
+ export default BpkHorizontalNavItem;