Dynamic (dino) truncation (trunk) for React button labels. ๐ฆ
A buttonโs label steps down through shorter variants as space runs out โ all the way to icon-only โ and steps back up when room returns, without flickering at the boundary.
wide โบ [๐ Download Profile (PDF)]
medium โบ [๐ Download Profile]
narrow โบ [๐ Profile]
tiny โบ [๐]
npm install dino-trunk
import { useRef } from 'react';
import { useCollapsingLabel } from 'dino-trunk';
function SaveButton() {
const ref = useRef<HTMLButtonElement>(null);
const label = useCollapsingLabel(ref, ['Save changes', 'Save', '']);
return (
<button ref={ref}>
<SaveIcon />
{label && <span>{label}</span>}
</button>
);
}
Pass checkpoints widest โ narrowest, end with '' for icon-only. Thatโs the
whole API โ no container ref, no config, no index math. The buttonโs parent
is measured automatically.
text-overflow: ellipsis?That clips one string mid-word. This picks between whole alternative labels based on real measured text width, so โDownload Profile (PDF)โ becomes โProfileโ becomes an icon โ each a deliberate choice. Shrink/grow use different thresholds (hysteresis), so a label parked near the boundary doesnโt flicker.
useCollapsingLabelspickLevel, availableWidthPerItemellipsizedocs/API.mdnpm install
npm test # unit tests for the pure core (vitest)
npm run typecheck
npm run build # emits dist/ (ESM + .d.ts) via tsc โ no bundler
MIT ยฉ Ooha Reddy