Repository navigation
Tab List: Add toolbar buttons to reorder tabs #80107
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
Changes from 5 commits
a3be3fa
5846294
36b52a1
339fd5b
2ae3493
09637ca
6ba3ae2
5f6d774
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| @use "@wordpress/base-styles/mixins" as *; | ||
| @use "@wordpress/base-styles/variables" as *; | ||
|
|
||
| .wp-block-tab-list__mover-button { | ||
| @include break-small() { | ||
| width: $button-size-small; | ||
| min-width: 0 !important; | ||
| } | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One way to avoid having to say "right" and "left" is to use words such as "previous" and "next", or "before" and "after", etc
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds like a good simplification to me!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice idea! Let's adopt that word.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A clever solution. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { __, isRTL } from '@wordpress/i18n'; | ||
| import { | ||
| BlockControls, | ||
| store as blockEditorStore, | ||
| } from '@wordpress/block-editor'; | ||
| import { ToolbarButton } from '@wordpress/components'; | ||
| import { chevronLeft, chevronRight } from '@wordpress/icons'; | ||
| import { useSelect } from '@wordpress/data'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import useTabActions from '../tabs/use-tab-actions'; | ||
|
|
||
| /** | ||
| * Block toolbar buttons that reorder the active tab one position left or right. | ||
| * Reordering acts on the underlying core/tab-panel blocks; the tab-list labels | ||
| * follow their panel automatically via useTabListItemsSync. The buttons are | ||
| * disabled at the first/last position. | ||
| * | ||
| * @param {Object} props | ||
| * @param {string} props.tabsClientId The client ID of the parent tabs block. | ||
| * @return {React.JSX.Element} The toolbar control element. | ||
| */ | ||
| export default function TabMovers( { tabsClientId } ) { | ||
| const { moveTab } = useTabActions( tabsClientId ); | ||
| const { tabCount, activeIndex } = useSelect( | ||
| ( select ) => { | ||
| if ( ! tabsClientId ) { | ||
| return { tabCount: 0, activeIndex: 0 }; | ||
| } | ||
| const { getBlocks, getBlockAttributes } = | ||
| select( blockEditorStore ); | ||
| const tabsAttributes = getBlockAttributes( tabsClientId ); | ||
| const tabPanels = getBlocks( tabsClientId ).find( | ||
| ( block ) => block.name === 'core/tab-panels' | ||
| ); | ||
| return { | ||
| tabCount: tabPanels?.innerBlocks.length ?? 0, | ||
| activeIndex: | ||
| tabsAttributes?.editorActiveTabIndex ?? | ||
| tabsAttributes?.activeTabIndex ?? | ||
| 0, | ||
| }; | ||
| }, | ||
| [ tabsClientId ] | ||
| ); | ||
|
|
||
| return ( | ||
| <BlockControls group="parent"> | ||
| <ToolbarButton | ||
| className="wp-block-tab-list__mover-button" | ||
| icon={ isRTL() ? chevronRight : chevronLeft } | ||
| label={ __( 'Move tab left' ) } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should these labels also get the isRTL check?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| onClick={ () => moveTab( -1 ) } | ||
| disabled={ activeIndex <= 0 } | ||
| accessibleWhenDisabled | ||
| /> | ||
| <ToolbarButton | ||
| className="wp-block-tab-list__mover-button" | ||
| icon={ isRTL() ? chevronLeft : chevronRight } | ||
| label={ __( 'Move tab right' ) } | ||
| onClick={ () => moveTab( 1 ) } | ||
| disabled={ activeIndex >= tabCount - 1 } | ||
| accessibleWhenDisabled | ||
| /> | ||
| </BlockControls> | ||
| ); | ||
| } | ||





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.
The button needs to resize according to the viewport, similar to the block mover button.