Composite

ItemsGrid

A responsive grid that renders a list of items, sizing its columns to the container it sits in.

ItemsGrid sizes its columns from the width of the element that contains it, not from the viewport. A grid asks for at most columns columns and, by default, steps down when its container is too narrow to fit them: two columns from 32rem, three from 48rem, four from 64rem. The same grid therefore renders three columns in a full-width section and one in a sidebar, with no change at the call site. Set responsive={false} to keep columns at any width (see Fixed columns).

Basic grid

No defaultItemConfig: each item carries its own icon, layout and actions.

No customization
Icon configuration is default.
Icon customization
Icon as been given a background, padding and rounded corners.
Vertical Layout with action
You shall see a button below.
No customization
Icon configuration is default.
Icon customization
Icon as been given a background, padding and rounded corners.
Horizontal Layout with action
You shall see a button below.
basic
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<ItemsGridBlock
  columns={3}
  items={[
    {
      icon: { name: 'heroicons:sun' },
      title: 'No customization',
      content: 'Icon configuration is default.',
    },
    {
      icon: {
        name: 'heroicons:sparkles',
        class: 'bg-accent text-background p-2 rounded-full',
      },
      title: 'Icon customization',
      content: 'Icon as been given a background, padding and rounded corners.',
    },
    {
      icon: { name: 'heroicons:bolt', class: 'text-success' },
      title: 'Vertical Layout with action',
      content: 'You shall see a button below.',
      actions: [{ label: 'Learn More', intent: 'success' }],
    },
    {
      icon: { name: 'heroicons:sun' },
      title: 'No customization',
      content: 'Icon configuration is default.',
      orientation: 'horizontal',
    },
    {
      icon: {
        name: 'heroicons:sparkles',
        class: 'bg-accent text-background p-2 rounded-full',
      },
      title: 'Icon customization',
      content: 'Icon as been given a background, padding and rounded corners.',
      orientation: 'horizontal',
    },
    {
      icon: { name: 'heroicons:bolt', class: 'text-success' },
      title: 'Horizontal Layout with action',
      content: 'You shall see a button below.',
      actions: [{ label: 'Learn More', intent: 'success' }],
      orientation: 'horizontal',
    },
  ]}
/>

Default layout and action

Default layout & action
This item uses the default horizontal layout and action.
Override action
This item overrides the default action.
Default layout & action
This item uses the default horizontal layout and action.
default-layout-action
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<ItemsGridBlock
  columns={2}
  defaultItemConfig={{
    orientation: 'horizontal',
    actions: [{ label: 'Default Action', intent: 'accent' }],
  }}
  items={[
    {
      icon: { name: 'heroicons:star' },
      title: 'Default layout & action',
      content: 'This item uses the default horizontal layout and action.',
    },
    {
      icon: { name: 'heroicons:fire', class: 'text-error' },
      title: 'Override action',
      content: 'This item overrides the default action.',
      actions: [{ label: 'Custom Action', intent: 'error' }],
    },
    {
      icon: { name: 'heroicons:moon' },
      title: 'Default layout & action',
      content: 'This item uses the default horizontal layout and action.',
    },
  ]}
/>

The same grid in a narrow container

Both grids below are columns={3} with identical items. The second one sits in a max-w-sm wrapper, so it stacks to a single column at any viewport width.

Wide container
Three columns once the container passes 48rem.
Wide container
Three columns once the container passes 48rem.
Wide container
Three columns once the container passes 48rem.
Narrow container
Same columns, but the container is under 32rem.
Narrow container
Same columns, but the container is under 32rem.
Narrow container
Same columns, but the container is under 32rem.
narrow-container
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<div class="space-y-8">
  <ItemsGridBlock
    columns={3}
    items={[
      {
        icon: { name: 'heroicons:sun' },
        title: 'Wide container',
        content: 'Three columns once the container passes 48rem.',
      },
      {
        icon: { name: 'heroicons:sparkles' },
        title: 'Wide container',
        content: 'Three columns once the container passes 48rem.',
      },
      {
        icon: { name: 'heroicons:bolt' },
        title: 'Wide container',
        content: 'Three columns once the container passes 48rem.',
      },
    ]}
  />
  <div class="max-w-sm">
    <ItemsGridBlock
      columns={3}
      items={[
        {
          icon: { name: 'heroicons:sun' },
          title: 'Narrow container',
          content: 'Same columns, but the container is under 32rem.',
        },
        {
          icon: { name: 'heroicons:sparkles' },
          title: 'Narrow container',
          content: 'Same columns, but the container is under 32rem.',
        },
        {
          icon: { name: 'heroicons:bolt' },
          title: 'Narrow container',
          content: 'Same columns, but the container is under 32rem.',
        },
      ]}
    />
  </div>
</div>

Single selection

With selection={{ mode: 'single', group, label }} every item becomes an option. Each item needs a code, the value published to the group; selected: true marks the option chosen on load. Options cannot carry actions.

Small
30 × 30 cm
Medium
45 × 45 cm
Large
60 × 60 cm
single-selection
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<ItemsGridBlock
  columns={3}
  selection={{ mode: 'single', group: 'itemsgrid-doc-single', label: 'Size' }}
  items={[
    { code: 'size-s', icon: { name: 'heroicons:cube' }, title: 'Small', content: '30 × 30 cm' },
    {
      code: 'size-m',
      icon: { name: 'heroicons:cube' },
      title: 'Medium',
      content: '45 × 45 cm',
      selected: true,
    },
    { code: 'size-l', icon: { name: 'heroicons:cube' }, title: 'Large', content: '60 × 60 cm' },
  ]}
/>

Multiple selection with min and max

mode: 'multiple' turns each option into a checkbox. max disables the remaining options once reached; min refuses a deselect that would drop below it. An initial state below min is allowed — nothing is selected implicitly.

Wi-Fi
Parking
Breakfast
Pool
multiple-selection
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<ItemsGridBlock
  columns={4}
  selection={{
    mode: 'multiple',
    group: 'itemsgrid-doc-multiple',
    label: 'Extras',
    min: 1,
    max: 2,
  }}
  items={[
    { code: 'wifi', icon: { name: 'heroicons:wifi' }, title: 'Wi-Fi', selected: true },
    { code: 'parking', icon: { name: 'heroicons:truck' }, title: 'Parking' },
    { code: 'breakfast', icon: { name: 'heroicons:cake' }, title: 'Breakfast' },
    { code: 'pool', icon: { name: 'heroicons:sun' }, title: 'Pool' },
  ]}
/>

Custom selectedItemConfig

selectedItemConfig is merged into every option’s selected variant after defaultItemConfig and before the item itself, like activeItemConfig in Tabs. This grid is named by the “Wood” label above it via labelledBy.

Wood

Oak
Walnut
Ash
custom-selected-config
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<div class="space-y-4">
  <p id="itemsgrid-custom-heading" class="font-semibold">
    Wood
  </p>
  <ItemsGridBlock
    columns={3}
    selection={{
      mode: 'single',
      group: 'itemsgrid-doc-custom',
      labelledBy: 'itemsgrid-custom-heading',
    }}
    defaultItemConfig={{ spacing: { padding: 'p-4' }, border: { radius: 'rounded-lg' } }}
    selectedItemConfig={{
      background: { class: 'bg-accent/10' },
      border: { color: 'border-accent' },
    }}
    items={[
      { code: 'oak', icon: { name: 'heroicons:swatch' }, title: 'Oak', selected: true },
      { code: 'walnut', icon: { name: 'heroicons:swatch' }, title: 'Walnut' },
      { code: 'ash', icon: { name: 'heroicons:swatch' }, title: 'Ash' },
    ]}
  />
</div>

Selection intent

selection.intent colours the selected border and the focus ring, with the same values as Button’s intent (default: 'primary'). selectedItemConfig.border still wins over it.

Matte
Satin
Gloss
intent
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<ItemsGridBlock
  columns={3}
  selection={{
    mode: 'single',
    group: 'itemsgrid-doc-intent',
    label: 'Finish',
    intent: 'accent',
  }}
  items={[
    { code: 'matte', icon: { name: 'heroicons:swatch' }, title: 'Matte', selected: true },
    { code: 'satin', icon: { name: 'heroicons:swatch' }, title: 'Satin' },
    { code: 'gloss', icon: { name: 'heroicons:swatch' }, title: 'Gloss' },
  ]}
/>

Listening to selection:change

The grid publishes to the selection runtime; anything on the page can read it with subscribe(group, listener) or a selection:change listener on document. Other publishers to the same group update the grid too — the button below sits outside the grid, in its own element declaring the same group.

Standard
Express
Pickup
No change yet
selection-events
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<ItemsGridBlock
  columns={3}
  selection={{ mode: 'single', group: 'itemsgrid-doc-events', label: 'Delivery' }}
  items={[
    { code: 'standard', icon: { name: 'heroicons:truck' }, title: 'Standard' },
    { code: 'express', icon: { name: 'heroicons:bolt' }, title: 'Express' },
    { code: 'pickup', icon: { name: 'heroicons:building-storefront' }, title: 'Pickup' },
  ]}
/>
<div
  data-selection-group="itemsgrid-doc-events"
  data-selection-mode="single"
  class="mt-4 flex items-center gap-4"
>
  <button
    type="button"
    data-selection-external="pickup"
    class="px-3 py-1 border border-border rounded"
  >
    Select Pickup externally
  </button>
  <output data-selection-log class="font-mono text-sm">
    No change yet
  </output>
</div>

<script>
  import { publish, subscribe } from '@alfadormx/blocks/selection';

  const SECTION = '[data-doc-section="selection-events"]';
  const GROUP = 'itemsgrid-doc-events';

  function initLog(): void {
    const log = document.querySelector<HTMLOutputElement>(`${SECTION} [data-selection-log]`);
    const button = document.querySelector<HTMLButtonElement>(
      `${SECTION} [data-selection-external]`
    );
    if (!log || !button) return;
    subscribe(GROUP, (detail) => (log.textContent = JSON.stringify(detail)));
    // onclick, not addEventListener: re-running initLog replaces the handler instead of stacking it.
    button.onclick = () => publish(button, [{ code: button.dataset.selectionExternal! }]);
  }

  document.addEventListener('astro:after-swap', initLog);
  initLog();
</script>

Fixed columns

responsive={false} keeps columns at any container width. This is the narrow grid from The same grid in a narrow container again, and it now holds three columns.

One
Two
Three
fixed-columns
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<div class="max-w-sm">
  <ItemsGridBlock
    columns={3}
    responsive={false}
    gap="tight"
    items={[
      { icon: { name: 'heroicons:sun' }, title: 'One' },
      { icon: { name: 'heroicons:sparkles' }, title: 'Two' },
      { icon: { name: 'heroicons:bolt' }, title: 'Three' },
    ]}
  />
</div>

Fill the last row

fillLastRow stretches a lone item in the last row across the full row. It is computed from columns, so it is exact only together with responsive={false}.

One
Two
Three
Four
Five
Alone in its row, so it spans both columns.
fill-last-row
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<ItemsGridBlock
  columns={2}
  responsive={false}
  fillLastRow
  gap="tight"
  defaultItemConfig={{
    border: { width: 'border', color: 'border-border' },
    spacing: { padding: 'p-4' },
  }}
  items={[
    { title: 'One' },
    { title: 'Two' },
    { title: 'Three' },
    { title: 'Four' },
    { title: 'Five', content: 'Alone in its row, so it spans both columns.' },
  ]}
/>

A consumer col-span-* on the last item wins over fillLastRow.

One
Two
Three
Four
Five
fill-last-row-opt-out
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<ItemsGridBlock
  columns={2}
  responsive={false}
  fillLastRow
  gap="tight"
  defaultItemConfig={{
    border: { width: 'border', color: 'border-border' },
    spacing: { padding: 'p-4' },
  }}
  items={[
    { title: 'One' },
    { title: 'Two' },
    { title: 'Three' },
    { title: 'Four' },
    { title: 'Five', class: 'col-span-1' },
  ]}
/>

Action defaults

defaultItemConfig.defaultActionConfig styles every action of every item. See the Item page’s Action defaults section for how it merges.

default-action-config
---
import ItemsGridBlock from '@alfadormx/blocks/composite/ItemsGrid.astro';
---

<ItemsGridBlock
  columns={2}
  defaultItemConfig={{ defaultActionConfig: { variant: 'ghost', intent: 'primary' } }}
  items={[
    {
      icon: { name: 'heroicons:book-open' },
      title: 'Guides',
      actions: [
        { label: 'Read', href: '#' },
        { label: 'Download', href: '#' },
      ],
    },
    {
      icon: { name: 'heroicons:play-circle' },
      title: 'Videos',
      actions: [
        { label: 'Watch', href: '#' },
        { label: 'Subscribe', href: '#' },
      ],
    },
  ]}
/>

Selection props

selection is an ItemsGridSelection: { mode: 'none' } (default, a static grid with no roles or script), { mode: 'single', group, label?, labelledBy?, intent? }, or { mode: 'multiple', group, label?, labelledBy?, min?, max?, intent? }. One of label / labelledBy is required; labelledBy wins. Items are ItemsGridItems — Item props plus code (required when selection is enabled, unique within the grid) and selected (initial state). Invalid configurations throw at build time.

API Reference

PropTypeDefaultDescription
itemsItemsGridItem[][]The list of items to render in the grid, each rendered via the Item component.
columns1 | 2 | 3 | 43Maximum number of grid columns, reached when the grid's container is wide enough; with `responsive` the grid steps down to fewer columns in narrower containers, without it the count is fixed (default: 3).
responsivebooleantrueSteps the column count down in narrower containers; `false` keeps `columns` at any width (default: true).
fillLastRowbooleanfalseA lone item in the last row spans the full row; computed from `columns`, so exact only with `responsive: false` (default: false).
gap'none' | 'tight' | 'normal' | 'loose''normal'Gap spacing between grid items (default: 'normal').
classstring''Additional CSS classes appended to the grid container (default: '').
defaultItemConfigLayerConfig<ItemProps>—Default values merged into every item's props before rendering, overridden by each item's own values.
selectionItemsGridSelection{ mode: 'none' }Selection mode of the grid; `'none'` renders a static, non-interactive grid (default: { mode: 'none' }).
selectedItemConfigLayerConfig<ItemProps>—Values merged into each item's selected variant, after `defaultItemConfig` and before the item's own values.