Skip to content

Add support for block anchor #346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 10, 2025
Merged
Changes from all commits
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
19 changes: 13 additions & 6 deletions src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,18 @@ public function getClasses(): string
);
}

/**
* Retrieve the component attribute bag.
*/
protected function getComponentAttributeBag(): ComponentAttributeBag
{
return (new ComponentAttributeBag)
->class($this->getClasses())
->style($this->getInlineStyle())
->merge(['id' => $this->block->anchor ?? null])
->filter(fn ($value) => filled($value) && $value !== ';');
}

/**
* Retrieve the block API version.
*/
Expand Down Expand Up @@ -778,11 +790,6 @@ public function render($block, $content = '', $preview = false, $post_id = 0, $w
$this->style = $this->getStyle();
$this->inlineStyle = $this->getInlineStyle();

$attributes = (new ComponentAttributeBag)
->class($this->classes)
->style($this->inlineStyle)
->filter(fn ($value) => filled($value) && $value !== ';');

if (! is_admin() && method_exists($this, 'assets')) {
$instance = (array) ($this->block ?? []);

Expand All @@ -793,7 +800,7 @@ public function render($block, $content = '', $preview = false, $post_id = 0, $w

return $this->view($this->view, [
'block' => $this,
'attributes' => $attributes,
'attributes' => $this->getComponentAttributeBag(),
]);
}

Expand Down