npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@dynamic-middle-ellipsis/react

v1.0.2

Published

React components for dynamic text truncation with middle ellipsis

Readme

@dynamic-middle-ellipsis/react

NPM GitHub Repo stars

React component to dynamically truncate long text in the middle. Preserves the most important parts of your text with smart, responsive, and pixel-perfect text truncation.

🔗 Live Demo

demo

Features

  • 🚀 Smart Truncation: Truncates in the middle, preserving important start/end content
  • 📱 Responsive: Automatically adapts to container width changes
  • 🎯 Precise: Font-aware calculations prevent over/under truncation
  • 🔧 Complex Layouts: Handles nested containers, parent element without width, shared siblings, padding, margins, etc.
  • 📝 Multi-line Support: Wraps to multiple lines before truncating
  • Performance: O(log(n)) width calculations and no re-renders
  • 🎨 Customizable: Custom ellipsis symbols and line limits
  • 🏗️ TypeScript: Full TypeScript support with proper types
  • ⚛️ React Optimized: Hooks-based with proper cleanup and context

Installation

npm install @dynamic-middle-ellipsis/react

Quick Start

import createMiddleEllipsis from "@dynamic-middle-ellipsis/react";

const MiddleEllipsis = createMiddleEllipsis();

function App() {
  return (
    <MiddleEllipsis.Span>
      This text will truncate in the middle when space is limited
    </MiddleEllipsis.Span>
  );
}

Examples

Basic Usage

<MiddleEllipsis.Span>
  Very long file name that needs truncation.pdf
</MiddleEllipsis.Span>
// Result: "Very long file na...cation.pdf"

Custom Ellipsis Symbol

<MiddleEllipsis.Span ellipsisSymbol="[---]">
  Very long file name that needs truncation.pdf
</MiddleEllipsis.Span>
// Result: "Very long file n[---]tion.pdf"

Multi-line Support

<MiddleEllipsis.Span lineLimit={2}>
  This text will wrap to 3 lines before truncating in the middle. You can customize however you like.
</MiddleEllipsis.Span>
// Result: 
// This text will wrap to 3 
// lines bef...dle. You can 
// customize however you like.

Multiple Elements Sharing Space

<MiddleEllipsis.BoundingDiv>
  <MiddleEllipsis.Span>
    First long text that needs truncation
  </MiddleEllipsis.Span>
  <MiddleEllipsis.Span>
    Second long text that also needs truncation
  </MiddleEllipsis.Span>
</MiddleEllipsis.BoundingDiv>
// Result: "First...tion"  "Second...tion"

API Reference

createMiddleEllipsis(config?)

Creates a MiddleEllipsis component factory with optional configuration.

Parameters:

  • config.customFontWidthMap?: FontWidthMap - Custom font family mapping for precise calculations.

Returns: MiddleEllipsis component with Span and BoundingDiv properties.

MiddleEllipsis.Span

React component for truncating text content.

Props:

  • children: string - Text content to truncate (required)
  • ellipsisSymbol?: string - Custom ellipsis symbol (default: "...")
  • lineLimit?: number - Maximum lines before truncation (default: 1)
  • ...rest - All standard HTML span props (className, style, onClick, etc.)

MiddleEllipsis.BoundingDiv

Container component for multiple truncating elements that share available space.

Props:

  • children: ReactNode[] - Child elements (MiddleEllipsis.Span components)
  • ...rest - All standard HTML div props

Advanced Usage

Precise Font Width Mapping

For pixel-perfect truncation across different browsers and fonts, you need to generate font width mapping for all the font-family in your website.

  1. Create custom-font-family-map.ts file:
import type { FontWidthMap } from "@lalit-rana/dynamic-middle-ellipsis";

const chromeFontWidthMap: FontWidthMap = {};
const firefoxFontWidthMap: FontWidthMap = {};

export const customFontWidthMap: FontWidthMap = {
  ...chrome,
  ...firefox,
};
  1. Open your website in chrome.
  2. Copy everything from generate-font-width-mapping and paste it in the browser console.
    • This'll generate character widths mapping for all font-families in your project.
  3. Copy the return object from the console and paste it against chromeFontWidthMap in custom-font-family-map.ts.
  4. Repeat for Firefox.
  5. Pass customFontWidthMap to createMiddleEllipsis:
import createMiddleEllipsis from "@dynamic-middle-ellipsis/react";
import { customFontWidthMap } from "./custom-font-width-map";

const MiddleEllipsis = createMiddleEllipsis({
  customFontWidthMap,
});

Performance Considerations

  • Uses ResizeObserver for efficient resize detection
  • Minimal re-renders - text updates happen directly in DOM
  • O(log(n)) complexity for parent/ancestor div width calculations where n is number of nodes in subtree
  • Automatic cleanup on component unmount
  • Shared calculations when using BoundingDiv

Technology Support

  • React 16.8+ (hooks support)
  • Modern browsers with ResizeObserver
  • TypeScript 4.0+ (for TypeScript users)

Related Packages

License

MIT © Lalit Rana