Skip to content

Form Directives

Form directives help you enhance form interactions.

v-copy

Copy text to clipboard with a simple directive.

Basic Usage

vue
<template>
  <!-- Simple usage -->
  <button v-copy="textToCopy">Copy to clipboard</button>

  <!-- With callbacks -->
  <button v-copy="{ value: text, onSuccess: handleSuccess, onError: handleError }">
    Copy with callback
  </button>
</template>

<script setup>
const textToCopy = 'Hello, World!'

function handleSuccess(text) {
  console.log('Copied:', text)
}

function handleError(error) {
  console.error('Copy failed:', error)
}
</script>

API

OptionTypeDefaultDescription
valuestring-Text to copy (required)
onSuccess(text: string) => void-Callback on successful copy
onError(error: Error) => void-Callback on copy failure
titlestring-Tooltip text for the button
disabledbooleanfalseDisable copy functionality

v-focus

Auto focus an element when mounted.

Basic Usage

vue
<template>
  <!-- Simple usage -->
  <input v-focus />

  <!-- With options -->
  <input v-focus="{ focus: true, refocus: true }" />
</template>

With Options

vue
<template>
  <!-- Focus when component mounts -->
  <input v-focus="{ focus: true }" />

  <!-- Refocus when element is shown -->
  <input v-if="show" v-focus="{ focus: true, refocus: true }" />

  <!-- With callbacks -->
  <input v-focus="{ onFocus: handleFocus, onBlur: handleBlur }" />
</template>

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

const show = ref(false)

function handleFocus(el) {
  console.log('Focused:', el)
}

function handleBlur(el) {
  console.log('Blurred:', el)
}
</script>

API

OptionTypeDefaultDescription
focusbooleantrueFocus element on mount
refocusbooleanfalseRefocus when binding value changes
onFocus(el: HTMLElement) => void-Callback when focused
onBlur(el: HTMLElement) => void-Callback when blurred

v-mask

Input mask formatting for structured input.

Basic Usage

vue
<template>
  <!-- Phone number -->
  <input v-mask="'(###) ###-####'" placeholder="Phone" />

  <!-- SSN -->
  <input v-mask="'###-##-####'" placeholder="SSN" />

  <!-- Date -->
  <input v-mask="'##/##/####'" placeholder="Date" />

  <!-- With options -->
  <input v-mask="{ mask: '##/##/####', placeholder: '_' }" placeholder="Date" />
</template>

Mask Tokens

TokenPatternDescription
#[0-9]Digit (0-9)
A[A-Za-z]Letter (a-z, A-Z)
N[A-Za-z0-9]Alphanumeric (a-z, A-Z, 0-9)
X.Any character

API

OptionTypeDefaultDescription
maskstring-Mask pattern (required)
placeholderstring'_'Placeholder character
showPlaceholderbooleantrueShow placeholder on focus
showMaskOnBlurbooleanfalseShow mask on blur
clearIncompletebooleanfalseClear incomplete value on blur
disabledbooleanfalseDisable the mask
onChange(value: string, rawValue: string) => void-Callback on value change
onComplete(value: string) => void-Callback when mask is complete

Examples

vue
<template>
  <div>
    <label>Phone:</label>
    <input v-mask="'(###) ###-####'" type="tel" />
  </div>

  <div>
    <label>Credit Card:</label>
    <input v-mask="'#### #### #### ####'" type="text" />
  </div>

  <div>
    <label>Date:</label>
    <input v-mask="'##/##/####'" type="text" />
  </div>

  <div>
    <label>Time:</label>
    <input v-mask="'##:##'" type="text" />
  </div>
</template>

v-permission

Permission-based element control for access management.

Basic Usage

vue
<template>
  <!-- Single permission -->
  <button v-permission="'admin'">Admin Only</button>

  <!-- Multiple permissions (OR logic by default) -->
  <button v-permission="['admin', 'editor']">Admin or Editor</button>

  <!-- AND logic with mode option -->
  <button v-permission="{ value: ['read', 'write'], mode: 'every' }">
    Read & Write Required
  </button>

  <!-- Different actions -->
  <button v-permission="{ value: 'admin', action: 'disable' }">
    Disabled for non-admin
  </button>
</template>

Configuration

Configure the permission directive in your app:

typescript
import { configurePermission } from 'directix'

configurePermission({
  getPermissions: () => store.getters.permissions,
  getRoles: () => store.getters.roles,
  roleMap: {
    admin: ['*'],
    editor: ['read', 'write'],
    viewer: ['read']
  }
})

API

OptionTypeDefaultDescription
valuestring | string[]-Permission(s) to check (required)
mode'some' | 'every''some'Logic for multiple permissions
action'remove' | 'disable' | 'hide''remove'Action when denied
check(permission: string | string[], mode: string) => boolean-Custom check function
onChange(hasPermission: boolean) => void-Callback on permission change

v-sanitize

Sanitize HTML content to prevent XSS attacks.

Basic Usage

vue
<template>
  <!-- Basic sanitization -->
  <div v-sanitize v-html="userContent"></div>

  <!-- With allowed tags -->
  <div v-sanitize="{ allowedTags: ['b', 'i', 'p'] }" v-html="userContent"></div>

  <!-- Custom handler -->
  <div v-sanitize="{ handler: customSanitizer }" v-html="userContent"></div>
</template>

API

OptionTypeDefaultDescription
allowedTagsstring[]['b', 'i', 'u', 'strong', 'em', 'br', 'p', 'span', 'div']Allowed HTML tags
allowedAttributesstring[]['title', 'alt', 'href', 'src']Allowed HTML attributes
allowDataUrlsbooleanfalseAllow data: URLs
allowStylesbooleanfalseAllow style attribute
allowClassbooleanfalseAllow class attribute
allowIdbooleanfalseAllow id attribute
handler(value: string) => string-Custom sanitize function
disabledbooleanfalseDisable sanitization
sanitizeOnUpdatebooleantrueSanitize on update

Released under the MIT License.