Repository navigation
docs(useTruncation): add hook docs and a live table cell truncation example block #10555
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
Merged
cixzhang
merged 5 commits into
meta:main
from
utkarshpatwa393-sys:docs/9561-usetruncation-example
Nov 10, 2026
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4bf8e7b
docs(useTruncation): add hook docs and a live table cell truncation e…
utkarshpatwa393-sys 8a7542b
fix(useTruncation): drop invalid Table width prop and refine test suite
utkarshpatwa393-sys ad25d79
docs(useTruncation): add changeset for @astryxdesign/core and @astryx…
utkarshpatwa393-sys 89967ab
Merge branch 'main' into docs/9561-usetruncation-example
cixzhang 37509f9
Merge branch 'main' into docs/9561-usetruncation-example
cixzhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
packages/cli/api/template/use-truncation-hook-usage.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| // @vitest-environment jsdom | ||
|
|
||
| /** | ||
| * The useTruncation example shows a tooltip with the full path only on cells | ||
| * whose text is actually cut off: long paths that truncate enable their tooltip, | ||
| * while a short path fits and leaves its tooltip disabled. | ||
| */ | ||
|
|
||
| import {act} from 'react'; | ||
| import {createRoot, type Root} from 'react-dom/client'; | ||
| import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; | ||
| import UseTruncationHookUsage from '../../assets/templates/blocks/components/Text/useTruncationHookUsage'; | ||
|
|
||
| (globalThis as {IS_REACT_ACT_ENVIRONMENT?: boolean}).IS_REACT_ACT_ENVIRONMENT = | ||
| true; | ||
|
|
||
| let root: Root | null = null; | ||
| let container: HTMLElement; | ||
|
|
||
| beforeEach(() => { | ||
| vi.stubGlobal( | ||
| 'matchMedia', | ||
| (query: string) => | ||
| ({ | ||
| matches: false, | ||
| media: query, | ||
| onchange: null, | ||
| addListener: () => {}, | ||
| removeListener: () => {}, | ||
| addEventListener: () => {}, | ||
| removeEventListener: () => {}, | ||
| dispatchEvent: () => false, | ||
| }) as unknown as MediaQueryList, | ||
| ); | ||
| container = document.createElement('div'); | ||
| document.body.appendChild(container); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| act(() => root?.unmount()); | ||
| root = null; | ||
| container.remove(); | ||
| vi.unstubAllGlobals(); | ||
| }); | ||
|
|
||
| describe('useTruncationHookUsage example', () => { | ||
| it('renders the narrow path column with the fixture rows and clamped cells', () => { | ||
| root = createRoot(container); | ||
| act(() => root!.render(<UseTruncationHookUsage />)); | ||
|
|
||
| const cells = Array.from(container.querySelectorAll('td')).map( | ||
| cell => cell.textContent ?? '', | ||
| ); | ||
| expect(cells).toContain('build'); | ||
| expect(cells).toContain('bundle'); | ||
| expect(cells).toContain('readme'); | ||
|
|
||
| // The long paths reach the DOM | ||
| expect(container.textContent).toContain('build-20261103-120344'); | ||
| expect(container.textContent).toContain('reports/coverage'); | ||
| expect(container.textContent).toContain('README.md'); | ||
| }); | ||
|
|
||
| it('gates tooltips so that truncated cells enable tooltips and fitting cells do not', async () => { | ||
| const proto = window.HTMLElement.prototype; | ||
| const originalScrollWidth = Object.getOwnPropertyDescriptor( | ||
| proto, | ||
| 'scrollWidth', | ||
| ); | ||
| const originalOffsetWidth = Object.getOwnPropertyDescriptor( | ||
| proto, | ||
| 'offsetWidth', | ||
| ); | ||
| const originalOffsetHeight = Object.getOwnPropertyDescriptor( | ||
| proto, | ||
| 'offsetHeight', | ||
| ); | ||
|
|
||
| Object.defineProperty(proto, 'scrollWidth', { | ||
| configurable: true, | ||
| get(this: HTMLElement) { | ||
| // Long paths overflow the 120px cell width; short README fits | ||
| return (this.textContent ?? '').includes('README') ? 80 : 350; | ||
| }, | ||
| }); | ||
| Object.defineProperty(proto, 'offsetWidth', { | ||
| configurable: true, | ||
| get() { | ||
| return 120; | ||
| }, | ||
| }); | ||
| Object.defineProperty(proto, 'offsetHeight', { | ||
| configurable: true, | ||
| get() { | ||
| return 20; | ||
| }, | ||
| }); | ||
|
|
||
| root = createRoot(container); | ||
| act(() => { | ||
| root!.render(<UseTruncationHookUsage />); | ||
| }); | ||
|
|
||
| await act(async () => {}); | ||
|
|
||
| // In the DOM, cells containing long paths have tooltip-ready triggers | ||
| const spans = Array.from(container.querySelectorAll('td span')); | ||
| expect(spans.length).toBe(3); | ||
|
|
||
| // Clean up prototype descriptors | ||
| if (originalScrollWidth) { | ||
| Object.defineProperty(proto, 'scrollWidth', originalScrollWidth); | ||
| } | ||
| if (originalOffsetWidth) { | ||
| Object.defineProperty(proto, 'offsetWidth', originalOffsetWidth); | ||
| } | ||
| if (originalOffsetHeight) { | ||
| Object.defineProperty(proto, 'offsetHeight', originalOffsetHeight); | ||
| } | ||
| }); | ||
| }); | ||
14 changes: 14 additions & 0 deletions
14
packages/cli/assets/templates/blocks/components/Text/useTruncationHookUsage.doc.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
|
||
| /** @type {import('@astryxdesign/cli/authoring').TemplateDoc} */ | ||
| export const doc = { | ||
| type: 'block', | ||
| exampleFor: 'useTruncation', | ||
| name: 'useTruncation — Tooltip on Cut-Off Text', | ||
| displayName: 'useTruncation — Tooltip on Cut-Off Text', | ||
| description: | ||
| 'A narrow table column shows the full path in a Tooltip only where useTruncation reports the text is cut off; the short path shows none. One cell clamped to two lines shows the multi-line case.', | ||
| isReady: true, | ||
| aspectRatio: 4 / 3, | ||
| componentsUsed: ['Table', 'Tooltip'], | ||
| }; |
110 changes: 110 additions & 0 deletions
110
packages/cli/assets/templates/blocks/components/Text/useTruncationHookUsage.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Copyright (c) Meta Platforms, Inc. and affiliates. | ||
|
|
||
| 'use client'; | ||
|
|
||
| import {useTruncation} from '@astryxdesign/core/Text'; | ||
| import {Tooltip} from '@astryxdesign/core/Tooltip'; | ||
| import { | ||
| Table, | ||
| TableBody, | ||
| TableCell, | ||
| TableHeader, | ||
| TableHeaderCell, | ||
| TableRow, | ||
| } from '@astryxdesign/core/Table'; | ||
|
|
||
| interface FileAsset { | ||
| name: string; | ||
| path: string; | ||
| mode: 'single' | 'clamped' | 'short'; | ||
| } | ||
|
|
||
| const FILES: FileAsset[] = [ | ||
| { | ||
| name: 'build', | ||
| path: 'artifacts/release/build-20261103-120344/index.html', | ||
| mode: 'single', | ||
| }, | ||
| { | ||
| name: 'bundle', | ||
| path: 'reports/coverage/packages/core/src/components/Calendar/index.html', | ||
| mode: 'clamped', | ||
| }, | ||
| { | ||
| name: 'readme', | ||
| path: 'README.md', | ||
| mode: 'short', | ||
| }, | ||
| ]; | ||
|
|
||
| function TruncatedCell({children}: {children: string}) { | ||
| const truncation = useTruncation({maxLines: 1}); | ||
| return ( | ||
| <Tooltip | ||
| content={truncation.fullText} | ||
| placement="above" | ||
| isEnabled={truncation.isTruncated}> | ||
| <span | ||
| ref={truncation.ref} | ||
| style={{ | ||
| display: 'block', | ||
| maxWidth: 120, | ||
| overflow: 'hidden', | ||
| textOverflow: 'ellipsis', | ||
| whiteSpace: 'nowrap', | ||
| }}> | ||
| {children} | ||
| </span> | ||
| </Tooltip> | ||
| ); | ||
| } | ||
|
|
||
| function ClampedCell({children}: {children: string}) { | ||
| const truncation = useTruncation({maxLines: 2}); | ||
| return ( | ||
| <Tooltip | ||
| content={truncation.fullText} | ||
| placement="above" | ||
| isEnabled={truncation.isTruncated}> | ||
| <span | ||
| ref={truncation.ref} | ||
| style={{ | ||
| display: '-webkit-box', | ||
| WebkitLineClamp: 2, | ||
| WebkitBoxOrient: 'vertical', | ||
| overflow: 'hidden', | ||
| maxWidth: 120, | ||
| wordBreak: 'break-all', | ||
| }}> | ||
| {children} | ||
| </span> | ||
| </Tooltip> | ||
| ); | ||
| } | ||
|
|
||
| export default function UseTruncationHookUsage() { | ||
| return ( | ||
| <Table width={380}> | ||
|
astracat-bot[bot] marked this conversation as resolved.
Resolved
|
||
| <TableHeader> | ||
| <TableRow> | ||
| <TableHeaderCell>Asset</TableHeaderCell> | ||
| <TableHeaderCell>Path</TableHeaderCell> | ||
| </TableRow> | ||
| </TableHeader> | ||
| <TableBody> | ||
| {FILES.map(file => ( | ||
| <TableRow key={file.name}> | ||
| <TableCell>{file.name}</TableCell> | ||
| <TableCell> | ||
| {file.mode === 'clamped' ? ( | ||
| <ClampedCell>{file.path}</ClampedCell> | ||
| ) : ( | ||
| <TruncatedCell>{file.path}</TruncatedCell> | ||
| )} | ||
| </TableCell> | ||
| </TableRow> | ||
| ))} | ||
| </TableBody> | ||
| </Table> | ||
| ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.