docs/.vitepress/theme/components/search/SearchMotion.ts

126 lines · 3095 bytes

6 symbols in this file
  1import { computed, type ComputedRef } from 'vue'
  2import { motion } from 'motion-v'
  3
  4export const SearchMotionDiv = motion.div
  5export const SearchMotionForm = motion.form
  6export const SearchMotionLi = motion.li
  7export const searchMotionEase = [0.16, 1, 0.3, 1]
  8
  9export function useSearchMotion(animationsEnabled: ComputedRef<boolean>) {
 10  const searchMotionDiv = computed(() =>
 11    animationsEnabled.value ? SearchMotionDiv : 'div'
 12  )
 13  const searchMotionForm = computed(() =>
 14    animationsEnabled.value ? SearchMotionForm : 'form'
 15  )
 16  const searchMotionLi = computed(() =>
 17    animationsEnabled.value ? SearchMotionLi : 'li'
 18  )
 19
 20  const backdropMotion = computed(() =>
 21    animationsEnabled.value
 22      ? {
 23        initial: { opacity: 0 },
 24        animate: { opacity: 1 },
 25        exit: { opacity: 0 },
 26        transition: { duration: 0.55, ease: searchMotionEase }
 27      }
 28      : {}
 29  )
 30
 31  const shellMotion = computed(() =>
 32    animationsEnabled.value
 33      ? {
 34        layout: 'size',
 35        initial: { opacity: 0, scale: 0.9375 },
 36        animate: { opacity: 1, scale: 1 },
 37        exit: { opacity: 0, scale: 0.9375 },
 38        transition: { duration: 0.55, ease: searchMotionEase }
 39      }
 40      : {}
 41  )
 42
 43  const formMotion = computed(() =>
 44    animationsEnabled.value ? { layout: 'position' } : {}
 45  )
 46
 47  const ribbonMotion = computed(() =>
 48    animationsEnabled.value
 49      ? {
 50        layout: 'position',
 51        initial: { opacity: 0, height: 0 },
 52        animate: { opacity: 1, height: 'auto' },
 53        exit: { opacity: 0, height: 0 },
 54        transition: { duration: 0.4, ease: searchMotionEase }
 55      }
 56      : {}
 57  )
 58
 59  const emptyStateMotion = computed(() =>
 60    animationsEnabled.value
 61      ? {
 62        initial: { opacity: 0 },
 63        animate: {
 64          opacity: 1,
 65          transition: { delay: 0.1 }
 66        },
 67        exit: {
 68          opacity: 0,
 69          height: 0,
 70          transition: { duration: 0.2 }
 71        },
 72        transition: { duration: 0.7, ease: searchMotionEase }
 73      }
 74      : {}
 75  )
 76
 77  function resultMotion(index: number) {
 78    if (!animationsEnabled.value) return {}
 79    return {
 80      layout: true,
 81      initial: { opacity: 0, scale: 0.9375 },
 82      animate: { opacity: 1, scale: 1 },
 83      exit: {
 84        opacity: 0,
 85        scale: 0.9375,
 86        height: 0,
 87        transition: { duration: 0.25 }
 88      },
 89      transition: {
 90        duration: 0.55,
 91        ease: searchMotionEase,
 92        delay: index * 0.02
 93      }
 94    }
 95  }
 96
 97  const excerptMotion = computed(() =>
 98    animationsEnabled.value
 99      ? {
100        layout: true,
101        initial: { height: 0 },
102        animate: { height: '84px' },
103        exit: { height: 0 },
104        transition: { duration: 0.4, ease: searchMotionEase }
105      }
106      : {}
107  )
108
109  const footerMotion = computed(() =>
110    animationsEnabled.value ? { layout: true } : {}
111  )
112
113  return {
114    searchMotionDiv,
115    searchMotionForm,
116    searchMotionLi,
117    backdropMotion,
118    shellMotion,
119    formMotion,
120    ribbonMotion,
121    emptyStateMotion,
122    resultMotion,
123    excerptMotion,
124    footerMotion
125  }
126}