@@ -11,7 +11,7 @@ import { account as icon } from '../../../packages/icons';
1111import { __ } from '@wordpress/i18n' ;
1212// eslint-disable-next-line @wordpress/no-unsafe-wp-apis
1313import { __unstableStripHTML as stripHTML } from '@wordpress/dom' ;
14- import { useEffect , useState } from '@wordpress/element' ;
14+ import { useState } from '@wordpress/element' ;
1515import {
1616 BlockControls ,
1717 RichText ,
@@ -24,52 +24,23 @@ import {
2424} from '@wordpress/block-editor' ;
2525import { ToolbarButton , ToolbarGroup } from '@wordpress/components' ;
2626
27- const styleToMode = [
28- [ 'is-style-icon-only' , 'icon' ] ,
29- [ 'is-style-text-only' , 'text' ] ,
30- [ 'is-style-default' , 'default' ] ,
31- ] ;
32-
33- const modeToAttrs = {
34- icon : { showIcon : true , showLabel : false } ,
35- text : { showIcon : false , showLabel : true } ,
36- default : { showIcon : true , showLabel : true } ,
37- } ;
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 ) ;
4432
45- // useBlockProps() returns the wrapper's className (includes block style); we use it to read the style and merge into the link.
46- const wrapperProps = useBlockProps ( ) ;
47- const wrapperClassName = wrapperProps ?. className ?? '' ;
48- // When no block style is selected there is no is-style-* class; fall back to attributes for display.
49- const displayModeFromStyle = styleToMode . find ( ( [ cls ] ) => wrapperClassName ?. includes ( cls ) ) ?. [ 1 ] ?? null ;
50- let displayModeFromAttrs = 'default' ;
51- if ( showIcon && ! showLabel ) {
52- displayModeFromAttrs = 'icon' ;
53- } else if ( ! showIcon && showLabel ) {
54- displayModeFromAttrs = 'text' ;
55- }
56- const displayMode = displayModeFromStyle ?? displayModeFromAttrs ;
57- const isLabelVisible = displayMode !== 'icon' ;
58- const isIconVisible = displayMode !== 'text' ;
59-
60- useEffect ( ( ) => {
61- if ( displayModeFromStyle === null ) {
62- return ;
63- }
64- const next = modeToAttrs [ displayModeFromStyle ] ;
65- if ( attributes . showIcon !== next . showIcon || attributes . showLabel !== next . showLabel ) {
66- setAttributes ( next ) ;
67- }
68- } , [ displayModeFromStyle , attributes . showIcon , attributes . showLabel , setAttributes ] ) ;
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 ;
6940
7041 const blockProps = useBlockProps ( {
7142 className : classnames (
72- wrapperClassName ,
43+ className ,
7344 'wp-block-button__link' ,
7445 'newspack-reader__account-link' ,
7546 'wp-block-newspack-my-account-button__link' ,
@@ -104,7 +75,7 @@ function MyAccountButtonEdit( { attributes, setAttributes } ) {
10475
10576 return (
10677 < >
107- { displayMode !== 'icon' && (
78+ { isLabelVisible && (
10879 < BlockControls >
10980 < ToolbarGroup >
11081 < ToolbarButton
@@ -128,7 +99,7 @@ function MyAccountButtonEdit( { attributes, setAttributes } ) {
12899 </ ToolbarGroup >
129100 </ BlockControls >
130101 ) }
131- < div className = { classnames ( 'wp-block-buttons' , customClassName ) } >
102+ < div className = { classnames ( 'wp-block-buttons' , blockClassName ) } >
132103 < div className = "wp-block-button" >
133104 < div { ...blockProps } >
134105 { isIconVisible && (
0 commit comments