Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic keys on useMotion directives when calling apply() and other methods #126

Merged
merged 4 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/components/Motion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ export default defineComponent({
},
// Instance
instance: {
type: Object as PropType<MotionInstance>,
type: Object as PropType<MotionInstance<string, MotionVariants<string>>>,
required: false,
},
// Variants
variants: {
type: Object as PropType<MotionVariants>,
type: Object as PropType<MotionVariants<string>>,
required: false,
},
// Initial variant
Expand Down Expand Up @@ -74,7 +74,7 @@ export default defineComponent({
const slots = useSlots()

// Instance map from component content
const instances = reactive<{ [key: number]: MotionInstance<any> }>({})
const instances = reactive<{ [key: number]: MotionInstance<string, MotionVariants<string>> }>({})

// Return empty component is `is` is absent
if (!props.is && !slots.default) return () => h('div', {})
Expand Down Expand Up @@ -128,7 +128,7 @@ export default defineComponent({

// Replay animations on component update Vue
if (process?.env?.NODE_ENV === 'development' || (process as any)?.dev) {
const replayAnimation = (instance: MotionInstance<any>) => {
const replayAnimation = (instance: MotionInstance<any, any>) => {
if (instance.variants?.initial) instance.set('initial')
setTimeout(() => {
if (instance.variants?.enter) instance.apply('enter')
Expand Down
8 changes: 4 additions & 4 deletions src/directive/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Directive, DirectiveBinding, VNode } from 'vue'
import type { Directive, DirectiveBinding, Ref, VNode } from 'vue'
import defu from 'defu'
import { ref, unref } from 'vue'
import { motionState } from '../features/state'
Expand All @@ -7,7 +7,7 @@ import { useMotion } from '../useMotion'
import { resolveVariants } from '../utils/directive'
import { variantToStyle } from '../utils/transform'

export function directive(variants?: MotionVariants): Directive<HTMLElement | SVGElement> {
export function directive<T extends string>(variants: MotionVariants<T> = {}): Directive<HTMLElement | SVGElement> {
const register = (el: HTMLElement | SVGElement, binding: DirectiveBinding, node: VNode<any, HTMLElement | SVGElement, Record<string, any>>) => {
// Get instance key if possible (binding value or element key in case of v-for's)
const key = (binding.value && typeof binding.value === 'string' ? binding.value : node.key) as string
Expand All @@ -16,13 +16,13 @@ export function directive(variants?: MotionVariants): Directive<HTMLElement | SV
if (key && motionState[key]) motionState[key].stop()

// Initialize variants with argument
const variantsRef = ref<MotionVariants>(variants || {})
const variantsRef = ref(variants) as Ref<MotionVariants<T>>

// Set variants from v-motion binding
if (typeof binding.value === 'object') variantsRef.value = binding.value

// Resolve variants from node props
resolveVariants(node, variantsRef)
resolveVariants<T>(node, variantsRef)

// Create motion instance
const motionInstance = useMotion(el, variantsRef)
Expand Down
3 changes: 2 additions & 1 deletion src/features/eventListeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { computed, ref, unref, watch } from 'vue'
import type { MotionInstance, MotionVariants } from '../types'
import { supportsMouseEvents, supportsPointerEvents, supportsTouchEvents } from '../utils/events'

export function registerEventListeners<T extends MotionVariants>({ target, state, variants, apply }: MotionInstance<T>) {
export function registerEventListeners<T extends string, V extends MotionVariants<T>>({ target, state, variants, apply }: MotionInstance<T, V>) {
const _variants = unref(variants)

// State
Expand Down Expand Up @@ -37,6 +37,7 @@ export function registerEventListeners<T extends MotionVariants>({ target, state
if (focused.value && _variants.focused) Object.assign(result, _variants.focused)

for (const key in result) {
// @ts-expect-error - Fix errors later for typescript 5
if (!mutableKeys.value.includes(key)) delete result[key]
}

Expand Down
2 changes: 1 addition & 1 deletion src/features/lifeCycleHooks.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { unref, watch } from 'vue'
import type { MotionInstance, MotionVariants } from '../types'

export function registerLifeCycleHooks<T extends MotionVariants>({ set, target, variants, variant }: MotionInstance<T>) {
export function registerLifeCycleHooks<T extends string, V extends MotionVariants<T>>({ set, target, variants, variant }: MotionInstance<T, V>) {
const _variants = unref(variants)

watch(
Expand Down
4 changes: 2 additions & 2 deletions src/features/state.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import type { MotionInstanceBindings } from '../types'
import type { MotionInstanceBindings, MotionVariants } from '../types'

export const motionState: MotionInstanceBindings<any> = {}
export const motionState: MotionInstanceBindings<string, MotionVariants<never>> = {}
2 changes: 1 addition & 1 deletion src/features/syncVariants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { watch } from 'vue'
import type { MotionInstance, MotionVariants } from '../types'

export function registerVariantsSync<T extends MotionVariants>({ state, apply }: MotionInstance<T>) {
export function registerVariantsSync<T extends string, V extends MotionVariants<T>>({ state, apply }: MotionInstance<T, V>) {
// Watch for variant changes and apply the new one
watch(
state,
Expand Down
2 changes: 1 addition & 1 deletion src/features/visibilityHooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useIntersectionObserver } from '@vueuse/core'
import { unref } from 'vue'
import type { MotionInstance, MotionVariants } from '../types'

export function registerVisibilityHooks<T extends MotionVariants>({ target, variants, variant }: MotionInstance<T>) {
export function registerVisibilityHooks<T extends string, V extends MotionVariants<T>>({ target, variants, variant }: MotionInstance<T, V>) {
const _variants = unref(variants)

// Bind intersection observer on target
Expand Down
2 changes: 1 addition & 1 deletion src/nuxt/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defu } from 'defu'
import { addImportsDir, addPlugin, createResolver, defineNuxtModule } from '@nuxt/kit'
import type { ModuleOptions as MotionModuleOpts } from '../types'

export interface ModuleOptions extends MotionModuleOpts {}
export interface ModuleOptions extends MotionModuleOpts<string> {}

export default defineNuxtModule<ModuleOptions>({
meta: {
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { MotionPluginOptions, MotionVariants } from '../types'
import { slugify } from '../utils/slugify'

export const MotionPlugin: Plugin = {
install(app, options: MotionPluginOptions) {
install(app, options: MotionPluginOptions<string>) {
// Register default `v-motion` directive
app.directive('motion', directive())

Expand All @@ -30,7 +30,7 @@ export const MotionPlugin: Plugin = {
// Loop on options, create a custom directive for each definition
for (const key in options.directives) {
// Get directive variants
const variants = options.directives[key] as MotionVariants
const variants = options.directives[key] as MotionVariants<any>

// Development warning, showing definitions missing `initial` key
if (!variants.initial && __DEV__) {
Expand Down
6 changes: 3 additions & 3 deletions src/presets/fade.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MotionVariants } from '../types'

export const fade: MotionVariants = {
export const fade: MotionVariants<never> = {
initial: {
opacity: 0,
},
Expand All @@ -9,7 +9,7 @@ export const fade: MotionVariants = {
},
}

export const fadeVisible: MotionVariants = {
export const fadeVisible: MotionVariants<never> = {
initial: {
opacity: 0,
},
Expand All @@ -18,7 +18,7 @@ export const fadeVisible: MotionVariants = {
},
}

export const fadeVisibleOnce: MotionVariants = {
export const fadeVisibleOnce: MotionVariants<never> = {
initial: {
opacity: 0,
},
Expand Down
6 changes: 3 additions & 3 deletions src/presets/pop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MotionVariants } from '../types'

export const pop: MotionVariants = {
export const pop: MotionVariants<never> = {
initial: {
scale: 0,
opacity: 0,
Expand All @@ -11,7 +11,7 @@ export const pop: MotionVariants = {
},
}

export const popVisible: MotionVariants = {
export const popVisible: MotionVariants<never> = {
initial: {
scale: 0,
opacity: 0,
Expand All @@ -22,7 +22,7 @@ export const popVisible: MotionVariants = {
},
}

export const popVisibleOnce: MotionVariants = {
export const popVisibleOnce: MotionVariants<never> = {
initial: {
scale: 0,
opacity: 0,
Expand Down
24 changes: 12 additions & 12 deletions src/presets/roll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { MotionVariants } from '../types'

// Roll from left

export const rollLeft: MotionVariants = {
export const rollLeft: MotionVariants<never> = {
initial: {
x: -100,
rotate: 90,
Expand All @@ -15,7 +15,7 @@ export const rollLeft: MotionVariants = {
},
}

export const rollVisibleLeft: MotionVariants = {
export const rollVisibleLeft: MotionVariants<never> = {
initial: {
x: -100,
rotate: 90,
Expand All @@ -28,7 +28,7 @@ export const rollVisibleLeft: MotionVariants = {
},
}

export const rollVisibleOnceLeft: MotionVariants = {
export const rollVisibleOnceLeft: MotionVariants<never> = {
initial: {
x: -100,
rotate: 90,
Expand All @@ -43,7 +43,7 @@ export const rollVisibleOnceLeft: MotionVariants = {

// Roll from right

export const rollRight: MotionVariants = {
export const rollRight: MotionVariants<never> = {
initial: {
x: 100,
rotate: -90,
Expand All @@ -56,7 +56,7 @@ export const rollRight: MotionVariants = {
},
}

export const rollVisibleRight: MotionVariants = {
export const rollVisibleRight: MotionVariants<never> = {
initial: {
x: 100,
rotate: -90,
Expand All @@ -69,7 +69,7 @@ export const rollVisibleRight: MotionVariants = {
},
}

export const rollVisibleOnceRight: MotionVariants = {
export const rollVisibleOnceRight: MotionVariants<never> = {
initial: {
x: 100,
rotate: -90,
Expand All @@ -84,7 +84,7 @@ export const rollVisibleOnceRight: MotionVariants = {

// Roll from top

export const rollTop: MotionVariants = {
export const rollTop: MotionVariants<never> = {
initial: {
y: -100,
rotate: -90,
Expand All @@ -97,7 +97,7 @@ export const rollTop: MotionVariants = {
},
}

export const rollVisibleTop: MotionVariants = {
export const rollVisibleTop: MotionVariants<never> = {
initial: {
y: -100,
rotate: -90,
Expand All @@ -110,7 +110,7 @@ export const rollVisibleTop: MotionVariants = {
},
}

export const rollVisibleOnceTop: MotionVariants = {
export const rollVisibleOnceTop: MotionVariants<never> = {
initial: {
y: -100,
rotate: -90,
Expand All @@ -125,7 +125,7 @@ export const rollVisibleOnceTop: MotionVariants = {

// Roll from bottom

export const rollBottom: MotionVariants = {
export const rollBottom: MotionVariants<never> = {
initial: {
y: 100,
rotate: 90,
Expand All @@ -138,7 +138,7 @@ export const rollBottom: MotionVariants = {
},
}

export const rollVisibleBottom: MotionVariants = {
export const rollVisibleBottom: MotionVariants<never> = {
initial: {
y: 100,
rotate: 90,
Expand All @@ -151,7 +151,7 @@ export const rollVisibleBottom: MotionVariants = {
},
}

export const rollVisibleOnceBottom: MotionVariants = {
export const rollVisibleOnceBottom: MotionVariants<never> = {
initial: {
y: 100,
rotate: 90,
Expand Down
Loading