Skip to content

Commit

Permalink
Masonry: Revamp Dynamic Heights (#3890)
Browse files Browse the repository at this point in the history
  • Loading branch information
bryamrrr authored Nov 22, 2024
1 parent fc9160e commit ad9e92a
Show file tree
Hide file tree
Showing 6 changed files with 550 additions and 20 deletions.
8 changes: 8 additions & 0 deletions docs/integration-test-helpers/masonry/MasonryContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ type Props<T> = {
collage?: boolean;
// Constrains the width of the grid rendering.
constrained?: boolean;
// Enable DynamicHeights
dynamicHeights?: boolean;
// Enable DynamicHeights and their v2 version
dynamicHeightsV2?: boolean;
// Whether or not to use an external cache
externalCache?: boolean;
// Does not allow infinite scroll.
Expand Down Expand Up @@ -326,6 +330,8 @@ export default class MasonryContainer extends Component<Props<Record<any, any>>,
MasonryComponent,
collage,
constrained,
dynamicHeights,
dynamicHeightsV2,
externalCache,
finiteLength,
flexible,
Expand Down Expand Up @@ -408,6 +414,8 @@ export default class MasonryContainer extends Component<Props<Record<any, any>>,
{mountGrid && (
<MasonryComponent
ref={this.gridRef}
_dynamicHeights={dynamicHeights || dynamicHeightsV2}
_dynamicHeightsV2Experiment={dynamicHeightsV2}
_getColumnSpanConfig={(item) => {
const columnSpan = item.columnSpan as number | undefined;
return columnSpan ?? 1;
Expand Down
4 changes: 4 additions & 0 deletions docs/pages/integration-test/masonry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export default function TestPage({
const {
constrained,
deferMount,
dynamicHeights,
dynamicHeightsV2,
externalCache,
experimental,
finiteLength,
Expand Down Expand Up @@ -111,6 +113,8 @@ export default function TestPage({
<MasonryContainer
// @ts-expect-error - TS2345 - Argument of type 'string | string[] | undefined' is not assignable to parameter of type 'string'.
constrained={booleanize(constrained)}
dynamicHeights={dynamicHeights}
dynamicHeightsV2={dynamicHeightsV2}
// @ts-expect-error - TS2345 - Argument of type 'string | string[] | undefined' is not assignable to parameter of type 'string'.
externalCache={booleanize(externalCache)}
// @ts-expect-error - TS2345 - Argument of type 'string | string[] | undefined' is not assignable to parameter of type 'string'.
Expand Down
43 changes: 35 additions & 8 deletions packages/gestalt/src/Masonry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import FetchItems from './FetchItems';
import styles from './Masonry.css';
import { Cache } from './Masonry/Cache';
import recalcHeights from './Masonry/dynamicHeightsUtils';
import recalcHeightsV2 from './Masonry/dynamicHeightsV2Utils';
import getLayoutAlgorithm from './Masonry/getLayoutAlgorithm';
import ItemResizeObserverWrapper from './Masonry/ItemResizeObserverWrapper';
import MeasurementStore from './Masonry/MeasurementStore';
Expand Down Expand Up @@ -148,6 +149,12 @@ type Props<T> = {
*/
_dynamicHeights?: boolean;
/**
* Experimental flag to enable an experiment to use a revamped version of dynamic heights (This needs _dynamicHeights enabled)
*/
_dynamicHeightsV2Experiment?: boolean;
/**
/**
*
* Experimental prop to enable early bailout when positioning multicolumn modules
*
* This is an experimental prop and may be removed or changed in the future
Expand Down Expand Up @@ -225,14 +232,34 @@ export default class Masonry<T> extends ReactComponent<Props<T>, State<T>> {
const changedItem: T = this.state.items[idx]!;
const newHeight = contentRect.height;

triggerUpdate =
recalcHeights({
items: this.state.items,
changedItem,
newHeight,
positionStore: this.positionStore,
measurementStore: this.state.measurementStore,
}) || triggerUpdate;
// TODO: DefaultGutter comes from getLayoutAlgorithm and their utils, everything should be in one place (this.gutter?)
const { layout, gutterWidth } = this.props;
let defaultGutter = 14;
if ((layout && layout === 'flexible') || layout === 'serverRenderedFlexible') {
defaultGutter = 0;
}

/* eslint-disable-next-line no-underscore-dangle */
if (props._dynamicHeightsV2Experiment) {
triggerUpdate =
recalcHeightsV2({
items: this.state.items,
changedItem,
newHeight,
positionStore: this.positionStore,
measurementStore: this.state.measurementStore,
gutterWidth: gutterWidth ?? defaultGutter,
}) || triggerUpdate;
} else {
triggerUpdate =
recalcHeights({
items: this.state.items,
changedItem,
newHeight,
positionStore: this.positionStore,
measurementStore: this.state.measurementStore,
}) || triggerUpdate;
}
}
});
if (triggerUpdate) {
Expand Down
Loading

0 comments on commit ad9e92a

Please sign in to comment.