@@ -12,13 +12,18 @@ import {
1212 Button ,
1313 __experimentalTruncate as Truncate ,
1414 Popover ,
15+ __experimentalToolsPanel as ToolsPanel ,
16+ __experimentalToolsPanelItem as ToolsPanelItem ,
1517} from '@wordpress/components' ;
18+ import { __ } from '@wordpress/i18n' ;
1619
1720/**
1821 * Internal dependencies
1922 */
2023import BlockStylesPreviewPanel from './preview-panel' ;
2124import useStylesForBlocks from './use-styles-for-block' ;
25+ import { useToolsPanelDropdownMenuProps } from '../global-styles/utils' ;
26+ import { getDefaultStyle } from './utils' ;
2227
2328const noop = ( ) => { } ;
2429
@@ -36,6 +41,7 @@ function BlockStyles( { clientId, onSwitch = noop, onHoverClassName = noop } ) {
3641 } ) ;
3742 const [ hoveredStyle , setHoveredStyle ] = useState ( null ) ;
3843 const isMobileViewport = useViewportMatch ( 'medium' , '<' ) ;
44+ const dropdownMenuProps = useToolsPanelDropdownMenuProps ( ) ;
3945
4046 if ( ! stylesToRender || stylesToRender . length === 0 ) {
4147 return null ;
@@ -59,62 +65,96 @@ function BlockStyles( { clientId, onSwitch = noop, onHoverClassName = noop } ) {
5965 onHoverClassName ( item ?. name ?? null ) ;
6066 } ;
6167
68+ const defaultStyle = getDefaultStyle ( stylesToRender ) ;
69+
70+ const hasValue = ( ) => {
71+ return activeStyle ?. name !== defaultStyle ?. name ;
72+ } ;
73+
74+ const onDeselect = ( ) => {
75+ onSelectStylePreview ( defaultStyle ) ;
76+ } ;
77+
6278 return (
63- < div className = "block-editor-block-styles" >
64- < div className = "block-editor-block-styles__variants" >
65- { stylesToRender . map ( ( style ) => {
66- const buttonText = style . label || style . name ;
79+ < ToolsPanel
80+ label = { __ ( 'Styles' ) }
81+ resetAll = { onDeselect }
82+ panelId = { clientId }
83+ hasInnerWrapper
84+ dropdownMenuProps = { dropdownMenuProps }
85+ >
86+ < ToolsPanelItem
87+ hasValue = { hasValue }
88+ label = { __ ( 'Variation' ) }
89+ onDeselect = { onDeselect }
90+ isShownByDefault
91+ panelId = { clientId }
92+ >
93+ < div className = "block-editor-block-styles" >
94+ < div className = "block-editor-block-styles__variants" >
95+ { stylesToRender . map ( ( style ) => {
96+ const buttonText = style . label || style . name ;
6797
68- return (
69- < Button
70- __next40pxDefaultSize
71- className = { clsx (
72- 'block-editor-block-styles__item' ,
73- {
74- 'is-active' :
75- activeStyle . name === style . name ,
76- }
77- ) }
78- key = { style . name }
79- variant = "secondary"
80- label = { buttonText }
81- onMouseEnter = { ( ) => styleItemHandler ( style ) }
82- onFocus = { ( ) => styleItemHandler ( style ) }
83- onMouseLeave = { ( ) => styleItemHandler ( null ) }
84- onBlur = { ( ) => styleItemHandler ( null ) }
85- onClick = { ( ) => onSelectStylePreview ( style ) }
86- aria-current = { activeStyle . name === style . name }
98+ return (
99+ < Button
100+ __next40pxDefaultSize
101+ className = { clsx (
102+ 'block-editor-block-styles__item' ,
103+ {
104+ 'is-active' :
105+ activeStyle . name === style . name ,
106+ }
107+ ) }
108+ key = { style . name }
109+ variant = "secondary"
110+ label = { buttonText }
111+ onMouseEnter = { ( ) =>
112+ styleItemHandler ( style )
113+ }
114+ onFocus = { ( ) => styleItemHandler ( style ) }
115+ onMouseLeave = { ( ) =>
116+ styleItemHandler ( null )
117+ }
118+ onBlur = { ( ) => styleItemHandler ( null ) }
119+ onClick = { ( ) =>
120+ onSelectStylePreview ( style )
121+ }
122+ aria-current = {
123+ activeStyle . name === style . name
124+ }
125+ >
126+ < Truncate
127+ numberOfLines = { 1 }
128+ className = "block-editor-block-styles__item-text"
129+ >
130+ { buttonText }
131+ </ Truncate >
132+ </ Button >
133+ ) ;
134+ } ) }
135+ </ div >
136+ { hoveredStyle && ! isMobileViewport && (
137+ < Popover
138+ placement = "left-start"
139+ offset = { 34 }
140+ focusOnMount = { false }
87141 >
88- < Truncate
89- numberOfLines = { 1 }
90- className = "block-editor-block-styles__item-text"
142+ < div
143+ className = "block-editor-block-styles__preview-panel"
144+ onMouseLeave = { ( ) => styleItemHandler ( null ) }
91145 >
92- { buttonText }
93- </ Truncate >
94- </ Button >
95- ) ;
96- } ) }
97- </ div >
98- { hoveredStyle && ! isMobileViewport && (
99- < Popover
100- placement = "left-start"
101- offset = { 34 }
102- focusOnMount = { false }
103- >
104- < div
105- className = "block-editor-block-styles__preview-panel"
106- onMouseLeave = { ( ) => styleItemHandler ( null ) }
107- >
108- < BlockStylesPreviewPanel
109- activeStyle = { activeStyle }
110- className = { previewClassName }
111- genericPreviewBlock = { genericPreviewBlock }
112- style = { hoveredStyle }
113- />
114- </ div >
115- </ Popover >
116- ) }
117- </ div >
146+ < BlockStylesPreviewPanel
147+ activeStyle = { activeStyle }
148+ className = { previewClassName }
149+ genericPreviewBlock = { genericPreviewBlock }
150+ style = { hoveredStyle }
151+ />
152+ </ div >
153+ </ Popover >
154+ ) }
155+ </ div >
156+ </ ToolsPanelItem >
157+ </ ToolsPanel >
118158 ) ;
119159}
120160
0 commit comments