Truncate text in the middle while preserving start and end.
runed
<MiddleTruncation>
/Users/ncdai/Code/chanhdai.com/src/components/ui/button.svelte<MiddleTruncation end={4}>
FY26_Q1_Consolidated_Financial_Statements.pdf<MiddleTruncation minEnd={12}>
/Users/ncdai/Code/chanhdai.com/node_modules/shadcn/package.json<MiddleTruncation ellipsis=" ... ">
https://www.apple.com/newsroom/pdfs/fy2026-q1/FY26_Q1_Consolidated_Financial_Statements.pdfInstallation
Install dependencies
Install the required packages for this component:
Copy the Source Code
Copy and paste the following code into your project:
index.ts
middle-truncation.svelte
middle-truncation.ts
Usage
usage.svelte
Features
- Three truncation strategies: fixed end, flexible with minimum, or even split
- Pixel-accurate measurement based on actual rendered text width
- Automatically recalculates when container resizes
Examples
File names
Preserve file extensions while truncating names inside a resizable container.
building-accessible-command-menus-in-svelte.mdresponsive-component-preview.sveltemasonry-grid-layout.ts
Props
MiddleTruncation
Responsive middle truncation for a single line of text.
| Name | Type | Default | Description |
|---|---|---|---|
text | string | required | Text content to truncate. |
ellipsis | string | '...' | Custom separator shown between the preserved text segments. |
end | number | undefined | Fixed number of characters preserved at the end; cannot be combined with minEnd. |
minEnd | number | undefined | Minimum trailing characters preserved during an even split; cannot be combined with end. |
class | string | undefined | Additional classes applied to the wrapper span. |