Skip to content

fix: use container queries for MediaBlock responsive sizing#1113

Open
claude[bot] wants to merge 8 commits into
mainfrom
claude/issue-757-20260611-2132
Open

fix: use container queries for MediaBlock responsive sizing#1113
claude[bot] wants to merge 8 commits into
mainfrom
claude/issue-757-20260611-2132

Conversation

@claude

@claude claude Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Description

Make MediaBlock (and InlineMedia) responsive sizing track the container the block occupies rather than the viewport, so the size options stay visually distinct in any context — a full-width page, the narrower blog body, or a column in a multi-column Content block.

Sizing is done entirely in CSS with container-query units (cqw): each size is a percentage of the block's @container, with a px floor. Because images are lazy-loaded, the sizes attribute is set to auto, which lets the browser read the actual rendered (cqw) width and pick the matching srcset candidate — no JS width estimation needed.

Also adds an Extra small size, the "additional, smaller size" requested in the issue.

Related Issues

Closes #757

Key Changes

  • src/blocks/Media/Component.tsx:
    • Wrapper marked @container; size classes use container units — xsmallmax-w-[max(12rem,33cqw)], smallmax-w-[max(16rem,50cqw)], mediummax-w-[max(20rem,75cqw)], largemax-w-[max(24rem,90cqw)]
    • sizes="auto" so the browser selects resolution from the rendered width
  • src/fields/imageSize.ts: new Extra small (xsmall) option
  • src/blocks/InlineMedia/Component.tsx: sizes="auto" for the percentage-width and fixed-height cases
  • Seed: added an extra-small example to media-blocks
  • No JS sizing layer and no migration (the select is stored as plain text, validated in the app layer)

How it behaves

Each size renders as a share of its container, so the same setting is larger in a full-width layout and proportionally smaller in a narrow column. In very narrow columns (e.g. 4-column) the px floor wins and the larger sizes can converge — there isn't room to differentiate. Percentages/floors are tunable via the cqw class literals in Media/Component.tsx.

Notes / caveats

  • sizes="auto" requires loading="lazy", which is the Media component's default. A priority (above-the-fold) image isn't lazy, so it falls back to Next's default sizes.
  • Browser support for sizes="auto" is modern (Chrome/Edge 121+, Firefox, Safari 18+). Older browsers ignore auto and fall back to a larger download — no breakage.
  • Verified locally: MediaBlock and inline media render loading="lazy" sizes="auto" with a full width-descriptor srcset (16w–3840w), no dev warning.

How to test

  1. Add a multi-column Content block with MediaBlocks at xsmall / small / medium / large inside columns.
  2. Confirm the sizes are visually distinct within a column and shrink as the column narrows.
  3. Compare with a single-column / full-width MediaBlock — the same setting renders larger there.
  4. In DevTools, confirm the <img> has sizes="auto" + a w-descriptor srcset, and that the network request scales with the rendered width.
  5. Test inline media in richText; confirm full-width / original still render correctly.

🤖 Generated with Claude Code

Switch MediaBlock from viewport-based breakpoints (md:, lg:) to CSS
container query breakpoints (@sm:, @lg:) so responsive image sizing
adapts to the parent container width instead of the viewport. This
fixes incorrect sizing when MediaBlock is embedded in narrower
containers like the blog post layout (max-w-[48rem]).

Also update InlineMediaBlock sizes attribute to use conservative
container-relative pixel estimates instead of viewport-relative vw
units.

Changes:
- MediaBlock: Add @container to wrapper div, use @sm:/@lg: container
  query classes instead of md:/lg: viewport breakpoints
- MediaBlock: Remove unused cssVariables import
- InlineMediaBlock: Replace vw-based sizes hints with container-aware
  pixel estimates

Closes #757

Co-authored-by: Rachel Fryan <rchlfryn@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

…hint

Switch from fixed breakpoint caps to container-relative `cqw` widths (with a px
floor) so each image size stays visually distinct in any container — full-width,
blog body, or a narrow grid column. Derive the `sizes` attribute from the same
formula and thread column context from Content blocks through RichText, so the
downloaded image matches what's rendered.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Adds an `xsmall` image size (33cqw, 12rem floor) below `small`, addressing the
"additional, smaller size" request in #757. No migration needed — the select is
stored as plain text, validated in the app layer.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Media block should use responsive image sizes based on it's container

1 participant