@@ -14,7 +14,6 @@ import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
1414import { useState } from '@wordpress/element' ;
1515import {
1616 BlockControls ,
17- InspectorControls ,
1817 RichText ,
1918 useBlockProps ,
2019 /* eslint-disable @wordpress/no-unsafe-wp-apis */
@@ -23,45 +22,25 @@ import {
2322 __experimentalGetSpacingClassesAndStyles as useSpacingProps ,
2423 /* eslint-enable @wordpress/no-unsafe-wp-apis */
2524} from '@wordpress/block-editor' ;
26- import {
27- PanelBody ,
28- ToolbarButton ,
29- ToolbarGroup ,
30- /* eslint-disable @wordpress/no-unsafe-wp-apis */
31- __experimentalToggleGroupControl as ToggleGroupControl ,
32- __experimentalToggleGroupControlOption as ToggleGroupControlOption ,
33- /* eslint-enable @wordpress/no-unsafe-wp-apis */
34- } from '@wordpress/components' ;
25+ import { ToolbarButton , ToolbarGroup } from '@wordpress/components' ;
3526
36- /**
37- * Internal dependencies
38- */
39- function MyAccountButtonEdit ( { attributes, setAttributes } ) {
40- const { signedInLabel, signedOutLabel, showLabel, showIcon, style, className : customClassName } = attributes ;
27+ function MyAccountButtonEdit ( { attributes, setAttributes, className : wrapperClassName } ) {
28+ const { signedInLabel, signedOutLabel, style, className : blockClassName } = attributes ;
4129 const borderProps = useBorderProps ( attributes ) ;
4230 const colorProps = useColorProps ( attributes ) ;
4331 const spacingProps = useSpacingProps ( attributes ) ;
44- const isLabelVisible = showLabel !== false ;
45- const isIconVisible = showIcon !== false ;
46-
47- const displayMode = ( ( ) => {
48- if ( isIconVisible && ! isLabelVisible ) {
49- return 'icon' ;
50- }
51- if ( ! isIconVisible && isLabelVisible ) {
52- return 'text' ;
53- }
54- return 'default' ;
55- } ) ( ) ;
5632
57- const displayModeHelp = {
58- default : __ ( 'Displays the icon with text.' , 'newspack-plugin' ) ,
59- icon : __ ( 'Displays the icon without text.' , 'newspack-plugin' ) ,
60- text : __ ( 'Displays the text without an icon.' , 'newspack-plugin' ) ,
61- } ;
33+ // Block style comes from the Styles panel (wrapper) or saved className.
34+ const blockWrapperProps = useBlockProps ( ) ;
35+ const className = blockWrapperProps ?. className ?? wrapperClassName ?? blockClassName ?? '' ;
36+ const isIconOnly = className . includes ( 'is-style-icon-only' ) ;
37+ const isTextOnly = className . includes ( 'is-style-text-only' ) ;
38+ const isLabelVisible = ! isIconOnly ;
39+ const isIconVisible = ! isTextOnly ;
6240
6341 const blockProps = useBlockProps ( {
6442 className : classnames (
43+ className ,
6544 'wp-block-button__link' ,
6645 'newspack-reader__account-link' ,
6746 'wp-block-newspack-my-account-button__link' ,
@@ -86,63 +65,41 @@ function MyAccountButtonEdit( { attributes, setAttributes } ) {
8665 const activeLabel = isSignedOutPreview ? signedOutLabel : signedInLabel ;
8766 const placeholderText = isSignedOutPreview ? __ ( 'Sign in' , 'newspack-plugin' ) : __ ( 'My Account' , 'newspack-plugin' ) ;
8867
89- function setDisplayMode ( value ) {
90- switch ( value ) {
91- case 'icon' :
92- setAttributes ( { showIcon : true , showLabel : false } ) ;
93- break ;
94- case 'text' :
95- setAttributes ( { showIcon : false , showLabel : true } ) ;
96- break ;
97- default :
98- setAttributes ( { showIcon : true , showLabel : true } ) ;
99- }
68+ function setButtonText ( newText ) {
69+ setAttributes ( isSignedOutPreview ? { signedOutLabel : stripHTML ( newText ) } : { signedInLabel : stripHTML ( newText ) } ) ;
10070 }
10171
102- function setButtonText ( newText ) {
103- const cleaned = stripHTML ( newText ) ;
104- setAttributes ( isSignedOutPreview ? { signedOutLabel : cleaned } : { signedInLabel : cleaned } ) ;
72+ if ( ! isReaderActivationEnabled ) {
73+ return < div { ...blockProps } style = { { ...blockProps . style , display : 'none' } } /> ;
10574 }
10675
107- return ! isReaderActivationEnabled ? (
108- < div { ...blockProps } style = { { ...blockProps . style , display : 'none' } } />
109- ) : (
76+ return (
11077 < >
111- < InspectorControls >
112- < PanelBody title = { __ ( 'Display' , 'newspack-plugin' ) } >
113- < ToggleGroupControl
114- help = { displayModeHelp [ displayMode ] }
115- isBlock
116- aria-label = { __ ( 'Button display mode' , 'newspack-plugin' ) }
117- onChange = { setDisplayMode }
118- value = { displayMode }
119- __next40pxDefaultSize
120- >
121- < ToggleGroupControlOption label = { __ ( 'Default' , 'newspack-plugin' ) } value = "default" />
122- < ToggleGroupControlOption label = { __ ( 'Icon only' , 'newspack-plugin' ) } value = "icon" />
123- < ToggleGroupControlOption label = { __ ( 'Text only' , 'newspack-plugin' ) } value = "text" />
124- </ ToggleGroupControl >
125- </ PanelBody >
126- </ InspectorControls >
127- < BlockControls >
128- < ToolbarGroup >
129- < ToolbarButton
130- isPressed = { isSignedOutPreview }
131- onClick = { ( ) => setPreviewState ( 'signedout' ) }
132- style = { { paddingLeft : '12px' , paddingRight : '12px' } }
133- >
134- { __ ( 'Signed out' , 'newspack-plugin' ) }
135- </ ToolbarButton >
136- < ToolbarButton
137- isPressed = { ! isSignedOutPreview }
138- onClick = { ( ) => setPreviewState ( 'signedin' ) }
139- style = { { paddingLeft : '12px' , paddingRight : '12px' } }
140- >
141- { __ ( 'Signed in' , 'newspack-plugin' ) }
142- </ ToolbarButton >
143- </ ToolbarGroup >
144- </ BlockControls >
145- < div className = { classnames ( 'wp-block-buttons' , customClassName ) } >
78+ { isLabelVisible && (
79+ < BlockControls >
80+ < ToolbarGroup >
81+ < ToolbarButton
82+ icon = { false }
83+ isPressed = { isSignedOutPreview }
84+ label = { __ ( 'Signed out' , 'newspack-plugin' ) }
85+ onClick = { ( ) => setPreviewState ( 'signedout' ) }
86+ style = { { paddingLeft : '12px' , paddingRight : '12px' } }
87+ >
88+ { __ ( 'Signed out' , 'newspack-plugin' ) }
89+ </ ToolbarButton >
90+ < ToolbarButton
91+ icon = { false }
92+ isPressed = { ! isSignedOutPreview }
93+ label = { __ ( 'Signed in' , 'newspack-plugin' ) }
94+ onClick = { ( ) => setPreviewState ( 'signedin' ) }
95+ style = { { paddingLeft : '12px' , paddingRight : '12px' } }
96+ >
97+ { __ ( 'Signed in' , 'newspack-plugin' ) }
98+ </ ToolbarButton >
99+ </ ToolbarGroup >
100+ </ BlockControls >
101+ ) }
102+ < div className = { classnames ( 'wp-block-buttons' , blockClassName ) } >
146103 < div className = "wp-block-button" >
147104 < div { ...blockProps } >
148105 { isIconVisible && (
@@ -156,7 +113,7 @@ function MyAccountButtonEdit( { attributes, setAttributes } ) {
156113 aria-label = { __ ( 'Button text' , 'newspack-plugin' ) }
157114 placeholder = { placeholderText }
158115 value = { activeLabel || '' }
159- onChange = { value => setButtonText ( value ) }
116+ onChange = { setButtonText }
160117 withoutInteractiveFormatting
161118 />
162119 </ div >
0 commit comments