{"id":12515,"library":"vue-line-clamp-3","title":"Vue Line Clamp 3","description":"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.","status":"active","version":"1.0.1","language":"javascript","source_language":"en","source_url":"https://github.com/neonpictures/vue-line-clamp-3","tags":["javascript","vue","vue3","vue 3","ellipsis","line-clamp","text-truncate","truncate","multiline"],"install":[{"cmd":"npm install vue-line-clamp-3","lang":"bash","label":"npm"},{"cmd":"yarn add vue-line-clamp-3","lang":"bash","label":"yarn"},{"cmd":"pnpm add vue-line-clamp-3","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency, required for the directive to function within a Vue 3 application.","package":"vue","optional":false}],"imports":[{"note":"The directive is exported as a default export and intended for use with `app.use()` in Vue 3 applications, which primarily use ESM.","wrong":"const lineClamp = require('vue-line-clamp-3')","symbol":"lineClamp","correct":"import lineClamp from 'vue-line-clamp-3'"},{"note":"Vue 3 removed the global `Vue` constructor. Applications are created via `createApp`, and plugins are installed using the `app.use()` instance method.","wrong":"Vue.use(lineClamp)","symbol":"Vue application setup","correct":"import { createApp } from 'vue';\nconst app = createApp({});\napp.use(lineClamp);"},{"note":"The directive expects a numeric argument for `lineHeight` (used for fallback) and a numeric value for `numberOfLines`. Example: `<p v-line-clamp:20=\"2\">`","symbol":"v-line-clamp directive usage","correct":"<p v-line-clamp:lineHeight=\"numberOfLines\">Long text...</p>"}],"quickstart":{"code":"import { createApp } from 'vue';\nimport lineClamp from 'vue-line-clamp-3';\n\nconst App = {\n  template: `\n    <div id=\"app\" style=\"max-width: 400px; margin: 20px auto; border: 1px solid #eee; padding: 15px;\">\n      <h2>Vue Line Clamp 3 Example</h2>\n      <p v-line-clamp:20=\"2\" style=\"font-family: sans-serif; font-size: 16px; line-height: 20px; color: #333;\">\n        This is a very long piece of text that needs to be truncated to exactly two lines. If the browser\n        supports '-webkit-line-clamp', it will use that for truncation and add an ellipsis. Otherwise,\n        a fallback method using a calculated maximum height based on the provided line-height of 20px\n        will be applied to clamp the text. This demonstrates the basic usage of the v-line-clamp directive\n        to manage multi-line text overflow across different browser environments.\n      </p>\n      <br/>\n      <p v-line-clamp:18=\"3\" style=\"font-family: sans-serif; font-size: 14px; line-height: 18px; color: #555;\">\n        Here's another example with a different line count and line height to show its flexibility.\n        The directive will adapt to these values to ensure the text is truncated correctly.\n        Remember that the argument after the colon (e.g., :18) specifies the line-height for the fallback mechanism,\n        and the value (e.g., =\"3\") specifies the desired number of lines.\n      </p>\n    </div>\n  `\n};\n\nconst app = createApp(App);\n\napp.use(lineClamp, {\n  // Optional: Set to true to import styles into <head> automatically.\n  // importCss: true,\n  // Optional: Customize the text-overflow property for modern browsers.\n  // textOverflow: '...', \n  // Optional: Provide a custom fallback function for unsupported browsers.\n  // fallbackFunc: (element, bindings, lines) => { /* custom logic */ }\n});\n\napp.mount('#app');\n","lang":"javascript","description":"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."},"warnings":[{"fix":"Rewrite plugin registration from `Vue.use(lineClamp)` to `import { createApp } from 'vue'; const app = createApp({}); app.use(lineClamp);`.","message":"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.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Ensure the argument after the colon is a numeric `line-height` (e.g., `v-line-clamp:20`) and the bound value is the desired `numberOfLines` (e.g., `= \"2\"`).","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Be aware of this visual difference in older/unsupported browsers. Consider providing alternative styling or custom fallback logic via the `fallbackFunc` plugin option if ellipsis is critical for all environments.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Test thoroughly with custom fonts. If issues arise, consider adjusting `line-height` values, or providing a custom `fallbackFunc` with more robust measurement techniques, or opening an issue with a reproduction.","message":"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.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure `app` is a valid Vue application instance: `import { createApp } from 'vue'; const app = createApp(YourRootComponent); app.use(lineClamp);`","cause":"Attempting to use `app.use()` on a raw component instance or an object that is not a Vue application instance created by `createApp()`.","error":"TypeError: app.use is not a function"},{"fix":"Provide 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\">`.","cause":"The directive argument (e.g., `:20` in `v-line-clamp:20`) for the line-height fallback is not a valid number.","error":"The argument passed to the directive must be a number"},{"fix":"Ensure 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.","cause":"TypeScript might not recognize the globally registered directive without proper type augmentation, or the plugin was not registered correctly.","error":"Property 'v-line-clamp' does not exist on type 'VNodeProps' or similar TypeScript error for directive"}],"ecosystem":"npm"}