Skip to content

feat: Add TruncatedText component#4543

Open
avinashbot wants to merge 2 commits into
mainfrom
feat/truncated-text-component
Open

feat: Add TruncatedText component#4543
avinashbot wants to merge 2 commits into
mainfrom
feat/truncated-text-component

Conversation

@avinashbot
Copy link
Copy Markdown
Member

@avinashbot avinashbot commented May 20, 2026

Description

Releases a new component, TruncatedText, based on tooltip, which handles things like truncation detection, correct
ARIA roles, and pointer and keyboard behavior.

Related links, issue #, if available: n/a

How has this been tested?

Unit tests, and a new visual test page for screenshot testing (will add pipeline test after merge)

Review checklist

The following items are to be evaluated by the author(s) and the reviewer(s).

Correctness

  • Changes include appropriate documentation updates.
  • Changes are backward-compatible if not indicated, see CONTRIBUTING.md.
  • Changes do not include unsupported browser features, see CONTRIBUTING.md.
  • Changes were manually tested for accessibility, see accessibility guidelines.

Security

Testing

  • Changes are covered with new/existing unit tests?
  • Changes are covered with new/existing integration tests?

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.43%. Comparing base (9c52a81) to head (876cd0b).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4543   +/-   ##
=======================================
  Coverage   97.42%   97.43%           
=======================================
  Files         938      941    +3     
  Lines       29680    29720   +40     
  Branches    10788    10798   +10     
=======================================
+ Hits        28917    28957   +40     
- Misses        716      756   +40     
+ Partials       47        7   -40     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@avinashbot avinashbot marked this pull request as ready for review May 21, 2026 08:09
@avinashbot avinashbot requested a review from a team as a code owner May 21, 2026 08:09
@avinashbot avinashbot requested review from a team, georgylobko and jperals and removed request for a team and georgylobko May 21, 2026 08:09
export default function TruncatedTextSimple() {
return (
<ScreenshotArea>
<h1>TruncatedText examples</h1>
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Minor: the page header should preferably be ourside the screenshot area

<Box>
<Box variant="awsui-key-label">With CopyToClipboard, internal truncation</Box>
<div style={containerStyle}>
<CopyToClipboard
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this wrapping after the icon expected because builders should instead wrap the CopyToClipboard inside TruncatedText, as in the previous element?

Image

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's not entirely expected; there's a parallel update to the component to support single-line wrapping; which has released since this PR was created. Will rebase.

<Box variant="awsui-key-label">With StatusIndicator (wrapText=true, default)</Box>
<div style={containerStyle}>
<TruncatedText>
<StatusIndicator type="success">
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it expected that there is no ellipsis shown after the truncated text?

Image

Comment thread src/truncated-text/index.tsx Outdated
export { TruncatedTextProps };

export default function TruncatedText({ children, tooltipText, ...rest }: TruncatedTextProps) {
const baseComponentProps = useBaseComponent('TruncatedText');
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Any props or metadata we want to track? For example whether tooltipText is set?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Couldn't really come up with anything. But that's a good suggestion, adding to the metadata.

if (element) {
// useResizeObserver fires initially at layoutEffect-time where the initial calculation
// is performed, but the calculation isn't always correct at that stage.
setTimeout(() => setIsTruncated(element.scrollWidth > element.clientWidth), 1);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there any alternative where arbitrary timeouts are not needed, such as requestAnimationFrame? We use requestAnimationFrame in 36 other places.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tried to use rAF, but it was still flaky. It's hard to tell at what point during rendering that wrapping/truncation happens, but it looks like every browser is a little unique in that regard, and this seemed to work the most consistently (timeout of 0 didn't work either).

return (
<ScreenshotArea>
<h1>TruncatedText examples</h1>
<SpaceBetween size="m">
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Note: permutations make the screenshots more isolated, and handle future additions more gracefully

Comment thread src/truncated-text/styles.scss Outdated
@include focus-visible.when-visible {
// The border is 2px wide, and since this component is commonly used in scenarios
// where the content can be truncated, let's play it safe by pulling in the outline.
@include styles.focus-highlight(-2px);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is there any design token we can use, maybe $border-radius-control-default-focus-ring?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks like $box-shadow-focused-width fits the bill.

max-inline-size: 100%;
min-inline-size: 0;

&:focus {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks like we need this ruleset every time we use the focus-visiblemixins. Could it integrated there instead? https://github.com/cloudscape-design/component-toolkit/blob/7794be880a5799abd7d578d7009dbfbf5cdedbba/src/internal/focus-visible/index.scss

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not a blocking comment I hope? I don't know how backward compatible this is, needs a little investigation. I know we sometimes use this to render "fake" focus rings too...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants