Skip to content

Commit ae94226

Browse files
authored
fix: accept SVG elements in autoAnimate TypeScript types (#243)
The core already tracks parents as Element and animates SVG at runtime, but the public signature only allowed HTMLElement, which broke Svelte actions and TS callers (issue #100). Widen the contract to HTMLElement | SVGElement and stop adapter instanceof HTMLElement gates from silently skipping SVG nodes.
1 parent 4ff0b72 commit ae94226

10 files changed

Lines changed: 77 additions & 18 deletions

File tree

‎src/index.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ function cleanUp(el: Element, styles?: Partial<CSSStyleDeclaration>) {
651651
setTimeout(() => {
652652
if (DEL in el) delete (el as any)[DEL]
653653
Object.defineProperty(el, NEW, { value: true, configurable: true })
654-
if (styles && el instanceof HTMLElement) {
654+
if (styles && (el instanceof HTMLElement || el instanceof SVGElement)) {
655655
for (const style in styles) {
656656
;(el.style as any)[style as any] = ""
657657
}
@@ -881,11 +881,11 @@ export interface AutoAnimationPlugin {
881881
* A function that automatically adds animation effects to itself and its
882882
* immediate children. Specifically it adds effects for adding, moving, and
883883
* removing DOM elements.
884-
* @param el - A parent element to add animations to.
884+
* @param el - An HTML or SVG parent element to add animations to.
885885
* @param options - An optional object of options.
886886
*/
887887
export default function autoAnimate(
888-
el: HTMLElement,
888+
el: HTMLElement | SVGElement,
889889
config: Partial<AutoAnimateOptions> | AutoAnimationPlugin = {},
890890
): AnimationController {
891891
if (supportedBrowser && resize) {
@@ -985,15 +985,15 @@ export { autoAnimate }
985985
*/
986986
export const vAutoAnimate = {
987987
mounted: (
988-
el: HTMLElement,
988+
el: HTMLElement | SVGElement,
989989
binding: {
990990
value: Partial<AutoAnimateOptions> | AutoAnimationPlugin | undefined
991991
},
992992
) => {
993993
const ctl = autoAnimate(el, binding.value || {})
994994
Object.defineProperty(el, "__aa_ctl", { value: ctl, configurable: true })
995995
},
996-
unmounted: (el: HTMLElement) => {
996+
unmounted: (el: HTMLElement | SVGElement) => {
997997
const ctl = (el as any)["__aa_ctl"] as AnimationController | undefined
998998
ctl?.destroy?.()
999999
try {

‎src/marko/auto-animate.marko‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
export interface Input {
99
// A reference to the parent element whose *direct children* will be animated.
1010
// Pass a native element tag-variable, e.g. `<ul/listRef>` then `parent=listRef`.
11-
parent: () => HTMLElement;
11+
parent: () => HTMLElement | SVGElement;
1212
// auto-animate options (duration / easing / disrespectUserMotionPreference)
1313
// OR a plugin function. Read once when the tag mounts; the core has no setOptions,
1414
// so changing this later has no effect (remount the tag to apply new options).

‎src/preact/index.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ export function useAutoAnimate<T extends Element>(
2323
}
2424
}
2525
useEffect(() => {
26-
if (element.current instanceof HTMLElement)
26+
if (
27+
element.current instanceof HTMLElement ||
28+
element.current instanceof SVGElement
29+
)
2730
setController(autoAnimate(element.current, options || {}))
2831
}, [])
2932
useEffect(() => {

‎src/qwik/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import autoAnimate, {
2020
* @param options - Auto animate options or a plugin
2121
* @returns
2222
*/
23-
export function useAutoAnimate<T extends HTMLElement>(
23+
export function useAutoAnimate<T extends HTMLElement | SVGElement>(
2424
options?: Partial<AutoAnimateOptions> | AutoAnimationPlugin
2525
): [
2626
Signal<T | undefined>,

‎src/react/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function useAutoAnimate<T extends Element>(
1919
const memoizedOptions = useMemo(() => options, [])
2020
const element = useCallback(
2121
(node: T) => {
22-
if (node instanceof HTMLElement) {
22+
if (node instanceof HTMLElement || node instanceof SVGElement) {
2323
setController(autoAnimate(node, memoizedOptions))
2424
} else {
2525
setController(undefined)

‎src/solid/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ declare module "solid-js" {
1313
}
1414
}
1515

16-
export const createAutoAnimate = <T extends HTMLElement>(
16+
export const createAutoAnimate = <T extends HTMLElement | SVGElement>(
1717
options: Partial<AutoAnimateOptions> | AutoAnimationPlugin = {}
1818
): [Setter<T | null>, (enabled: boolean) => void] => {
1919
const [element, setElement] = createSignal<T | null>(null)
@@ -43,7 +43,7 @@ export const createAutoAnimate = <T extends HTMLElement>(
4343

4444
export const createAutoAnimateDirective = () => {
4545
return (
46-
el: HTMLElement,
46+
el: HTMLElement | SVGElement,
4747
options: Accessor<Partial<AutoAnimateOptions> | AutoAnimationPlugin | true>
4848
) => {
4949
let optionsValue = options()

‎src/vue/index.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import autoAnimate, {
88
} from "../index"
99

1010
export const vAutoAnimate: Directive<
11-
HTMLElement | Component,
11+
HTMLElement | SVGElement | Component,
1212
Partial<AutoAnimateOptions>
1313
> = autoAnimateDirective as unknown as Directive<
14-
HTMLElement | Component,
14+
HTMLElement | SVGElement | Component,
1515
Partial<AutoAnimateOptions>
1616
>
1717

@@ -20,7 +20,10 @@ export const vAutoAnimate: Directive<
2020
*/
2121
export function createVAutoAnimate(
2222
defaults?: Partial<AutoAnimateOptions> | AutoAnimationPlugin
23-
): Directive<HTMLElement, Partial<AutoAnimateOptions> | AutoAnimationPlugin> {
23+
): Directive<
24+
HTMLElement | SVGElement,
25+
Partial<AutoAnimateOptions> | AutoAnimationPlugin
26+
> {
2427
return {
2528
mounted(el, binding) {
2629
let resolved: Partial<AutoAnimateOptions> | AutoAnimationPlugin = {}
@@ -44,7 +47,7 @@ export function createVAutoAnimate(
4447
},
4548
getSSRProps: () => ({}),
4649
} as unknown as Directive<
47-
HTMLElement,
50+
HTMLElement | SVGElement,
4851
Partial<AutoAnimateOptions> | AutoAnimationPlugin
4952
>
5053
}
@@ -73,13 +76,17 @@ export function useAutoAnimate<T extends Element | Component>(
7376
}
7477
onMounted(() => {
7578
watchEffect((onCleanup) => {
76-
let el: HTMLElement | undefined
77-
if (element.value instanceof HTMLElement) {
79+
let el: HTMLElement | SVGElement | undefined
80+
if (
81+
element.value instanceof HTMLElement ||
82+
element.value instanceof SVGElement
83+
) {
7884
el = element.value
7985
} else if (
8086
element.value &&
8187
"$el" in element.value &&
82-
element.value.$el instanceof HTMLElement
88+
(element.value.$el instanceof HTMLElement ||
89+
element.value.$el instanceof SVGElement)
8390
) {
8491
el = element.value.$el
8592
}

‎tests/e2e/svg-types.spec.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { test, expect } from '@playwright/test'
2+
import { execFileSync } from 'node:child_process'
3+
import { fileURLToPath } from 'node:url'
4+
import path from 'node:path'
5+
6+
test.describe('SVG type assignability (issue #100)', () => {
7+
test('autoAnimate and vAutoAnimate accept SVG elements', () => {
8+
const root = fileURLToPath(new URL('../..', import.meta.url))
9+
const tsc = path.join(root, 'node_modules/typescript/bin/tsc')
10+
let output = ''
11+
try {
12+
output = execFileSync(
13+
tsc,
14+
['--pretty', 'false', '-p', 'tests/types/tsconfig.json'],
15+
{ encoding: 'utf8', cwd: root },
16+
)
17+
} catch (err) {
18+
const error = err as { stdout?: string; stderr?: string }
19+
throw new Error(error.stdout || error.stderr || String(err))
20+
}
21+
expect(output).toBe('')
22+
})
23+
})

‎tests/types/svg-element.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import autoAnimate, { vAutoAnimate } from "../../src/index"
2+
3+
declare const svg: SVGSVGElement
4+
declare const html: HTMLElement
5+
declare const group: SVGGElement
6+
7+
autoAnimate(svg)
8+
autoAnimate(html)
9+
autoAnimate(group)
10+
11+
const binding = {
12+
value: undefined as undefined,
13+
}
14+
vAutoAnimate.mounted(svg, binding)
15+
vAutoAnimate.mounted(html, binding)
16+
vAutoAnimate.unmounted(svg)
17+
vAutoAnimate.unmounted(html)

‎tests/types/tsconfig.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": true,
5+
"noUnusedLocals": false,
6+
"noUnusedParameters": false
7+
},
8+
"include": ["./svg-element.ts"]
9+
}

0 commit comments

Comments
 (0)