-
Notifications
You must be signed in to change notification settings - Fork 235
docs(sp-truncated): updated sp-truncated pattern #5839
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
base: main
Are you sure you want to change the base?
Changes from all commits
78be3f6
69e086b
1fa7876
b1e52d9
ddb5353
a1357d1
096681d
caefec6
0b6acbc
018f3a8
b93fcf4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,246 @@ | ||
| ## Description | ||
| ## Overview | ||
|
|
||
| `<sp-truncated>` renders a line of text, truncating it if it overflows its container. When overflowing, a tooltip is automatically created | ||
| that renders the entire non-truncated content. | ||
| `<sp-truncated>` renders a line of text, truncating it if it overflows its container. When overflowing, a tooltip is automatically created that renders the entire non-truncated content. Additionally, clicking on overflowing text copies the full content to the clipboard and displays a success message. | ||
|
|
||
| It is used like a `<span>` to contain potentially-long strings that are important for users to see, even when in small containers, like full | ||
| names and email addresses. | ||
| This component is designed like a `<span>` to contain potentially-long strings that are important for users to see, even when in small containers, such as full names, email addresses, file paths, and other text data that may exceed the available space. | ||
|
|
||
| ### Usage | ||
|
|
||
| [](https://www.npmjs.com/package/@spectrum-web-components/truncated) | ||
| [](https://bundlephobia.com/result?p=@spectrum-web-components/truncated) | ||
|
|
||
| ``` | ||
| ```bash | ||
| yarn add @spectrum-web-components/truncated | ||
| ``` | ||
|
|
||
| Import the side effectful registration of `<sp-truncated>` via: | ||
| Import the side effectful registration of `<sp-truncated>` as follows: | ||
|
|
||
| ``` | ||
| ```typescript | ||
| import '@spectrum-web-components/truncated/sp-truncated.js'; | ||
| ``` | ||
|
|
||
| When looking to leverage the `Truncated` base class as a type and/or for extension purposes, do so via: | ||
|
|
||
| ``` | ||
| ```typescript | ||
| import { Truncated } from '@spectrum-web-components/truncated'; | ||
| ``` | ||
|
|
||
| ## Example | ||
| ### Anatomy | ||
|
|
||
| The basic structure of the truncated component: | ||
|
|
||
| ```html | ||
| <sp-truncated> | ||
| This will truncate into a tooltip if there isn't enough space for it. | ||
| </sp-truncated> | ||
| ``` | ||
|
|
||
| ### With specific overflow content | ||
| ### Examples | ||
|
|
||
| #### Basic usage | ||
|
|
||
| By default, `<sp-truncated>` will automatically detect when text overflows its container and display a tooltip with the full content on hover. When the truncated text is clicked, it copies the full text to the clipboard. | ||
|
|
||
| ```html demo | ||
| <div | ||
| style="width: 200px; border: 1px solid var(--spectrum-gray-300); padding: 8px; overflow: hidden; resize: both;" | ||
| > | ||
| <sp-truncated> | ||
| This is a very long sentence that should be truncated when there isn't | ||
| enough space to display it fully. | ||
| </sp-truncated> | ||
| </div> | ||
| ``` | ||
|
|
||
| #### With mixed content | ||
|
|
||
| `<sp-truncated>` supports mixed content including text formatting and inline elements: | ||
|
|
||
| ```html demo | ||
| <div | ||
| style="width: 250px; border: 1px solid var(--spectrum-gray-300); padding: 8px;" | ||
| > | ||
| <sp-truncated> | ||
| This is a | ||
| <strong>very long</strong> | ||
| sentence with | ||
| <em>formatted text</em> | ||
| that should be truncated. | ||
| </sp-truncated> | ||
| </div> | ||
| ``` | ||
|
|
||
| #### Long words without spaces | ||
|
|
||
| The component handles long words or strings without spaces appropriately: | ||
|
|
||
| ```html demo | ||
| <div | ||
| style="width: 150px; border: 1px solid var(--spectrum-gray-300); padding: 8px;" | ||
| > | ||
| <sp-truncated> | ||
| ThisIsAVeryLongWordWithoutAnySpacesThatShouldBeTruncated | ||
| </sp-truncated> | ||
|
Comment on lines
+81
to
+83
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example isn't working on the docs page as the tooltip does not show the entire content.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be fixed with this PR |
||
| </div> | ||
| ``` | ||
|
|
||
| #### Custom overflow content | ||
|
|
||
| By default, tooltip text will be extracted from the overflowing content. To provide your own custom overflow content for the tooltip, slot it into the `overflow` slot: | ||
|
|
||
| ```html demo | ||
| <div | ||
| style="width: 200px; border: 1px solid var(--spectrum-gray-300); padding: 8px;" | ||
| > | ||
| <sp-truncated> | ||
| This is the inline content that gets truncated | ||
| <span slot="overflow"> | ||
| This custom overflow content will appear in the tooltip with any | ||
| additional information or formatting you need. | ||
| </span> | ||
| </sp-truncated> | ||
|
Comment on lines
+95
to
+101
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed in the examples that only Should everything before the overflow be displayed, i.e If the latter is true, we should warn consumers that setting the overflow might cause some words to be neither visible in the truncated line nor the tooltip.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why would we even include content in an overflowed slot that isnt the actual text? im needing a use case to understand how and when this should be used in product. its feeling like a dark UX pattern.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be fixed with this PR. There was a bug with the tooltip. |
||
| </div> | ||
| ``` | ||
|
|
||
| #### Tooltip placement | ||
|
|
||
| You can control the placement of the tooltip using the `placement` attribute. The default placement is `top-start`. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| ```html demo | ||
| <div style="display: flex; gap: 16px; flex-wrap: wrap;"> | ||
| <div | ||
| style="width: 150px; border: 1px solid var(--spectrum-gray-300); padding: 8px;" | ||
| > | ||
| <sp-truncated placement="top"> | ||
| Top placement for this truncated text | ||
| </sp-truncated> | ||
| </div> | ||
| <div | ||
| style="width: 150px; border: 1px solid var(--spectrum-gray-300); padding: 8px;" | ||
| > | ||
| <sp-truncated placement="bottom"> | ||
| Bottom placement for this truncated text | ||
| </sp-truncated> | ||
| </div> | ||
| <div | ||
| style="width: 150px; border: 1px solid var(--spectrum-gray-300); padding: 8px;" | ||
| > | ||
| <sp-truncated placement="left"> | ||
| Left placement for this truncated text | ||
| </sp-truncated> | ||
| </div> | ||
| <div | ||
| style="width: 150px; border: 1px solid var(--spectrum-gray-300); padding: 8px;" | ||
| > | ||
| <sp-truncated placement="right"> | ||
| Right placement for this truncated text | ||
| </sp-truncated> | ||
| </div> | ||
| </div> | ||
| ``` | ||
|
|
||
| #### Custom success message | ||
|
|
||
| When text is copied to the clipboard, a custom success message can be displayed: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried copying the text, and while I got the success message, when I pasted I noticed the copy wasn't the full text. I didn't realize that the act of clicking the div would copy the whole thing.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again this feels like a dark pattern, does this mean that anything wrapped in a truncated is keyboard navigable? how would this functionality work for now mouse users? what visual queue are we providing that its copyable? |
||
|
|
||
| ```html demo | ||
| <div | ||
| style="width: 200px; border: 1px solid var(--spectrum-gray-300); padding: 8px;" | ||
| > | ||
| <sp-truncated success-message="Email copied successfully!"> | ||
| iamaverylongusernametoshowthecustom.message.text@example.com | ||
| </sp-truncated> | ||
| </div> | ||
| ``` | ||
|
|
||
| ### Usage in patterns | ||
|
|
||
| #### Displaying form values | ||
|
|
||
| By default, tooltip text will be extracted from overflowing content. To provide your own overflow content, slot it into "overflow": | ||
| `<sp-truncated>` is commonly used to display long values that might overflow, such as saved form data: | ||
|
|
||
| ```html demo | ||
| <div style="width: 250px;"> | ||
| <p | ||
| style="margin: 0 0 8px 0; font-size: 12px; color: var(--spectrum-gray-700);" | ||
| > | ||
| Email address | ||
| </p> | ||
| <div | ||
| style="border: 1px solid var(--spectrum-gray-300); padding: 8px; border-radius: 4px;" | ||
| > | ||
| <sp-truncated> | ||
| very.long.email.address@subdomain.example.com | ||
| </sp-truncated> | ||
| </div> | ||
| </div> | ||
| ``` | ||
|
|
||
| #### In popovers | ||
|
|
||
| For use within overlays, ensure the popover has appropriate width constraints: | ||
|
|
||
| ```html | ||
| <sp-truncated placement="right"> | ||
| This is the inline content | ||
| <span slot="overflow"> | ||
| And this overflow content will go into the tooltip, on the right | ||
| </span> | ||
| </sp-truncated> | ||
| <sp-button id="user-trigger">User Info</sp-button> | ||
| <sp-overlay trigger="user-trigger@click" placement="bottom"> | ||
| <sp-popover style="width: 250px;"> | ||
| <div style="padding: 16px;"> | ||
| <sp-field-label>Username</sp-field-label> | ||
| <div style="width: 200px;"> | ||
| <sp-truncated> | ||
| very.long.username.that.exceeds.the.available.width | ||
| </sp-truncated> | ||
|
Comment on lines
+190
to
+192
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tooltip here is also not displaying the full content.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be fixed with this PR |
||
| </div> | ||
| </div> | ||
| </sp-popover> | ||
| </sp-overlay> | ||
| ``` | ||
|
|
||
| ### Accessibility | ||
|
|
||
| #### Keyboard navigation | ||
|
|
||
| The `<sp-truncated>` component does not receive keyboard focus by itself, as it functions as inline content. The tooltip only appears on mouse hover, not on keyboard focus. This means keyboard-only users (who are not using screen readers) cannot access the tooltip to view the full content. | ||
|
|
||
| **Important accessibility limitation:** Since `<sp-truncated>` does not receive keyboard focus, keyboard-only users cannot trigger the tooltip hover interaction. Additionally, the click handler does not include keyboard event handlers (`/* eslint-disable lit-a11y/click-events-have-key-events */`), so keyboard users cannot copy the full content via keyboard alone. This is a known accessibility limitation. | ||
|
|
||
| #### Click to copy | ||
|
|
||
| When truncated text is clicked, the full text content is copied to the clipboard. This interaction provides a quick way for mouse users to access the complete content. Keyboard users cannot trigger this interaction. | ||
|
|
||
| #### Screen readers | ||
|
|
||
| Screen readers will announce the full text content directly from the DOM, so screen reader users do not need the tooltip to access the complete content. Note that screen reader users are often but not always keyboard users, and keyboard-only users who are not using screen readers will not be able to access the tooltip content. | ||
|
|
||
| For custom overflow content, ensure that the slotted content is semantically meaningful and properly structured for screen readers. | ||
|
|
||
| ### Content guidelines | ||
|
|
||
| #### Do | ||
|
|
||
| - Use `<sp-truncated>` for non-critical text that users may need to see in full but can afford to be shortened in compact views | ||
| - Provide meaningful text that makes sense even when truncated | ||
| - Use for email addresses, file paths, long names, and similar data | ||
| - Test with actual content to ensure the truncation point makes sense | ||
|
|
||
| #### Don't | ||
|
|
||
| - Don't use for critical actions or navigation labels that must always be visible | ||
| - Don't use for very short text that is unlikely to overflow | ||
| - Don't truncate text where the beginning or end context is essential for comprehension | ||
| - Don't rely solely on truncated text for important information without providing alternative access | ||
|
|
||
| ### Additional resources | ||
|
|
||
| For more information on accessibility best practices for truncated text, refer to: | ||
|
|
||
| - [Spectrum Design System - Text Overflow Guidelines](https://spectrum.adobe.com/page/text-field/#Text-overflow) | ||
| - [WCAG 2.1 Success Criterion 1.4.8 - Visual Presentation](https://www.w3.org/WAI/WCAG21/Understanding/visual-presentation.html) | ||
|
|
||
| ### Design reference | ||
|
|
||
| For design specifications and usage guidelines, refer to the Spectrum Design System documentation: | ||
|
|
||
| - [Spectrum Text Field Component - Text Overflow](https://spectrum.adobe.com/page/text-field/#Text-overflow) | ||
|
|
||
| **Note:** While `<sp-truncated>` is not a standalone component in the official Spectrum Design System, it implements common text truncation patterns used throughout Spectrum components. | ||

Uh oh!
There was an error while loading. Please reload this page.