-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New Block: core/tabs
#69789
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
New Block: core/tabs
#69789
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
fbfa318
Initial commit
sethrubenstein d26e8b7
Moving back to a manually constructed list in the parent block
sethrubenstein 6c020e0
Moving more toward iAPI for easier use
sethrubenstein d60a969
Moving more toward iAPI for easier use
sethrubenstein 2f30c9d
Some final cleanup before submitting pr
sethrubenstein 47c2063
Fixes php linting errors and forgot to hook on style functions for co…
sethrubenstein a9df24d
Adding fixtures/tests updating block library version number
sethrubenstein 2836077
Why is PHPCS reporting this as misformatted??
sethrubenstein cf93391
Whoops mis-replaced this file
sethrubenstein fed61c1
iAPI bug fix, merge artifact - ensuring dynamic styles generation wor…
sethrubenstein fa90dbe
iAPI bug fix, merge artifact - ensuring dynamic styles generation wor…
sethrubenstein 329f3a2
Fixing failed tabs fixtures and removing old readme files from prc-bl…
sethrubenstein 2ebfd35
PHPCS linting
sethrubenstein 863f737
I was really overthinking that, untill I was up late overthinking it.
sethrubenstein 88df184
Accessibility improvements and cleanup
sethrubenstein 8b21aee
Regen tabs fixtures
sethrubenstein 759836b
Cool, that slotfill key scoping solution from @Mamaduka works
sethrubenstein 9045eec
Moving tabs icons into icon library proper as requested by @Infinite-…
sethrubenstein 3d69321
Some updates for better keyboard handler support
sethrubenstein 598981a
#34079 Fixing accesibility on frontend tab render
sethrubenstein b38e279
Refactor tabs, pulling the latest out of prc-block-library/tabs for G…
sethrubenstein ef1eaaf
Update included context to match core block library pattern
sethrubenstein 6832705
Making tabs & tab blocks experimental
sethrubenstein cee89d3
Merge branch 'trunk' into core/tabs
sethrubenstein 9816923
Fix tabs experimental placement
sethrubenstein 9684714
Pull custom event. Per work on core/dialog
sethrubenstein b60598b
Resolving comments in review
sethrubenstein 779349c
Moving to requestAnimationFrame and cancelAnimationFrame for cleaning…
sethrubenstein b08e10b
alphabetical order
sethrubenstein 4e45b8c
alphabetical order
sethrubenstein be1830a
Merge branch 'trunk' into core/tabs
sethrubenstein 1402c06
Refactor color styles function to remove unnecessary parameter
sethrubenstein 0017090
Merge branch 'core/tabs' of github.com:pewresearch/gutenberg into cor…
sethrubenstein 9e734b9
List order
sethrubenstein d166b86
list order
sethrubenstein 98a3c55
Order
sethrubenstein 040fb5e
Order
sethrubenstein 0e596b5
Updates:
sethrubenstein bbf0700
Updates
sethrubenstein ea2322e
Updated to private store method that we implemented in core/dialog
sethrubenstein 907bf94
Fix initial mount of tabs and focusing on first tab richtext
sethrubenstein 62eb812
Cleanup
sethrubenstein 0510796
Cleanup
sethrubenstein 76c06bb
Remove tests for now
sethrubenstein 79e09d9
Cleanup
sethrubenstein 684d6bd
Cleanup for coding standards
sethrubenstein 1406cd3
Move away from slotfill, can restore if desired.
sethrubenstein 8b36b2f
Replaced dynamic tab label with sprintf, replaced hard coded formats …
sethrubenstein 3e8e4f5
Make markup render correctly when not focused in tabslist
sethrubenstein c4973ad
Add StaticLabel component to TabsList for improved tab label rendering
sethrubenstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Why is PHPCS reporting this as misformatted??
- Loading branch information
commit 2836077505ba43180b642f6aa3ef6c2526804f3a
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,123 +1,222 @@ | ||
| <?php | ||
| /** | ||
| * Returns typography classnames depending on whether there are named font sizes/families. | ||
| * Server-side rendering of the `core/tabs` block. | ||
| * | ||
| * @package WordPress | ||
| */ | ||
|
|
||
| /** | ||
| * Constructs a string of CSS color variables for the tabs block. | ||
| * - customTabBackgroundColor - The background color of the tabs. | ||
| * - customTabHoverColor - The hover background color of the tabs. | ||
| * - customTabActiveColor - The active background color of the tabs. | ||
| * - customTabTextColor - The text color of the tabs. | ||
| * - customTabHoverTextColor - The hover text color of the tabs. | ||
| * - customTabActiveTextColor - The active text color of the tabs. | ||
| * | ||
| * @since 9.22.0 | ||
| * | ||
| * @param array $attributes The block attributes. | ||
| * @return string The typography color classnames to be applied to the block elements. | ||
| * @param array $attributes Block attributes. | ||
| * @return string A string of CSS variables. | ||
| */ | ||
| function block_core_tab_get_typography_classes( $attributes ) { | ||
| $typography_classes = array(); | ||
| $has_named_font_family = ! empty( $attributes['fontFamily'] ); | ||
| $has_named_font_size = ! empty( $attributes['fontSize'] ); | ||
|
|
||
| if ( $has_named_font_size ) { | ||
| $typography_classes[] = sprintf( 'has-%s-font-size', esc_attr( $attributes['fontSize'] ) ); | ||
| } | ||
| function block_core_tabs_generate_color_variables( $attributes ) { | ||
| $tab_inactive = array_key_exists( 'customTabInactiveColor', $attributes ) ? $attributes['customTabInactiveColor'] : ''; | ||
| $tab_hover = array_key_exists( 'customTabHoverColor', $attributes ) ? $attributes['customTabHoverColor'] : ''; | ||
| $tab_active = array_key_exists( 'customTabActiveColor', $attributes ) ? $attributes['customTabActiveColor'] : ''; | ||
| $tab_text = array_key_exists( 'customTabTextColor', $attributes ) ? $attributes['customTabTextColor'] : ''; | ||
| $hover_text = array_key_exists( 'customTabHoverTextColor', $attributes ) ? $attributes['customTabHoverTextColor'] : ''; | ||
| $active_text = array_key_exists( 'customTabActiveTextColor', $attributes ) ? $attributes['customTabActiveTextColor'] : ''; | ||
|
|
||
| $styles = array( | ||
| '--custom-tab-inactive-color' => $tab_inactive, | ||
| '--custom-tab-hover-color' => $tab_hover, | ||
| '--custom-tab-active-color' => $tab_active, | ||
| '--custom-tab-text-color' => $tab_text, | ||
| '--custom-tab-hover-text-color' => $hover_text, | ||
| '--custom-tab-active-text-color' => $active_text, | ||
| ); | ||
|
|
||
| if ( $has_named_font_family ) { | ||
| $typography_classes[] = sprintf( 'has-%s-font-family', esc_attr( $attributes['fontFamily'] ) ); | ||
| } | ||
| $style_string = array_map( | ||
| function( $key, $value ) { | ||
| return ! empty( $value ) ? $key . ': ' . $value . ';' : ''; | ||
| }, | ||
| array_keys( $styles ), | ||
| $styles | ||
| ); | ||
| $style_string = implode( ' ', array_filter( $style_string ) ); | ||
|
|
||
| return implode( ' ', $typography_classes ); | ||
| return $style_string; | ||
| } | ||
|
|
||
| /** | ||
| * Returns typography styles to be included in an HTML style tag. | ||
| * This excludes text-decoration, which is applied only to the label and button elements of the search block. | ||
| * Generates a string of CSS block gap variables for the tabs block. | ||
| * | ||
| * @since 9.22.0 | ||
| * | ||
| * @param array $attributes The block attributes. | ||
| * @return string A string of typography CSS declarations. | ||
| * @param array $attributes Block attributes. | ||
| * @return string A string of CSS variables. | ||
| */ | ||
| function block_core_tab_get_typography_styles( $attributes ) { | ||
| $typography_styles = array(); | ||
|
|
||
| // Add typography styles. | ||
| if ( ! empty( $attributes['style']['typography']['fontSize'] ) ) { | ||
| $typography_styles[] = sprintf( | ||
| 'font-size: %s;', | ||
| wp_get_typography_font_size_value( | ||
| array( | ||
| 'size' => $attributes['style']['typography']['fontSize'], | ||
| ) | ||
| ) | ||
| ); | ||
| function block_core_tabs_generate_gap_styles( $attributes ) { | ||
| if ( ! array_key_exists( 'style', $attributes ) || ! is_array( $attributes['style'] ) ) { | ||
| return '--wp--style--tabs-gap-default: 0.5em;'; | ||
| } | ||
|
|
||
| if ( ! empty( $attributes['style']['typography']['fontFamily'] ) ) { | ||
| $typography_styles[] = sprintf( 'font-family: %s;', $attributes['style']['typography']['fontFamily'] ); | ||
| // Check that 'spacing' exists and it's an array. | ||
| if ( ! array_key_exists( 'spacing', $attributes['style'] ) || ! is_array( $attributes['style']['spacing'] ) ) { | ||
| return '--wp--style--tabs-gap-default: 0.5em;'; | ||
| } | ||
|
|
||
| if ( ! empty( $attributes['style']['typography']['letterSpacing'] ) ) { | ||
| $typography_styles[] = sprintf( 'letter-spacing: %s;', $attributes['style']['typography']['letterSpacing'] ); | ||
| if ( ! array_key_exists( 'blockGap', $attributes['style']['spacing'] ) ) { | ||
| return '--wp--style--tabs-gap-default: 0.5em;'; | ||
| } | ||
|
|
||
| if ( ! empty( $attributes['style']['typography']['fontWeight'] ) ) { | ||
| $typography_styles[] = sprintf( 'font-weight: %s;', $attributes['style']['typography']['fontWeight'] ); | ||
| } | ||
| $orientation = array_key_exists( 'orientation', $attributes ) ? $attributes['orientation'] : 'horizontal'; | ||
|
|
||
| if ( ! empty( $attributes['style']['typography']['fontStyle'] ) ) { | ||
| $typography_styles[] = sprintf( 'font-style: %s;', $attributes['style']['typography']['fontStyle'] ); | ||
| } | ||
| $block_gap = $attributes['style']['spacing']['blockGap']; | ||
|
|
||
| if ( ! empty( $attributes['style']['typography']['lineHeight'] ) ) { | ||
| $typography_styles[] = sprintf( 'line-height: %s;', $attributes['style']['typography']['lineHeight'] ); | ||
| // Check if block_gap is an array and has both left and top values, if not, return the default value. | ||
| if ( is_array( $block_gap ) && array_key_exists( 'left', $block_gap ) && array_key_exists( 'top', $block_gap ) ) { | ||
| $block_gap_horizontal = $block_gap['left']; | ||
| $block_gap_vertical = $block_gap['top']; | ||
| } else { | ||
| return '--wp--style--tabs-gap-default: 0.5em;'; | ||
| } | ||
|
|
||
| if ( ! empty( $attributes['style']['typography']['textTransform'] ) ) { | ||
| $typography_styles[] = sprintf( 'text-transform: %s;', $attributes['style']['typography']['textTransform'] ); | ||
| if ( 'vertical' === $orientation ) { | ||
| $block_gap_horizontal = $block_gap['top']; | ||
| $block_gap_vertical = $block_gap['left']; | ||
| } | ||
|
|
||
| return implode( '', $typography_styles ); | ||
| $block_gap_horizontal = preg_match( '/^var:preset\|spacing\|\d+$/', $block_gap_horizontal ) | ||
| ? 'var(--wp--preset--spacing--' . substr( $block_gap_horizontal, strrpos( $block_gap_horizontal, '|' ) + 1 ) . ')' | ||
| : $block_gap_horizontal; | ||
| $block_gap_vertical = preg_match( '/^var:preset\|spacing\|\d+$/', $block_gap_vertical ) | ||
| ? 'var(--wp--preset--spacing--' . substr( $block_gap_vertical, strrpos( $block_gap_vertical, '|' ) + 1 ) . ')' | ||
| : $block_gap_vertical; | ||
|
|
||
| return wp_sprintf( | ||
| '--wp--style--unstable-tabs-list-gap: %s;--wp--style--unstable-tabs-gap: %s;', | ||
| $block_gap_horizontal, | ||
| $block_gap_vertical | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Render the core/tab block. | ||
| * This function adds Interactivity API directives to the tabpanel. | ||
| * Generates a usable list of tab attributes from the innerblocks of core/tabs. | ||
| * | ||
| * @since 9.22.0 | ||
| * | ||
| * @param array $attributes Block attributes. | ||
| * @param string $content Block content. | ||
| * @return string | ||
| * @param array $innerblocks The innerblocks of the tabs block. | ||
| * @return array The list of tabs. | ||
| */ | ||
| function render_block_core_tab( $attributes, $content ) { | ||
| $tag_processor = new WP_HTML_Tag_Processor( $content ); | ||
| $tag_processor->next_tag( array( 'class_name' => 'wp-block-tab' ) ); | ||
| function block_core_tabs_generate_tabs_list_from_innerblocks( $innerblocks = array() ) { | ||
| $tab_index = 0; | ||
| $tabs_list = array_map( | ||
| function( $tab ) use ( &$tab_index ) { | ||
| $attrs = $tab['attrs']; | ||
|
|
||
| $tag_processor = new WP_HTML_Tag_Processor( $tab['innerHTML'] ); | ||
| $tag_processor->next_tag( array( 'class_name' => 'wp-block-tab' ) ); | ||
|
|
||
| $tab_id = $tag_processor->get_attribute( 'id' ); | ||
| $tab_id = $tag_processor->get_attribute( 'id' ); | ||
| $tab_label = array_key_exists( 'label', $attrs ) ? $attrs['label'] : ''; | ||
|
|
||
| $tag_processor->set_attribute( 'role', 'tabpanel' ); | ||
| $tag_processor->set_attribute( 'aria-labelledby', $tab_id ); | ||
| $tag_processor->set_attribute( 'data-wp-tab-id', $tab_id ); | ||
| $tag_processor->set_attribute( 'data-wp-bind--hidden', '!state.isActiveTab' ); | ||
| $tag_processor->set_attribute( 'data-wp-bind--tabindex', 'state.tabIndexAttribute' ); | ||
| $attrs['id'] = $tab_id; | ||
| $attrs['label'] = esc_html( $tab_label ); | ||
| $attrs['index'] = $tab_index; | ||
|
|
||
| $tab_index++; | ||
|
|
||
| return $attrs; | ||
| }, | ||
| $innerblocks | ||
| ); | ||
| return $tabs_list; | ||
| } | ||
|
|
||
| /** | ||
| * Render the block | ||
| * | ||
| * @since 9.22.0 | ||
| * | ||
| * @param array $attributes Block attributes. | ||
| * @param string $content Block content. | ||
| * @param WP_Block $block WP_Block object. | ||
| * @return string | ||
| */ | ||
| function render_block_core_tabs( $attributes, $content, $block ) { | ||
| wp_enqueue_script_module( '@wordpress/block-library/tabs/view' ); | ||
| // Get the starting active tab index. | ||
| $active_tab_index = $attributes['activeTabIndex']; | ||
|
|
||
| // Construct an array of the innerblocks as tabs_list. | ||
| // We use innerblocks instead of parsing each .wp-block-tab because it scopes | ||
| // the tab_index to just this instance of the tabs block. This allows | ||
| // inner tabs to have unique tabs_list indexes, even if they're nested. | ||
| $tabs_list = block_core_tabs_generate_tabs_list_from_innerblocks( $block->parsed_block['innerBlocks'] ); | ||
|
|
||
| // Generate the color styles and gap styles. | ||
| $color_styles = block_core_tabs_generate_color_variables( $attributes ); | ||
| $gap_styles = block_core_tabs_generate_gap_styles( $attributes ); | ||
|
|
||
| // Modify the wrapper and setup initial interactivity directives and context. | ||
| $tag_processor = new WP_HTML_Tag_Processor( $content ); | ||
| $tag_processor->next_tag( array( 'class_name' => 'wp-block-tabs' ) ); | ||
| $tag_processor->add_class( 'vertical' === $attributes['orientation'] ? 'is-vertical' : 'is-horizontal' ); | ||
| $tag_processor->set_attribute( 'data-wp-interactive', 'core/tabs' ); | ||
| $tag_processor->set_attribute( | ||
| 'data-wp-context', | ||
| wp_json_encode( | ||
| array( | ||
| 'activeTabIndex' => $active_tab_index, | ||
| 'tabsList' => $tabs_list, | ||
| ) | ||
| ) | ||
| ); | ||
| $tag_processor->set_attribute( 'data-wp-init', 'callbacks.onTabsInit' ); | ||
| // Get the current hardcoded styles. | ||
| $style = $tag_processor->get_attribute( 'style' ); | ||
| $style .= block_core_tab_get_typography_styles( $attributes ); | ||
| $style .= block_core_tab_get_typography_classes( $attributes ); | ||
| // Add the color styles and gap styles to the existing styles. | ||
| $style .= $color_styles; | ||
| $style .= $gap_styles; | ||
| // Set the updated styles. | ||
| $tag_processor->set_attribute( 'style', $style ); | ||
|
|
||
| $content = $tag_processor->get_updated_html(); | ||
|
|
||
| $tabs_list = array_map( | ||
| function( $tab ) { | ||
| return wp_sprintf( | ||
| '<li class="tabs__list-item" role="presentation"><a data-wp-tab-id="%s" class="tabs__tab-label" data-wp-bind--href="state.getTabHref" role="tab" data-wp-on--click="actions.handleTabClick" data-wp-on--keydown="actions.handleTabKeyDown" data-wp-bind--aria-selected="state.isActiveTab" data-wp-bind--tabindex="state.tabIndexAttribute">%s</a></li>', | ||
| $tab['id'], | ||
| $tab['label'] | ||
| ); | ||
| }, | ||
| $tabs_list | ||
| ); | ||
| $tabs_list = implode( '', $tabs_list ); | ||
|
|
||
| // Splice the tabs_template into the updated_content. | ||
| $list_start_pos = strpos( $content, '<ul class="tabs__list"' ); | ||
| if ( false !== $list_start_pos ) { | ||
| $list_open_end = strpos( $content, '>', $list_start_pos ) + 1; | ||
| $list_close_start = strpos( $content, '</ul>', $list_open_end ); | ||
|
|
||
| $content = substr( $content, 0, $list_open_end ) . $tabs_list . substr( $content, $list_close_start ); | ||
| } | ||
|
|
||
| return $content; | ||
| } | ||
|
|
||
| /** | ||
| * Registers the `core/tab` block on the server. | ||
| * | ||
| * @since 9.22.0 | ||
| * @since 6.8.0 | ||
| */ | ||
| function register_block_core_tab() { | ||
| function register_block_core_tabs() { | ||
| register_block_type_from_metadata( | ||
| __DIR__ . '/tab', | ||
| __DIR__ . '/tabs', | ||
| array( | ||
| 'render_callback' => 'render_block_core_tab', | ||
| 'render_callback' => 'render_block_core_tabs', | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| add_action( 'init', 'register_block_core_tab' ); | ||
| add_action( 'init', 'register_block_core_tabs' ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.