Skip to main content

Utilities & Helpers

Helper functions and hooks for building RTL-first applications. These utilities handle common patterns like date formatting, text direction, and multilingual number display.

useRelativeTime

Hook

Format dates as relative time strings with auto-updates and multilingual support

Overview

The useRelativeTime hook formats dates as relative time strings (e.g., "2 hours ago", "منذ ساعتين") and automatically updates them at a specified interval.

It supports 4 locales (English, Arabic, French, Urdu) and handles all time ranges from seconds to years with proper pluralization.

Usagetsx

API Reference

ParameterTypeDefaultDescription
dateDate | string-The date to format (Date object or ISO string)
locale'en' | 'ar' | 'fr' | 'ur''en'Locale for translations
options.updateIntervalnumber60000Update interval in milliseconds (default: 1 minute)
options.format'short' | 'long''long'Format style (not yet implemented)

Features

  • Automatic updates at customizable intervals
  • Full localization for 4 locales (en, ar, fr, ur)
  • Handles all time ranges (seconds, minutes, hours, days, weeks, months, years)
  • Proper pluralization for each locale
  • Automatic cleanup on unmount
  • TypeScript support with full type safety

useSwipeDirection

Hook

RTL-aware swipe directions and animation values for gesture-based interactions

Overview

The useSwipeDirection hook provides directional values that automatically adjust based on text direction. In LTR, swiping left goes "next" — in RTL, swiping right goes "next". Works with any gesture library (react-swipeable, Framer Motion, etc.).

Also exports a standalone getCarouselDirection utility for carousel/pagination animation direction.

Usagetsx

API Reference — Return Values

PropertyTypeLTRRTLDescription
nextSwipeDirection'left''right'Forward in content flow
previousSwipeDirection'right''left'Backward in content flow
forwardnumber1-1Numeric direction multiplier
slideInnumber100-100Slide-in animation value
slideOutnumber-100100Slide-out animation value
isRTLbooleanfalsetrueCurrent direction state
mirror(value)(T) => TidentityflippedMirror any number or direction string

Features

  • All directional values flip automatically in RTL
  • Works with react-swipeable, Framer Motion, or any gesture library
  • Provides both string directions and numeric multipliers
  • Generic mirror() helper for custom values
  • Standalone getCarouselDirection() for pagination logic
  • TypeScript support with full type safety

useRTLAnimation

Hook

Framer Motion animation variants that automatically adjust for RTL layouts

Overview

The useRTLAnimation hook provides pre-built Framer Motion variants for slides, fades, and dismissals that automatically flip direction in RTL. Configurable distance, duration, and easing.

Also exports useRTLDragConstraints for flipping horizontal drag bounds in RTL.

Usagetsx

API Reference — Config

ParameterTypeDefaultDescription
distancenumber100Animation distance in pixels
durationnumber0.3Animation duration in seconds
easeEasing'easeInOut'Framer Motion easing function

API Reference — Return Values

PropertyTypeDescription
slideVariantsVariantsFull slide animation (enter/center/exit) — ideal for carousels
fadeSlideVariantsVariantsFade + half-distance slide — smoother for content transitions
swipeVariantsVariantsSwipe-to-dismiss (initial/dismissed) — for cards and modals
isRTLbooleanCurrent direction state
getDirection(n)(number) => numberFlip a direction value for RTL

Features

  • Three variant sets covering common animation patterns
  • All variants accept a custom direction prop for bidirectional navigation
  • Configurable distance, duration, and easing
  • Built on useSwipeDirection — no duplicate RTL logic
  • Companion useRTLDragConstraints for drag interactions
  • TypeScript support with full type safety

Coming Soon

Date & Time

formatDate

Locale-aware date formatting with Arabic numerals

Numbers

formatNumber

Locale-aware number formatting

Numbers

toArabicNumerals

Convert numbers to Eastern Arabic numerals (٠١٢٣)

Text & Direction

getTextDirection

Auto-detect text direction from content

Text & Direction

isRTL

Check if a locale is RTL