Skip to content

[WIP] Block Support: Add anchor support for dynamic blocks #70995

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

Draft
wants to merge 1 commit into
base: update-anchor-support
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
120 changes: 60 additions & 60 deletions docs/reference-guides/core-blocks.md

Large diffs are not rendered by default.

63 changes: 63 additions & 0 deletions lib/block-supports/anchor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* Anchor block support flag.
*
* @package gutenberg
*/

/**
* Registers the anchor block attribute for block types that support it.
*
* @param WP_Block_Type $block_type Block Type.
*/
function gutenberg_register_anchor_support( $block_type ) {
$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );

if ( ! $has_anchor_support ) {
return;
}

if ( ! $block_type->attributes ) {
$block_type->attributes = array();
}

if ( ! array_key_exists( 'anchor', $block_type->attributes ) ) {
$block_type->attributes['anchor'] = array(
'type' => 'string',
);
}
}

/**
* Add the anchor to the output.
*
* @param WP_Block_Type $block_type Block Type.
* @param array $block_attributes Block attributes.
*
* @return array Block anchor.
*/
function gutenberg_apply_anchor_support( $block_type, $block_attributes ) {
if ( ! $block_attributes ) {
return array();
}

$has_anchor_support = block_has_support( $block_type, array( 'anchor' ), false );
if ( ! $has_anchor_support ) {
return array();
}

$has_anchor = array_key_exists( 'anchor', $block_attributes );
if ( ! $has_anchor ) {
return array();
}
return array( 'id' => $block_attributes['anchor'] );
}

// Register the block support.
WP_Block_Supports::get_instance()->register(
'anchor',
array(
'register_attribute' => 'gutenberg_register_anchor_support',
'apply' => 'gutenberg_apply_anchor_support',
)
);
1 change: 1 addition & 0 deletions lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/block-supports/shadow.php';
require __DIR__ . '/block-supports/background.php';
require __DIR__ . '/block-supports/block-style-variations.php';
require __DIR__ . '/block-supports/anchor.php';
require __DIR__ . '/block-supports/aria-label.php';

// Data views.
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/archives/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"supports": {
"align": true,
"anchor": true,
"__experimentalBorder": {
"radius": true,
"color": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/avatar/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"supports": {
"html": false,
"align": true,
"anchor": true,
"alignWide": false,
"spacing": {
"margin": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/calendar/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"supports": {
"align": true,
"anchor": true,
"html": false,
"color": {
"link": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/categories/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"usesContext": [ "enhancedPagination" ],
"supports": {
"align": true,
"anchor": true,
"html": false,
"spacing": {
"margin": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"usesContext": [ "commentId" ],
"supports": {
"anchor": true,
"html": false,
"inserter": false,
"__experimentalBorder": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"usesContext": [ "commentId" ],
"supports": {
"anchor": true,
"html": false,
"spacing": {
"margin": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-content/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
}
},
"supports": {
"anchor": true,
"color": {
"gradients": true,
"link": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"usesContext": [ "commentId" ],
"supports": {
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-edit-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
}
},
"supports": {
"anchor": true,
"html": false,
"color": {
"link": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-reply-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
}
},
"supports": {
"anchor": true,
"color": {
"gradients": true,
"link": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comment-template/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"usesContext": [ "postId" ],
"supports": {
"align": true,
"anchor": true,
"html": false,
"reusable": false,
"spacing": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"usesContext": [ "postId", "comments/paginationArrow" ],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"color": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"textdomain": "default",
"usesContext": [ "postId" ],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"color": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"usesContext": [ "postId", "comments/paginationArrow" ],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"color": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
},
"supports": {
"align": true,
"anchor": true,
"reusable": false,
"html": false,
"color": {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/comments-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
}
},
"supports": {
"anchor": false,
"anchor": true,
"align": true,
"html": false,
"__experimentalBorder": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/comments/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"supports": {
"align": [ "wide", "full" ],
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/footnotes/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"textdomain": "default",
"usesContext": [ "postId", "postType" ],
"supports": {
"anchor": true,
"__experimentalBorder": {
"radius": true,
"color": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/home-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"style"
],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"typography": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/latest-comments/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"supports": {
"align": true,
"anchor": true,
"color": {
"gradients": true,
"link": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/latest-posts/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
},
"supports": {
"align": true,
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/loginout/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"supports": {
"className": true,
"anchor": true,
"color": {
"background": true,
"text": false,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"style"
],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"__experimentalSlashInserter": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation-submenu/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"style"
],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"typography": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
},
"supports": {
"align": [ "wide", "full" ],
"anchor": true,
"ariaLabel": true,
"html": false,
"inserter": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/page-list/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"openSubmenusOnClick"
],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"typography": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"spacing": {
"margin": true,
"padding": true
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-author-name/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"html": false,
"spacing": {
"margin": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-author/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"usesContext": [ "postType", "postId", "queryId" ],
"supports": {
"anchor": true,
"html": false,
"spacing": {
"margin": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-comment/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"commentId": "commentId"
},
"supports": {
"anchor": true,
"html": false,
"inserter": false,
"interactivity": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"usesContext": [ "postId" ],
"supports": {
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-comments-form/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"usesContext": [ "postId", "postType" ],
"supports": {
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-comments-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
}
},
"supports": {
"anchor": true,
"html": false,
"color": {
"link": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-content/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"supports": {
"align": [ "wide", "full" ],
"anchor": true,
"html": false,
"layout": true,
"background": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-date/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-excerpt/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
},
"supports": {
"align": [ "left", "right", "center", "wide", "full" ],
"anchor": true,
"color": {
"text": false,
"background": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"usesContext": [ "postType" ],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"color": {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-template/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"postType"
],
"supports": {
"anchor": true,
"reusable": false,
"html": false,
"align": [ "wide", "full" ],
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/post-terms/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"viewportWidth": 350
},
"supports": {
"anchor": true,
"html": false,
"color": {
"gradients": true,
Expand Down
Loading
Loading