Lazy-load "Beautiful Drag-N-Drop" (#859)

Code-splits the module into "dnd.js", reducing the ui.js bundle size. This module is only needed when viewing lists.

## Room for improvement
`ClaimList` can probably improve further by only using the dnd components in "edit" mode.

## Implementation notes
- The modules are not default exports, so the additional chaining to `React.lazy` was required.
- Experimenting with using an Object to store the import so that a single "prettier-ignore" can be used to make it not wrap.
- The FlowFixMe came from the original code. It is unclear why it is needed to resolve the module, but Haffa mentioned it's something related to .flowconfig.
This commit is contained in:
infinite-persistence 2022-02-14 11:54:23 -08:00 committed by GitHub
parent ecc3599a85
commit b3020b6cfb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 100 additions and 80 deletions

View file

@ -1,8 +1,4 @@
// @flow // @flow
// $FlowFixMe
import { Draggable } from 'react-beautiful-dnd';
import { MAIN_CLASS } from 'constants/classnames'; import { MAIN_CLASS } from 'constants/classnames';
import type { Node } from 'react'; import type { Node } from 'react';
import React, { useEffect } from 'react'; import React, { useEffect } from 'react';
@ -14,6 +10,11 @@ import usePersistedState from 'effects/use-persisted-state';
import debounce from 'util/debounce'; import debounce from 'util/debounce';
import ClaimPreviewTile from 'component/claimPreviewTile'; import ClaimPreviewTile from 'component/claimPreviewTile';
const Draggable = React.lazy(() =>
// $FlowFixMe
import('react-beautiful-dnd' /* webpackChunkName: "dnd" */).then((module) => ({ default: module.Draggable }))
);
const DEBOUNCE_SCROLL_HANDLER_MS = 150; const DEBOUNCE_SCROLL_HANDLER_MS = 150;
const SORT_NEW = 'new'; const SORT_NEW = 'new';
const SORT_OLD = 'old'; const SORT_OLD = 'old';
@ -250,6 +251,7 @@ export default function ClaimList(props: Props) {
{sortedUris.map((uri, index) => {sortedUris.map((uri, index) =>
droppableProvided ? ( droppableProvided ? (
<React.Suspense fallback={null}>
<Draggable key={uri} draggableId={uri} index={index}> <Draggable key={uri} draggableId={uri} index={index}>
{(draggableProvided, draggableSnapshot) => { {(draggableProvided, draggableSnapshot) => {
// Restrict dragging to vertical axis // Restrict dragging to vertical axis
@ -275,6 +277,7 @@ export default function ClaimList(props: Props) {
); );
}} }}
</Draggable> </Draggable>
</React.Suspense>
) : ( ) : (
getClaimPreview(uri, index) getClaimPreview(uri, index)
) )

View file

@ -1,8 +1,4 @@
// @flow // @flow
// $FlowFixMe
import { DragDropContext, Droppable } from 'react-beautiful-dnd';
import React from 'react'; import React from 'react';
import classnames from 'classnames'; import classnames from 'classnames';
import ClaimList from 'component/claimList'; import ClaimList from 'component/claimList';
@ -13,6 +9,14 @@ import * as COLLECTIONS_CONSTS from 'constants/collections';
import Icon from 'component/common/icon'; import Icon from 'component/common/icon';
import * as ICONS from 'constants/icons'; import * as ICONS from 'constants/icons';
// prettier-ignore
const Lazy = {
// $FlowFixMe
DragDropContext: React.lazy(() => import('react-beautiful-dnd' /* webpackChunkName: "dnd" */).then((module) => ({ default: module.DragDropContext }))),
// $FlowFixMe
Droppable: React.lazy(() => import('react-beautiful-dnd' /* webpackChunkName: "dnd" */).then((module) => ({ default: module.Droppable }))),
};
type Props = { type Props = {
id: string, id: string,
url: string, url: string,
@ -105,8 +109,9 @@ export default function CollectionContent(props: Props) {
) )
} }
body={ body={
<DragDropContext onDragEnd={handleOnDragEnd}> <React.Suspense fallback={null}>
<Droppable droppableId="list__ordering"> <Lazy.DragDropContext onDragEnd={handleOnDragEnd}>
<Lazy.Droppable droppableId="list__ordering">
{(DroppableProvided) => ( {(DroppableProvided) => (
<ClaimList <ClaimList
isCardBody isCardBody
@ -119,8 +124,9 @@ export default function CollectionContent(props: Props) {
droppableProvided={DroppableProvided} droppableProvided={DroppableProvided}
/> />
)} )}
</Droppable> </Lazy.Droppable>
</DragDropContext> </Lazy.DragDropContext>
</React.Suspense>
} }
/> />
); );

View file

@ -1,8 +1,4 @@
// @flow // @flow
// $FlowFixMe
import { DragDropContext, Droppable } from 'react-beautiful-dnd';
import { DOMAIN } from 'config'; import { DOMAIN } from 'config';
import React from 'react'; import React from 'react';
import classnames from 'classnames'; import classnames from 'classnames';
@ -27,6 +23,14 @@ import SUPPORTED_LANGUAGES from 'constants/supported_languages';
import * as PAGES from 'constants/pages'; import * as PAGES from 'constants/pages';
import analytics from 'analytics'; import analytics from 'analytics';
// prettier-ignore
const Lazy = {
// $FlowFixMe
DragDropContext: React.lazy(() => import('react-beautiful-dnd' /* webpackChunkName: "dnd" */).then((module) => ({ default: module.DragDropContext }))),
// $FlowFixMe
Droppable: React.lazy(() => import('react-beautiful-dnd' /* webpackChunkName: "dnd" */).then((module) => ({ default: module.Droppable }))),
};
const LANG_NONE = 'none'; const LANG_NONE = 'none';
const MAX_TAG_SELECT = 5; const MAX_TAG_SELECT = 5;
@ -374,8 +378,9 @@ function CollectionForm(props: Props) {
</div> </div>
</TabPanel> </TabPanel>
<TabPanel> <TabPanel>
<DragDropContext onDragEnd={handleOnDragEnd}> <React.Suspense fallback={null}>
<Droppable droppableId="list__ordering"> <Lazy.DragDropContext onDragEnd={handleOnDragEnd}>
<Lazy.Droppable droppableId="list__ordering">
{(DroppableProvided) => ( {(DroppableProvided) => (
<ClaimList <ClaimList
uris={collectionUrls} uris={collectionUrls}
@ -385,8 +390,9 @@ function CollectionForm(props: Props) {
droppableProvided={DroppableProvided} droppableProvided={DroppableProvided}
/> />
)} )}
</Droppable> </Lazy.Droppable>
</DragDropContext> </Lazy.DragDropContext>
</React.Suspense>
</TabPanel> </TabPanel>
<TabPanel> <TabPanel>
<Card <Card

View file

@ -1,8 +1,4 @@
// @flow // @flow
// $FlowFixMe
import { DragDropContext, Droppable } from 'react-beautiful-dnd';
import React from 'react'; import React from 'react';
import ClaimList from 'component/claimList'; import ClaimList from 'component/claimList';
import Page from 'component/page'; import Page from 'component/page';
@ -20,6 +16,14 @@ import Icon from 'component/common/icon';
import * as ICONS from 'constants/icons'; import * as ICONS from 'constants/icons';
import Spinner from 'component/spinner'; import Spinner from 'component/spinner';
// prettier-ignore
const Lazy = {
// $FlowFixMe
DragDropContext: React.lazy(() => import('react-beautiful-dnd' /* webpackChunkName: "dnd" */).then((module) => ({ default: module.DragDropContext }))),
// $FlowFixMe
Droppable: React.lazy(() => import('react-beautiful-dnd' /* webpackChunkName: "dnd" */).then((module) => ({ default: module.Droppable }))),
};
export const PAGE_VIEW_QUERY = 'view'; export const PAGE_VIEW_QUERY = 'view';
export const EDIT_PAGE = 'edit'; export const EDIT_PAGE = 'edit';
@ -225,9 +229,9 @@ export default function CollectionPage(props: Props) {
{editing} {editing}
<div className={classnames('section card-stack')}> <div className={classnames('section card-stack')}>
{info} {info}
<React.Suspense fallback={null}>
<DragDropContext onDragEnd={handleOnDragEnd}> <Lazy.DragDropContext onDragEnd={handleOnDragEnd}>
<Droppable droppableId="list__ordering"> <Lazy.Droppable droppableId="list__ordering">
{(DroppableProvided) => ( {(DroppableProvided) => (
<ClaimList <ClaimList
uris={collectionUrls} uris={collectionUrls}
@ -237,8 +241,9 @@ export default function CollectionPage(props: Props) {
unavailableUris={unavailableUris} unavailableUris={unavailableUris}
/> />
)} )}
</Droppable> </Lazy.Droppable>
</DragDropContext> </Lazy.DragDropContext>
</React.Suspense>
</div> </div>
</Page> </Page>
); );