feat(vue): allow useAutoAnimate with Vue component ref#186
Merged
justin-schroeder merged 1 commit intoformkit:release/0.8.5from Aug 27, 2025
Merged
feat(vue): allow useAutoAnimate with Vue component ref#186justin-schroeder merged 1 commit intoformkit:release/0.8.5from
justin-schroeder merged 1 commit intoformkit:release/0.8.5from
Conversation
This adds support for using `useAutoAnimate` with a component as the parent. When you add `ref="parent"` to a component, the element is found at `parent.value.$el`. This update checks for component elements or a plain HTML element ref.
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Contributor
Author
|
I just noticed this other PR. #159 I think their objective is the same, based on this comment: #159 (comment). |
Contributor
Author
|
Sorry about being lazy with the types. I'm not a TS ninja by any means, but I took a crack at it. Maybe you'll see a more optimal way to handle the import { onMounted, ref, watchEffect } from 'vue'
import type { Directive, Plugin, Ref } from 'vue'
import type {
AnimationController,
AutoAnimateOptions,
AutoAnimationPlugin,
} from '@formkit/auto-animate'
import autoAnimate, { vAutoAnimate as autoAnimateDirective,
} from '@formkit/auto-animate'
export const vAutoAnimate: Directive<
HTMLElement,
Partial<AutoAnimateOptions>
> = autoAnimateDirective
export const autoAnimatePlugin: Plugin = {
install(app) {
app.directive('auto-animate', vAutoAnimate)
},
}
export type ElementOrComponentRef<T extends Element> = T & { $el?: Element }
/**
* AutoAnimate hook for adding dead-simple transitions and animations to Vue.
* @param options - Auto animate options or a plugin
* @returns A template ref. Use the `ref` attribute of your parent element
* to store the element in this template ref.
*/
export function useAutoAnimate<T extends Element>(
options?: Partial<AutoAnimateOptions> | AutoAnimationPlugin,
): [Ref<ElementOrComponentRef<T>>, (enabled: boolean) => void] {
const element = ref<ElementOrComponentRef<T>>()
let controller: AnimationController | undefined
function setEnabled(enabled: boolean) {
if (controller)
enabled ? controller.enable() : controller.disable()
}
onMounted(() => {
watchEffect(() => {
const el = element.value?.$el || element.value
if (el instanceof HTMLElement)
controller = autoAnimate(el, options || {})
})
})
return [element as Ref<T>, setEnabled]
} |
Member
|
I might rework this a bit, but I think the concept is solid. thanks! Sorry its taken a couple years haha 🤣 |
justin-schroeder
added a commit
that referenced
this pull request
Sep 5, 2025
* test(e2e): add Playwright visual tests (video + pixel diffs); stabilize animation waits; upload videos in CI * fix: jumping when aligned at bottom (#69) (#211) * fix: jumping when aligned at bottom (#69) * chore: use const instead of let * test(e2e): add Playwright visual tests (video + pixel diffs); stabilize animation waits; upload videos in CI --------- Co-authored-by: Justin Schroeder <justin@formkit.com> * fix: #69 — nice * Adding bun to installation instructions (#205) * feat: allow useAutoAnimate with Vue component ref (#186) This adds support for using `useAutoAnimate` with a component as the parent. When you add `ref="parent"` to a component, the element is found at `parent.value.$el`. This update checks for component elements or a plain HTML element ref. * feat: adds component support to useAutoAnimate for Vue * Investigate and resolve auto-animate issues (#225) * Improve offscreen handling, Vue integration, and cleanup in AutoAnimate Co-authored-by: justin <justin@formkit.com> * Add e2e tests for various autoAnimate scenarios and behaviors Co-authored-by: justin <justin@formkit.com> * chore: remove playwright report * Angular 17.1+ support (#206) * Angular >v17.1 support * angular example rewrite * Angular docs change * Update package.json * angular pnpm-lock & usage import fix * ' -> " typo fix --------- Co-authored-by: Gergely Dremak <gergely.dremak@hu.bosch.com> Co-authored-by: Justin Schroeder <justin@formkit.com> * chore: lockfile bump * fix: bottom anchored animations * chore: remove local claude * fix: restore bottom-aligned animation fix from PR #211 and fix CI test failures - Restored deltaBottom/deltaRight checks to detect anchored elements - Fixed immediate position updates after animations (debounce=false) - Restored proper parent coordinate calculations in deletePosition - Added named export for autoAnimate to fix exports test - Fixed flaky framework animation tests by ensuring elements are visible - Added scrollIntoViewIfNeeded and increased wait times for CI reliability All bottom-jump-fix tests now pass across all browsers * fix: add build step to CI and make tests more robust for slow environments - Added 'pnpm build' step to GitHub Actions workflow before running tests - Fixed exports test by ensuring dist files are built in CI - Made framework animation tests more robust with polling approach - Added browser-specific timeouts for WebKit compatibility - Increased wait times and retry attempts for slow CI environments * fix: no nuxt build on ci * chore: gh caching --------- Co-authored-by: Maik Kowol <maik.s.kowol@gmail.com> Co-authored-by: Abi <86650485+iabii@users.noreply.github.com> Co-authored-by: Marshall Thompson <marshall@creativeideal.net> Co-authored-by: Gergely Dremák <gergely089@gmail.com> Co-authored-by: Gergely Dremak <gergely.dremak@hu.bosch.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds support for using
useAutoAnimatewith a component as the parent.When you add
ref="parent"to a component, the element is found atparent.value.$el. This update checks for component elements or a plain HTML element ref.Without this change, you have to create a proxy ref to extract the element in the
onMountedhook, like this:This PR doesn't address the types. I'm not sure the best way to handle that. I did pull the code into a local repo to try it out and took this screenshot of the type issue.
If you want to instruct me how to proceed on the types, I'll update the code. Or feel free to edit this PR directly.