m-truncate
Version:
`mTruncate` is a **lightweight, flexible, and fully standalone Angular directive** that truncates text dynamically and optionally displays a **tooltip** with the full text. It supports **character-based**, **line-based**, and **width-based** truncation
177 lines (124 loc) โข 6.59 kB
Markdown
# ๐งฉ mTruncate โ Angular Text Truncation Directive
`mTruncate` is a **lightweight, flexible, and fully standalone Angular directive** that truncates text dynamically and optionally displays a **tooltip** with the full text.
It supports **character-based**, **line-based**, and **width-based** truncation, while ensuring excellent performance and responsiveness.
## ๐ Features
โ
**Dynamic Truncation** โ Truncate text by character count, width, or number of lines.
๐ฌ **Tooltip Support** โ Displays the full text when hovered.
โ๏ธ **Customizable** โ Control tooltip color, direction, position, and suffix.
โก **Performance Optimized** โ Uses event debouncing and efficient DOM updates.
๐ฑ **Responsive** โ Automatically adjusts on window resize.
๐ **RTL/LTR Support** โ Seamless for Arabic and multilingual applications.
๐ง **Standalone Directive** โ No need for NgModules; works in any Angular component.
## ๐ฆ Installation
Install via **npm**:
```bash
npm install m-truncate
```
or via **yarn**:
```bash
yarn add m-truncate
```
## ๐ง Quick Start
### 1. Import the Directive
Since `mTruncate` is a **standalone directive**, simply import it into your component:
```typescript
import { MTruncateDirective } from "m-truncate";
```
### 2. Apply It in a Component
```typescript
import { Component } from "@angular/core";
import { MTruncateDirective } from "m-truncate";
@Component({
selector: "app-example",
standalone: true,
imports: [MTruncateDirective],
template: ` <div mTruncate>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus lacinia.</div> `,
})
export class ExampleComponent {}
```
## ๐ก Usage Examples
### ๐น Automatic / Dynamic Truncation
```html
<p mTruncate>This text will be truncated if it exceeds the element width.</p>
```
### ๐น Truncate by Characters
```html
<p mTruncate [truncateChar]="40">This text will be truncated after 40 characters.</p>
```
### ๐น Truncate by Number of Lines
```html
<div mTruncate [truncateLines]="2">This paragraph will be truncated after two lines of text.</div>
```
### ๐น Truncate by Element Width
```html
<span mTruncate [maxTruncateWidth]="120"> This text will be truncated if it exceeds 120px in width. </span>
```
### ๐น Tooltip Position
```html
<p mTruncate [truncateChar]="30" [tooltipPosition]="'bottom'">This tooltip appears below the text.</p>
```
| Input Property | Type | Default | Description |
| ------------------ | --------- | ----------- | ----------------------------------------------------------------------------------------------------------------------- |
| `truncateChar` | `number` | `0` | Maximum number of characters before truncation occurs. |
| `truncateLines` | `number` | `0` | Number of lines before truncation. |
| `maxTruncateWidth` | `number` | `0` | Maximum width (in pixels) before truncation is applied. |
| `maxTooltipWidth` | `number` | `0` | Maximum width for tooltip. |
| `showTooltip` | `boolean` | `true` | Show tooltip on hover. |
| `tooltipPosition` | `string` | `'top'` | Tooltip position (`top`, `bottom`, `left`, or `right`). |
| `tooltipDirection` | `string` | `'ltr'` | Tooltip text direction (`ltr` or `rtl`). |
| `bgColor` | `string` | `'#fff'` | Tooltip background color. |
| `color` | `string` | `'#1c1c28'` | Tooltip text color. |
| `id` | `string` | `'mId'` | ID for the tooltip element. |
| `truncateSuffix` | `string` | `'...'` | Suffix appended when text is truncated. |
| `resize` | `boolean` | `false` | If `true`, the directive automatically re-evaluates truncation when the window is resized. |
| `textChange` | `string` | `''` | When this input value changes, the directive re-checks and re-applies truncation (useful for dynamically updated text). |
## ๐จ Styling
You can customize the tooltip globally using CSS:
```css
.mTooltip {
font-family: "Arial", sans-serif;
font-size: 13px;
padding: 8px 10px;
border-radius: 8px;
background: #1c1c28;
color: #fff;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.25);
transition: opacity 0.2s ease;
}
```
## ๐ Direction Support (LTR / RTL)
If your application supports **multiple languages** or you want to **set tooltip direction globally**, you can use the provided service.
### Example: Set Global Tooltip Direction
```typescript
import { Component } from "@angular/core";
import { MTruncateService } from "m-truncate";
@Component({
selector: "app-root",
templateUrl: "./app.component.html",
styleUrl: "./app.component.scss",
})
export class AppComponent {
constructor(private mTruncateService: MTruncateService) {}
ngOnInit() {
this.mTruncateService.setDirection("rtl"); // Global RTL support
}
}
```
## ๐งพ License
MIT License ยฉ 2025 [Mo'men Waled](https://github.com/MomenWalid)
## ๐ฌ Contact
If you have questions, feedback, or would like to contribute:
- ๐ง **Email:** [momenwaled60@gmail.com](mailto:momenwaled60@gmail.com)
- ๐ **GitHub:** [MomenWalid](https://github.com/MomenWalid)
- ๐ **Portfolio:** [https://momenwalid.github.io/Portfolio/](https://momenwalid.github.io/Portfolio/)
> Made with โค๏ธ by **Mo'men Waled**