PluginProbe
Gutenberg / 23.6.2
Gutenberg v23.6.2
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / tab-list.php

tab-list.php in Gutenberg 23.6.2, at build/scripts/block-library/tab-list.php

77 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Tab List Block
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Render callback for core/tab-list.
10 *
11 * Injects IAPI directives into the saved button HTML. The buttons already
12 * carry color/border/padding styles from save.js; this callback adds
13 * tab-specific attributes (id, aria-controls, context) and interactivity
14 * directives using data from the tabs-list context.
15 *
16 * @since 7.1.0
17 *
18 * @param array $attributes Block attributes.
19 * @param string $content Block content (rendered buttons from save.js).
20 * @param \WP_Block $block WP_Block instance.
21 *
22 * @return string Updated HTML.
23 */
24 function gutenberg_block_core_tab_list_render_callback( array $attributes, string $content, \WP_Block $block ): string {
25 $tabs_list = $block->context['core/tabs-list'] ?? array();
26 $aria_label = empty( $attributes['ariaLabel'] ) ? __( 'Tabbed content' ) : wp_strip_all_tags( $attributes['ariaLabel'] );
27
28 $tag_processor = new WP_HTML_Tag_Processor( $content );
29 if ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-tab-list' ) ) ) {
30 $tag_processor->set_attribute( 'aria-label', $aria_label );
31 }
32
33 if ( empty( $tabs_list ) ) {
34 return $tag_processor->get_updated_html();
35 }
36
37 $tab_index = 0;
38
39 while ( $tag_processor->next_tag( 'button' ) ) {
40 $tab_id = $tabs_list[ $tab_index ] ?? null;
41
42 if ( null === $tab_id ) {
43 break;
44 }
45
46 $tag_processor->set_attribute( 'id', 'tab__' . $tab_id );
47 $tag_processor->set_attribute( 'aria-controls', $tab_id );
48 $tag_processor->set_attribute( 'data-wp-on--click', 'actions.handleTabClick' );
49 $tag_processor->set_attribute( 'data-wp-on--keydown', 'actions.handleTabKeyDown' );
50 $tag_processor->set_attribute( 'data-wp-bind--aria-selected', 'state.isActiveTab' );
51 $tag_processor->set_attribute( 'data-wp-bind--tabindex', 'state.tabIndexAttribute' );
52 $tag_processor->set_attribute(
53 'data-wp-context',
54 wp_json_encode( array( 'tabIndex' => $tab_index ) )
55 );
56
57 ++$tab_index;
58 }
59
60 return $tag_processor->get_updated_html();
61 }
62
63 /**
64 * Registers the `core/tab-list` block on the server.
65 *
66 * @since 7.1.0
67 */
68 function gutenberg_register_block_core_tab_list() {
69 register_block_type_from_metadata(
70 __DIR__ . '/tab-list',
71 array(
72 'render_callback' => 'gutenberg_block_core_tab_list_render_callback',
73 )
74 );
75 }
76 add_action( 'init', 'gutenberg_register_block_core_tab_list', 20 );
77