Vue Line Clamp 3

1.0.1 · active · verified Sun Apr 19

vue-line-clamp-3 is a Vue 3 directive designed for truncating multi-line text with an ellipsis, offering a simple, fast, and lightweight solution. It's a direct port of the original vue-line-clamp for Vue 2, ensuring similar functionality and API. The current stable version is 1.0.1, indicating an active but possibly infrequent release cadence since its initial port. A key differentiator is its 'cross-browser' strategy, utilizing `-webkit-line-clamp` for modern browsers and providing a robust JavaScript fallback that calculates `max-height` based on a specified `line-height` for older browsers. This ensures broad compatibility, though the fallback method does not display an ellipsis. The directive allows customization of `text-overflow` and even the entire fallback function through plugin options, making it adaptable to various UI requirements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install and register the `vue-line-clamp-3` directive, then apply it to multiple `<p>` elements in a Vue 3 component. It shows the `v-line-clamp:lineHeight="numberOfLines"` syntax, highlighting its role in controlling text truncation and cross-browser fallbacks.

import { createApp } from 'vue';
import lineClamp from 'vue-line-clamp-3';

const App = {
  template: `
    <div id="app" style="max-width: 400px; margin: 20px auto; border: 1px solid #eee; padding: 15px;">
      <h2>Vue Line Clamp 3 Example</h2>
      <p v-line-clamp:20="2" style="font-family: sans-serif; font-size: 16px; line-height: 20px; color: #333;">
        This is a very long piece of text that needs to be truncated to exactly two lines. If the browser
        supports '-webkit-line-clamp', it will use that for truncation and add an ellipsis. Otherwise,
        a fallback method using a calculated maximum height based on the provided line-height of 20px
        will be applied to clamp the text. This demonstrates the basic usage of the v-line-clamp directive
        to manage multi-line text overflow across different browser environments.
      </p>
      <br/>
      <p v-line-clamp:18="3" style="font-family: sans-serif; font-size: 14px; line-height: 18px; color: #555;">
        Here's another example with a different line count and line height to show its flexibility.
        The directive will adapt to these values to ensure the text is truncated correctly.
        Remember that the argument after the colon (e.g., :18) specifies the line-height for the fallback mechanism,
        and the value (e.g., ="3") specifies the desired number of lines.
      </p>
    </div>
  `
};

const app = createApp(App);

app.use(lineClamp, {
  // Optional: Set to true to import styles into <head> automatically.
  // importCss: true,
  // Optional: Customize the text-overflow property for modern browsers.
  // textOverflow: '...', 
  // Optional: Provide a custom fallback function for unsupported browsers.
  // fallbackFunc: (element, bindings, lines) => { /* custom logic */ }
});

app.mount('#app');

view raw JSON →