Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Animated change transitions #26

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ Alternatively, instead of loading livereload.js from the LiveReload server, you
<script src="https://github.com/livereload/livereload-js/raw/master/dist/livereload.js?host=localhost"></script>
```

### Animated transitions

LiveReload can animate changes. To enable animated transitions pass `animate=true` to `livereload.js` when including in the script tag. The default transition duration is 280ms and configurable when an integer is passed as value to `animate`, e.g. `animate=400` (in milliseconds).


Issues & Limitations
--------------------
Expand Down
25 changes: 25 additions & 0 deletions src/livereload.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,19 @@ exports.LiveReload = class LiveReload
originalPath: message.originalPath || ''
overrideURL: message.overrideURL || ''
serverURL: "http://#{@options.host}:#{@options.port}"
@performTransition() if @options.animate

performAlert: (message) ->
alert message.message

performTransition: ->
html = document.body.parentNode
reloadedClass = ' livereload-reloaded '
existingHtmlClass = html.getAttribute('class') ? ''
html.setAttribute('class', "#{existingHtmlClass.replace(reloadedClass, '')} #{reloadedClass}")
setTimeout (-> html.setAttribute('class', existingHtmlClass.replace(reloadedClass, ''))),
parseInt(@options.animate, 10)

shutDown: ->
@connector.disconnect()
@log "LiveReload disconnected."
Expand Down Expand Up @@ -143,3 +152,19 @@ exports.LiveReload = class LiveReload

@connector.sendCommand { command: 'info', plugins: pluginsData, url: @window.location.href }
return

setUpCSSTransitions: ->
prefixer = (declaration) ->
(['-webkit-', '-moz-', ''].map (item) -> ("#{item}#{declaration}")).join(' ')

head = document.getElementsByTagName('head')[0]
styleNode = document.createElement("style")
cssText = ".livereload-reloaded * { #{prefixer('transition: all ' +
@options.animate + 'ms ease-out;')} }"

if styleNode.styleSheet
styleNode.styleSheet.cssText = cssText
else
styleNode.appendChild(document.createTextNode(cssText))

head.appendChild(styleNode)
6 changes: 6 additions & 0 deletions src/options.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,19 @@ exports.Options = class Options
@maxdelay = 60000
@handshake_timeout = 5000

@animate = false # true or value in milliseconds, e.g. 420

set: (name, value) ->
if typeof value is 'undefined'
return

if not isNaN(+value)
value = +value

if name == 'animate'
value = 280 if value is 'true' # default animation duration
return if !/true|^\d+$/.test(value)

@[name] = value

Options.extract = (document) ->
Expand Down
1 change: 1 addition & 0 deletions src/startup.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ LiveReload.addPlugin require('./less')

LiveReload.on 'shutdown', -> delete window.LiveReload
LiveReload.on 'connect', ->
LiveReload.setUpCSSTransitions() if LiveReload.options.animate
CustomEvents.fire document, 'LiveReloadConnect'
LiveReload.on 'disconnect', ->
CustomEvents.fire document, 'LiveReloadDisconnect'
Expand Down
3 changes: 3 additions & 0 deletions test/html/animation/test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
body {
background: green;
}
11 changes: 11 additions & 0 deletions test/html/animation/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>LiveReload Test</title>
<link rel="stylesheet" href="test.css">
</head>
<body>
<h1>Test animated transitions</h1>
<script src="../../../dist/livereload.js?host=localhost&amp;animation=true" async defer></script>
</body>
</html>