Skip to content

Commit b305d4e

Browse files
committed
✨ Add view attribute bag support to blocks
🩹 Fix typography attribute compatibility (Fixes #316) 💄 Update the block view stub to use the attribute bag
1 parent 442b21b commit b305d4e

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

src/Block.php

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Arr;
77
use Illuminate\Support\Collection;
88
use Illuminate\Support\Str;
9+
use Illuminate\View\ComponentAttributeBag;
910
use Log1x\AcfComposer\Concerns\InteractsWithBlade;
1011
use Log1x\AcfComposer\Contracts\Block as BlockContract;
1112
use WP_Block_Supports;
@@ -374,6 +375,30 @@ public function getStyles(): array
374375
return $styles->all();
375376
}
376377

378+
/**
379+
* Retrieve the block supports.
380+
*/
381+
public function getSupports(): array
382+
{
383+
$supports = $this->collect($this->supports)
384+
->mapWithKeys(fn ($value, $key) => [Str::camel($key) => $value])
385+
->merge($this->supports);
386+
387+
$typography = $supports->get('typography', []);
388+
389+
if ($supports->has('alignText')) {
390+
$typography['textAlign'] = $supports->get('alignText');
391+
392+
$supports->forget(['alignText', 'align_text']);
393+
}
394+
395+
if ($typography) {
396+
$supports->put('typography', $typography);
397+
}
398+
399+
return $supports->all();
400+
}
401+
377402
/**
378403
* Retrieve the block support attributes.
379404
*/
@@ -388,28 +413,29 @@ public function getSupportAttributes(): array
388413
];
389414
}
390415

391-
if ($this->align_text) {
392-
$attributes['alignText'] = [
393-
'type' => 'string',
394-
'default' => $this->align_text,
395-
];
396-
}
397-
398416
if ($this->align_content) {
399417
$attributes['alignContent'] = [
400418
'type' => 'string',
401419
'default' => $this->align_content,
402420
];
403421
}
404422

423+
$styles = [];
424+
425+
if ($this->align_text) {
426+
$styles['typography']['textAlign'] = $this->align_text;
427+
}
428+
405429
$spacing = array_filter($this->spacing);
406430

407431
if ($spacing) {
432+
$styles['spacing'] = $spacing;
433+
}
434+
435+
if ($styles) {
408436
$attributes['style'] = [
409437
'type' => 'object',
410-
'default' => [
411-
'spacing' => $spacing,
412-
],
438+
'default' => $styles,
413439
];
414440
}
415441

@@ -579,13 +605,6 @@ public function settings(): Collection
579605
return $this->settings;
580606
}
581607

582-
if ($this->supports) {
583-
$this->supports = $this->collect($this->supports)
584-
->mapWithKeys(fn ($value, $key) => [Str::camel($key) => $value])
585-
->merge($this->supports)
586-
->all();
587-
}
588-
589608
$settings = Collection::make([
590609
'name' => $this->slug,
591610
'title' => $this->getName(),
@@ -600,7 +619,7 @@ public function settings(): Collection
600619
'alignText' => $this->align_text ?? $this->align,
601620
'alignContent' => $this->align_content,
602621
'styles' => $this->getStyles(),
603-
'supports' => $this->supports,
622+
'supports' => $this->getSupports(),
604623
'textdomain' => $this->getTextDomain(),
605624
'acf_block_version' => $this->blockVersion,
606625
'api_version' => 2,
@@ -726,7 +745,15 @@ public function render($block, $content = '', $preview = false, $post_id = 0, $w
726745
$this->style = $this->getStyle();
727746
$this->inlineStyle = $this->getInlineStyle();
728747

729-
return $this->view($this->view, ['block' => $this]);
748+
$attributes = (new ComponentAttributeBag)
749+
->class($this->classes)
750+
->style($this->inlineStyle)
751+
->filter(fn ($value) => filled($value) && $value !== ';');
752+
753+
return $this->view($this->view, [
754+
'block' => $this,
755+
'attributes' => $attributes,
756+
]);
730757
}
731758

732759
/**

src/Console/stubs/views/block.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<div class="{{ $block->classes }}" style="{{ $block->inlineStyle }}">
1+
<div {{ $attributes }}>
22
@if ($items)
33
<ul>
44
@foreach ($items as $item)

0 commit comments

Comments
 (0)