Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/sandbox/scripts/next-export-routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@
"/templates/useToastHookUsage/",
"/templates/useTranslatorHookUsage/",
"/templates/useTreeFocusHookUsage/",
"/templates/useTruncationHookUsage/",
"/templates/useTypeaheadHookUsage/",
"/templates/work-item-detail/",
"/themes/",
Expand Down
10 changes: 10 additions & 0 deletions internal/shadcn-registry/routes.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -3941,6 +3941,11 @@
"path": "examples/use-tree-focus/keyboard-file-tree",
"aliases": []
},
{
"name": "example-use-truncation-tooltip-on-cut-off-text",
"path": "examples/use-truncation/tooltip-on-cut-off-text",
"aliases": []
},
{
"name": "example-use-typeahead-type-to-focus-a-country",
"path": "examples/use-typeahead/type-to-focus-a-country",
Expand Down Expand Up @@ -4331,6 +4336,11 @@
"path": "hooks/use-tree-focus",
"aliases": []
},
{
"name": "hook-use-truncation",
"path": "hooks/use-truncation",
"aliases": []
},
{
"name": "hook-use-typeahead",
"path": "hooks/use-typeahead",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,5 +303,5 @@ useTableSortableState useTableSortableState Table/plugins/sortable/useTableSorta
useToastGesture useToastGesture Toast/Toast.doc.mjs Toast/useToastGesture.ts
useTouchTrigger useTouchTrigger Layer/Layer.doc.mjs Layer/useTouchTrigger.ts
useTriggerMenu useTriggerMenu Chat/Chat.doc.mjs Chat/useTriggerMenu.tsx
useTruncation useTruncation Text/Heading.doc.mjs Text/useTruncation.ts
useTruncation useTruncation Text/useTruncation.doc.mjs Text/useTruncation.ts
utils utils Calendar/Calendar.doc.mjs Calendar/utils.ts
122 changes: 122 additions & 0 deletions packages/cli/api/template/use-truncation-hook-usage.test.tsx
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);
Comment thread
astracat-bot[bot] marked this conversation as resolved.
Resolved

// 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);
}
});
});
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'],
};
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}>
Comment thread
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>
);
}
Loading