Vue Snip

2.0.2 · active · verified Tue Apr 21

vue-snip is a Vue.js directive designed for clamping the content of text elements when they exceed a specified number of lines. It offers two primary snipping approaches: CSS-based (for performance) and JavaScript-based (for greater accuracy and control), selectable on a per-element basis to optimize for different scenarios. A key feature is its ability to automatically re-snip content when the element's size changes (e.g., due to window resize) or when reactive data influencing the text content updates, eliminating the need for manual recalculations. The library intelligently determines line heights without requiring explicit specification from the developer. Under the hood, vue-snip leverages the `js-snip` library for its core snipping logic. The current stable version is 2.0.2, and it supports both Vue 2 and Vue 3 environments through distinct installation patterns. While there isn't a stated strict release cadence, updates tend to align with Vue ecosystem changes or enhancements to the underlying snipping logic, focusing on stability and performance for text truncation needs.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to integrate `vue-snip` into a Vue 3 application, applying the `v-snip` directive to paragraphs with various options, including dynamic content toggling and an `onSnipped` callback to react to truncation changes. It showcases both JavaScript and CSS snipping modes.

import { createApp, ref } from 'vue';
import VueSnip from 'vue-snip';

const App = {
  template: `
    <div id="app">
      <h1>Vue Snip Example</h1>
      <p v-snip="{ lines: 3, mode: 'js', midWord: false, onSnipped }" :title="currentContent">
        {{ currentContent }}
      </p>
      <p>Has ellipsis: {{ hasEllipsis }}</p>
      <button @click="toggleContent">Toggle Content Length</button>
      <p v-snip="{ lines: 2, mode: 'css' }">
        This is another paragraph demonstrating CSS mode snipping. It prioritizes
        browser performance but might have varying compatibility. This example
        will limit the text to two lines using CSS truncation. It's often a good
        choice for static content or when maximum performance is critical.
      </p>
    </div>
  `,
  setup() {
    const hasEllipsis = ref(false);
    const fullContent = `This is a very long paragraph that demonstrates the functionality of vue-snip. It will be truncated to a maximum of three lines. If the content exceeds these three lines, an ellipsis will be added to indicate that more text is available. The 'onSnipped' callback will be triggered when the snipping occurs or changes, allowing us to react to the state of the ellipsis. This example uses JavaScript mode for snipping and ensures that words are not cut in half, making the text more readable. The content might change dynamically, and vue-snip should handle re-snipping automatically.`;
    const shortContent = `This is a short paragraph.`;
    const currentContent = ref(fullContent);

    const onSnipped = (newState: { hasEllipsis: boolean }) => {
      hasEllipsis.value = newState.hasEllipsis;
      console.log('Snipped state changed:', newState.hasEllipsis);
    };

    const toggleContent = () => {
      currentContent.value = currentContent.value === fullContent ? shortContent : fullContent;
    };

    return {
      hasEllipsis,
      onSnipped,
      toggleContent,
      currentContent,
    };
  },
};

createApp(App).use(VueSnip).mount('#app');

view raw JSON →