Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lalitsinghrana/dynamic-middle-ellipsis
Framework agnostic code to dynamically truncate long text in the center.
https://github.com/lalitsinghrana/dynamic-middle-ellipsis
abbreviate clip dynamic middle-ellipsis react text-overflow truncate-text vanilla-js web-components
Last synced: about 1 month ago
JSON representation
Framework agnostic code to dynamically truncate long text in the center.
- Host: GitHub
- URL: https://github.com/lalitsinghrana/dynamic-middle-ellipsis
- Owner: LalitSinghRana
- Created: 2024-02-25T11:49:16.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-03-23T11:14:58.000Z (8 months ago)
- Last Synced: 2024-10-14T02:01:44.429Z (about 1 month ago)
- Topics: abbreviate, clip, dynamic, middle-ellipsis, react, text-overflow, truncate-text, vanilla-js, web-components
- Language: TypeScript
- Homepage: https://dynamic-middle-ellipsis.vercel.app/
- Size: 175 KB
- Stars: 7
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Dynamic Middle Ellipsis
[![NPM](https://img.shields.io/npm/v/@lalit-rana/dynamic-middle-ellipsis)](https://www.npmjs.com/package/@lalit-rana/dynamic-middle-ellipsis)
[![GitHub Repo stars](https://img.shields.io/github/stars/LalitSinghRana/dynamic-middle-ellipsis)](https://github.com/LalitSinghRana/dynamic-middle-ellipsis.git)
[![Website](https://img.shields.io/website?url=https%3A%2F%2Fdynamic-middle-ellipsis.vercel.app%2F)](https://dynamic-middle-ellipsis.vercel.app/)Framework agnostic code to dynamically truncate long text in the middle.
Component wrappers provided for:
- React (TS/JS)
- Vanilla (TS/JS)
- Svelte (TS/JS) - In progress
- Solid (TS/JS) - In progress[Live demo website](https://dynamic-middle-ellipsis.vercel.app/)
![Demo gif](https://github.com/LalitSinghRana/dynamic-middle-ellipsis/assets/31415645/d0f97746-1129-44f0-8cbe-c5c10f477dd6)
---
### Features:- :ocean: **Dynamic**: Automatically truncate on element resize.
- :dart: **Precise**: Use of space, it does not over or under truncate.
- :gear: **Custom ellipsis string**: Can pass any ellipsis string.
- :zap: **Fast**:
- Calculating available width is O(h) operation,
- where "h" is height of DOM sub-tree from offset-parent to target node.
- In most cases, this will be a constant O(1) operation.
- Font character width are calculated at build time and cached in advance.
- So we don't have to do this calculation at run time.
- All calculations and updating DOM element happens in JS land.
- So that it'll not cause unnecessary re-renders and slow down your webapp.
- :white_check_mark: **Handle all use cases**, e.g.:
- Parent width is dependent on child (circular dependency).
- Can handle different font families, sizes and mixed-cases.
- Wrap to multiple lines before truncating.
- Truncating text share space with other fixed size elements.
- Multiple separate truncating element share same space.
- Accounts for padding and margin on parent element
- :no_entry: **Not a dependency**: In your project, you don't install this as a package dependency. But rather get the code/logic.
- :control_knobs: **Full control**: You get access to full code, so that you can modify it according to your project's need.
- :package: **Component wrappers** available in:
- React (TS/JS)
- Vanilla (TS/JS)
- Svelte (TS/JS) - In progress
- Solid (TS/JS) - In progress---
### Installation:
```bash
npx @lalit-rana/dynamic-middle-ellipsis
```Follow the CLI instructions to get the code for your choice of framework and language.
---
### How to use:
###### Simple usage:
React:
```jsx
import MiddleEllipsis from "@/components/MiddleEllipsis";Ellipsis text in center automatically when element resize
```
Vanilla:
```htmlEllipsis text in center automatically when element resize
```
###### For multiple truncating elements sharing same space:
React:
```jsx
import MiddleEllipsis from "@/components/MiddleEllipsis";
import { Badge } from "@/components/ui/badge";
Truncating text 1
Truncating text 2
Truncating text 3
```
Vanilla:
```html
Truncating text 1
Truncating text 2
Truncating text 3
```