Skip to content

API Reference

This section provides detailed API documentation for all available directives in Directix.

Available Directives

Directix provides 32 production-ready directives organized into the following categories:

Event Directives

DirectiveDescription
v-click-outsideDetect clicks outside an element
v-debounceDebounce event handlers
v-throttleThrottle event handlers
v-long-pressDetect long press gestures
v-hoverTrack hover state with callbacks
v-rippleMaterial design ripple effect
v-touchTouch gesture detection (swipe, pinch, rotate, tap)

Visibility Directives

DirectiveDescription
v-lazyLazy load images
v-intersectObserve element intersection
v-visibleToggle element visibility
v-loadingShow loading overlay
v-image-previewModal image preview with gestures

Scroll Directives

DirectiveDescription
v-scrollTrack scroll position
v-infinite-scrollInfinite scroll loading
v-stickySticky positioning

Form Directives

DirectiveDescription
v-copyCopy text to clipboard
v-focusAuto focus an element
v-maskInput mask formatting
v-trimTrim whitespace from input
v-capitalcaseTransform to capital case
v-lowercaseTransform to lowercase
v-uppercaseTransform to uppercase
v-moneyFormat as currency
v-numberFormat and validate numbers

UI Directives

DirectiveDescription
v-tooltipDisplay tooltips
v-draggableMake elements draggable
v-truncateTruncate text with ellipsis

Security Directives

DirectiveDescription
v-permissionPermission-based element control
v-sanitizeSanitize HTML content

Observer Directives

DirectiveDescription
v-resizeObserve element resize
v-mutationObserve DOM mutations

Installation Options

typescript
interface DirectiveInstallOptions {
  /** Register specific directives only */
  directives?: string[]
  /** Register all directives (default: true) */
  all?: boolean
  /** Global configuration for directives */
  config?: Record<string, any>
}

Import Methods

Named Import

typescript
import {
  vClickOutside,
  vCopy,
  vDebounce,
  vThrottle,
  vFocus,
  vLazy,
  vIntersect,
  vVisible,
  vLoading,
  vScroll,
  vInfiniteScroll,
  vSticky,
  vLongPress,
  vHover,
  vRipple,
  vMask,
  vPermission,
  vSanitize,
  vResize,
  vMutation,
  // New in v1.1.0
  vTouch,
  vImagePreview,
  vDraggable,
  vTooltip,
  vTruncate,
  vTrim,
  vCapitalcase,
  vLowercase,
  vUppercase,
  vMoney,
  vNumber,
} from 'directix'

Global Registration

typescript
import Directix from 'directix'

app.use(Directix)

Selective Registration

typescript
app.use(Directix, {
  directives: ['click-outside', 'copy', 'debounce', 'throttle', 'focus']
})

Utility Exports

Directix also exports utility functions:

typescript
import {
  // Type guards
  isString,
  isNumber,
  isBoolean,
  isFunction,
  isObject,
  isArray,
  isEmpty,
  isPromise,

  // Object utilities
  deepClone,
  deepMerge,
  get,
  set,

  // Time utilities
  parseTime,
  generateId,

  // Function utilities (aliased to avoid conflict with directives)
  debounceFn,
  throttleFn
} from 'directix'

Core Exports

For advanced usage, Directix exports core utilities:

typescript
import {
  // Vue version detection
  getVueVersion,
  setVueVersion,
  isVue2,
  isVue3,

  // Environment detection
  isBrowser,
  isSSR,

  // Feature support
  supportsPassive,
  supportsIntersectionObserver,
  supportsResizeObserver,
  supportsClipboard,
  supportsMutationObserver,

  // Directive definition
  defineDirective,
  defineDirectiveGroup
} from 'directix'

Web Components API (v2.1.0)

Directix provides comprehensive Web Components support for using directives with Custom Elements:

Basic Functions

typescript
import {
  // Check if element is a custom element
  isCustomElement,
  
  // Apply directive to custom element
  applyDirectiveToCustomElement,
  
  // Define custom element from directive
  defineCustomElementDirective,
  
  // Create custom element class
  createDirectiveElement,
  
  // Register multiple elements
  registerDirectiveElements,
  
  // Check if element is defined
  isCustomElementDefined,
  
  // Wait for element definition
  whenCustomElementDefined,
  
  // Get registered elements
  getRegisteredCustomElements,
  
  // Hydrate elements (SSR)
  hydrateCustomElements,
  
  // Create SSR-safe element
  createSSRSafeCustomElement
} from 'directix'

Usage Examples

typescript
// Define a custom element
defineCustomElementDirective({
  name: 'lazy-img',
  directive: vLazy,
  shadow: true,
  styles: ':host { display: block; }',
  lifecycle: {
    onConnect: (el) => console.log('Connected'),
    onDisconnect: (el) => console.log('Disconnected'),
  },
})

// SSR-safe rendering
const LazyImage = createSSRSafeCustomElement('lazy-img', vLazy, {
  shadow: true,
  styles: ':host { display: block; }',
})

const html = LazyImage.ssrRender({ src: 'image.jpg' })
// <lazy-img src="image.jpg"><template shadowroot="open">...</template></lazy-img>

// Client hydration
hydrateCustomElements(document.body)

Types

typescript
interface CustomElementLifecycleHooks {
  onConnect?: (el: HTMLElement) => void
  onDisconnect?: (el: HTMLElement) => void
  onAdopt?: (el: HTMLElement) => void
  onAttributeChange?: (el: HTMLElement, name: string, oldValue: string | null, newValue: string | null) => void
}

interface CustomElementDirectiveOptions {
  name: string
  directive: Directive
  defaultValue?: any
  shadow?: boolean
  shadowMode?: 'open' | 'closed'
  styles?: string | string[]
  observedAttributes?: string[]
  lifecycle?: CustomElementLifecycleHooks
  slots?: boolean
}

interface SSRSafeCustomElement {
  elementClass: CustomElementConstructor
  ssrRender: (attrs?: Record<string, string>) => string
}

Released under the MIT License.