Ant Design Svelte

Masonry

Cards of different heights: measured layout, images and adaptive columns.

When to use

Masonry places cards or images of varying heights in the shortest columns. Use it for collections and galleries with uneven content. For regular lines with a common height, Grid is suitable.

Layout depends on DOM measurements. In the current API, elements appear after mount; with SSR, an empty root remains. If the content must be read without JavaScript, choose a different page composition.

Card data and markings

items contains key, data and optional children, column and height. The key must remain unique after conversion to a string and persist when the array is updated.

Problem Setting Example
Place cards of different heights items, itemRender Cards of different heights
Change the number of columns on the screen responsive columns Adaptive speakers
Change topic thresholds and intervals theme.token.screen* Thresholds, columns and intervals
Lay out images Content Dimensions Images with different aspect ratios
Update card contents fresh Adding and editing cards
Prefer a specific column item.column Preferred Column

itemRender - Svelte snippet with item fields, index and calculated column. item.children takes precedence if not null/undefined: even 0, false and the empty string override the renderer. The general children of Masonry itself does not create cards.

The item.height field does not specify the layout geometry: the measured DOM height is used. Assign height to content or let text and image determine it, as in the examples.

Columns, spacing and measurement

columns accepts a positive integer or a responsive map by viewport. By default there are three columns; for a map without a match, xs is used, then one column. Fractional values ​​are not rounded and are not suitable for this setting.

gutter sets the spacing, a couple - horizontal and vertical separately. For the current implementation, use numbers in px or responsive map of numbers. The GridGutter type allows a CSS string, but Masonry adds px and uses the value in arithmetic without string normalization; this scenario is still limited.

In a theme with the Source engine enabled, responsive columns and both gutter axes use the global theme.token.screen*. In normal mode without a theme, standard borders apply. Xs reads screenXSMax, sm and further - screenSM/screenMD/…; min/max values ​​additionally check the order. The wrong order is rejected already at SSR, although the cards have not yet been created.

Example of thresholds changes 576/768 to 640/900, shows inheritance and theme.inherit:false. Design tokens are updated without remount: root, editor, selection and FormData are preserved. If screenSM is greater than screenSMMin, there may be a gap between xs and sm without a match. columns uses xs or one column, and each gutter is resolved separately and can become null; fallback of columns is not carried over to intervals.

By default, the component monitors the root and load/error events of images; changing items also triggers recalculation. fresh includes additional monitoring of card sizes when the content changes arbitrarily. Dynamic example changes height without changing card identity.

onLayoutChange returns the elements in their original order with the calculated column and their original fields. This is a notice of allocation, not a new order of data. The preferred item.column is limited to existing columns.

Design and life cycle

classNames/styles set root/item object or function from props; callback sees the allowed number of columns. styles contains CSS strings. The card geometry is applied after semantic item styles, so set the position and dimensions via the layout API and its contents.

ConfigProvider.masonry inherits the design; the passed nested record replaces the parent one. Semantic example shows the provider's local styles and defaults. There are no proprietary named component tokens; the colors and font set the cards' content and overall theme.

bind:nativeElement returns the root div. When unmounted, the component clears the ref, observers, and subscriptions. Native requests, Object URLs, and content resources are released by the application.

Visual distribution by columns preserves the original order of elements in the DOM. For cards with interactive activities, check the reading and Tab sequence along with changing the number of columns.

A non-empty local prefixCls changes the root/item/motion classes. The empty string and undefined inherit ConfigProvider.prefixCls with the -masonry suffix, defaulting to ant-masonry. Semantic callbacks receive the original local prefixCls value rather than the full resolved class. Example of prefixes shows the same class in areas with different numbers of columns, fresh and saving the editor when updating.

The cell width service variable follows the general prefix without dots: --suite-masonry-item-width. It is independent of the local class and theme.cssVar.prefix; set columns and intervals using columns/gutter. Svelte preserves the direction from ConfigProvider.direction and isolates root/RTL rules. The RTL side effect of the name ant-masonry-rtl from the general reference stylesheet is not reproduced.

Examples

After measurement

Cards of different heights

Elements are distributed into the shortest columns after measuring the DOM. onLayoutChange shows the calculated distribution.

Adaptive speakers

The number of columns and both intervals change according to the viewport. Grid thresholds from xs to xxxl are supported.

Images with different aspect ratios

Masonry monitors image loading and re-measures height. This uses locally generated SVGs without external requests.

3

Adding and changing cards

fresh observes the height of items. A stable key preserves content as it is added, removed, and changed in height.

Preferred column

column specifies the index from zero. As the number of columns decreases, the index is limited to the last available column.

Different axis spacing

Numeric pair specifies horizontal and vertical gutter in px. The control changes the geometry without recreating the data.

Semantic slots and provider

ConfigProvider.masonry sets the general design. The local function styles gets the allowed number of columns.

General: suite; class: project-masonry

The same local class is used for different numbers of columns. Fresh updates the layout after changing the height, saving the editor.

Local and general prefix

Two numbers of columns with one local class; fresh styles, raw prefix in and saving the editor.

Project cards

Initial review example with Card, adaptability and order change.

Thresholds Masonry parent

Speakers: 0 Waiting mount

Inherited Masonry Thresholds

Speakers: 0 Waiting mount

Reset Masonry Thresholds

Speakers: 0 Waiting mount

Columns and both gutter axes read the global theme.token.screen*. Its sm/md thresholds are 640/900px; the nested area inherits them, inherit:false returns 576/768px. The sm query uses screenSM rather than screenSMMin. In the interval without matches, columns reverts to xs, intervals are resolved separately. Changing tokens saves the elements and the editor; with SSR, an empty root without cards remains.

Topic thresholds, columns and spacing

Global screen* tokens change columns and both gutter axes. Comparison of inheritance and inherit:false; saving cards, editor and nativeElement when updating thresholds.

API

Themes of the ant.design live page are mapped to the public API of the installed package. Full definitions of Props are in @woodger/antd.

Topics of official examples
ThemeExample or limitation
CSS prefixesLocal classes, common namespace width and saving measured cells.
BasicHeight measurement and short columns.
ResponsiveColumns and gutter by viewport.
ImageLoading images of different proportions.
DynamicAdding, deleting and fresh monitoring.
Custom semantic dom stylingLocal functions and ConfigProvider.masonry.
Main parameters Masonry
PropertyDescriptionTypeDefault
prefixClsFull local prefix of root/item/motion classes. The empty string and undefined inherit the same prefix. styles/classNames get the original local prefix; The width service variable depends on ConfigProvider.prefixCls.stringConfigProvider + -masonry
itemsUnique key, data and optional children/column/height.MasonryItemType<Data>[]—
itemRenderSvelte snippet; gets the original fields, index and calculated column.Snippet<[MasonryRenderInfo<Data>]>—
columnsPositive integer or breakpoint map by theme.token.screen*. Tokens are inherited and change queries without remount. For map without a match, use xs, then 1.number | GridResponsive<number>3
gutterOne numeric interval for both axes or a pair of horizontal and vertical. Each axis' responsive map uses global screen* theme thresholds.GridGutter | [GridGutter, GridGutter]0
freshContinuous monitoring of the sizes of individual elements via ResizeObserver.booleanfalse
onLayoutChangeInitial order of items with calculated column after measurement.(items with column) => void—
classNamesClasses root/item, object or function.MasonrySemantic<Data>—
stylesCSS lines root/item; the function receives props with columns enabled.MasonrySemantic<Data>—
nativeElementbind:nativeElement returns root after mount.HTMLDivElement—
childrenCompatible Props field; cells are specified through items, and not through general children.MasonryContent—

MasonryItem

key - a unique string or number, uniqueness is checked after conversion to a string. column is the preferred index from zero. data stores application data; itemRender receives them along with index and column. height is adopted for compatibility, but does not replace the DOM dimension.

Semantic DOM

root is a div with a calculated height, item is a positioned cell. The visual distribution by columns does not change the original reading and keyboard order. ConfigProvider.masonry and local classNames/styles style root/item.

On SSR root is empty; items appear after mount, just like the original effect. If you require server-side content, select a different application composition. bind:nativeElement is cleared on unmount; The component removes observers and subscriptions on its own.

Registration tokens

Masonry does not have its own component tokens. The colors and font of the cards set the child components and the overall theme; root/item are styled using semantic styles. The layout and spacing are set by columns/gutter.

General movement tokens of the current topic
TokenCurrent value
motionDurationFast0.1s
motionDurationSlow0.3s
motionEaseOutcubic-bezier(0.215, 0.61, 0.355, 1)

FAQ

Why doesn't changing the height rearrange the layout?

For arbitrary content changes, enable fresh. Without it, the root, image loading and items changes are observed.

What takes precedence over itemRender?

item.children, except null/undefined. The general children of the cell component does not create.

Why doesn't height set the height of the card?

Layout measures DOM. Set the size of the content itself or use its natural height.

Is it possible to pass gutter as a CSS string?

The type allows this, but there is no string normalization in the geometry yet. Use a numeric interval or a responsive map of numbers.