Skip to content
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
22 changes: 21 additions & 1 deletion build/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", [
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -330,6 +349,7 @@ async function main() {
await baseBuildMin()
await reactBuild()
await preactBuild()
await svelteBuild()
await solidBuild()
await vueBuild()
await angularBuild()
Expand Down
23 changes: 23 additions & 0 deletions docs/src/examples/svelte/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,26 @@ export default {
`,
},
}

export const svelteAttachment = {
svelte: {
ext: "svelte",
language: "html",
example: `<script>
import { autoAnimate } from '@formkit/auto-animate/svelte'
let tags = $state(['Rock', 'Punk'])
let duration = $state(250)
</script>

<!-- 👀 bare -->
<ul {@attach autoAnimate}>
{#each tags as tag (tag)}<li>{tag}</li>{/each}
</ul>

<!-- with options (reactive) -->
<ul {@attach autoAnimate({ duration })}>
{#each tags as tag (tag)}<li>{tag}</li>{/each}
</ul>
`,
},
}
10 changes: 9 additions & 1 deletion docs/src/sections/SectionUsage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -261,6 +261,14 @@ import IconQwik from "../components/IconQwik.vue"
&#125;&#125;&gt;</code
>
</AsideTip>
<h3 id="usage-svelte-attach">Svelte 5 attachment</h3>
<p>
On Svelte 5, import <code>autoAnimate</code> from
<code>@formkit/auto-animate/svelte</code> and use it as an
<a href="https://svelte.dev/docs/svelte/@attach">attachment</a> — bare,
or called with options (which stay reactive):
</p>
<CodeExample :examples="svelteAttachment" title="App" />

<h2 id="usage-angular">Angular directive</h2>
<p>
Expand Down
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
59 changes: 59 additions & 0 deletions src/svelte/index.ts
Original file line number Diff line number Diff line change
@@ -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<AutoAnimateOptions> | 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
* <ul {@attach autoAnimate}>…</ul>
* <ul {@attach autoAnimate({ duration: 100 })}>…</ul>
* ```
*/
export function autoAnimate(el: HTMLElement | SVGElement): Cleanup
export function autoAnimate(
config?: Partial<AutoAnimateOptions> | AutoAnimationPlugin
): AutoAnimateAttachment
export function autoAnimate(
elOrConfig?:
| HTMLElement
| SVGElement
| Partial<AutoAnimateOptions>
| AutoAnimationPlugin
): Cleanup | AutoAnimateAttachment {
if (
typeof Element !== "undefined" &&
elOrConfig instanceof Element
) {
return attach(elOrConfig as HTMLElement | SVGElement)
}
const config = elOrConfig as
| Partial<AutoAnimateOptions>
| AutoAnimationPlugin
| undefined
return (el) => attach(el, config)
}

export default autoAnimate
11 changes: 11 additions & 0 deletions tests/e2e/exports.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
54 changes: 54 additions & 0 deletions tests/e2e/svelte-attachment.spec.ts
Original file line number Diff line number Diff line change
@@ -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)
})
Loading