Vue Line Clamp 3
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
-
TypeError: app.use is not a function
cause Attempting to use `app.use()` on a raw component instance or an object that is not a Vue application instance created by `createApp()`.fixEnsure `app` is a valid Vue application instance: `import { createApp } from 'vue'; const app = createApp(YourRootComponent); app.use(lineClamp);` -
The argument passed to the directive must be a number
cause The directive argument (e.g., `:20` in `v-line-clamp:20`) for the line-height fallback is not a valid number.fixProvide a numeric value for the line-height argument, typically corresponding to your text's CSS `line-height` in pixels. Example: `<p v-line-clamp:18="3">`. -
Property 'v-line-clamp' does not exist on type 'VNodeProps' or similar TypeScript error for directive
cause TypeScript might not recognize the globally registered directive without proper type augmentation, or the plugin was not registered correctly.fixEnsure the plugin is registered via `app.use(lineClamp)`. For TypeScript, you might need to augment Vue's `GlobalComponents` or `Directive` types in a `d.ts` file if the library doesn't provide it automatically.
Warnings
- breaking Migrating from the original `vue-line-clamp` (Vue 2) to `vue-line-clamp-3` (Vue 3) requires updating to the Vue 3 application setup (`createApp` and `app.use()`) as `Vue.use()` is no longer available. This is a fundamental change in Vue's plugin registration API.
- gotcha The directive syntax `v-line-clamp:lineHeight="numberOfLines"` can be confusing. The argument after the colon (`:lineHeight`) *must* be a number representing the `line-height` in pixels, which is used for the fallback method in browsers not supporting `-webkit-line-clamp`. The bound value (`="numberOfLines"`) is the actual number of lines to clamp to.
- gotcha The JavaScript fallback method, used for browsers that do not support `-webkit-line-clamp`, does not display an ellipsis (`...`) at the end of the truncated text. This is a known limitation because the fallback cannot reliably control the specific part of the text node to clamp.
- gotcha There may be potential inconsistencies or problems when loading custom fonts. While tests have not revealed issues, custom font rendering and measurement can sometimes interfere with the directive's height calculations, particularly for the JavaScript fallback.
Install
-
npm install vue-line-clamp-3 -
yarn add vue-line-clamp-3 -
pnpm add vue-line-clamp-3
Imports
- lineClamp
const lineClamp = require('vue-line-clamp-3')import lineClamp from 'vue-line-clamp-3'
- Vue application setup
Vue.use(lineClamp)
import { createApp } from 'vue'; const app = createApp({}); app.use(lineClamp); - v-line-clamp directive usage
<p v-line-clamp:lineHeight="numberOfLines">Long text...</p>
Quickstart
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');