Skip to content

Commit

Permalink
enable selection between icon and label
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicente Canales committed Nov 10, 2021
1 parent 1dc8d6d commit 0912320
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 19 deletions.
4 changes: 4 additions & 0 deletions packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"type": "string",
"default": "mobile"
},
"useIcon": {
"type": "boolean",
"default": true
},
"__unstableLocation": {
"type": "string"
},
Expand Down
47 changes: 46 additions & 1 deletion packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
__experimentalToggleGroupControlOption as ToggleGroupControlOption,
ToolbarGroup,
ToolbarDropdownMenu,
Button,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';

Expand All @@ -51,6 +52,7 @@ import NavigationMenuNameControl from './navigation-menu-name-control';
import NavigationMenuPublishButton from './navigation-menu-publish-button';
import UnsavedInnerBlocks from './unsaved-inner-blocks';
import NavigationMenuDeleteControl from './navigation-menu-delete-control';
import OverlayMenuIcon from './overlay-menu-icon';

function getComputedStyle( node ) {
return node.ownerDocument.defaultView.getComputedStyle( node );
Expand Down Expand Up @@ -104,10 +106,12 @@ function Navigation( {
} ) {
const {
itemsJustification,
label,
openSubmenusOnClick,
orientation,
overlayMenu,
showSubmenuIcon,
useIcon,
} = attributes;

const [ areaMenu, setAreaMenu ] = useEntityProp(
Expand Down Expand Up @@ -167,6 +171,8 @@ function Navigation( {
false
);

const [ overlayMenuPreview, setOverlayMenuPreview ] = useState( false );

const {
isNavigationMenuResolved,
isNavigationMenuMissing,
Expand Down Expand Up @@ -299,6 +305,13 @@ function Navigation( {
? CustomPlaceholder
: Placeholder;

const isResponsive = 'never' !== overlayMenu;

const overlayMenuPreviewClasses = classnames(
'wp-block-navigation__overlay-menu-preview',
{ open: overlayMenuPreview }
);

return (
<EntityProvider
kind="postType"
Expand Down Expand Up @@ -346,6 +359,33 @@ function Navigation( {
{ hasSubmenuIndicatorSetting && (
<PanelBody title={ __( 'Display' ) }>
<h3>{ __( 'Overlay Menu' ) }</h3>
{ isResponsive && (
<Button
className={ overlayMenuPreviewClasses }
onClick={ () => {
setOverlayMenuPreview(
! overlayMenuPreview
);
} }
>
{ useIcon && <OverlayMenuIcon /> }
{ ! useIcon && (
<span>{ __( 'Menu' ) }</span>
) }
</Button>
) }
{ overlayMenuPreview && (
<ToggleControl
label={ __( 'Show icon button' ) }
help={ __(
'Configure the visual appearance of the button opening the overlay menu.'
) }
onChange={ ( value ) =>
setAttributes( { useIcon: value } )
}
checked={ useIcon }
/>
) }
<ToggleGroupControl
label={ __( 'Configure overlay menu' ) }
value={ overlayMenu }
Expand Down Expand Up @@ -479,8 +519,13 @@ function Navigation( {
<ResponsiveWrapper
id={ clientId }
onToggle={ setResponsiveMenuVisibility }
onLabelChange={ ( value ) =>
setAttributes( { label: value } )
}
label={ label }
useIcon={ useIcon }
isOpen={ isResponsiveMenuOpen }
isResponsive={ 'never' !== overlayMenu }
isResponsive={ isResponsive }
isHiddenByDefault={ 'always' === overlayMenu }
>
{ isEntityAvailable && (
Expand Down
21 changes: 21 additions & 0 deletions packages/block-library/src/navigation/edit/overlay-menu-icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* WordPress dependencies
*/
import { SVG, Rect } from '@wordpress/primitives';

export default function OverlayMenuIcon() {
return (
<SVG
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
width="24"
height="24"
role="img"
aria-hidden="true"
focusable="false"
>
<Rect x="4" y="7.5" width="16" height="1.5" />
<Rect x="4" y="15" width="16" height="1.5" />
</SVG>
);
}
26 changes: 12 additions & 14 deletions packages/block-library/src/navigation/edit/responsive-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ import classnames from 'classnames';
*/
import { close, Icon } from '@wordpress/icons';
import { Button } from '@wordpress/components';
import { SVG, Rect } from '@wordpress/primitives';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import OverlayMenuIcon from './overlay-menu-icon';

export default function ResponsiveWrapper( {
children,
id,
isOpen,
isResponsive,
onToggle,
isHiddenByDefault,
useIcon,
} ) {
if ( ! isResponsive ) {
return children;
Expand Down Expand Up @@ -46,21 +51,14 @@ export default function ResponsiveWrapper( {
className={ openButtonClasses }
onClick={ () => onToggle( true ) }
>
<SVG
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
width="24"
height="24"
role="img"
aria-hidden="true"
focusable="false"
>
<Rect x="4" y="7.5" width="16" height="1.5" />
<Rect x="4" y="15" width="16" height="1.5" />
</SVG>
{ useIcon && <OverlayMenuIcon /> }
{ ! useIcon && (
<span className="wp-block-navigation__toggle_button_label">
{ __( 'Menu' ) }
</span>
) }
</Button>
) }

<div className={ responsiveContainerClasses } id={ modalId }>
<div
className="wp-block-navigation__responsive-close"
Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/navigation/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -570,3 +570,15 @@ body.editor-styles-wrapper
justify-content: center;
margin-bottom: $grid-unit-20;
}

.wp-block-navigation__overlay-menu-preview {
display: flex;
align-items: center;
width: 100%;
background-color: $gray-200;
padding: 3rem 2rem;
margin-bottom: 1rem;
&.open {
background-color: $white;
}
}
11 changes: 8 additions & 3 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,19 @@ function( $block ) {
$is_hidden_by_default ? 'always-shown' : '',
);

$toggle_button_icon = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false"><rect x="4" y="7.5" width="16" height="1.5" /><rect x="4" y="15" width="16" height="1.5" /></svg>';
$should_display_icon_label = isset( $attributes['useIcon'] ) && true === $attributes['useIcon'];
$toggle_button_content = $should_display_icon_label ? $toggle_button_icon : 'Menu';

$responsive_container_markup = sprintf(
'<button aria-expanded="false" aria-haspopup="true" aria-label="%3$s" class="%6$s" data-micromodal-trigger="modal-%1$s"><svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" role="img" aria-hidden="true" focusable="false"><rect x="4" y="7.5" width="16" height="1.5" /><rect x="4" y="15" width="16" height="1.5" /></svg></button>
'<button aria-expanded="false" aria-haspopup="true" aria-label="%3$s" class="%6$s" data-micromodal-trigger="modal-%1$s">%7$s</button>
<div class="%5$s" id="modal-%1$s">
<div class="wp-block-navigation__responsive-close" tabindex="-1" data-micromodal-close>
<div class="wp-block-navigation__responsive-dialog" role="dialog" aria-modal="true" aria-labelledby="modal-%1$s-title" >
<button aria-label="%4$s" data-micromodal-close class="wp-block-navigation__responsive-container-close"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" role="img" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg></button>
<div class="wp-block-navigation__responsive-container-content" id="modal-%1$s-content">
%2$s
</div>
</di v>
</div>
</div>
</div>',
Expand All @@ -258,7 +262,8 @@ function( $block ) {
__( 'Open menu' ), // Open button label.
__( 'Close menu' ), // Close button label.
implode( ' ', $responsive_container_classes ),
implode( ' ', $open_button_classes )
implode( ' ', $open_button_classes ),
$toggle_button_content
);

return sprintf(
Expand Down
6 changes: 6 additions & 0 deletions packages/block-library/src/navigation/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@
color: #000;
}

// Overlay menu toggle button label
.wp-block-navigation__toggle_button_label {
font-size: 1rem;
font-weight: bold;
}

// Menu and close buttons.
.wp-block-navigation__responsive-container-open,
.wp-block-navigation__responsive-container-close {
Expand Down
3 changes: 2 additions & 1 deletion test/integration/fixtures/blocks/core__navigation.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"attributes": {
"showSubmenuIcon": true,
"openSubmenusOnClick": false,
"overlayMenu": "mobile"
"overlayMenu": "mobile",
"useIcon": true
},
"innerBlocks": [],
"originalContent": ""
Expand Down

0 comments on commit 0912320

Please sign in to comment.