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

Block Styles: Truncate long button labels #42975

Merged
merged 3 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 4 additions & 7 deletions packages/block-editor/src/components/block-styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useState, useLayoutEffect } from '@wordpress/element';
import { useViewportMatch } from '@wordpress/compose';
import {
Button,
__experimentalText as Text,
__experimentalTruncate as Truncate,
Slot,
Fill,
} from '@wordpress/components';
Expand Down Expand Up @@ -117,15 +117,12 @@ function BlockStyles( {
onClick={ () => onSelectStylePreview( style ) }
aria-current={ activeStyle.name === style.name }
>
<Text
as="span"
limit={ 12 }
ellipsizeMode="tail"
<Truncate
numberOfLines={ 1 }
className="block-editor-block-styles__item-text"
truncate
>
{ buttonText }
</Text>
</Truncate>
</Button>
);
} ) }
Expand Down
10 changes: 10 additions & 0 deletions packages/block-editor/src/components/block-styles/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@
display: inline-block;
width: calc(50% - #{$grid-unit-05});

&-text {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd prefer this to be written out (.block-editor-block-styles__item-text) so it can be searched easily across the codebase.

Also, no need to nest it this deep since the class will never appear anywhere else, and it just adds unnecessary specificity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mirka
Thanks for the review.

I've adjusted the .block-editor-block-styles__item-text class to make it easier to isolate and search.

How about something like this?

word-break: break-all;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, I looked into this a little further and it's pretty interesting. This might be something we need to bake into Truncate. Maybe word-break: break-all when numberOfLines is 1, and overflow-wrap: break-word when it's multiple lines. I'll keep track of this as a separate issue.

// The Button component is white-space: nowrap, and that won't work with line-clamp.
white-space: normal;

// Without this, the ellipsis can sometimes be partially hidden by the Button padding.
text-align: start;
text-align-last: center;
}

&:focus,
&:hover {
color: var(--wp-admin-theme-color);
Expand Down