Coder Social home page Coder Social logo

turbolinks-animate's Introduction

Turbolinks Animate

NPM Version Travis

A dead simple & powerful way of adding rich & adaptive animations to your app which is already using Turbolinks™.

Extensions


Table of Contents


Usage

Functions

// Shows the initialized element
TurbolinksAnimate.appear();

// Hides the initialized element
TurbolinksAnimate.disappear();

Options

There are a number of ways in which you can adopt Turbolinks Animate to your needs:

Animations:

The vital part is choosing an animation to play. Turbolinks Animate utilizes Animate.css to power them. These are the animations which are currently accessible:

Full list of animations
  • fadeIn
  • fadeInUp
  • fadeInDown
  • fadeInRight
  • fadeInLeft
  • fadeInUpBig
  • fadeInDownBig
  • fadeInRightBig
  • fadeInLeftBig
  • fadeOut
  • fadeOutUp
  • fadeOutDown
  • fadeOutRight
  • fadeOutLeft
  • fadeOutUpBig
  • fadeOutDownBig
  • fadeOutRightBig
  • fadeOutLeftBig
  • bounceIn
  • bounceInUp
  • bounceInDown
  • bounceInRight
  • bounceInLeft
  • bounceOut
  • bounceOutUp
  • bounceOutDown
  • bounceOutRight
  • bounceOutLeft
  • flipInX
  • flipInY
  • flipOutX
  • flipOutY
  • lightSpeedIn
  • lightSpeedOut
  • rotateIn
  • rotateInDownLeft
  • rotateInDownRight
  • rotateInUpRight
  • rotateInUpLeft
  • rotateOut
  • rotateOutDownLeft
  • rotateOutDownRight
  • rotateOutUpRight
  • rotateOutUpLeft
  • rollIn
  • rollOut
  • zoomIn
  • zoomInUp
  • zoomInDown
  • zoomInRight
  • zoomInLeft
  • zoomOut
  • zoomOutUp
  • zoomOutDown
  • zoomOutRight
  • zoomOutLeft
  • slideInUp
  • slideInDown
  • slideInRight
  • slideInLeft
  • slideOutUp
  • slideOutDown
  • slideOutRight
  • slideOutLeft

There are three ways in which you can specify the animation you want to use. To choose a globally used animation pass an option when initializing Turbolinks Animate:

TurbolinksAnimate.init({animation: 'fadeinright'});

Note: The option falls back to fadein.

Note: As a global choice you would only want to use appearing animations, as they will get fade out automatically when the current view disappears.

For alternate approaches take a look at inline animations and animations overriding animations.

Options:

  • duration CSS value for animation-duration. Accepts a string. Defaults to 0.3s.

  • delay Milliseconds after which animation starts. Accepts an integer or false. Defaults to false.

  • reversedDisappearing Whether or not a reversed animation should be used when disappearing. Accepts a boolean. Defaults to false.

  • breakpoints An array of breakpoint objects to specify breakpoints used for Per Device-Type animations. Accepts an array. Defaults to: [{ name: 'mobile', width: 500 },{ name: 'tablet', width: 1024 },{ name: 'desktop', width: 1440 }]

  • customListeners Restore the behavior of versions < 2 to set custom listeners to run appear() and disappear() functions. Accepts a boolean. Defaults to false.

Example:

TurbolinksAnimate.init({animation: 'fadeinright', duration: '1s', delay: 1000});

Inline animations

With Turbolinks Animate you are able to set animations based on the links, who got clicked:

<a href="" data-turbolinks-animate-animation="fadeout" data-turbolinks-animate-duration="0.3s" data-turbolinks-animate-delay="250">I am a link!</a>

Attributes:

  • data-turbolinks-animate-animation Animation to be applied when disappearing after a hyperlink got clicked. Accepts a string. Set it to 'false' to disable Turbolinks Animate on this specific link.

  • data-turbolinks-animate-appear Animation to be applied when appearing on the next view after a hyperlink got clicked. Accepts a string.

  • data-turbolinks-animate-duration CSS value for animation-duration. Accepts a string.

  • data-turbolinks-animate-delay Milliseconds after which animation starts. Accepts an integer or false.

Per Device-Type

In addition you can specify animations specifically for certain screen sizes, just pass a hash:

TurbolinksAnimate.init({animation: {'mobile': 'fadeinup', 'tablet': 'fadeindown', 'desktop': 'fadein'}});

Note: You can customize the breakpoints through the options.

Overriding animations

A lot of times with frameworks like Ruby on Rails you want to be able to specify animations from within your controllers and views without nasty javascript nesting.

With Turbolinks Animate you can just add a data attribute to your initialized element, naming the animation you want to use. It will override the global default:

<body data-turbolinks-animate-animation="fadeinup"></body>

Persistent elements

A lot of times you want to persist certain elements throughout requests, for example a navigation bar or other parts of your layout that is being shared between views. Turbolinks Animate makes it dead simple to declare persistent elements in your view:

<body data-turbolinks-animate-animation="fadein">
  <h1 data-turbolinks-animate-persist="true">My app</h1>
  <p>This is specific to my view!</p>
</body>

Note: Elements don't actually persist, the get replaced by the fetched page just like any other element. But because no animation gets applied, they look just as if the persist (as long as the newly fetched page includes the exact same element in the same position).

Setting data-turbolinks-animate-persist to true will result in the entire element (including its children) being excluded from the applied animations. If you want to apply the animations to children of the persistent element, but still keep it untouched, append -itself to the data attribute. This is especially useful, when you apply a background color to your element, which remains the same, but changes it contents:

<body data-turbolinks-animate-animation="fadein">
  <nav data-turbolinks-animate-persist-itself="true" style="background: black;">
    <h1 style="color: white;">View specific title</h1>
  </nav>
  <p>This is specific to my view!</p>
</body>

Animation types

Often your permanent elements depend on the hyperlink clicked. Just specify the animation type on the hyperlink tag, and replace true with the chosen type on the persistent element:

<body data-turbolinks-animate-animation="fadein">
  <nav data-turbolinks-animate-persist-itself="nav" style="background: black;">
    <h1 style="color: white;">View specific title</h1>
  </nav>
  <a href="/do" data-turbolinks-animate-type="nav">Persist navigation!</a>
  <a href="/doo">Don't persist navigation!</a>
</body>

Element transitions

In a lot of cases it can be useful to apply custom CSS transitions to specific elements when the page changes. This works especially well with background colors of persisted elements, but can be used for any CSS property on any element. Multiple properties can be transitioned using using comma separated values.

<header data-turbolinks-animate-persist-itself="true" data-turbolinks-animate-transition="background-color,opacity">
  <!-- ... -->
</header>
header {
  transition: 0.25s background-color ease-out;
}

#page1 header {
  background-color: blue;
}

#page2 header {
  background-color: red;
}

When you have a third page, which doesn't contain a header element, the page transition performs normally as this method only applies when Turbolinks Animate can find a matching element on the new page.

Important: Unless you only use this data attribute for elements that can be distinguished by their HTML tag, you have to declare an id.

Events

Turbolinks Animate emits events that allow you to track the animation lifecycle. Turbolinks Animate fires events on the document object.

  • turbolinks:animation-start fires when an animation starts. The main Turbolinks Animate element can be accessed with event.data.element. Access the animation with event.data.animation. Access whether content appears or disappears with event.data.disappearing.

  • turbolinks:animation-end fires when an animation ends. The main Turbolinks Animate element can be accessed with event.data.element. Access whether content appeared or disappeared with event.data.disappearing.


Testing

  1. Fork this repository

  2. Clone your forked git locally

  3. Install dependencies

    $ yarn install

  4. Run ESLint

    $ yarn eslint


Release

  1. Review breaking changes and deprecations in CHANGELOG.md.
  2. Change the version in package.json.
  3. Reset CHANGELOG.md.
  4. Create a pull request to merge the changes into master.
  5. After the pull request was merged, create a new release listing the breaking changes and commits on master since the last release.
  6. The release workflow will publish the package to NPM and GPR.

To Do

We use GitHub projects to coordinate the work on this project.

To propose your ideas, initiate the discussion by adding a new issue.


Contributing

We hope that you will consider contributing to Turbolinks Animate. Please read this short overview for some information about how to get started:

Learn more about contributing to this repository, Code of Conduct

Semantic Versioning

Turbolinks Animate follows Semantic Versioning 2.0 as defined at http://semver.org.

turbolinks-animate's People

Contributors

daltonrooney avatar dependabot[bot] avatar depfu[bot] avatar jonhue avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

turbolinks-animate's Issues

Enable cache between pages

If I'm understanding right turbolinks-animate works removing "preview" page which on navigation between pages, right?

Is there way to enable the default Turbolinks cache?

Prevent Animations when actions are within the same controller URL

Is your feature request related to a problem? Please describe.
When viewing, editing and saving data from within the same model/controller the constant animation between screens gives the user the sense that the context of the app is changing.

Describe the solution you'd like
Simply prevent the animations from firing when the base url path is not changing.

works on first load but then I get "cannot ready property 'desktop' of undefined

I added the demo code exactly as is

$(document).on( 'turbolinks:load', function() {
    $('body').turbolinksAnimate();
    turbolinksAnimateAppear();
});
$(document).on( 'turbolinks:request-start', function() {
    turbolinksAnimateDisappear();
});
$(window).on( 'popstate beforeunload', function(event) {
    turbolinksAnimateDisappear();
});

It works beautifully on initial load, but then on subsequent clicks I get this error

screenshot 2017-12-27 19 09 49

any idea what could be wrong?

Persist itself with a loader

Allow for the specification of a loader element within persistent (itself) elements. This element would then replace (fadein-fadeout/custom) the persistent elements content while the request is in progress.

Release 3.1.0

9aa532c [#28] Transition persisted elements (#31)
34ce41f [#15] Update README.md (#30)
5b9298f [#19] Implement CI (#26)
78f19f4 [#17] Update contribution guidelines (#25)
2cf0955 [#16] Update pull request template (#24)
c3fa6d3 [#18] Update code of conduct (#23)
d1798fd Update turbolinks to version 5.2.0 (#21)
a237c6f Update animate.css to version 3.7.0 (#20)
c2d480d [#11] Add issue templates (#12)
ac35abf Update turbolinks to version 5.1.1 (#9)
7b82393 Update animate.css to version 3.6.1 (#10)
d83dfc2 Update README.md

Can't link to specific part of page

Describe the bug
While using turbolinks animate, the normal browser behavior of linking a specific element on the page from the url breaks

To Reproduce
Steps to reproduce the behavior:

  1. Have turbolinks animate on the page. I currently have document.addEventListener('turbolinks:load', () => TurbolinksAnimate.init()) run in my
  2. Have an element with an id of #some-id down the page
  3. visit http://your-site.com#some-id
  4. It doesn't scroll down to that id
  5. remove the TurbolinksAnimate.init() from the
  6. visit http://your-site.com#some-id
  7. It scrolls down to the part of the page it should as browsers normally do.

Expected behavior
visiting http://your-site.com#some-id should scroll the element with id="some-id" should work by default in browsers

Desktop (please complete the following information):

  • OS: MacOS
  • Browser: chrome
  • Version: 79.0.3945.130

adding data-turbolinks-animate-persist completely changes overall behaviour

If I leave the defaults then the entire body fades completely in and out whethere I click a link or reload the whole browser.. working good so far

I would like to persist my navbar which is inside a

tag. So I added the persist option to it

<header data-turbolinks-animate-persist="true">

This breaks the behaviour. Now if I reload the whole browser (cmd-R) then it works, everything except the header properly fades in and out

but now if I use clicks from within the navbar then it doesn't work, the fade-in still works but the fade-out is completely gone

works on cmd-r, doesn't work on click

but without the persisted navbar, it works on both

For additional debugging I also tried

<header data-turbolinks-animate-persist-itself="header">

and even

<header data-turbolinks-animate-persist="false">

It seems like the very presence of any of these (even if set to false) on the header causes the fade-out animation to be skipped

any idea what could be wrong?

thanks!

Weird doc and usage

Is your feature request related to a problem? Please describe.
How is supposed to use this lib?

I'm using turbolinks on a static site, but i can't figure out how to use this. I just have a couple of html pages and 4 lines of js, but the readme doesn't tell much info under the usage section. This is my js, and turbolinks-animate doesn't work. What i'm missing?

import Turbolinks from 'turbolinks'
import TurbolinksAnimate from 'turbolinks-animate'

/**
 * Init Turbolinks within the site
 */
Turbolinks.start()
TurbolinksAnimate.init({ animation: 'fadeinright' })

1.3.1 - Reverse animation does not appear

My site's init:

document.addEventListener 'turbolinks:load', ->
  $('body').turbolinksAnimate({ animation: 'fadein', duration: '0.3s', delay: false })
  turbolinksAnimateAppear()

document.addEventListener 'turbolinks:request-start', ->
  turbolinksAnimateDisappear()

$(window).bind 'popstate', (event) ->
  turbolinksAnimateDisappear()

Expected behavior

When clicking on the link, the page is expected to fade out before fading in the new page.

Actual behavior

Page's fadeout animation does not happen, and jumps straight into fading in the next page

Steps to reproduce

Click on a link

Note - this did not happen on version 1.2.0

Documentation: emphasize need for importing Animate.css

First off, thank you for your great library! It's really useful. However, reading the documentation, it wasn't clear to me that I had to manually import animate.css in my CSS. Animations weren't working until I added @import '../../../node_modules/animate.css/animate.css' to my master scss file.

Perhaps you could add this to your documentation! Cheers!

Specify animation for element

Hi,

First of all, thanks for this awesome library!

I'm trying to animate page changes, where a specific element needs a different appear/disappear animation than the rest of the page.

Example markup:

<body>
    <div id="banner">...</div>
    <section id="page">...</page>
</body>

In this example I'd want all content to fadeIn between navigation, except #banner, which should use bounceIn. It seems that this is not possible with the current options.

I've not been able to find a way to achieve easily.

I'm suggesting allowing data-turbolinks-animate-animation to be added to child elements, effectively disabling disabling the parents animation on that element (kind of like persisting at the moment), and instead applying the animation specified by the data attribute, eg:

<body data-turbolinks-animate-animation="fadeIn">
    <div id="banner" data-turbolinks-animate-animation="bounceIn">...</div>
    <section id="page">...</page>
</body>

What do you think?

Feature request: Separate appear and disappear animations for inline syntax

Maybe I missed this in your implementation but is there a way to set different appear and disappear animations with the inline syntax?

I'm using slideInRight and slideInLeft for a next and previous button. The animation to let disappear the current page is done with the data attributes you named in the README. But I can't find a way to override the default (from init) to set the appear animation of the new page.

esm compatibility for three-shaking with Rollup

I would like to use Rollup to three-shaking this amazing code (e.g. I'm using only fadeIn).

Can you export default your code making it esm compatible?

Actually I'm using it like this:

import Turbolinks from 'turbolinks'
import 'turbolinks-animate'

Turbolinks.start()

document.addEventListener('turbolinks:load', () => window.TurbolinksAnimate.init())

Am I wrong in my desire?

Thanks.

init is not a function

Describe the bug
Uncaught TypeError: TurbolinksAnimate.init is not a function

To Reproduce
1 . npm i turbolinks-animate
2. In app.js file.
const NProgress = require("nprogress");
const TurbolinksAnimate = require("turbolinks-animate");

window.addEventListener("turbolinks:load", () => {
TurbolinksAnimate.init({
animation: "fadeinright",
duration: "1s",
delay: 1000
});
});

So I got the wrong message .
I don't know if this is a mistake or if I'm overlooking something.

Thank .

Demo is not available

Describe the bug
The demo of turbolinks-animate is not avalaible.

To Reproduce
Steps to reproduce the behavior:

  1. Go to 'https://jonhue.me/' the demo link
  2. See error

Expected behavior
A sample of the library in use.

Screenshots
noppe

Desktop (please complete the following information):
N/A

Smartphone (please complete the following information):
N/A

Additional context
N/A

Transition persisted elements

Is your feature request related to a problem? Please describe.
I'm trying to transition between two pages where a solid-color background banner element persists but changes color. Even though I've added a transition via CSS, it doesn't take place because the actual element is replaced in the DOM.

Describe the solution you'd like
It would be cool to have some way to define specific transitions on elements between pages as a data attribute. Specifically height and background color in this example, but I'm sure other attributes would be helpful as well.

can no longer persist since latest changes

Previously I had this problem #5 where the smooth body fadeout animation would not work if I added my navbar to be persisted.. but at least the navbar did persist throughout the not-smooth transition

since your latest changes, the animation on the body fadeout is smooth but I can no longer persist any elements.

I tried with your version 1.3.4 through 1.3.7 where the nav was persisted, and then 1.3.8 where the same data-turbolinks-animate-persist="true" applied to the same navbar causes the whole body to fadeout and the navbar not to persist

Any idea what's going on?

In 1.3.4 through 1.3.7 it all exhibits the same behaviour.. the nav persists but there is no fadeout when clicking on links.. however if I perform a browser refresh THEN there's a nice smooth fadeout before reloading the page

how can I get that nice smooth fadeout with a persisted navbar ?? I tried adding

    $(document).on('turbolinks:before-visit', function (event) {
        turbolinksAnimateDisappear();
    });

but to no avail..

Animate over existing page

Is your feature request related to a problem? Please describe.
I would like to keep the existing page in the background while an animation completes. As it is I think it is quite weird that the page disappears altogether and a new one comes in from an animation standpoint.

Describe the solution you'd like
Animate the new page in and clear the old one only after the animation has completed.

Describe alternatives you've considered
In case this is technically not possible, maybe taking a screenshot of the current page contents to visually achieve this feeling.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.