Skip to content

v-truncate

Truncate text content with ellipsis and optional expand functionality.

Since: 1.2.0

Usage

Basic

vue
<template>
  <p v-truncate="100">
    This is a very long text that will be truncated after 100 characters...
  </p>
</template>

With Options

vue
<template>
  <p v-truncate="{
    length: 50,
    suffix: '...',
    position: 'end'
  }">
    Long text here
  </p>
</template>

API

Types

typescript
type TruncatePosition = 'start' | 'middle' | 'end'

interface TruncateOptions {
  /** Maximum length before truncation */
  length?: number
  /** Suffix to show @default '...' */
  suffix?: string
  /** Truncation position @default 'end' */
  position?: TruncatePosition
  /** Custom class for truncated text */
  class?: string
  /** Disable truncation @default false */
  disabled?: boolean
}

type TruncateBinding = number | TruncateOptions

Options

OptionTypeDefaultDescription
lengthnumber100Maximum character length
suffixstring'...'Ellipsis string
positionstring'end'Where to truncate
classstring-Custom CSS class
disabledbooleanfalseDisable truncation

Examples

Middle Truncation

vue
<template>
  <!-- Good for file paths -->
  <span v-truncate="{ length: 20, position: 'middle' }">
    /very/long/path/to/some/file.txt
  </span>
  <!-- Output: /very/long...file.txt -->
</template>

Start Truncation

vue
<template>
  <span v-truncate="{ length: 15, position: 'start' }">
    Long text here
  </span>
  <!-- Output: ...ng text here -->
</template>

Custom Suffix

vue
<template>
  <span v-truncate="{ length: 30, suffix: ' [read more]' }">
    Very long content here
  </span>
</template>

Composable API

For programmatic use, you can use the useEllipsis composable:

typescript
import { useEllipsis, truncateText, wouldTextTruncate } from 'directix'

const { truncated, isTruncated, original, calculateForWidth, wouldTruncate } = useEllipsis({
  text: longText,
  lines: 1,
  ellipsis: '...',
  maxWidth: 0 // 0 = no width limit
})

// Utility function
const short = truncateText('Very long text here', 10) // 'Very l...'

// Check if text would truncate
if (wouldTextTruncate('Long text', 100)) {
  console.log('Text would be truncated')
}

UseEllipsisOptions

OptionTypeDefaultDescription
textstring | Ref<string>-The text to potentially truncate (required)
linesnumber | Ref<number>1Number of lines before truncating
ellipsisstring | Ref<string>'...'Custom ellipsis string
maxWidthnumber | Ref<number>0Maximum width in pixels (0 = no limit)

UseEllipsisReturn

PropertyTypeDescription
truncatedRef<string>The truncated text
isTruncatedRef<boolean>Whether the text is truncated
originalRef<string>Original text
calculateForWidth(width: number) => stringCalculate truncation for given width
wouldTruncate(width: number) => booleanCheck if text would truncate

Example

vue
<script setup>
import { ref } from 'vue'
import { useEllipsis } from 'directix'

const longText = ref('This is a very long text that needs to be truncated')
const { truncated, isTruncated } = useEllipsis({
  text: longText,
  maxWidth: 200
})
</script>

<template>
  <span :title="isTruncated ? longText : ''">
    {{ truncated }}
  </span>
</template>

Code Generator

Quick Code Generator
<template>
  <div v-truncate="{ length: 100, suffix: '...' }">
    <!-- Truncate text content with ellipsis and optional expand functionality. directive -->
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

// Configure your options here
const options = { length: 100, suffix: '...' }
</script>

Released under the MIT License.