@@ -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,20 +22,25 @@ import {
2322 __experimentalGetSpacingClassesAndStyles as useSpacingProps ,
2423 /* eslint-enable @wordpress/no-unsafe-wp-apis */
2524} from '@wordpress/block-editor' ;
26- import { PanelBody , ToggleControl , ToolbarButton , ToolbarGroup } from '@wordpress/components' ;
25+ import { ToolbarButton , ToolbarGroup } from '@wordpress/components' ;
2726
28- /**
29- * Internal dependencies
30- */
31- function MyAccountButtonEdit ( { attributes, setAttributes } ) {
32- const { signedInLabel, signedOutLabel, showLabel, showIcon, style, className : customClassName } = attributes ;
27+ function MyAccountButtonEdit ( { attributes, setAttributes, className : wrapperClassName } ) {
28+ const { signedInLabel, signedOutLabel, style, className : blockClassName } = attributes ;
3329 const borderProps = useBorderProps ( attributes ) ;
3430 const colorProps = useColorProps ( attributes ) ;
3531 const spacingProps = useSpacingProps ( attributes ) ;
36- const isLabelVisible = showLabel !== false ;
37- const isIconVisible = showIcon !== false ;
32+
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 ;
40+
3841 const blockProps = useBlockProps ( {
3942 className : classnames (
43+ className ,
4044 'wp-block-button__link' ,
4145 'newspack-reader__account-link' ,
4246 'wp-block-newspack-my-account-button__link' ,
@@ -54,62 +58,48 @@ function MyAccountButtonEdit( { attributes, setAttributes } ) {
5458 ...spacingProps . style ,
5559 } ,
5660 } ) ;
57- const isReaderActivationEnabled = typeof newspack_blocks === 'undefined' || newspack_blocks . has_reader_activation ;
5861
62+ const isReaderActivationEnabled = typeof newspack_blocks === 'undefined' || newspack_blocks . has_reader_activation ;
5963 const [ previewState , setPreviewState ] = useState ( 'signedout' ) ;
6064 const isSignedOutPreview = previewState === 'signedout' ;
6165 const activeLabel = isSignedOutPreview ? signedOutLabel : signedInLabel ;
6266 const placeholderText = isSignedOutPreview ? __ ( 'Sign in' , 'newspack-plugin' ) : __ ( 'My Account' , 'newspack-plugin' ) ;
63- function setShowLabel ( nextValue ) {
64- if ( ! nextValue && ! isIconVisible ) {
65- setAttributes ( { showLabel : false , showIcon : true } ) ;
66- return ;
67- }
68- setAttributes ( { showLabel : nextValue } ) ;
69- }
7067
71- function setShowIcon ( nextValue ) {
72- if ( ! nextValue && ! isLabelVisible ) {
73- setAttributes ( { showLabel : true , showIcon : false } ) ;
74- return ;
75- }
76- setAttributes ( { showIcon : nextValue } ) ;
68+ function setButtonText ( newText ) {
69+ setAttributes ( isSignedOutPreview ? { signedOutLabel : stripHTML ( newText ) } : { signedInLabel : stripHTML ( newText ) } ) ;
7770 }
7871
79- function setButtonText ( newText ) {
80- const cleaned = stripHTML ( newText ) ;
81- setAttributes ( isSignedOutPreview ? { signedOutLabel : cleaned } : { signedInLabel : cleaned } ) ;
72+ if ( ! isReaderActivationEnabled ) {
73+ return < div { ...blockProps } style = { { ...blockProps . style , display : 'none' } } /> ;
8274 }
8375
84- return ! isReaderActivationEnabled ? (
85- < div { ...blockProps } style = { { ...blockProps . style , display : 'none' } } />
86- ) : (
76+ return (
8777 < >
88- < InspectorControls >
89- < PanelBody title = { __ ( 'Display' , 'newspack-plugin' ) } >
90- < ToggleControl label = { __ ( 'Show label' , 'newspack-plugin' ) } checked = { isLabelVisible } onChange = { setShowLabel } / >
91- < ToggleControl label = { __ ( 'Show icon' , 'newspack-plugin' ) } checked = { isIconVisible } onChange = { setShowIcon } />
92- </ PanelBody >
93- </ InspectorControls >
94- < BlockControls >
95- < ToolbarGroup >
96- < ToolbarButton
97- isPressed = { isSignedOutPreview }
98- onClick = { ( ) => setPreviewState ( 'signedout ' ) }
99- style = { { paddingLeft : '12px' , paddingRight : '12px' } }
100- >
101- { __ ( 'Signed out' , 'newspack-plugin' ) }
102- </ ToolbarButton >
103- < ToolbarButton
104- isPressed = { ! isSignedOutPreview }
105- onClick = { ( ) => setPreviewState ( 'signedin' ) }
106- style = { { paddingLeft : '12px' , paddingRight : '12px' } }
107- >
108- { __ ( 'Signed in' , 'newspack-plugin' ) }
109- </ ToolbarButton >
110- </ ToolbarGroup >
111- </ BlockControls >
112- < 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 ) } >
113103 < div className = "wp-block-button" >
114104 < div { ...blockProps } >
115105 { isIconVisible && (
@@ -123,7 +113,7 @@ function MyAccountButtonEdit( { attributes, setAttributes } ) {
123113 aria-label = { __ ( 'Button text' , 'newspack-plugin' ) }
124114 placeholder = { placeholderText }
125115 value = { activeLabel || '' }
126- onChange = { value => setButtonText ( value ) }
116+ onChange = { setButtonText }
127117 withoutInteractiveFormatting
128118 />
129119 </ div >
0 commit comments