-
Notifications
You must be signed in to change notification settings - Fork 59
feat[my-account-block]: add option to toggle off icon/text, and update tests #4443
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 6 commits
c25371f
9d9a68d
84a9ef8
ced713a
d315db9
47dba2a
9fed074
0581242
81ee30d
a49f0e0
cf68793
86d6e24
44f111f
b47c2a2
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 |
|---|---|---|
|
|
@@ -93,13 +93,26 @@ public static function render_block( $attrs ) { | |
| $default_attrs = [ | ||
| 'signedInLabel' => __( 'My Account', 'newspack-plugin' ), | ||
| 'signedOutLabel' => __( 'Sign in', 'newspack-plugin' ), | ||
| 'className' => '', | ||
| ]; | ||
| $attrs = \wp_parse_args( $attrs, $default_attrs ); | ||
|
|
||
| $is_signed_in = \is_user_logged_in(); | ||
| $label = $is_signed_in ? $attrs['signedInLabel'] : $attrs['signedOutLabel']; | ||
| if ( '' === trim( (string) $label ) ) { | ||
| return ''; | ||
| $is_signed_in = \is_user_logged_in(); | ||
| $signed_in_label = '' === trim( (string) $attrs['signedInLabel'] ) ? $default_attrs['signedInLabel'] : $attrs['signedInLabel']; | ||
| $signed_out_label = '' === trim( (string) $attrs['signedOutLabel'] ) ? $default_attrs['signedOutLabel'] : $attrs['signedOutLabel']; | ||
| $label = $is_signed_in ? $signed_in_label : $signed_out_label; | ||
|
|
||
| /** Display mode from block style class in className (default = icon + text). */ | ||
| $wrapper_class = (string) $attrs['className']; | ||
| if ( \strpos( $wrapper_class, 'is-style-icon-only' ) !== false ) { | ||
|
||
| $show_label = false; | ||
| $show_icon = true; | ||
| } elseif ( \strpos( $wrapper_class, 'is-style-text-only' ) !== false ) { | ||
| $show_label = true; | ||
| $show_icon = false; | ||
| } else { | ||
| $show_label = true; | ||
| $show_icon = true; | ||
| } | ||
|
|
||
| $account_url = self::get_account_url(); | ||
|
|
@@ -118,8 +131,8 @@ public static function render_block( $attrs ) { | |
| } | ||
|
|
||
| $labels = [ | ||
| 'signedin' => $attrs['signedInLabel'], | ||
| 'signedout' => $attrs['signedOutLabel'], | ||
| 'signedin' => $signed_in_label, | ||
| 'signedout' => $signed_out_label, | ||
| ]; | ||
|
|
||
| $extra_classes = [ | ||
|
|
@@ -153,20 +166,26 @@ public static function render_block( $attrs ) { | |
| $wrapper_div_classes = \array_merge( $wrapper_div_classes, $custom_classes ); | ||
| } | ||
|
|
||
| $wrapper_attributes = \get_block_wrapper_attributes( | ||
| [ | ||
| 'class' => implode( ' ', $extra_classes ), | ||
| 'href' => \esc_url_raw( $href ), | ||
| ] | ||
| ); | ||
| $wrapper_attribute_args = [ | ||
| 'class' => implode( ' ', $extra_classes ), | ||
| 'href' => \esc_url_raw( $href ), | ||
| 'data-wp-logged-in' => $is_signed_in ? '1' : '0', | ||
|
||
| ]; | ||
| $wrapper_attributes = \get_block_wrapper_attributes( $wrapper_attribute_args ); | ||
|
|
||
| $link = '<div class="' . \esc_attr( implode( ' ', $wrapper_div_classes ) ) . '">'; | ||
| $link .= '<div class="wp-block-button">'; | ||
| $link .= '<a ' . $wrapper_attributes . ' data-labels="' . \esc_attr( \wp_json_encode( $labels ) ) . '" ' . $should_modal_trigger . '>'; | ||
| $link .= '<span class="wp-block-newspack-my-account-button__icon" aria-hidden="true">'; | ||
| $link .= Newspack_UI_Icons::get_svg( 'account' ); | ||
| $link .= '</span>'; | ||
| $link .= '<span class="newspack-reader__account-link__label">' . \esc_html( $label ) . '</span>'; | ||
| if ( $show_icon ) { | ||
| $link .= '<span class="wp-block-newspack-my-account-button__icon" aria-hidden="true">'; | ||
| $link .= Newspack_UI_Icons::get_svg( 'account' ); | ||
| $link .= '</span>'; | ||
| } | ||
| $label_classes = [ 'newspack-reader__account-link__label' ]; | ||
| if ( ! $show_label ) { | ||
| $label_classes[] = 'screen-reader-text'; | ||
| } | ||
| $link .= '<span class="' . \esc_attr( implode( ' ', $label_classes ) ) . '">' . \esc_html( $label ) . '</span>'; | ||
| $link .= '</a>'; | ||
| $link .= '</div>'; | ||
| $link .= '</div>'; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,16 +24,23 @@ import { | |
| } from '@wordpress/block-editor'; | ||
| import { ToolbarButton, ToolbarGroup } from '@wordpress/components'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| function MyAccountButtonEdit( { attributes, setAttributes } ) { | ||
| const { signedInLabel, signedOutLabel, style, className: customClassName } = attributes; | ||
| function MyAccountButtonEdit( { attributes, setAttributes, className: wrapperClassName } ) { | ||
| const { signedInLabel, signedOutLabel, style, className: blockClassName } = attributes; | ||
| const borderProps = useBorderProps( attributes ); | ||
| const colorProps = useColorProps( attributes ); | ||
| const spacingProps = useSpacingProps( attributes ); | ||
|
|
||
| // Block style comes from the Styles panel (wrapper) or saved className. | ||
| const blockWrapperProps = useBlockProps(); | ||
|
||
| const className = blockWrapperProps?.className ?? wrapperClassName ?? blockClassName ?? ''; | ||
| const isIconOnly = className.includes( 'is-style-icon-only' ); | ||
| const isTextOnly = className.includes( 'is-style-text-only' ); | ||
|
||
| const isLabelVisible = ! isIconOnly; | ||
| const isIconVisible = ! isTextOnly; | ||
|
|
||
| const blockProps = useBlockProps( { | ||
| className: classnames( | ||
| className, | ||
| 'wp-block-button__link', | ||
| 'newspack-reader__account-link', | ||
| 'wp-block-newspack-my-account-button__link', | ||
|
|
@@ -51,52 +58,62 @@ function MyAccountButtonEdit( { attributes, setAttributes } ) { | |
| ...spacingProps.style, | ||
| }, | ||
| } ); | ||
| const isReaderActivationEnabled = typeof newspack_blocks === 'undefined' || newspack_blocks.has_reader_activation; | ||
|
|
||
| const isReaderActivationEnabled = typeof newspack_blocks === 'undefined' || newspack_blocks.has_reader_activation; | ||
| const [ previewState, setPreviewState ] = useState( 'signedout' ); | ||
| const isSignedOutPreview = previewState === 'signedout'; | ||
| const activeLabel = isSignedOutPreview ? signedOutLabel : signedInLabel; | ||
| const placeholderText = isSignedOutPreview ? __( 'Sign in', 'newspack-plugin' ) : __( 'My Account', 'newspack-plugin' ); | ||
|
|
||
| function setButtonText( newText ) { | ||
| const cleaned = stripHTML( newText ); | ||
| setAttributes( isSignedOutPreview ? { signedOutLabel: cleaned } : { signedInLabel: cleaned } ); | ||
| setAttributes( isSignedOutPreview ? { signedOutLabel: stripHTML( newText ) } : { signedInLabel: stripHTML( newText ) } ); | ||
| } | ||
|
|
||
| if ( ! isReaderActivationEnabled ) { | ||
| return <div { ...blockProps } style={ { ...blockProps.style, display: 'none' } } />; | ||
| } | ||
|
|
||
| return ! isReaderActivationEnabled ? ( | ||
| <div { ...blockProps } style={ { ...blockProps.style, display: 'none' } } /> | ||
| ) : ( | ||
| return ( | ||
| <> | ||
| <BlockControls> | ||
| <ToolbarGroup> | ||
| <ToolbarButton | ||
| isPressed={ isSignedOutPreview } | ||
| onClick={ () => setPreviewState( 'signedout' ) } | ||
| style={ { paddingLeft: '12px', paddingRight: '12px' } } | ||
| > | ||
| { __( 'Signed out', 'newspack-plugin' ) } | ||
| </ToolbarButton> | ||
| <ToolbarButton | ||
| isPressed={ ! isSignedOutPreview } | ||
| onClick={ () => setPreviewState( 'signedin' ) } | ||
| style={ { paddingLeft: '12px', paddingRight: '12px' } } | ||
| > | ||
| { __( 'Signed in', 'newspack-plugin' ) } | ||
| </ToolbarButton> | ||
| </ToolbarGroup> | ||
| </BlockControls> | ||
| <div className={ classnames( 'wp-block-buttons', customClassName ) }> | ||
| { isLabelVisible && ( | ||
|
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. When icon-only style is selected,
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. Yeah but I think it's OK since it uses the default aria-labels
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. It is a little funky, but I think it's probably okay as is for now -- or at least the kind of thing that should be covered in the documentation (how to edit the hidden label), and if we get a lot of feedback about friction, we can revisit 🙂
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. Readme has been updated in cf68793!
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. I'd say keep it as it is for now. If we get feedback about this, we can always add 2 textcontrols to the sidebar |
||
| <BlockControls> | ||
| <ToolbarGroup> | ||
| <ToolbarButton | ||
| icon={ false } | ||
| isPressed={ isSignedOutPreview } | ||
| label={ __( 'Signed out', 'newspack-plugin' ) } | ||
| onClick={ () => setPreviewState( 'signedout' ) } | ||
| style={ { paddingLeft: '12px', paddingRight: '12px' } } | ||
| > | ||
| { __( 'Signed out', 'newspack-plugin' ) } | ||
| </ToolbarButton> | ||
| <ToolbarButton | ||
| icon={ false } | ||
| isPressed={ ! isSignedOutPreview } | ||
| label={ __( 'Signed in', 'newspack-plugin' ) } | ||
| onClick={ () => setPreviewState( 'signedin' ) } | ||
| style={ { paddingLeft: '12px', paddingRight: '12px' } } | ||
| > | ||
| { __( 'Signed in', 'newspack-plugin' ) } | ||
| </ToolbarButton> | ||
| </ToolbarGroup> | ||
| </BlockControls> | ||
| ) } | ||
| <div className={ classnames( 'wp-block-buttons', blockClassName ) }> | ||
| <div className="wp-block-button"> | ||
| <div { ...blockProps }> | ||
| <span className="wp-block-newspack-my-account-button__icon" aria-hidden="true"> | ||
| { icon } | ||
| </span> | ||
| { isIconVisible && ( | ||
| <span className="wp-block-newspack-my-account-button__icon" aria-hidden="true"> | ||
| { icon } | ||
| </span> | ||
| ) } | ||
| <RichText | ||
| tagName="span" | ||
| className={ ! isLabelVisible ? 'screen-reader-text' : undefined } | ||
| aria-label={ __( 'Button text', 'newspack-plugin' ) } | ||
| placeholder={ placeholderText } | ||
| value={ activeLabel || '' } | ||
| onChange={ value => setButtonText( value ) } | ||
| onChange={ setButtonText } | ||
| withoutInteractiveFormatting | ||
| /> | ||
| </div> | ||
|
|
||
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.
Nit: WPCS recommends
//for inline comments./** */style is reserved for docblocks.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.
9fed074