|
| 1 | +# animated-outlet component |
| 2 | + |
| 3 | +Enable animation on web component swapping triggered by route transitions. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +1) Register a `AnimatedOutlet` web component to the tag to be used as router outlet. wc middleware uses 'router-outlet' tag as default, but any tag can be used. |
| 8 | + |
| 9 | +```javascript |
| 10 | +import { AnimatedOutlet } from 'slick-router/components/animated-outlet' |
| 11 | + |
| 12 | +customElements.define('router-outlet', AnimatedOutlet) |
| 13 | +``` |
| 14 | + |
| 15 | +2) Add an 'animation' attribute to the outlet that will be animated |
| 16 | + |
| 17 | +```html |
| 18 | +<router-outlet animation></router-outlet> |
| 19 | +``` |
| 20 | + |
| 21 | +3) Write the animation using CSS transition or animation |
| 22 | + |
| 23 | +```css |
| 24 | +.outlet-enter-active, |
| 25 | +.outlet-leave-active { |
| 26 | + transition: opacity 0.5s; |
| 27 | +} |
| 28 | + |
| 29 | +.outlet-enter, .outlet-leave-to { |
| 30 | + opacity: 0; |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +The above example adds a fading effect to the element that is entering and the one which is leaving |
| 35 | + |
| 36 | +> The API is based on [Vue one](https://vuejs.org/v2/guide/transitions.html#Transition-Classes) and most of the Vue animations can be converted with little changes |
| 37 | +
|
| 38 | +Is possible to configure the CSS classes prefix through animation attribute, allowing to create more than one animation in same app: |
| 39 | + |
| 40 | +```html |
| 41 | +<router-outlet animation="bounce"></router-outlet> |
| 42 | +``` |
| 43 | + |
| 44 | +```css |
| 45 | +.bounce-enter { |
| 46 | + opacity: 0; |
| 47 | +} |
| 48 | + |
| 49 | +.bounce-enter-active { |
| 50 | + animation: bounce-in 0.5s; |
| 51 | +} |
| 52 | + |
| 53 | +.bounce-leave-active { |
| 54 | + animation: bounce-in 0.5s reverse; |
| 55 | +} |
| 56 | + |
| 57 | +@keyframes bounce-in { |
| 58 | + 0% { |
| 59 | + transform: scale(0); |
| 60 | + } |
| 61 | + 50% { |
| 62 | + transform: scale(1.5); |
| 63 | + } |
| 64 | + 100% { |
| 65 | + transform: scale(1); |
| 66 | + } |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +The example above uses classes prefixed with 'bounce-' instead of 'outlet-' |
| 71 | + |
| 72 | +[Live Demo](https://codesandbox.io/s/slick-router-css-animations-q0fzs) |
| 73 | + |
| 74 | +## Customization |
| 75 | + |
| 76 | +Is possible to customize how animation is done by creating and registering animation hook classes. It must extend from AnimationHook: |
| 77 | + |
| 78 | +```js |
| 79 | +import { AnimationHook } from 'slick-router/components/animated-outlet.js' |
| 80 | + |
| 81 | +class MyAnimation extends AnimationHook { |
| 82 | + beforeEnter (outlet, el) { |
| 83 | + // prepare element before is connected |
| 84 | + } |
| 85 | + |
| 86 | + enter (outlet, el) { |
| 87 | + // run enter animation |
| 88 | + } |
| 89 | + |
| 90 | + leave (outlet, el, done) { |
| 91 | + // run leave animation and call done on finish |
| 92 | + done() |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +The hook class can be registered as default with `setDefaultAnimation` or to predefined animations using `registerAnimation` |
| 98 | + |
| 99 | +Out of box, is provided the `AnimateCSS` class that allows to use [animate.css](https://github.com/daneden/animate.css) |
| 100 | + |
| 101 | +```js |
| 102 | +import { |
| 103 | + AnimatedOutlet, |
| 104 | + AnimateCSS, |
| 105 | + setDefaultAnimation, |
| 106 | + registerAnimation |
| 107 | +} from 'slick-router/components/animated-outlet.js' |
| 108 | + |
| 109 | +setDefaultAnimation(AnimateCSS, { enter: 'fadeIn', leave: 'fadeOut' }) |
| 110 | + |
| 111 | +registerAnimation('funky', AnimateCSS, { enter: 'rotateInDownRight', leave: 'hinge' }) |
| 112 | +``` |
| 113 | + |
| 114 | +```html |
| 115 | +<router-outlet animation></router-outlet> <!-- the default animate.css fade --> |
| 116 | + |
| 117 | +<router-outlet animation enter="zoomIn" leave="zoomOut"></router-outlet> <!-- override animation using enter and leave attrs --> |
| 118 | + |
| 119 | +<router-outlet animation="funky"></router-outlet> <!-- use the predefined funky animation --> |
| 120 | +``` |
| 121 | + |
| 122 | +[Live demo](https://codesandbox.io/s/slick-router-animate-css-zpg96) |
| 123 | + |
| 124 | +It's possible to use JS animation libraries like [GSAP](https://codesandbox.io/s/slick-router-gsap-animations-oqbp5) or even as [standalone component](https://codesandbox.io/s/animated-outlet-page-transitions-7vgcy) (without routing envolved) |
| 125 | + |
| 126 | + |
0 commit comments