Masonry
import { Masonry } from '@dxos/react-ui-masonry';npm install @dxos/react-ui-masonryMasonry packs tiles of different heights into columns. It puts each tile in the shortest column, so the columns have
no gaps.
The number of columns follows the width the masonry has. When the items change, the tiles that stay move to their new places, and the masonry measures only the new ones.
| Part | What it is |
|---|---|
Masonry.Root |
The root, which takes the Tile that draws one item, and the column sizes |
Masonry.Content |
The scroll area for the tiles |
Masonry.Viewport |
The tiles. It takes the items and a getId for each |
Masonry.Root
Section titled “Masonry.Root”Masonry.Root takes these props:
| Prop | Type | Effect |
|---|---|---|
Tile |
ComponentType<{ data }> |
Draws one item |
minColumnWidth |
number |
Sets the narrowest a column gets, in rem |
maxColumnWidth |
number |
Sets the widest a column gets, in rem |
maxColumns |
number |
Sets the most columns |
gap |
number |
Sets the space between tiles, in rem |
Lay out tiles
Section titled “Lay out tiles”Build each tile from a Card from @dxos/react-ui, so the tiles match the rest of the app.
Example (Laying out a photo gallery)
import { const Masonry: { Root: { ({ children, Tile, columns, maxColumns, minColumnWidth, maxColumnWidth, gap, animate, centered, }: MasonryRootProps): React.JSX.Element; displayName: string; }; Content: (props: MasonryContentProps & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element; Viewport: <Item>(props: MasonryViewportProps<Item> & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element;}
Masonry } from '@dxos/react-ui-masonry';import * as import Card
Card from '@dxos/react-ui/Card';
type type Photo = { id: string; title: string; color: string; height: number; caption?: string;}
Photo = { id: string
id: string; title: string
title: string; color: string
color: string; height: number
height: number; caption?: string | undefined
caption?: string;};
const const PHOTOS: Photo[]
PHOTOS: type Photo = { id: string; title: string; color: string; height: number; caption?: string;}
Photo[] = [ { id: string
id: '1', title: string
title: 'Harbor', color: string
color: '#0891b2', height: number
height: 80, caption?: string | undefined
caption: 'Boats at first light.', }, { id: string
id: '2', title: string
title: 'Dunes', color: string
color: '#ca8a04', height: number
height: 48 }, { id: string
id: '3', title: string
title: 'Forest', color: string
color: '#059669', height: number
height: 104, caption?: string | undefined
caption: 'A path through the pines, after rain.', }, { id: string
id: '4', title: string
title: 'Peaks', color: string
color: '#4f46e5', height: number
height: 64 }, { id: string
id: '5', title: string
title: 'Market', color: string
color: '#e11d48', height: number
height: 88, caption?: string | undefined
caption: 'Saturday stalls.', }, { id: string
id: '6', title: string
title: 'Canyon', color: string
color: '#ea580c', height: number
height: 56 }, { id: string
id: '7', title: string
title: 'Lagoon', color: string
color: '#2563eb', height: number
height: 72, caption?: string | undefined
caption: 'Still water, no wind.', }, { id: string
id: '8', title: string
title: 'Meadow', color: string
color: '#65a30d', height: number
height: 60 }, { id: string
id: '9', title: string
title: 'Glacier', color: string
color: '#0ea5e9', height: number
height: 120, caption?: string | undefined
caption: 'Blue ice under a gray sky.', }, { id: string
id: '10', title: string
title: 'Orchard', color: string
color: '#16a34a', height: number
height: 52 }, { id: string
id: '11', title: string
title: 'Lighthouse', color: string
color: '#dc2626', height: number
height: 96, caption?: string | undefined
caption: 'The lamp turns at dusk.', }, { id: string
id: '12', title: string
title: 'Desert', color: string
color: '#d97706', height: number
height: 68 }, { id: string
id: '13', title: string
title: 'Bridge', color: string
color: '#7c3aed', height: number
height: 84, caption?: string | undefined
caption: 'Fog over the river.', }, { id: string
id: '14', title: string
title: 'Reef', color: string
color: '#0d9488', height: number
height: 44 }, { id: string
id: '15', title: string
title: 'Vineyard', color: string
color: '#9333ea', height: number
height: 112, caption?: string | undefined
caption: 'Rows down to the lake.', }, { id: string
id: '16', title: string
title: 'Tundra', color: string
color: '#64748b', height: number
height: 58 },];
/** A tile draws one item; the masonry measures each tile and places it in the shortest column. */const const PhotoTile: ({ data: photo }: { data: Photo;}) => React.JSX.Element
A tile draws one item; the masonry measures each tile and places it in the shortest column.
PhotoTile = ({ data: Photo
data: photo: Photo
photo }: { data: Photo
data: type Photo = { id: string; title: string; color: string; height: number; caption?: string;}
Photo }) => ( <import Card
Card.const Root: React.ForwardRefExoticComponent<Omit<Omit<Card.RootProps, "classNames">, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>export Root
A card one level above its host (level='+1'), whose Header, Body and Footer share one content edge. With grid it
is a rail Container (the child div wins the asChild merge, so the part keeps the card scope); without, a plain
column that creates no grid. With onClick the card is a button (Enter and Space activate it); nested actions and
menus stop their clicks reaching it.
Root> <JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div HTMLAttributes<T>.style?: React.CSSProperties | undefined
style={{ StandardLonghandProperties<string | number, string & {}>.height?: Property.Height<string | number> | undefined
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Syntax: auto | <length-percentage [0,∞]> | min-content | max-content | fit-content | fit-content(<length-percentage [0,∞]>) | <calc-size()> | <anchor-size()>
Initial value: auto
| Chrome | Firefox | Safari | Edge | IE |
| :----: | :-----: | :----: | :----: | :---: |
| 1 | 1 | 1 | 12 | 4 |
height: photo: Photo
photo.height: number
height, StandardShorthandProperties<string | number, string & {}>.background?: Property.Background<string | number> | undefined
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Syntax: <bg-layer>#? , <final-bg-layer>
| Chrome | Firefox | Safari | Edge | IE |
| :----: | :-----: | :----: | :----: | :---: |
| 1 | 1 | 1 | 12 | 4 |
background: photo: Photo
photo.color: string
color }} /> <import Card
Card.const Header: React.ForwardRefExoticComponent<Omit<Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref">, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & React.RefAttributes<HTMLDivElement>>export Header
A block row across the card's rails holding the Title: a Block or icon-only Button before the Title fills the start
rail, and the last one after it the end rail, so the Title starts at the content edge.
Header> <import Card
Card.const Title: React.ForwardRefExoticComponent<Omit<Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>, "ref">, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & Pick<TypographyProps, "lines" | "tone" | "truncate"> & React.RefAttributes<HTMLHeadingElement>>export Title
An h3 on Typography, so it clamps (lines), truncates and takes a tone like any text.
Title>{photo: Photo
photo.title: string
title}</import Card
Card.const Title: React.ForwardRefExoticComponent<Omit<Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>, "ref">, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & Pick<TypographyProps, "lines" | "tone" | "truncate"> & React.RefAttributes<HTMLHeadingElement>>export Title
An h3 on Typography, so it clamps (lines), truncates and takes a tone like any text.
Title> </import Card
Card.const Header: React.ForwardRefExoticComponent<Omit<Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref">, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & React.RefAttributes<HTMLDivElement>>export Header
A block row across the card's rails holding the Title: a Block or icon-only Button before the Title fills the start
rail, and the last one after it the end rail, so the Title starts at the content edge.
Header> {photo: Photo
photo.caption?: string | undefined
caption && ( <import Card
Card.const Row: React.ForwardRefExoticComponent<Omit<Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref">, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & { icon?: string; leading?: React.ReactNode; trailing?: React.ReactNode; end?: React.ReactNode; current?: boolean; span?: "full" | "end"; align?: "start";} & React.RefAttributes<HTMLDivElement>>export Row
A block-tall row of icon, text and trailing content, laid out inline; in a grid card it is a subgrid, so the icon
lands in the card's start rail, the text in its content track and the trailing content in its end rail. With onClick the row is a button (Enter and Space activate
it), as the current Card.Action row was.
Row classNames?: ClassNameValue
classNames='pb-2'> <import Card
Card.const Text: React.ForwardRefExoticComponent<Omit<Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>, "ref">, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & { truncate?: boolean; lines?: number; variant?: "default" | "muted";} & React.RefAttributes<HTMLParagraphElement>>export Text
Card text on Typography, with the current Card.Text variants.
Text variant?: "default" | "muted" | undefined
description reads as secondary text.
variant='muted' lines?: number | undefined
At most this many lines, the last ending in an ellipsis.
lines={2}> {photo: Photo
photo.caption?: string
caption} </import Card
Card.const Text: React.ForwardRefExoticComponent<Omit<Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>, "ref">, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & { truncate?: boolean; lines?: number; variant?: "default" | "muted";} & React.RefAttributes<HTMLParagraphElement>>export Text
Card text on Typography, with the current Card.Text variants.
Text> </import Card
Card.const Row: React.ForwardRefExoticComponent<Omit<Omit<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref">, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & { icon?: string; leading?: React.ReactNode; trailing?: React.ReactNode; end?: React.ReactNode; current?: boolean; span?: "full" | "end"; align?: "start";} & React.RefAttributes<HTMLDivElement>>export Row
A block-tall row of icon, text and trailing content, laid out inline; in a grid card it is a subgrid, so the icon
lands in the card's start rail, the text in its content track and the trailing content in its end rail. With onClick the row is a button (Enter and Space activate
it), as the current Card.Action row was.
Row> )} </import Card
Card.const Root: React.ForwardRefExoticComponent<Omit<Omit<Card.RootProps, "classNames">, "className"> & { classNames?: import("@dxos/ui-types").ClassNameValue;} & Pick<React.HTMLAttributes<Element>, "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>export Root
A card one level above its host (level='+1'), whose Header, Body and Footer share one content edge. With grid it
is a rail Container (the child div wins the asChild merge, so the part keeps the card scope); without, a plain
column that creates no grid. With onClick the card is a button (Enter and Space activate it); nested actions and
menus stop their clicks reaching it.
Root>);
/** Tiles of different heights packed into columns; the number of columns follows the width, at least 9rem each. */export const const PhotoGallery: () => React.JSX.Element
Tiles of different heights packed into columns; the number of columns follows the width, at least 9rem each.
PhotoGallery = () => ( <const Masonry: { Root: { ({ children, Tile, columns, maxColumns, minColumnWidth, maxColumnWidth, gap, animate, centered, }: MasonryRootProps): React.JSX.Element; displayName: string; }; Content: (props: MasonryContentProps & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element; Viewport: <Item>(props: MasonryViewportProps<Item> & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element;}
Masonry.type Root: { ({ children, Tile, columns, maxColumns, minColumnWidth, maxColumnWidth, gap, animate, centered, }: MasonryRootProps): React.JSX.Element; displayName: string;}
Root Tile?: React.ComponentType<{ data: any; index: number; selected?: boolean;}> | undefined
Render component for each masonry item. Receives selected when the grid is selectable.
Tile={const PhotoTile: ({ data: photo }: { data: Photo;}) => React.JSX.Element
A tile draws one item; the masonry measures each tile and places it in the shortest column.
PhotoTile} minColumnWidth?: number | undefined
Minimum column width in rem.
minColumnWidth={9}> <const Masonry: { Root: { ({ children, Tile, columns, maxColumns, minColumnWidth, maxColumnWidth, gap, animate, centered, }: MasonryRootProps): React.JSX.Element; displayName: string; }; Content: (props: MasonryContentProps & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element; Viewport: <Item>(props: MasonryViewportProps<Item> & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element;}
Masonry.type Content: (props: MasonryContentProps & { ref?: React.Ref<HTMLDivElement | null>;}) => React.JSX.Element
Content> <const Masonry: { Root: { ({ children, Tile, columns, maxColumns, minColumnWidth, maxColumnWidth, gap, animate, centered, }: MasonryRootProps): React.JSX.Element; displayName: string; }; Content: (props: MasonryContentProps & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element; Viewport: <Item>(props: MasonryViewportProps<Item> & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element;}
Masonry.type Viewport: <Item>(props: MasonryViewportProps<Item> & { ref?: React.Ref<HTMLDivElement | null>;}) => React.JSX.Element
Viewport items: readonly Photo[]
Items to render in the masonry grid.
items={const PHOTOS: Photo[]
PHOTOS} getId?: ((data: Photo) => string) | undefined
Extract a stable key from an item, aligned with react-ui-mosaic's getId.
getId={(photo: Photo
photo) => photo: Photo
photo.id: string
id} /> </const Masonry: { Root: { ({ children, Tile, columns, maxColumns, minColumnWidth, maxColumnWidth, gap, animate, centered, }: MasonryRootProps): React.JSX.Element; displayName: string; }; Content: (props: MasonryContentProps & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element; Viewport: <Item>(props: MasonryViewportProps<Item> & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element;}
Masonry.type Content: (props: MasonryContentProps & { ref?: React.Ref<HTMLDivElement | null>;}) => React.JSX.Element
Content> </const Masonry: { Root: { ({ children, Tile, columns, maxColumns, minColumnWidth, maxColumnWidth, gap, animate, centered, }: MasonryRootProps): React.JSX.Element; displayName: string; }; Content: (props: MasonryContentProps & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element; Viewport: <Item>(props: MasonryViewportProps<Item> & { ref?: React.Ref<HTMLDivElement | null>; }) => React.JSX.Element;}
Masonry.type Root: { ({ children, Tile, columns, maxColumns, minColumnWidth, maxColumnWidth, gap, animate, centered, }: MasonryRootProps): React.JSX.Element; displayName: string;}
Root>);
PhotoTile draws one photo as a card, and minColumnWidth keeps each column at least 9rem wide. Masonry.Viewport
takes the photos as items and reads each one’s id with getId.
The story at the top of this page is a different gallery. It shows portrait cards of people, in columns at least 10rem wide.