Skip to content
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

Login/out Block: Add button display option #68233

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ Show login & logout links. ([Source](https://github.com/WordPress/gutenberg/tree
- **Name:** core/loginout
- **Category:** theme
- **Supports:** className, color (background, gradients, link, ~~text~~), interactivity (clientNavigation), spacing (margin, padding), typography (fontSize, lineHeight)
- **Attributes:** displayLoginAsForm, redirectToCurrent
- **Attributes:** displayAsButton, displayLoginAsForm, redirectToCurrent

## Media & Text

Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/loginout/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
"type": "boolean",
"default": false
},
"displayAsButton": {
"type": "boolean",
"default": false
},
"redirectToCurrent": {
"type": "boolean",
"default": true
Expand Down
34 changes: 31 additions & 3 deletions packages/block-library/src/loginout/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { __ } from '@wordpress/i18n';
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';

export default function LoginOutEdit( { attributes, setAttributes } ) {
const { displayLoginAsForm, redirectToCurrent } = attributes;
const { displayLoginAsForm, displayAsButton, redirectToCurrent } =
attributes;
const dropdownMenuProps = useToolsPanelDropdownMenuProps();

return (
Expand Down Expand Up @@ -49,6 +50,25 @@ export default function LoginOutEdit( { attributes, setAttributes } ) {
}
/>
</ToolsPanelItem>
<ToolsPanelItem
label={ __( 'Display as button' ) }
isShownByDefault
hasValue={ () => displayAsButton }
onDeselect={ () =>
setAttributes( { displayAsButton: false } )
}
>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Display as button' ) }
checked={ displayAsButton }
onChange={ () =>
setAttributes( {
displayAsButton: ! displayAsButton,
} )
}
/>
</ToolsPanelItem>
<ToolsPanelItem
label={ __( 'Redirect to current URL' ) }
isShownByDefault
Expand All @@ -72,10 +92,18 @@ export default function LoginOutEdit( { attributes, setAttributes } ) {
</InspectorControls>
<div
{ ...useBlockProps( {
className: 'logged-in',
className: `logged-in${
displayAsButton ? ' wp-block-button' : ''
}`,
} ) }
>
<a href="#login-pseudo-link">{ __( 'Log out' ) }</a>
{ displayAsButton ? (
<div className="wp-block-button__link wp-element-button">
<a href="#login-pseudo-link">{ __( 'Log out' ) }</a>
</div>
) : (
<a href="#login-pseudo-link">{ __( 'Log out' ) }</a>
) }
</div>
</>
);
Expand Down
9 changes: 9 additions & 0 deletions packages/block-library/src/loginout/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ function render_block_core_loginout( $attributes ) {
false
);

// If display as button is enabled, wrap the link in button classes
if ( ! empty( $attributes['displayAsButton'] ) ) {
$classes .= ' wp-block-button';
$contents = sprintf(
'<div class="wp-block-button__link wp-element-button">%s</div>',
$contents
);
}

// If logged-out and displayLoginAsForm is true, show the login form.
if ( ! is_user_logged_in() && ! empty( $attributes['displayLoginAsForm'] ) ) {
// Add a class.
Expand Down
Loading