From 5341ec12dd87c3a67bf61109167423a93fdbfda1 Mon Sep 17 00:00:00 2001 From: Justin Schroeder Date: Sun, 22 Nov 2026 15:42:22 -0500 Subject: [PATCH] feat: Svelte 5 attachment at @formkit/auto-animate/svelte (#235) --- build/bundle.ts | 22 ++++++++++- docs/src/examples/svelte/index.ts | 23 +++++++++++ docs/src/sections/SectionUsage.vue | 10 ++++- package.json | 5 +++ src/svelte/index.ts | 59 +++++++++++++++++++++++++++++ tests/e2e/exports.spec.ts | 11 ++++++ tests/e2e/svelte-attachment.spec.ts | 54 ++++++++++++++++++++++++++ 7 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 src/svelte/index.ts create mode 100644 tests/e2e/svelte-attachment.spec.ts diff --git a/build/bundle.ts b/build/bundle.ts index 613e50a4..4659d5d6 100644 --- a/build/bundle.ts +++ b/build/bundle.ts @@ -91,6 +91,20 @@ async function preactBuild() { await writeFile(resolve(rootDir, "dist/preact/index.mjs"), raw) } +async function svelteBuild() { + info("Rolling up Svelte attachment") + await execa("npx", [ + "rollup", + "-c", + "rollup.config.js", + "--environment", + "FRAMEWORK:svelte", + ]) + let raw = await readFile(resolve(rootDir, "dist/svelte/index.mjs"), "utf8") + raw = raw.replace("from '../index'", "from '../index.mjs'") + await writeFile(resolve(rootDir, "dist/svelte/index.mjs"), raw) +} + async function vueBuild() { info("Rolling up Vue package") await execa("npx", [ @@ -204,6 +218,11 @@ async function bundleDeclarations() { `${rootDir}/dist/src/qwik/index.d.ts`, `${rootDir}/dist/qwik/index.d.ts`, ]) + await execa("shx", [ + "mv", + `${rootDir}/dist/src/svelte/index.d.ts`, + `${rootDir}/dist/svelte/index.d.ts`, + ]) await execa("shx", ["rm", "-rf", `${rootDir}/dist/src`]) await execa("shx", ["rm", `${rootDir}/dist/index.js`]) await fixDeclarationImports() @@ -216,7 +235,7 @@ async function bundleDeclarations() { * resolution mode (node10, node16/nodenext, bundler). See #110. */ async function fixDeclarationImports() { - for (const framework of ["react", "preact", "solid", "vue", "angular", "qwik"]) { + for (const framework of ["react", "preact", "solid", "vue", "angular", "qwik", "svelte"]) { const file = `${rootDir}/dist/${framework}/index.d.ts` let contents: string try { @@ -330,6 +349,7 @@ async function main() { await baseBuildMin() await reactBuild() await preactBuild() + await svelteBuild() await solidBuild() await vueBuild() await angularBuild() diff --git a/docs/src/examples/svelte/index.ts b/docs/src/examples/svelte/index.ts index 17097357..7f3c8ccf 100644 --- a/docs/src/examples/svelte/index.ts +++ b/docs/src/examples/svelte/index.ts @@ -38,3 +38,26 @@ export default { `, }, } + +export const svelteAttachment = { + svelte: { + ext: "svelte", + language: "html", + example: ` + + + + + + +`, + }, +} diff --git a/docs/src/sections/SectionUsage.vue b/docs/src/sections/SectionUsage.vue index a3d81bef..982d2c19 100644 --- a/docs/src/sections/SectionUsage.vue +++ b/docs/src/sections/SectionUsage.vue @@ -17,7 +17,7 @@ import ActualReactApp from "../examples/react/ActualReactApp.vue" import ActualPreactApp from "../examples/preact/ActualPreactApp.vue" import ActualSolidApp from "../examples/solid/ActualSolidApp.vue" import ActualDropdown from "../examples/dropdown/ActualDropdown.vue" -import svelteAction from "../examples/svelte" +import svelteAction, { svelteAttachment } from "../examples/svelte" import markoTag from "../examples/marko" import ActualMarkoApp from "../examples/marko/ActualMarkoApp.vue" import qwikHook from "../examples/qwik" @@ -261,6 +261,14 @@ import IconQwik from "../components/IconQwik.vue" }}> +

Svelte 5 attachment

+

+ On Svelte 5, import autoAnimate from + @formkit/auto-animate/svelte and use it as an + attachment — bare, + or called with options (which stay reactive): +

+

Angular directive

diff --git a/package.json b/package.json index 4e8d018c..716093a2 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,11 @@ "import": "./preact/index.mjs", "default": "./preact/index.mjs" }, + "./svelte": { + "types": "./svelte/index.d.ts", + "import": "./svelte/index.mjs", + "default": "./svelte/index.mjs" + }, "./react": { "types": "./react/index.d.ts", "import": "./react/index.mjs", diff --git a/src/svelte/index.ts b/src/svelte/index.ts new file mode 100644 index 00000000..00216bea --- /dev/null +++ b/src/svelte/index.ts @@ -0,0 +1,59 @@ +import autoAnimateCore, { + AutoAnimateOptions, + AutoAnimationPlugin, +} from "../index" + +/** + * A Svelte 5 attachment cleanup function. + */ +type Cleanup = () => void + +/** + * A Svelte 5 attachment: `(element) => cleanup`. + */ +export type AutoAnimateAttachment = (el: HTMLElement | SVGElement) => Cleanup + +function attach( + el: HTMLElement | SVGElement, + config?: Partial | AutoAnimationPlugin +): Cleanup { + const controller = autoAnimateCore(el as HTMLElement, config ?? {}) + return () => controller.destroy?.() +} + +/** + * AutoAnimate as a Svelte 5 attachment + * (https://svelte.dev/docs/svelte/@attach). Use it bare, or call it with + * options or a plugin. Options read inside the call are reactive: when they + * change, Svelte tears the attachment down and re-attaches it. + * + * ```svelte + *

+ * + * ``` + */ +export function autoAnimate(el: HTMLElement | SVGElement): Cleanup +export function autoAnimate( + config?: Partial | AutoAnimationPlugin +): AutoAnimateAttachment +export function autoAnimate( + elOrConfig?: + | HTMLElement + | SVGElement + | Partial + | AutoAnimationPlugin +): Cleanup | AutoAnimateAttachment { + if ( + typeof Element !== "undefined" && + elOrConfig instanceof Element + ) { + return attach(elOrConfig as HTMLElement | SVGElement) + } + const config = elOrConfig as + | Partial + | AutoAnimationPlugin + | undefined + return (el) => attach(el, config) +} + +export default autoAnimate diff --git a/tests/e2e/exports.spec.ts b/tests/e2e/exports.spec.ts index 76bd3a16..01a18eb6 100644 --- a/tests/e2e/exports.spec.ts +++ b/tests/e2e/exports.spec.ts @@ -10,3 +10,14 @@ test.describe('ESM exports', () => { }) }) + +test.describe('Svelte attachment export', () => { + test('@formkit/auto-animate/svelte exports an attachment factory', async () => { + const url = new URL('../../dist/svelte/index.mjs', import.meta.url).href + const mod = await import(url) + expect(typeof mod.autoAnimate).toBe('function') + expect(mod.default).toBe(mod.autoAnimate) + // called with options it returns an attachment (el => cleanup) + expect(typeof mod.autoAnimate({ duration: 100 })).toBe('function') + }) +}) diff --git a/tests/e2e/svelte-attachment.spec.ts b/tests/e2e/svelte-attachment.spec.ts new file mode 100644 index 00000000..3262b50f --- /dev/null +++ b/tests/e2e/svelte-attachment.spec.ts @@ -0,0 +1,54 @@ +import { test, expect } from '@playwright/test' +import { fileURLToPath } from 'node:url' + +const fixtureUrl = `/@fs${fileURLToPath(new URL('../fixtures/removal.html', import.meta.url))}` +const moduleUrl = `/@fs${fileURLToPath(new URL('../../src/svelte/index.ts', import.meta.url))}` + +test('attachment animates (bare and with options) and cleans up', async ({ page }) => { + await page.emulateMedia({ reducedMotion: 'no-preference' }) + await page.goto(fixtureUrl) + const r = await page.evaluate(async (moduleUrl) => { + const { autoAnimate } = (await import(moduleUrl)) as typeof import('../../src/svelte/index') + const tick = (ms = 30) => new Promise((res) => setTimeout(res, ms)) + const make = () => { + const ul = document.createElement('ul') + document.body.prepend(ul) + return ul + } + const add = (ul: Element) => { + const li = document.createElement('li') + li.textContent = 'x' + li.style.height = '20px' + ul.appendChild(li) + return li + } + const running = (el: Element) => el.getAnimations().length + + // bare: {@attach autoAnimate} + const bare = make() + const cleanupBare = autoAnimate(bare) + await tick() + const bareLi = add(bare) + await tick() + const bareAnimated = running(bareLi) > 0 + + // with options: {@attach autoAnimate({ duration: 400 })} + const withOpts = make() + const cleanup = autoAnimate({ duration: 400 })(withOpts) + await tick() + const li = add(withOpts) + await tick() + const duration = li.getAnimations()[0]?.effect?.getTiming().duration + + // after cleanup (Svelte detaching), changes no longer animate + cleanupBare() + cleanup() + await tick() + const after = add(withOpts) + await tick() + return { bareAnimated, duration, afterCleanup: running(after) } + }, moduleUrl) + expect(r.bareAnimated).toBe(true) + expect(r.duration).toBe(600) // add runs at duration × 1.5 + expect(r.afterCleanup).toBe(0) +})