feat: Add TruncatedText component#4543
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
| export default function TruncatedTextSimple() { | ||
| return ( | ||
| <ScreenshotArea> | ||
| <h1>TruncatedText examples</h1> |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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"> |
| export { TruncatedTextProps }; | ||
|
|
||
| export default function TruncatedText({ children, tooltipText, ...rest }: TruncatedTextProps) { | ||
| const baseComponentProps = useBaseComponent('TruncatedText'); |
There was a problem hiding this comment.
Any props or metadata we want to track? For example whether tooltipText is set?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Is there any alternative where arbitrary timeouts are not needed, such as requestAnimationFrame? We use requestAnimationFrame in 36 other places.
There was a problem hiding this comment.
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"> |
There was a problem hiding this comment.
Note: permutations make the screenshots more isolated, and handle future additions more gracefully
| @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); |
There was a problem hiding this comment.
Is there any design token we can use, maybe $border-radius-control-default-focus-ring?
There was a problem hiding this comment.
Looks like $box-shadow-focused-width fits the bill.
| max-inline-size: 100%; | ||
| min-inline-size: 0; | ||
|
|
||
| &:focus { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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...
275de6a to
876cd0b
Compare

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
CONTRIBUTING.md.CONTRIBUTING.md.Security
checkSafeUrlfunction.Testing
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.