dino-trunk

dino-trunk logo โ€” a dinosaur with an elephant trunk

dino-trunk

Dynamic (dino) truncation (trunk) for React button labels. ๐Ÿฆ•

CI npm MIT

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   โ–บ  [๐Ÿ“„]

โ–ถ Live demo

Install

npm install dino-trunk

Use it

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.

Why not CSS 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.

More

Development

npm 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

License

MIT ยฉ Ooha Reddy