Skip to content

Commit 155a410

Browse files
Rmnllyjuzser
authored andcommitted
[Navigation] Add a new prop to truncate nav item text (Shopify#7619)
<!-- ☝️How to write a good PR title: - Prefix it with [ComponentName] (if applicable), for example: [Button] - Start with a verb, for example: Add, Delete, Improve, Fix… - Give as much context as necessary and as little as possible - Prefix it with [WIP] while it’s a work in progress --> ### WHY are these changes introduced? Overwrites have been used in admin to get navigation item text to truncate with ellipsis since the app pinning project. This is an effort to port the changes to Polaris to reduce the overwriting and tech debt in core. Fixes https://github.com/Shopify/core-workflows/issues/561 <!-- link to issue if one exists --> <!-- Context about the problem that’s being addressed. --> ### WHAT is this pull request doing? I've added a prop to allow text truncation in navigation items. It applies a class that stops the line wrapping of item text and instead does an ellipsis truncation through CSS. <!-- Summary of the changes committed. Before / after screenshots are appreciated for UI changes. Make sure to include alt text that describes the screenshot. If you include an animated gif showing your change, wrapping it in a details tag is recommended. Gifs usually autoplay, which can cause accessibility issues for people reviewing your PR: <details> <summary>Summary of your gif(s)</summary> <img src="..." alt="Description of what the gif shows"> </details> --> <img width="275" alt="Screen Shot 2022-11-04 at 12 20 29 PM" src="https://user-images.githubusercontent.com/33904740/200025372-15995417-9fda-468f-9ab0-75d1a24b8c89.png"> There is a new story for the truncation enabled items. Here you can see anything going beyond the width of the text container will be truncated with ellipsis. Notice that "Inventory" is fine as it not long enough to trigger it. Secondary navigation items are also treated the same as their parent. <!-- ℹ️ Delete the following for small / trivial changes --> ### How to 🎩 🖥 [Local development instructions](https://github.com/Shopify/polaris/blob/main/README.md#local-development) 🗒 [General tophatting guidelines](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting.md) 📄 [Changelog guidelines](https://github.com/Shopify/polaris/blob/main/.github/CONTRIBUTING.md#changelog) <!-- Give as much information as needed to experiment with the component in the playground. --> <details> <summary>Copy-paste this code in <code>playground/Playground.tsx</code>:</summary> ```jsx import React from 'react'; import {Page} from '../src'; export function Playground() { return ( <Page title="Playground"> {/* Add the code you want to test in here */} </Page> ); } ``` </details> ### 🎩 checklist - [x] Tested on [mobile](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting.md#cross-browser-testing) - [x] Tested on [multiple browsers](https://help.shopify.com/en/manual/shopify-admin/supported-browsers) - [x] Tested for [accessibility](https://github.com/Shopify/polaris/blob/main/documentation/Accessibility%20testing.md) - [ ] Updated the component's `README.md` with documentation changes - [ ] [Tophatted documentation](https://github.com/Shopify/polaris/blob/main/documentation/Tophatting%20documentation.md) changes in the style guide
1 parent 6910beb commit 155a410

File tree

5 files changed

+108
-3
lines changed

5 files changed

+108
-3
lines changed

.changeset/tidy-waves-live.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/polaris': minor
3+
---
4+
5+
Added `truncateText` prop to `Navigation.Item` to prevent text wrapping

polaris-react/src/components/Navigation/Navigation.scss

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ $disabled-fade: 0.6;
9292
.Item {
9393
@include nav-item-attributes;
9494
position: relative;
95+
96+
margin-inline-start: var(--p-space-2);
97+
98+
&:last-child {
99+
margin-inline-end: var(--p-space-2);
100+
}
95101
}
96102

97103
.Item-selected {
@@ -213,13 +219,22 @@ $disabled-fade: 0.6;
213219
}
214220
}
215221

222+
.Text-truncated {
223+
// stylelint-disable-next-line value-no-vendor-prefix
224+
display: -webkit-box;
225+
overflow: hidden;
226+
-webkit-line-clamp: 1;
227+
-webkit-box-orient: vertical;
228+
word-break: break-all;
229+
}
230+
216231
.SecondaryAction {
217232
@include recolor-icon($fill-color: var(--p-icon));
218233
display: flex;
219234
align-items: center;
220235
height: nav(mobile-height);
221-
margin-right: var(--p-space-1);
222236
padding: var(--p-space-1) var(--p-space-4);
237+
margin-right: var(--p-space-1);
223238
border-radius: var(--p-border-radius-1);
224239

225240
@media #{$p-breakpoints-md-up} {

polaris-react/src/components/Navigation/Navigation.stories.tsx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,83 @@ export function WithASecondaryActionForAnItem() {
249249
);
250250
}
251251

252+
export function WithTruncationForVariousStates() {
253+
return (
254+
<Frame>
255+
<Navigation location="/">
256+
<Navigation.Section
257+
items={[
258+
{
259+
url: '/path/to/place',
260+
label: 'A very long label to ellipsize',
261+
truncateText: true,
262+
icon: OrdersMinor,
263+
selected: false,
264+
},
265+
{
266+
url: '/path/to/place',
267+
label: 'Lengthy label with secondary action',
268+
icon: OrdersMinor,
269+
selected: false,
270+
truncateText: true,
271+
secondaryAction: {
272+
url: '/admin/orders/add',
273+
accessibilityLabel: 'Add an order',
274+
icon: CirclePlusOutlineMinor,
275+
tooltip: {
276+
content: 'Add a lengthy order',
277+
},
278+
},
279+
},
280+
{
281+
url: '/path/to/place',
282+
label: 'Lengthy label with badge',
283+
truncateText: true,
284+
badge: 'Old',
285+
icon: HomeMinor,
286+
},
287+
{
288+
url: '/path/to/place',
289+
label: 'Lengthy label with secondary action',
290+
icon: OrdersMinor,
291+
selected: false,
292+
truncateText: true,
293+
badge: 'Old',
294+
secondaryAction: {
295+
url: '/admin/orders/add',
296+
accessibilityLabel: 'Add an order',
297+
icon: CirclePlusOutlineMinor,
298+
tooltip: {
299+
content: 'Add a lengthy order',
300+
},
301+
},
302+
},
303+
{
304+
url: '/admin/products',
305+
label: 'Truncated secondary navigation items',
306+
icon: ProductsMinor,
307+
selected: true,
308+
truncateText: true,
309+
subNavigationItems: [
310+
{
311+
url: '/admin/products/collections',
312+
disabled: false,
313+
label: 'Something longer than collections',
314+
},
315+
{
316+
url: '/admin/products/inventory',
317+
disabled: false,
318+
label: 'Inventoy',
319+
},
320+
],
321+
},
322+
]}
323+
/>
324+
</Navigation>
325+
</Frame>
326+
);
327+
}
328+
252329
export function WithSectionRollup() {
253330
return (
254331
<Frame>

polaris-react/src/components/Navigation/_variables.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ $nav-animation-variables: (
2828
align-items: flex-start;
2929
max-width: 100%;
3030
padding: 0 var(--p-space-1) 0 var(--p-space-3);
31-
margin: 0 var(--p-space-2);
31+
margin: 0;
3232
border-radius: var(--p-border-radius-1);
3333
color: var(--p-text);
3434
text-decoration: none;

polaris-react/src/components/Navigation/components/Item/Item.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export interface ItemProps extends ItemURLDetails {
6262
onToggleExpandedState?(): void;
6363
expanded?: boolean;
6464
shouldResizeIcon?: boolean;
65+
truncateText?: boolean;
6566
}
6667

6768
enum MatchState {
@@ -92,6 +93,7 @@ export function Item({
9293
onToggleExpandedState,
9394
expanded,
9495
shouldResizeIcon,
96+
truncateText,
9597
}: ItemProps) {
9698
const i18n = useI18n();
9799
const {isNavigationCollapsed} = useMediaQuery();
@@ -162,7 +164,12 @@ export function Item({
162164
const itemContentMarkup = (
163165
<>
164166
{iconMarkup}
165-
<span className={styles.Text}>
167+
<span
168+
className={classNames(
169+
styles.Text,
170+
truncateText && styles['Text-truncated'],
171+
)}
172+
>
166173
{label}
167174
{indicatorMarkup}
168175
</span>
@@ -291,6 +298,7 @@ export function Item({
291298
label={label}
292299
matches={item === longestMatch}
293300
onClick={onClick}
301+
truncateText={truncateText}
294302
/>
295303
);
296304
})}

0 commit comments

Comments
 (0)