Skip to content

Commit

Permalink
Image block: Move UI for lightbox from sidebar to the content toolbar…
Browse files Browse the repository at this point in the history
… alongside link settings (#57608)

* Add first pass at Expand on Click in toolbar

* Style changes

* Fix lightbox setting check for toolbar

* Remove old UI

* Sync toolbar UI with global styles settings

* Compact buttons

* Update style.scss

* Update UI

* Make input width smaller

* Remove chevron in Expand on Click popover

* Improve markup and styles

* Remove erroneous closing of popover after link is removed

* Improve button label for removing lightbox

* Update label name in test

---------

Co-authored-by: Rich Tabor <hi@richtabor.com>
  • Loading branch information
artemiomorales and richtabor authored Jan 29, 2024
1 parent 0d5e9ee commit 84b09ce
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
}

.block-editor-link-control {
width: 300px; // Hardcoded width avoids resizing of control when switching between preview/edit.
width: 200px; // Hardcoded width avoids resizing of control when switching between preview/edit.

.block-editor-url-input {
padding: 0; // Cancel unnecessary default 1px padding in this case.
Expand Down
123 changes: 86 additions & 37 deletions packages/block-editor/src/components/url-popover/image-url-input-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import { useRef, useState } from '@wordpress/element';
import {
ToolbarButton,
Button,
NavigableMenu,
MenuItem,
ToggleControl,
TextControl,
SVG,
Path,
MenuGroup,
__experimentalVStack as VStack,
} from '@wordpress/components';
import { link as linkIcon, close } from '@wordpress/icons';
import {
link as linkIcon,
image,
page,
fullscreen,
linkOff,
} from '@wordpress/icons';

/**
* Internal dependencies
Expand All @@ -27,14 +31,6 @@ const LINK_DESTINATION_MEDIA = 'media';
const LINK_DESTINATION_ATTACHMENT = 'attachment';
const NEW_TAB_REL = [ 'noreferrer', 'noopener' ];

const icon = (
<SVG viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<Path d="M0,0h24v24H0V0z" fill="none" />
<Path d="m19 5v14h-14v-14h14m0-2h-14c-1.1 0-2 0.9-2 2v14c0 1.1 0.9 2 2 2h14c1.1 0 2-0.9 2-2v-14c0-1.1-0.9-2-2-2z" />
<Path d="m14.14 11.86l-3 3.87-2.14-2.59-3 3.86h12l-3.86-5.14z" />
</SVG>
);

const ImageURLInputUI = ( {
linkDestination,
onChangeUrl,
Expand All @@ -45,6 +41,9 @@ const ImageURLInputUI = ( {
linkTarget,
linkClass,
rel,
showLightboxSetting,
lightboxEnabled,
onSetLightbox,
} ) => {
const [ isOpen, setIsOpen ] = useState( false );
// Use internal state instead of a ref to make sure that the component
Expand Down Expand Up @@ -138,6 +137,7 @@ const ImageURLInputUI = ( {
onChangeUrl( {
href: urlInput,
linkDestination: selectedDestination,
lightbox: { enabled: false },
} );
}
stopEditLink();
Expand All @@ -157,22 +157,17 @@ const ImageURLInputUI = ( {
const linkDestinations = [
{
linkDestination: LINK_DESTINATION_MEDIA,
title: __( 'Media File' ),
title: __( 'Link to image file' ),
url: mediaType === 'image' ? mediaUrl : undefined,
icon,
icon: image,
},
];
if ( mediaType === 'image' && mediaLink ) {
linkDestinations.push( {
linkDestination: LINK_DESTINATION_ATTACHMENT,
title: __( 'Attachment Page' ),
title: __( 'Link to attachment page' ),
url: mediaType === 'image' ? mediaLink : undefined,
icon: (
<SVG viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<Path d="M0 0h24v24H0V0z" fill="none" />
<Path d="M14 2H6c-1.1 0-1.99.9-1.99 2L4 20c0 1.1.89 2 1.99 2H18c1.1 0 2-.9 2-2V8l-6-6zM6 20V4h7v5h5v11H6z" />
</SVG>
),
icon: page,
} );
}
return linkDestinations;
Expand Down Expand Up @@ -225,14 +220,15 @@ const ImageURLInputUI = ( {
/>
<TextControl
__nextHasNoMarginBottom
label={ __( 'Link CSS Class' ) }
label={ __( 'Link CSS class' ) }
value={ linkClass || '' }
onChange={ onSetLinkClass }
/>
</VStack>
);

const linkEditorValue = urlInput !== null ? urlInput : url;
const showLinkEditor = ( ! linkEditorValue && ! lightboxEnabled ) === true;

const urlLabel = (
getLinkDestinations().find(
Expand All @@ -245,7 +241,7 @@ const ImageURLInputUI = ( {
<ToolbarButton
icon={ linkIcon }
className="components-toolbar__control"
label={ url ? __( 'Edit link' ) : __( 'Insert link' ) }
label={ url ? __( 'Edit link' ) : __( 'Add link' ) }
aria-expanded={ isOpen }
onClick={ openLinkUI }
ref={ setPopoverAnchor }
Expand All @@ -256,14 +252,17 @@ const ImageURLInputUI = ( {
anchor={ popoverAnchor }
onFocusOutside={ onFocusOutside() }
onClose={ closeLinkUI }
renderSettings={ () => advancedOptions }
renderSettings={
! lightboxEnabled ? () => advancedOptions : null
}
additionalControls={
! linkEditorValue && (
<NavigableMenu>
showLinkEditor && (
<MenuGroup>
{ getLinkDestinations().map( ( link ) => (
<MenuItem
key={ link.linkDestination }
icon={ link.icon }
iconPosition="left"
onClick={ () => {
setUrlInput( null );
onSetHref( link.url );
Expand All @@ -273,20 +272,45 @@ const ImageURLInputUI = ( {
{ link.title }
</MenuItem>
) ) }
</NavigableMenu>
{ showLightboxSetting && (
<MenuItem
key="expand-on-click"
className="block-editor-url-popover__expand-on-click"
icon={ fullscreen }
info={ __(
'Scale the image with a lightbox effect.'
) }
iconPosition="left"
onClick={ () => {
setUrlInput( null );
onChangeUrl( {
linkDestination:
LINK_DESTINATION_NONE,
href: '',
} );
onSetLightbox( true );
stopEditLink();
} }
>
{ __( 'Expand on click' ) }
</MenuItem>
) }
</MenuGroup>
)
}
>
{ ( ! url || isEditingLink ) && (
<URLPopover.LinkEditor
className="block-editor-format-toolbar__link-container-content"
value={ linkEditorValue }
onChangeInputValue={ setUrlInput }
onSubmit={ onSubmitLinkChange() }
autocompleteRef={ autocompleteRef }
/>
{ ( ! url || isEditingLink ) && ! lightboxEnabled && (
<>
<URLPopover.LinkEditor
className="block-editor-format-toolbar__link-container-content"
value={ linkEditorValue }
onChangeInputValue={ setUrlInput }
onSubmit={ onSubmitLinkChange() }
autocompleteRef={ autocompleteRef }
/>
</>
) }
{ url && ! isEditingLink && (
{ url && ! isEditingLink && ! lightboxEnabled && (
<>
<URLPopover.LinkViewer
className="block-editor-format-toolbar__link-container-content"
Expand All @@ -295,12 +319,37 @@ const ImageURLInputUI = ( {
urlLabel={ urlLabel }
/>
<Button
icon={ close }
icon={ linkOff }
label={ __( 'Remove link' ) }
onClick={ onLinkRemove }
size="compact"
/>
</>
) }
{ ! url && ! isEditingLink && lightboxEnabled && (
<div className="block-editor-url-popover__expand-on-click">
<div className="fullscreen-icon">
{ fullscreen }
</div>
<div className="text">
<p>{ __( 'Expand on click' ) }</p>
<p className="description">
{ __(
'Scales the image with a lightbox effect'
) }
</p>
</div>
<Button
icon={ linkOff }
className="remove-link"
label={ __( 'Disable expand on click' ) }
onClick={ () => {
onSetLightbox( false );
} }
size="compact"
/>
</div>
) }
</URLPopover>
) }
</>
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/url-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function URLPopover( {
focusOnMount={ focusOnMount }
placement={ computedPlacement }
shift
variant="toolbar"
{ ...popoverProps }
>
<div className="block-editor-url-popover__input-container">
Expand All @@ -82,6 +83,7 @@ function URLPopover( {
label={ __( 'Link settings' ) }
onClick={ toggleSettingsVisibility }
aria-expanded={ isSettingsExpanded }
size="compact"
/>
) }
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default function LinkEditor( {
icon={ keyboardReturn }
label={ __( 'Apply' ) }
type="submit"
size="compact"
/>
</form>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default function LinkViewer( {
icon={ edit }
label={ __( 'Edit' ) }
onClick={ onEditLinkClick }
size="compact"
/>
) }
</div>
Expand Down
82 changes: 51 additions & 31 deletions packages/block-editor/src/components/url-popover/style.scss
Original file line number Diff line number Diff line change
@@ -1,53 +1,39 @@
.block-editor-url-popover__additional-controls {
border-top: $border-width solid $gray-300;
border-top: $border-width solid $gray-900;
padding: $grid-unit-10 $grid-unit-10;
}

.block-editor-url-popover__additional-controls > div[role="menu"] .components-button:not(:disabled):not([aria-disabled="true"]):not(.is-secondary) > svg {
box-shadow: none;
}

.block-editor-url-popover__additional-controls div[role="menu"] > .components-button {
padding-left: $grid-unit-15;
.block-editor-url-popover__input-container {
padding: $grid-unit-10;
padding-left: $grid-unit-20;
}

.block-editor-url-popover__row {
display: flex;
gap: $grid-unit-05;
}

// Any children of the popover-row that are not the settings-toggle
// should take up as much space as possible.
.block-editor-url-popover__row > :not(.block-editor-url-popover__settings-toggle) {
flex-grow: 1;
gap: $grid-unit-05;
}

// Mimic toolbar component styles for the icons in this popover.
.block-editor-url-popover .components-button.has-icon {
padding: 3px;
.block-editor-url-popover__additional-controls .components-button.has-icon {
padding-left: $grid-unit-10;
padding-right: $grid-unit-10;
height: auto;
text-align: left;

> svg {
padding: 5px;
border-radius: $radius-block-ui;
height: 30px;
width: 30px;
}

&:not(:disabled):focus {
box-shadow: none;

> svg {
@include block-toolbar-button-style__focus();
}
margin-right: $grid-unit-10;
}
}

.block-editor-url-popover__settings-toggle {
flex-shrink: 0;

// Add a left divider to the toggle button.
border-radius: 0;
border-left: $border-width solid $gray-300;
margin-left: 1px;

&[aria-expanded="true"] .dashicon {
transform: rotate(180deg);
}
Expand All @@ -56,7 +42,7 @@
.block-editor-url-popover__settings {
display: block;
padding: $grid-unit-20;
border-top: $border-width solid $gray-300;
border-top: $border-width solid $gray-900;
}

.block-editor-url-popover__link-editor,
Expand All @@ -65,16 +51,50 @@
}

.block-editor-url-popover__link-viewer-url {
margin: $grid-unit-10 - $border-width;
display: flex;
align-items: center;
flex-grow: 1;
flex-shrink: 1;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
min-width: 150px;
max-width: 500px;
margin-right: $grid-unit-10;

&.has-invalid-link {
color: $alert-red;
}
}

.block-editor-url-popover__expand-on-click {
display: flex;
align-items: center;
min-width: $modal-min-width;
overflow: hidden;
white-space: nowrap;

.fullscreen-icon {
padding-right: $grid-unit-05;

> svg {
width: $icon-size;
height: $icon-size;
}
}

.text {
flex-grow: 1;

p {
margin: 0;
}
}

.description {
color: $gray-600;
}

.remove-link {
margin-right: $grid-unit-10;
}

}
Loading

0 comments on commit 84b09ce

Please sign in to comment.