@skyscanner/backpack-web 37.9.0 → 37.9.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.
@@ -28,6 +28,7 @@ const getClassName = cssModules(STYLES);
28
28
  const DEFAULT_ITEMS = 3;
29
29
  const BpkCardList = props => {
30
30
  const {
31
+ accessibilityLabels,
31
32
  accessoryDesktop,
32
33
  accessoryMobile,
33
34
  buttonContent,
@@ -74,6 +75,7 @@ const BpkCardList = props => {
74
75
  children: [layoutMobile === LAYOUTS.rail && /*#__PURE__*/_jsx(BpkCardListRowRailContainer, {
75
76
  initiallyShownCards: initiallyShownCardsMobile,
76
77
  layout: layoutMobile,
78
+ accessibilityLabels: accessibilityLabels,
77
79
  isMobile: true,
78
80
  children: cardList
79
81
  }), layoutMobile === LAYOUTS.stack && /*#__PURE__*/_jsx(BpkCardListGridStack, {
@@ -92,6 +94,7 @@ const BpkCardList = props => {
92
94
  accessory: accessoryDesktop,
93
95
  initiallyShownCards: initiallyShownCardsDesktop,
94
96
  layout: layoutDesktop,
97
+ accessibilityLabels: accessibilityLabels,
95
98
  children: cardList
96
99
  }), layoutDesktop === LAYOUTS.grid && accessoryDesktop !== ACCESSORY_DESKTOP_TYPES.pagination && /*#__PURE__*/_jsx(BpkCardListGridStack, {
97
100
  accessory: accessoryDesktop,
@@ -25,12 +25,14 @@ import { jsx as _jsx } from "react/jsx-runtime";
25
25
  const getClassName = cssModules(STYLES);
26
26
  const BpkCardListCarousel = props => {
27
27
  const {
28
+ carouselLabel = (initiallyShownCards, childrenLength) => `Entering Carousel with ${initiallyShownCards} slides shown at a time, ${childrenLength} slides in total. Please use Pagination below with the Previous and Next buttons to navigate, or the slide dot buttons at the end to jump to slides.`,
28
29
  children,
29
30
  currentIndex,
30
31
  initiallyShownCards,
31
32
  isMobile = false,
32
33
  layout,
33
- setCurrentIndex
34
+ setCurrentIndex,
35
+ slideLabel = (index, childrenLength) => `slide ${index + 1} of ${childrenLength}`
34
36
  } = props;
35
37
  const shownNumberStyle = {
36
38
  '--initially-shown-cards': initiallyShownCards
@@ -63,14 +65,16 @@ const BpkCardListCarousel = props => {
63
65
  }, [root]);
64
66
  useUpdateCurrentIndexByVisibility(isMobile, visibilityList, setCurrentIndex, stateScrollingLockRef, openSetStateLockTimeoutRef);
65
67
  useScrollToCard(currentIndex, root, cardRefs, stateScrollingLockRef);
68
+
69
+ // Similar to Virtual Scrolling to improve performance
66
70
  const firstVisibleIndex = Math.max(0, visibilityList.indexOf(1));
67
71
  const lastVisibleIndex = firstVisibleIndex + initiallyShownCards - 1;
68
72
  const renderList = visibilityList.map((_, index) => index >= firstVisibleIndex - RENDER_BUFFER_SIZE && index <= lastVisibleIndex + RENDER_BUFFER_SIZE ? 1 : 0);
69
- const carouselAriaLabel = `Entering Carousel with ${initiallyShownCards} slides shown at a time, ${childrenLength} slides in total. Please use Pagination below with the Previous and Next buttons to navigate, or the slide dot buttons at the end to jump to slides.`;
70
73
  return /*#__PURE__*/_jsx("div", {
71
74
  className: getClassName(`bpk-card-list-row-rail__${layout}`),
72
75
  "data-testid": "bpk-card-list-row-rail__carousel",
73
- "aria-label": carouselAriaLabel
76
+ "aria-label": carouselLabel(initiallyShownCards, childrenLength),
77
+ "aria-roledescription": "carousel"
74
78
  // eslint-disable-next-line jsx-a11y/no-noninteractive-tabindex
75
79
  ,
76
80
  tabIndex: 0,
@@ -83,17 +87,16 @@ const BpkCardListCarousel = props => {
83
87
  observerVisibility(el, index);
84
88
  setA11yTabIndex(el, index, visibilityList);
85
89
  };
86
- const slideAriaLabel = `slide ${index + 1} of ${childrenLength}`;
87
90
  const cardStyle = isMobile ? {
88
91
  ...shownNumberStyle,
89
- visibility: renderList[index] === 1 ? 'visible' : 'hidden'
92
+ visibility: renderList[index] === 1 ? 'visible' : 'hidden' // for mobile, {renderList[index] === 1 && card} will cause reflowing and bugs, implementing visibility improvement instead
90
93
  } : shownNumberStyle;
91
94
  return /*#__PURE__*/_jsx("div", {
92
95
  className: getClassName(`bpk-card-list-row-rail__${layout}__card`),
93
96
  ref: cardRefCallback,
94
97
  style: cardStyle,
95
98
  role: "group",
96
- "aria-label": slideAriaLabel,
99
+ "aria-label": slideLabel(index, childrenLength),
97
100
  "aria-current": index === currentIndex ? 'true' : 'false',
98
101
  children: isMobile ? card : renderList[index] === 1 && card
99
102
  }, `carousel-card-${index.toString()}`);
@@ -25,6 +25,7 @@ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
25
25
  const getClassName = cssModules(STYLES);
26
26
  const BpkCardListRowRailContainer = props => {
27
27
  const {
28
+ accessibilityLabels,
28
29
  accessory,
29
30
  children,
30
31
  initiallyShownCards,
@@ -37,13 +38,11 @@ const BpkCardListRowRailContainer = props => {
37
38
  const accessoryContent = layout === LAYOUTS.row && accessory === ACCESSORY_DESKTOP_TYPES.pagination ? /*#__PURE__*/_jsx(BpkPageIndicator, {
38
39
  currentIndex: currentIndex,
39
40
  totalIndicators: totalIndicators,
40
- onClick: (_e, index) => {
41
- setCurrentIndex(index);
42
- },
41
+ onClick: (_e, index) => setCurrentIndex(index),
43
42
  showNav: true,
44
- indicatorLabel: "Go to slide",
45
- prevNavLabel: "Previous slide",
46
- nextNavLabel: "Next slide"
43
+ indicatorLabel: accessibilityLabels?.indicatorLabel ?? 'Go to slide',
44
+ prevNavLabel: accessibilityLabels?.prevNavLabel ?? 'Previous slide',
45
+ nextNavLabel: accessibilityLabels?.nextNavLabel ?? 'Next slide'
47
46
  }) : null;
48
47
  return /*#__PURE__*/_jsxs("div", {
49
48
  className: getClassName('bpk-card-list-row-rail'),
@@ -54,6 +53,8 @@ const BpkCardListRowRailContainer = props => {
54
53
  currentIndex: currentIndex,
55
54
  setCurrentIndex: setCurrentIndex,
56
55
  isMobile: isMobile,
56
+ carouselLabel: accessibilityLabels?.carouselLabel,
57
+ slideLabel: accessibilityLabels?.slideLabel,
57
58
  children: children
58
59
  }), accessoryContent && showAccessory && /*#__PURE__*/_jsx("div", {
59
60
  role: "region",
@@ -1,4 +1,15 @@
1
+ /**
2
+ * Typically used to prevent useScrollToCard() from being called, to prevent scrollings caused by state changes, so as to avoid cyclic dependencies.
3
+ * @param stateScrollingLockRef - Ref to the state that indicates if scrollIntoView should be locked
4
+ * @param openSetStateLockTimeoutRef - Ref to the timeout that releases the lock after a delay. Should be renewed every time another scroll action is triggered, with a new lock is added.
5
+ */
1
6
  export declare const lockScroll: (stateScrollingLockRef: React.MutableRefObject<boolean>, openSetStateLockTimeoutRef: React.MutableRefObject<NodeJS.Timeout | null>) => void;
7
+ /**
8
+ * Only sets the tabIndex of focusable elements as 0 if the card is visible, otherwise sets it to -1, including all its children.
9
+ * For example, if there is a button inside a card which is not shown, it cannot be focused as well.
10
+ * @param el - Card container element, typically a div used to wrap the card content.
11
+ * @param index - Current container index
12
+ */
2
13
  export declare const setA11yTabIndex: (el: HTMLDivElement | null, index: number, visibilityList: number[]) => void;
3
14
  export declare const useUpdateCurrentIndexByVisibility: (isMobile: boolean, visibilityList: number[], setCurrentIndex: (index: number) => void, stateScrollingLockRef: React.MutableRefObject<boolean>, openSetStateLockTimeoutRef: React.MutableRefObject<NodeJS.Timeout | null>) => void;
4
15
  export declare const useScrollToCard: (currentIndex: number, container: HTMLElement | null, cardRefs: React.MutableRefObject<Array<HTMLDivElement | null>>, stateScrollingLockRef: React.MutableRefObject<boolean>) => void;
@@ -18,6 +18,13 @@
18
18
 
19
19
  import { useEffect, useRef, useMemo } from 'react';
20
20
  import { RELEASE_LOCK_DELAY } from "./constants";
21
+
22
+ /**
23
+ * Typically used to prevent useScrollToCard() from being called, to prevent scrollings caused by state changes, so as to avoid cyclic dependencies.
24
+ * @param stateScrollingLockRef - Ref to the state that indicates if scrollIntoView should be locked
25
+ * @param openSetStateLockTimeoutRef - Ref to the timeout that releases the lock after a delay. Should be renewed every time another scroll action is triggered, with a new lock is added.
26
+ */
27
+
21
28
  export const lockScroll = (stateScrollingLockRef, openSetStateLockTimeoutRef) => {
22
29
  // eslint-disable-next-line no-param-reassign
23
30
  stateScrollingLockRef.current = true;
@@ -30,6 +37,14 @@ export const lockScroll = (stateScrollingLockRef, openSetStateLockTimeoutRef) =>
30
37
  stateScrollingLockRef.current = false;
31
38
  }, RELEASE_LOCK_DELAY);
32
39
  };
40
+
41
+ /**
42
+ * Only sets the tabIndex of focusable elements as 0 if the card is visible, otherwise sets it to -1, including all its children.
43
+ * For example, if there is a button inside a card which is not shown, it cannot be focused as well.
44
+ * @param el - Card container element, typically a div used to wrap the card content.
45
+ * @param index - Current container index
46
+ */
47
+
33
48
  export const setA11yTabIndex = (el, index, visibilityList) => {
34
49
  if (!el) return;
35
50
  const focusableElements = el.querySelectorAll('a, button, input, textarea, select, [tabindex]:not([tabindex="-1"])');
@@ -40,19 +55,19 @@ export const setA11yTabIndex = (el, index, visibilityList) => {
40
55
  };
41
56
  export const useUpdateCurrentIndexByVisibility = (isMobile, visibilityList, setCurrentIndex, stateScrollingLockRef, openSetStateLockTimeoutRef) => {
42
57
  useEffect(() => {
43
- if (isMobile) return;
58
+ if (isMobile) return; // No pagination on mobile, so no need to update the current index
44
59
  if (!visibilityList || visibilityList.length === 0) return;
45
60
  const firstVisibleIndex = visibilityList.findIndex(visibility => visibility === 1);
46
61
  if (firstVisibleIndex !== -1) {
47
62
  setCurrentIndex(firstVisibleIndex);
48
- lockScroll(stateScrollingLockRef, openSetStateLockTimeoutRef);
63
+ lockScroll(stateScrollingLockRef, openSetStateLockTimeoutRef); // prevent scrollIntoView from being called immediately after the current index is set
49
64
  }
50
65
  }, [visibilityList]);
51
66
  };
52
67
  export const useScrollToCard = (currentIndex, container, cardRefs, stateScrollingLockRef) => {
53
68
  useEffect(() => {
54
69
  const isVisible = container && container.getBoundingClientRect().bottom > 0 && container.getBoundingClientRect().bottom <= window.innerHeight;
55
- if (!isVisible) return; // Escape from scrollIntoView if the container is not visible
70
+ if (!isVisible) return; // Escape from scrollIntoView if the container is not visible, otherwise the webpage will scroll down to the last rendered & non-visible container
56
71
 
57
72
  if (stateScrollingLockRef.current && stateScrollingLockRef.current === true) return;
58
73
  const targetCard = cardRefs.current[currentIndex];
@@ -21,6 +21,13 @@ type ExpandProps = {
21
21
  collapsed: boolean;
22
22
  onExpandToggle: () => void;
23
23
  };
24
+ type AccessibilityLabels = {
25
+ indicatorLabel?: string;
26
+ prevNavLabel?: string;
27
+ nextNavLabel?: string;
28
+ carouselLabel?: (initiallyShownCards: number, childrenLength: number) => string;
29
+ slideLabel?: (index: number, childrenLength: number) => string;
30
+ };
24
31
  type CardListBaseProps = {
25
32
  title: string;
26
33
  description?: string;
@@ -37,6 +44,7 @@ type CardListBaseProps = {
37
44
  onExpandClick?: () => void;
38
45
  buttonHref?: string;
39
46
  expandText?: string;
47
+ accessibilityLabels?: AccessibilityLabels;
40
48
  };
41
49
  type CardListGridStackProps = {
42
50
  children: ReactElement[];
@@ -55,6 +63,7 @@ type CardListRowRailProps = {
55
63
  layout: typeof LAYOUTS.row | typeof LAYOUTS.rail;
56
64
  accessory?: typeof ACCESSORY_DESKTOP_TYPES.pagination;
57
65
  isMobile?: boolean;
66
+ accessibilityLabels?: AccessibilityLabels;
58
67
  };
59
68
  type CardListCarouselProps = {
60
69
  children: Array<ReactElement<HTMLDivElement | HTMLAnchorElement>>;
@@ -63,6 +72,8 @@ type CardListCarouselProps = {
63
72
  currentIndex: number;
64
73
  setCurrentIndex: Dispatch<SetStateAction<number>>;
65
74
  isMobile?: boolean;
75
+ carouselLabel?: (initiallyShownCards: number, childrenLength: number) => string;
76
+ slideLabel?: (index: number, childrenLength: number) => string;
66
77
  };
67
78
  type CardListProps = CardListBaseProps;
68
79
  export default CardListProps;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@skyscanner/backpack-web",
3
- "version": "37.9.0",
3
+ "version": "37.9.1",
4
4
  "description": "Backpack Design System web library",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,7 +22,7 @@
22
22
  "access": "public"
23
23
  },
24
24
  "dependencies": {
25
- "@floating-ui/react": "^0.27.8",
25
+ "@floating-ui/react": "^0.26.12",
26
26
  "@popperjs/core": "^2.11.8",
27
27
  "@radix-ui/react-compose-refs": "^1.1.1",
28
28
  "@radix-ui/react-slider": "1.1.2",