Skip to content

Commit 1e91fda

Browse files
committed
CardSwiperCodeSplit
1 parent 45a6d70 commit 1e91fda

6 files changed

Lines changed: 49 additions & 27 deletions

File tree

src/components/BookCardGroup.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from "../connection/LibraryQueryHooks";
1818
import { BookCard, useBookCardSpec } from "./BookCard";
1919
import { MoreCard } from "./MoreCard";
20-
import { CardSwiperLazy } from "./CardSwiper";
20+
import { CardSwiperCodeSplit } from "./CardSwiperCodeSplit";
2121
import { ICollection } from "../model/ContentInterfaces";
2222
import Typography from "@material-ui/core/Typography";
2323
import { getLocalizedCollectionLabel } from "../localization/CollectionLabel";
@@ -157,7 +157,7 @@ const BookCardGroupInner: React.FunctionComponent<IProps> = (props) => {
157157
data.push("more");
158158
}
159159
bookList = (
160-
<CardSwiperLazy
160+
<CardSwiperCodeSplit
161161
data={data}
162162
cardSpec={cardSpec}
163163
getReactElement={(item: IBasicBookInfo | "more", index) => {
@@ -180,7 +180,7 @@ const BookCardGroupInner: React.FunctionComponent<IProps> = (props) => {
180180
);
181181
}
182182
}}
183-
></CardSwiperLazy>
183+
></CardSwiperCodeSplit>
184184
);
185185
} else {
186186
const verticalSpacing = cardSpec.cardSpacingPx;

src/components/BookGroup.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { BookCard, useBookCardSpec } from "./BookCard";
1818
import { useResponsiveChoice } from "../responsiveUtilities";
1919
import { BookOrderingScheme } from "../model/ContentInterfaces";
20-
import { CardSwiperLazy } from "./CardSwiper";
20+
import { CardSwiperCodeSplit } from "./CardSwiperCodeSplit";
2121

2222
interface IProps {
2323
title: string;
@@ -184,14 +184,14 @@ export const BookGroupInner: React.FunctionComponent<IProps> = (props) => {
184184
// not to try to fix it.
185185
const cardSpec = useBookCardSpec();
186186
const bookList = showInOneRow ? (
187-
<CardSwiperLazy
187+
<CardSwiperCodeSplit
188188
data={cards}
189189
getReactElement={(x) => x}
190190
wrapperRole="list"
191191
cardSpec={cardSpec}
192192
>
193193
{cards}
194-
</CardSwiperLazy>
194+
</CardSwiperCodeSplit>
195195
) : (
196196
<div
197197
css={css`

src/components/CardRow.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { jsx } from "@emotion/core";
55
/** @jsx jsx */
66
import React, { ReactElement } from "react";
77
import LazyLoad from "react-lazyload";
8-
import { CardSwiperLazy } from "./CardSwiper";
8+
import { CardSwiperCodeSplit } from "./CardSwiperCodeSplit";
99
import { ICollection } from "../model/ContentInterfaces";
1010
import {
1111
CollectionLabel,
@@ -39,7 +39,7 @@ export const CardRow: React.FunctionComponent<IProps> = (props) => {
3939
padding-left: 0;
4040
`}
4141
>
42-
<CardSwiperLazy
42+
<CardSwiperCodeSplit
4343
wrapperRole="list"
4444
data={props.data}
4545
getReactElement={props.getCards}

src/components/CardSwiper.tsx

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import "swiper/components/a11y/a11y.min.css";
1313
import { useResponsiveChoice } from "../responsiveUtilities";
1414
import { ICardSpec } from "./CardGroup";
1515
import { commonUI } from "../theme";
16+
import { ICardSwiperProps } from "./CardSwiperCodeSplit";
1617

1718
SwiperCore.use([Navigation, A11y]);
1819

19-
export const swiperConfig: Swiper = {
20+
const swiperConfig: Swiper = {
2021
preloadImages: false,
2122
lazy: {
2223
loadPrevNext: true,
@@ -48,22 +49,7 @@ export const swiperConfig: Swiper = {
4849
// small windows.
4950
// Enhance: this could be made generic, with a type param indicating that the type of objects in data
5051
// is the same as the type passed as the first argument of getReactElement
51-
export const CardSwiperLazy: React.FunctionComponent<{
52-
data: any[];
53-
// Given one of the items in data (and its index), return the react element that should be
54-
// shown for that card when it is visible.
55-
getReactElement: (card: any, index: number) => ReactElement;
56-
57-
// Typically the swiper is a list. I can't find a way to configure it so that the element containing
58-
// the cards is a UL, but by setting this to 'list' and making items with role listitem we achieve
59-
// the same accessibility goals. This role becomes the value of the role attribute of the swiper
60-
// wrapper element, which is the immediate parent of the items. Note that it's not always a list, e.g.,
61-
// in the LanguageGroup a further-out element is a listbox and the items have role 'option'.
62-
// If you set a wrapperRole, make sure the children you pass have role listitem.
63-
wrapperRole?: string;
64-
65-
cardSpec: ICardSpec;
66-
}> = (props) => {
52+
const CardSwiperLazy: React.FunctionComponent<ICardSwiperProps> = (props) => {
6753
const [swiper, setSwiper] = useState<any | null>(null);
6854
const getResponsiveChoice = useResponsiveChoice();
6955
const [showAll, setShowAll] = useState(false);
@@ -212,3 +198,5 @@ export const CardSwiperLazy: React.FunctionComponent<{
212198
</Swiper>
213199
);
214200
};
201+
202+
export default CardSwiperLazy;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React, { ReactElement } from "react";
2+
import { ICardSpec } from "./CardGroup";
3+
4+
export interface ICardSwiperProps {
5+
data: any[];
6+
// Given one of the items in data (and its index), return the react element that should be
7+
// shown for that card when it is visible.
8+
getReactElement: (card: any, index: number) => ReactElement;
9+
10+
// Typically the swiper is a list. I can't find a way to configure it so that the element containing
11+
// the cards is a UL, but by setting this to 'list' and making items with role listitem we achieve
12+
// the same accessibility goals. This role becomes the value of the role attribute of the swiper
13+
// wrapper element, which is the immediate parent of the items. Note that it's not always a list, e.g.,
14+
// in the LanguageGroup a further-out element is a listbox and the items have role 'option'.
15+
// If you set a wrapperRole, make sure the children you pass have role listitem.
16+
wrapperRole?: string;
17+
18+
cardSpec: ICardSpec;
19+
}
20+
21+
// This is wrapped so that we can keep all the javascript involved in the CardSwiper
22+
// in a separate js file, downloaded to the user's browser only if he/she needs it.
23+
export const CardSwiperCodeSplit: React.FunctionComponent<ICardSwiperProps> = (
24+
props
25+
) => {
26+
const CardSwiperLazy = React.lazy(
27+
() => import(/* webpackChunkName: "cardSwiper" */ "./CardSwiper")
28+
);
29+
return (
30+
<React.Suspense fallback={<div>Loading Cards...</div>}>
31+
<CardSwiperLazy {...props} />
32+
</React.Suspense>
33+
);
34+
};

src/components/LanguageGroup.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import matchSorter from "match-sorter";
1818
import searchIcon from "../search.png";
1919
import { CachedTablesContext } from "../model/CacheProvider";
2020
import { ILanguage } from "../model/Language";
21-
import { CardSwiperLazy } from "./CardSwiper";
21+
import { CardSwiperCodeSplit } from "./CardSwiperCodeSplit";
2222
import { Redirect } from "react-router-dom";
2323
import { FormattedMessage, useIntl } from "react-intl";
2424
import { propsToHideAccessibilityElement } from "../Utilities";
@@ -54,7 +54,7 @@ export const LanguageGroup: React.FunctionComponent = () => {
5454
if (languagesToDisplay.length) {
5555
return (
5656
<div {...getMenuProps({})}>
57-
<CardSwiperLazy
57+
<CardSwiperCodeSplit
5858
data={languagesToDisplay}
5959
cardSpec={cardSpec}
6060
getReactElement={(l: ILanguage, index: number) => (

0 commit comments

Comments
 (0)