-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
Copy pathSectionUsage.vue
291 lines (283 loc) · 12 KB
/
SectionUsage.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<script setup lang="ts">
import CodeExample from "../components/CodeExample.vue"
import AsideTip from "../components/AsideTip.vue"
import dropdown from "../examples/dropdown"
import config from "../examples/config"
import {
vueDirectiveMain,
vueDirectiveApp,
vueComposable,
} from "../examples/vue"
import { angularDirectiveMain, angularDirectiveApp } from "../examples/angular"
import { solidPrimitive, solidDirective } from "../examples/solid"
import reactHook from "../examples/react"
import disabledExamples from "../examples/disable"
import ActualReactApp from "../examples/react/ActualReactApp.vue"
import ActualSolidApp from "../examples/solid/ActualSolidApp.vue"
import ActualDropdown from "../examples/dropdown/ActualDropdown.vue"
import svelteAction from "../examples/svelte"
import ActualSvelteApp from "../examples/svelte/ActualSvelteApp.vue"
import ActualVueApp from "../examples/vue/ActualVueApp.vue"
import ActualVueAppHook from "../examples/vue/ActualVueAppHook.vue"
import ActualAngularApp from "../examples/angular/ActualAngularApp.vue"
import ActualDisable from "../examples/disable/ActualDisable.vue"
import Vitest from '../examples/vitest/index.ts'
import jestReactHook from "../examples/jest-react-hook"
import IconReact from "../components/IconReact.vue"
import IconVue from "../components/IconVue.vue"
import IconAngular from "../components/IconAngular.vue"
import IconSvelte from "../components/IconSvelte.vue"
import IconSolid from "../components/IconSolid.vue"
</script>
<template>
<section id="usage">
<h2>Usage</h2>
<p>
AutoAnimate is fundamentally a single function —
<code>autoAnimate</code> — that accepts a parent element. Automatic
animations will be applied to the parent element and its immediate
children. Animations are specifically triggered when one of three events
occurs:
</p>
<ul>
<li>A child is <strong>added</strong> in the DOM.</li>
<li>A child is <strong>removed</strong> in the DOM.</li>
<li>A child is <strong>moved</strong> in the DOM.</li>
</ul>
<p>
Let’s see what this looks like in practice. For now we'll use the
<code>autoAnimate</code> function directly. React and Vue users — you’ll
get some additional syntactic sugar later on — but for now let's learn the
fundamentals:
</p>
<CodeExample :examples="dropdown" title="Dropdown" />
<ActualDropdown />
<p>
Too easy! A gentle, smooth shift without adding any transition classes or
custom CSS. This is a notable upgrade for end users with minimal developer
effort required. <a href="#examples">Checkout the examples</a>
to see other use cases.
</p>
<h3>Tips for success</h3>
<ul>
<li>
It’s still ok to use other kinds of transitions. For example, if you are
making stylistic changes with just CSS (such as a hover effect), then
use standard CSS transitions for these kinds of styling tweaks.
</li>
<li>
Animations are only triggered when immediate children of the parent
element (the one you passed to <code>autoAnimate</code>) are added,
removed, or moved.
</li>
<li>
The parent element will automatically receive
<code>position: relative</code> if it is statically positioned. Keep
this in mind when writing your styles.
</li>
<li>
Sometimes flexbox layouts don’t resize their children immediately. A
child with a <code>flex-grow: 1</code> property waits for the
surrounding content before snapping to its full width. AutoAnimate
doesn’t work well in these cases, but if you give the element a more
explicit width it should work like a charm.
</li>
</ul>
<AsideTip
>AutoAnimate respects a user’s <code>prefers-reduced-motion</code> setting
and will automatic disable if the user has indicated they want reduced
motion. Checkout the
<a
href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion"
>MDN docs for more information</a
>
on this media feature.
</AsideTip>
<h3>Configuration</h3>
<p>
AutoAnimate is intended to be used with <em>zero-configuration</em>. We
believe the default configuration falls in line with the project’s
objective:
<em class="important"
>AutoAnimate’s goal is to substantially improve an application’s
user-experience without impacting the developer’s implementation time or
performance budget.</em
>
That said, some minor configuration options are available. AutoAnimate
allows you to pass a second argument to <code>autoAnimate</code> with the
following options:
</p>
<CodeExample :examples="config" title="config" />
<p>
If your project’s specific requirements make it necessary to dramatically
change the default animations, then you should
<a href="#plugins">check out the plugins documentation</a>. Next, checkout
the usage documentation for your framework.
</p>
<ul class="framework-jumplinks">
<li>
<a href="#usage-react"
><span>React</span>
<IconReact />
</a>
</li>
<li>
<a href="#usage-solid"><span>Solid</span><IconSolid /></a>
</li>
<li>
<a href="#usage-vue"><span>Vue</span><IconVue /></a>
</li>
<li>
<a href="#usage-svelte"><span>Svelte</span><IconSvelte /></a>
</li>
<li>
<a href="#usage-angular"><span>Angular</span><IconAngular /></a>
</li>
</ul>
<h2 id="usage-react">React hook</h2>
<p>
React users can use the hook <code>useAutoAnimate</code> by importing it
from <code>@formkit/auto-animate/react</code>. This hook returns a ref to
apply to the parent element, as well as a function to
<a href="#usage-disable">enable or disable</a> animations.
</p>
<CodeExample :examples="reactHook" title="App" />
<ActualReactApp />
<h3>Testing</h3>
<p>
For now, if you're trying to use auto-animate with tests, you might face
some difficulties along the way due to two main problems. One is because
tools like Vitest and Jest use JSDom to emulates the browser, and some DOM
features that auto-animate uses don't yet exist in the JSDom package.
For these cases, we can mock the respective resources in our test setup,
like this example using Vitest, but you can do the same with any test framework.
</p>
<h4>Setup with Vitest:</h4>
<CodeExample :examples="Vitest" title="Vitest" />
<p>
The second issue is with some other testing tools that cannot handle ES Modules, such as Jest.
In that case we can try to configure your project to handle ESM, and then
you won't face these problems.
</p>
<p>
But if you have some problems with this setup, because Jest's own support
for ESM is an experimental feature, you can mock the entirely auto-animate hook:
</p>
<h4>Mocking the auto-animate Hook on Jest</h4>
<CodeExample :examples="jestReactHook" title="Jest" />
<AsideTip>
Your Jest config should includes the path of your mock, on this example
it looks like this: <br />
<code>modulePaths: ["<rootDir>/jest"]</code>
</AsideTip>
<h2 id="usage-solid">Solid Primitive</h2>
<p>
Solid users can use the function <code>createAutoAnimate</code> by
importing it from <code>@formkit/auto-animate/solid</code>. This hook
returns a ref to apply to the parent element, as well as a function to
<a href="#usage-disable">enable or disable</a> animations.
</p>
<CodeExample :examples="solidPrimitive" title="App" />
<ActualSolidApp />
<h3 id="usage-solid-directive">Solid Directive</h3>
<p>
Solid users can also use the directive <code>autoAnimate</code> by
importing it from <code>@formkit/auto-animate/solid</code>.
</p>
<CodeExample :examples="solidDirective" title="App" />
<h2 id="usage-vue">Vue directive</h2>
<p>
Vue users can globally register the
<code>v-auto-animate</code> directive. This makes adding transitions and
animations as easy as applying an attribute. Import the Vue plugin from
<code>@formkit/auto-animate/vue</code> and register it with your Vue app:
</p>
<CodeExample :examples="vueDirectiveMain" title="main" />
<p>
Once you’ve registered the plugin, it can be applied anywhere in your
application by adding the <code>v-auto-animate</code> directive to the
parent element:
</p>
<CodeExample :examples="vueDirectiveApp" title="App" />
<ActualVueApp />
<AsideTip>
Please bare in mind that value passed to <code>:key</code> should be a
unique value, otherwise animation might not work as expected.
</AsideTip>
<AsideTip>
Vue users can pass options by directly setting the directive’s value
<code><ul v-auto-animate="{ duration: 100 }"></code>
</AsideTip>
<h3 id="usage-vue-composable">Vue composable</h3>
<p>
As an alternative to the <code>v-auto-animate</code> directive, Vue devs
can use the <code>useAutoAnimate</code> composable. This composable
supports all the same great features, but also provides a mechanism to
<a href="usage-disable">enable and disable</a> animations.
</p>
<p>
Import the composable from <code>@formkit/auto-animate/vue</code>, and
call it in <code>script setup</code> to create a
<a
href="https://vuejs.org/guide/essentials/template-refs.html#template-refs"
target="_blank"
rel="noopener noreferrer"
>template ref</a
>. Use the <code>ref</code> attribute on your parent element to store it
in the template ref:
</p>
<CodeExample :examples="vueComposable" title="App" />
<ActualVueAppHook />
<AsideTip>
Vue users can pass options directly to the composable: <br />
<code>useAutoAnimate({ duration: 100 })</code>
</AsideTip>
<h2 id="usage-svelte">Svelte action</h2>
<p>
The root <code>autoAnimate</code> function can be directly used as a
Svelte action. Just import <code>autoAnimate</code> and use it as a
directive on the parent element.
</p>
<CodeExample :examples="svelteAction" title="App" />
<ActualSvelteApp />
<AsideTip>
Svelte users can pass options by directly setting the action’s value
<code
><ul use:autoAnimate={{ duration: 1000
}}></code
>
</AsideTip>
<h2 id="usage-angular">Angular directive</h2>
<p>
Angular users can globally register the
<code>auto-animate</code> directive. This makes adding transitions and
animations as easy as applying an attribute. Import the AutoAnimateModule
from <code>@formkit/auto-animate/angular</code> and register it with your
Angular app:
</p>
<CodeExample :examples="angularDirectiveMain" title="App" />
<p>
Once you’ve registered the plugin, it can be applied anywhere in your
application by adding the <code>auto-animate</code> directive to the
parent element:
</p>
<CodeExample :examples="angularDirectiveApp" title="App" />
<ActualAngularApp />
<AsideTip>
Angular users can pass options by directly setting the options input
<code><ul auto-animate [options]="{ duration: 100 }"></code>
</AsideTip>
<h2 id="usage-disable">Disable animations</h2>
<p>
Occasionally it may be necessary to temporarily disable animations and
then re-enable them later on. The <code>autoAnimate</code> function
returns an animation controller with <code>enable()</code> and
<code>disable()</code> methods for this purpose, and the Vue and React
hooks (<code>useAutoAnimate</code>) return a tuple with the second value
being an enable/disable function that accepts a boolean to enable or
disable animations.
</p>
<CodeExample :examples="disabledExamples" title="Juggle" />
<ActualDisable />
</section>
</template>