Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Post Template grid container styles (#592).
*
* Upstream `core/post-template` uses a flex-with-percent-widths layout
* for its grid view via `is-flex-container` + `columns-N`. The
* ArtisanPack fork renders the same block as a real CSS grid via
* `is-layout-grid` + `columns-N`, both in the editor canvas and the
* Blade / Vue / React renderers, so per-post column / row spans on
* post-variants (#592) actually have a grid to span across.
*
* `.is-layout-grid` already picks up `display: grid` from the layout
* baseline emitted by <x-ve-blocks-styles />. These rules add the
* track definition and item reset that the baseline doesn't supply.
*/

.wp-block-post-template.is-layout-grid {
grid-template-columns: repeat(1, minmax(0, 1fr));
gap: var(--wp--style--block-gap, 1.25em);
list-style: none;
padding: 0;
}

.wp-block-post-template.is-layout-grid.columns-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.wp-block-post-template.is-layout-grid.columns-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.wp-block-post-template.is-layout-grid.columns-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.wp-block-post-template.is-layout-grid.columns-5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.wp-block-post-template.is-layout-grid.columns-6 { grid-template-columns: repeat(6, minmax(0, 1fr)); }

.wp-block-post-template.is-layout-grid > .wp-block-post-template-item {
margin: 0;
width: auto;
min-width: 0;
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
@php
use ArtisanPackUI\VisualEditor\Responsive\BreakpointRegistry;

$postId = isset( $attributes['postId'] ) ? (int) $attributes['postId'] : 0;
$className = isset( $attributes['className'] ) && is_string( $attributes['className'] )
? trim( $attributes['className'] )
: '';

$classes = array_filter( [ 'wp-block-post-template-item', $className ] );
$spanClasses = [];
$resolvedSpan = $attributes['_resolvedGridSpan'] ?? null;

if ( is_array( $resolvedSpan ) ) {
$breakpointRegistry = app( BreakpointRegistry::class );

$emitSpan = static function ( $overrides, string $suffix ) use ( $breakpointRegistry ): array {
$out = [];

if ( ! is_array( $overrides ) ) {
return $out;
}

foreach ( $overrides as $bp => $value ) {
if ( ! is_string( $bp ) || '' === $bp ) {
continue;
}

if ( ! is_numeric( $value ) ) {
continue;
}

if ( BreakpointRegistry::BASE_KEY !== $bp && null === $breakpointRegistry->get( $bp ) ) {
continue;
}

$amount = (int) $value;

if ( $amount < 1 ) {
$amount = 1;
} elseif ( $amount > 12 ) {
$amount = 12;
}

$out[] = 'ap-post-span-' . $amount . '-' . $bp . '-' . $suffix;
}

return $out;
};

$spanClasses = array_merge(
$emitSpan( $resolvedSpan['columns'] ?? null, 'columns' ),
$emitSpan( $resolvedSpan['rows'] ?? null, 'row' ),
);
}

$classes = array_filter( array_merge( [ 'wp-block-post-template-item', $className ], $spanClasses ) );
$classAttr = sprintf( 'class="%s"', e( implode( ' ', $classes ) ) );
$idAttr = $postId > 0 ? sprintf( ' id="post-%d"', $postId ) : '';
@endphp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@
through `PhotoGridSupport::wrapperForBlock` + the responsive CSS
accumulator. --}}
<link rel="stylesheet" href="{{ $photoGridStyleHref }}" data-ve-photo-grid>
{{-- #592 — Post Template grid container. Sets `grid-template-columns`
on `.wp-block-post-template.is-layout-grid.columns-N` so the
post-template's `is-layout-grid` wrapper renders as an actual
N-column CSS grid (the layout baseline only supplies `display: grid`). --}}
<link rel="stylesheet" href="{{ $postTemplateStyleHref }}" data-ve-post-template>
{{-- #592 — Post Variant grid spans. Adds `ap-post-span-N-{bp}-{columns,row}`
rules scoped to `.wp-block-post-template.is-layout-grid > .wp-block-post-template-item`
so a variant's per-breakpoint `gridColumnSpan` / `gridRowSpan` only takes
effect inside a grid-layout Query Loop. --}}
<link rel="stylesheet" href="{{ $postVariantStyleHref }}" data-ve-post-variant>
@if( '' !== $themeTokensCss )
<style data-ve-theme-tokens>{!! $themeTokensCss !!}</style>
@endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ class BlocksStylesComponent extends Component

public string $photoGridStyleHref;

public string $postTemplateStyleHref;

public string $postVariantStyleHref;

public string $interactivityScriptSrc;

public bool $emitBlockLibrary;
Expand Down Expand Up @@ -114,6 +118,8 @@ public function __construct(
$this->queryPaginationStyleHref = $base . '/frontend/query-pagination.css';
$this->flexLayoutStyleHref = $base . '/frontend/flex-layout.css';
$this->photoGridStyleHref = $base . '/frontend/photo-grid.css';
$this->postTemplateStyleHref = $base . '/frontend/post-template.css';
$this->postVariantStyleHref = $base . '/frontend/post-variant.css';
$this->interactivityScriptSrc = $base . '/frontend/interactivity.js';
$this->emitBlockLibrary = $bundle;
$this->emitInteractive = $interactive;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

declare( strict_types=1 );

use ArtisanPackUI\VisualEditor\Services\QueryResolverContract;
use ArtisanPackUI\VisualEditorRendererBlade\View\Components\BlocksComponent;
use Tests\Fixtures\FakeQueryResolver;

/**
* Issue #592 — variable column / row spans on post-variants in a
* grid-layout Query Loop.
*
* End-to-end render coverage: the QueryInliner stamps
* `_resolvedGridSpan` on the synthetic `core/post-template-item`
* wrapper, and this Blade partial translates that into a flat
* `ap-post-span-N-{bp}-{columns,row}` class list scoped via the CSS
* bundle to grid-layout post-templates only.
*/

beforeEach( function (): void {
$this->fake = new FakeQueryResolver();
$this->app->instance( QueryResolverContract::class, $this->fake );
} );

function spanRenderablePost( int $id, string $title, array $extras = [] ): object
{
$post = new stdClass();
$post->id = $id;
$post->title = $title;
$post->excerpt = '';
$post->content = "<p>{$title}</p>";
$post->permalink = "/posts/{$id}";
$post->author = null;
$post->published_at = null;
$post->updated_at = null;
$post->featured_image_id = null;

foreach ( $extras as $key => $value ) {
$post->{$key} = $value;
}

return $post;
}

function gridTreeWithVariantSpan(
int $columnSpan,
int $rowSpan,
?array $responsive = null,
string $layout = 'grid'
): array {
$variantAttributes = [
'matcher' => [ 'kind' => 'position', 'value' => 'first' ],
'priority' => 10,
'gridColumnSpan' => $columnSpan,
'gridRowSpan' => $rowSpan,
];

if ( null !== $responsive ) {
$variantAttributes['responsive'] = $responsive;
}

return [
[
'name' => 'core/query',
'attributes' => [ 'query' => [ 'postType' => 'post', 'perPage' => 2 ] ],
'innerBlocks' => [
[
'name' => 'core/post-template',
'attributes' => [ 'layout' => $layout ],
'innerBlocks' => [
[
'name' => 'core/post-title',
'attributes' => [],
'innerBlocks' => [],
],
[
'name' => 'artisanpack/post-variant',
'attributes' => $variantAttributes,
'innerBlocks' => [
[
'name' => 'core/post-excerpt',
'attributes' => [],
'innerBlocks' => [],
],
],
],
],
],
],
],
];
}

it( 'emits ap-post-span classes on the first iteration <li> when the variant matched in a grid layout', function () {
$this->fake->setItems( [
spanRenderablePost( 1, 'Hero' ),
spanRenderablePost( 2, 'Listed' ),
] );

$tree = gridTreeWithVariantSpan( 2, 2 );
$component = $this->app->make( BlocksComponent::class, [ 'tree' => $tree ] );
$html = $component->render()->with( $component->data() )->render();

expect( $html )->toContain( 'ap-post-span-2-base-columns' )
->and( $html )->toContain( 'ap-post-span-2-base-row' )
// Non-matched item should not pick up span classes from the
// matched item.
->and( substr_count( $html, 'ap-post-span-2-base-columns' ) )->toBe( 1 );
} );

it( 'does not emit ap-post-span classes when the post-template layout is not grid', function () {
$this->fake->setItems( [
spanRenderablePost( 1, 'Hero' ),
] );

$tree = gridTreeWithVariantSpan( 2, 2, null, 'list' );
$component = $this->app->make( BlocksComponent::class, [ 'tree' => $tree ] );
$html = $component->render()->with( $component->data() )->render();

expect( $html )->not->toContain( 'ap-post-span-' );
} );

it( 'emits per-breakpoint span classes when the variant carries responsive overrides', function () {
$this->fake->setItems( [
spanRenderablePost( 1, 'Hero' ),
] );

$tree = gridTreeWithVariantSpan( 2, 1, [
'gridColumnSpan' => [ 'md' => 3, 'lg' => 4 ],
'gridRowSpan' => [ 'md' => 2 ],
] );

$component = $this->app->make( BlocksComponent::class, [ 'tree' => $tree ] );
$html = $component->render()->with( $component->data() )->render();

expect( $html )->toContain( 'ap-post-span-2-base-columns' )
->and( $html )->toContain( 'ap-post-span-3-md-columns' )
->and( $html )->toContain( 'ap-post-span-4-lg-columns' )
->and( $html )->toContain( 'ap-post-span-1-base-row' )
->and( $html )->toContain( 'ap-post-span-2-md-row' );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

import type { JSX, ReactNode } from 'react';
import { attrString, classList } from '../../support/attributes';
import { attrString, classList, postTemplateItemSpanClasses } from '../../support/attributes';
import type { BlockRendererProps } from '../../types';

interface WrapperProps {
Expand Down Expand Up @@ -93,8 +93,9 @@ export function PostTemplateItemBlock({ attributes, children }: BlockRendererPro
const parsedPostId = typeof rawPostId === 'number' ? rawPostId : Number(rawPostId);
const postId = Number.isFinite(parsedPostId) ? parsedPostId : 0;
const className = attrString(attributes.className);
const spanClasses = postTemplateItemSpanClasses(attributes._resolvedGridSpan);

const classes = classList(['wp-block-post-template-item', className]);
const classes = classList(['wp-block-post-template-item', className, ...spanClasses]);

return <li id={postId > 0 ? `post-${postId}` : undefined} className={classes}>{children}</li>;
}
101 changes: 101 additions & 0 deletions packages/visual-editor-renderer-react/src/support/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,104 @@ export function formatPercent(value: number): string {

return (trimmed === '' ? '0' : trimmed) + '%';
}

/**
* Tailwind v4 default breakpoint keys recognised by the post-variant
* span emitter (#592). Anything outside this list is dropped silently
* so a malformed save can't produce CSS classes the stylesheet has no
* matching rule for.
*/
const POST_TEMPLATE_ITEM_SPAN_BREAKPOINTS = new Set<string>([
'base',
'sm',
'md',
'lg',
'xl',
'2xl',
]);

const POST_TEMPLATE_ITEM_SPAN_MIN = 1;
const POST_TEMPLATE_ITEM_SPAN_MAX = 12;

function clampSpan(value: unknown): number | null {
if (typeof value === 'number' && Number.isFinite(value)) {
const trimmed = Math.trunc(value);

if (trimmed < POST_TEMPLATE_ITEM_SPAN_MIN) {
return POST_TEMPLATE_ITEM_SPAN_MIN;
}

if (trimmed > POST_TEMPLATE_ITEM_SPAN_MAX) {
return POST_TEMPLATE_ITEM_SPAN_MAX;
}

return trimmed;
}

if (typeof value === 'string' && value.trim() !== '') {
const parsed = Number.parseInt(value, 10);

if (Number.isFinite(parsed)) {
return clampSpan(parsed);
}
}

return null;
}

/**
* Build the `ap-post-span-N-{bp}-{columns,row}` class list for a
* post-template-item from the `_resolvedGridSpan` attribute the
* server-side `QueryInliner` (PHP) or its client-side mirror stamps
* onto each iteration wrapper when (a) a variant matched and (b) the
* post-template's layout is grid.
*
* The shape mirrors the PHP `resolveVariantSpans()` output:
*
* { columns: { base: 2, md: 3 }, rows: { base: 1 } }
*
* Unknown breakpoint keys are dropped so the public CSS bundle stays
* the only source of truth for the rule set. Returns an empty array
* when the attribute is absent or empty so the caller can spread it
* unconditionally.
*/
export function postTemplateItemSpanClasses(value: unknown): string[] {
if (value === null || value === undefined || typeof value !== 'object') {
return [];
}

const record = value as Record<string, unknown>;

return [
...buildSpanClasses(record.columns, 'columns'),
...buildSpanClasses(record.rows, 'row'),
];
}

function buildSpanClasses(overrides: unknown, suffix: 'columns' | 'row'): string[] {
if (overrides === null || overrides === undefined || typeof overrides !== 'object') {
return [];
}

if (Array.isArray(overrides)) {
return [];
}

const out: string[] = [];

for (const [bp, raw] of Object.entries(overrides as Record<string, unknown>)) {
if (!POST_TEMPLATE_ITEM_SPAN_BREAKPOINTS.has(bp)) {
continue;
}

const span = clampSpan(raw);

if (span === null) {
continue;
}

out.push(`ap-post-span-${span}-${bp}-${suffix}`);
}

return out;
}
Loading
Loading