|
| 1 | +--- |
| 2 | +title: '@masonry-grid/svelte' |
| 3 | +description: Documentation for the Svelte package of Masonry Grid |
| 4 | +sidebar: |
| 5 | + order: 3 |
| 6 | +--- |
| 7 | + |
| 8 | +## MasonryGrid |
| 9 | + |
| 10 | +Standard masonry layout that stacks items by pulling them up to fill gaps. |
| 11 | + |
| 12 | +```svelte |
| 13 | +<script> |
| 14 | + import { MasonryGrid, Frame } from '@masonry-grid/svelte' |
| 15 | +</script> |
| 16 | +
|
| 17 | +<MasonryGrid |
| 18 | + frameWidth={200} |
| 19 | + gap={10} |
| 20 | +> |
| 21 | + <Frame width={4} height={3}> |
| 22 | + <img src='...' alt='' /> |
| 23 | + </Frame> |
| 24 | + <!-- more frames... --> |
| 25 | +</MasonryGrid> |
| 26 | +``` |
| 27 | + |
| 28 | +### Props |
| 29 | + |
| 30 | +- `frameWidth?: number | string` - Minimum width of each frame. Used to calculate `grid-template-columns`. Can be a number (px) or string with units. |
| 31 | +- `gap?: number | string` - Gap between items. Can be a number (px) or string with units. |
| 32 | +- `disabled?: boolean` - Disable the masonry layout (no transforms/reordering will be applied). |
| 33 | +- `as?: string` - Render as a different element (default: `'div'`). |
| 34 | +- All other HTML attributes are passed through. |
| 35 | + |
| 36 | +## BalancedMasonryGrid |
| 37 | + |
| 38 | +Balanced masonry layout that reorders items inside rows to minimize overall grid height. |
| 39 | + |
| 40 | +```svelte |
| 41 | +<script> |
| 42 | + import { BalancedMasonryGrid, Frame } from '@masonry-grid/svelte' |
| 43 | +</script> |
| 44 | +
|
| 45 | +<BalancedMasonryGrid |
| 46 | + frameWidth={200} |
| 47 | + gap={10} |
| 48 | +> |
| 49 | + <Frame width={4} height={3}> |
| 50 | + <img src='...' alt='' /> |
| 51 | + </Frame> |
| 52 | + <!-- more frames... --> |
| 53 | +</BalancedMasonryGrid> |
| 54 | +``` |
| 55 | + |
| 56 | +### Props |
| 57 | + |
| 58 | +Same as `MasonryGrid`. |
| 59 | + |
| 60 | +## Frame |
| 61 | + |
| 62 | +Component for defining masonry grid item with aspect ratio. |
| 63 | + |
| 64 | +```svelte |
| 65 | +<script> |
| 66 | + import { Frame } from '@masonry-grid/svelte' |
| 67 | +</script> |
| 68 | +
|
| 69 | +<Frame |
| 70 | + width={16} |
| 71 | + height={9} |
| 72 | + as='li' |
| 73 | +> |
| 74 | + <img src='...' alt='' /> |
| 75 | +</Frame> |
| 76 | +``` |
| 77 | + |
| 78 | +### Props |
| 79 | + |
| 80 | +- `width: number` - Width for aspect ratio calculation (not necessarily real pixel width). |
| 81 | +- `height: number` - Height for aspect ratio calculation (not necessarily real pixel height). |
| 82 | +- `as?: string` - Render as a different element (default: `'div'`). |
| 83 | +- All other HTML attributes are passed through. |
| 84 | + |
| 85 | +## masonry |
| 86 | + |
| 87 | +Svelte action for advanced use cases when you need direct access to the masonry grid instance. |
| 88 | + |
| 89 | +```svelte |
| 90 | +<script> |
| 91 | + import { masonry } from '@masonry-grid/svelte' |
| 92 | + import { MasonryGrid as VanillaMasonryGrid } from '@masonry-grid/vanilla' |
| 93 | +</script> |
| 94 | +
|
| 95 | +<div |
| 96 | + use:masonry={{ type: VanillaMasonryGrid }} |
| 97 | + style="display: grid; overflow: hidden; gap: 10px;" |
| 98 | +> |
| 99 | + <!-- children --> |
| 100 | +</div> |
| 101 | +``` |
0 commit comments