Skip to content

v-trim

Trim whitespace from input values with configurable position.

Since: 1.2.0

Usage

Basic

vue
<template>
  <input v-trim v-model="text" />
</template>

Trim on Blur (Default)

vue
<template>
  <input v-trim="'blur'" v-model="text" />
</template>

Trim on Input

vue
<template>
  <input v-trim="'input'" v-model="text" />
</template>

API

Types

typescript
type TrimPosition = 'start' | 'end' | 'both'

interface TrimOptions {
  /** When to trim: 'input' or 'blur' @default 'blur' */
  on?: 'input' | 'blur'
  /** Which side to trim @default 'both' */
  position?: TrimPosition
  /** Disable trimming @default false */
  disabled?: boolean
}

type TrimBinding = 'input' | 'blur' | TrimOptions

Options

OptionTypeDefaultDescription
onstring'blur'When to apply trim
positionstring'both'Which side to trim
disabledbooleanfalseDisable trimming

Examples

Trim Start Only

vue
<template>
  <input v-trim="{ position: 'start' }" v-model="text" />
</template>

Trim End Only

vue
<template>
  <textarea v-trim="{ position: 'end' }" v-model="content" />
</template>

Real-time Trimming

vue
<template>
  <input v-trim="{ on: 'input' }" v-model="searchQuery" />
</template>

Composable API

For programmatic use, you can use the useTrim composable:

typescript
import { useTrim, trimText, createTrimmer } from 'directix'

const { trimmed, original, wasTrimmed } = useTrim({
  text: inputText,
  position: 'both', // 'start' | 'end' | 'both'
  chars: undefined // Custom characters to trim
})

// Utility function
const result = trimText('  hello world  ') // 'hello world'
const startOnly = trimText('  hello world  ', 'start') // 'hello world  '
const custom = trimText('**hello**', 'both', '*') // 'hello'

// Create reusable trimmer
const trimStart = createTrimmer('start')
const trimmed = trimStart('  hello  ') // 'hello  '

UseTrimOptions

OptionTypeDefaultDescription
textstring | Ref<string>-The text to trim (required)
position'start' | 'end' | 'both' | Ref'both'Trim position
charsstring | Ref<string>-Custom characters to trim

UseTrimReturn

PropertyTypeDescription
trimmedRef<string>The trimmed text
originalRef<string>Original text
wasTrimmedRef<boolean>Whether the text was trimmed

Example

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

const text = ref('  hello world  ')
const { trimmed, wasTrimmed } = useTrim({ text })
// trimmed.value = 'hello world'
// wasTrimmed.value = true
</script>

<template>
  <p>{{ trimmed }}</p>
</template>

Code Generator

Quick Code Generator
<template>
  <div v-trim="{ position: 'both' }">
    <!-- Trim whitespace from input values with configurable position. directive -->
  </div>
</template>

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

// Configure your options here
const options = { position: 'both' }
</script>

Released under the MIT License.