WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.3k stars 4.11k forks source link

Explore how to re-add anchor support to dynamic blocks #51402

Open t-hamano opened 1 year ago

t-hamano commented 1 year ago

In #44771, anchor support was added to dynamic blocks, but static blocks with existing anchor were broken (See #48232, #49382, and #49578).

This feature was originally planned to be included in WordPress 6.2, but #48232 was the first to report this issue during the Beta phase. Therefore, by #48592, this feature was excluded in the WordPress 6.2 release.

However, this feature still remains on the Gutenberg plugin, which will be bundled with WordPress 6.3 and will cause similar problems. A solution was attempted in #48438, but we reverted in #51288 anchor support for dynamic blocks in the Gutenberg plugin as well, in order to find a more ideal solution.

One approach I have explored is described in this comment:

  • The allowed values for supports.anchor are false, true, "html", or "delimiter". The block.json schema and document have also been updated.
  • If the value is true or "html", the attribute is taken from the markup of the root element.
  • If the value is delimiter, the attribute is taken from the comment delimiter.
  • Based on these specifications, in dynamic blocks where anchor support was added in #44771, the value of the anchor property was changed to "delimiter".
  • With the increased selection of possible values for anchor, several tests have been added to the unit test for addAttribute().
stefanfisk commented 4 months ago

Is there any sort of workaround available for this? I build all of my sites with 100% dynamic blocks, and I've just found that I have several customers whose site's are now somewhat broken :/

Previously I just hade to use supports.anchor = true and an "anchor": {"type":"string"} attribute for it to work.

t-hamano commented 4 months ago

Hi @stefanfisk,

Anchor support for dynamic blocks should not currently be available in both WordPress core and the Gutenberg repository, as an ideal approach has not yet been found.

At this time, anchor support is probably not available unless you utilize some kind of your own custom hook.

stefanfisk commented 4 months ago

@t-hamano I understand that anchor support has been removed for dynamic blocks due to bugs.

What I am asking is if there is anything I can in the scope of "utilize some kind of your own custom hook" to restore the functionality.

My current situation is that I have a handful of custom sites that have partially broken navigation because this functionality was removed.

I'm fine with custom workarounds while you sort out a permanent solution. I just need some sort of pointer on how to implement it.

t-hamano commented 4 months ago

If you want to add anchor functionality to a dynamic block that you have created yourself, one approach is as follows.

I build all of my sites with 100% dynamic blocks, and I've just found that I have several customers whose site's are now somewhat broken :/

What errors are you seeing in the browser console? Also, does it have something to do with the removal of anchor support for dynamic blocks from the Gutenberg plugin?

stefanfisk commented 4 months ago

Nevermind!

My issue was with sloppy debugging and unfounded assumptions after reading that anchor support had been removed for dynamic blocks.

AFAICT combining supports.anchor = true and "anchor": {"type":"string"} still works great with my setup.