Skip to content

Commit 05ca1c0

Browse files
fade response element using CSS animation
1 parent e2611b0 commit 05ca1c0

File tree

8 files changed

+47
-42
lines changed

8 files changed

+47
-42
lines changed

assets/browserify/bar.js

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const Loader = require('./loader.js')
44

55
function throttle (fn, threshold, scope) {
66
threshold || (threshold = 600)
7-
var last
7+
let last
88
let deferTimer
99
return function () {
1010
const context = scope || this
@@ -25,20 +25,20 @@ function throttle (fn, threshold, scope) {
2525
}
2626

2727
function Bar (wrapperEl, config) {
28-
var barEl = wrapperEl.querySelector('.mctb-bar')
29-
var iconEl = document.createElement('span')
30-
var responseEl = wrapperEl.querySelector('.mctb-response')
31-
var formEl = barEl.querySelector('form')
32-
var visible = false
33-
var originalBodyPadding = 0
34-
var bodyPadding = 0
35-
var isBottomBar = (config.position === 'bottom')
36-
var state = config.state
28+
const barEl = wrapperEl.querySelector('.mctb-bar')
29+
const iconEl = document.createElement('span')
30+
const formEl = barEl.querySelector('form')
31+
let responseEl = wrapperEl.querySelector('.mctb-response')
32+
let visible = false
33+
let originalBodyPadding = 0
34+
let bodyPadding = 0
35+
const isBottomBar = (config.position === 'bottom')
36+
const state = config.state
3737

3838
// Functions
3939
function init () {
4040
// remove "no_js" field
41-
var noJsField = barEl.querySelector('input[name="_mctb_no_js"]')
41+
const noJsField = barEl.querySelector('input[name="_mctb_no_js"]')
4242
noJsField.parentElement.removeChild(noJsField)
4343

4444
formEl.addEventListener('submit', submitForm)
@@ -78,15 +78,15 @@ function Bar (wrapperEl, config) {
7878
window.setTimeout(fadeResponse, 4000)
7979
}
8080

81-
window.addEventListener('resize', throttle(calculateDimensions, 40))
81+
window.addEventListener('resize', throttle(calculateDimensions))
8282
}
8383

8484
function submitForm (evt) {
85-
var loader = new Loader(formEl)
86-
var data = new FormData(formEl)
87-
var request = new XMLHttpRequest()
85+
const loader = new Loader(formEl)
86+
const data = new FormData(formEl)
87+
let request = new XMLHttpRequest()
8888
request.onreadystatechange = function () {
89-
var response
89+
let response
9090

9191
// are we done?
9292
if (this.readyState !== 4) {
@@ -139,7 +139,7 @@ function Bar (wrapperEl, config) {
139139
responseEl = document.createElement('div')
140140
responseEl.className = 'mctb-response'
141141

142-
var labelEl = document.createElement('label')
142+
const labelEl = document.createElement('label')
143143
labelEl.className = 'mctb-response-label'
144144
labelEl.innerText = msg
145145
responseEl.appendChild(labelEl)
@@ -151,8 +151,7 @@ function Bar (wrapperEl, config) {
151151

152152
function calculateDimensions () {
153153
// make sure bar is visible
154-
var origBarDisplay = barEl.style.display
155-
154+
const origBarDisplay = barEl.style.display
156155
if (origBarDisplay !== 'block') {
157156
barEl.style.visibility = 'hidden'
158157
}
@@ -165,8 +164,8 @@ function Bar (wrapperEl, config) {
165164
}
166165

167166
// would the close icon fit inside the bar?
168-
var elementsWidth = 0
169-
for (var i = 0; i < barEl.firstElementChild.children.length; i++) {
167+
let elementsWidth = 0
168+
for (let i = 0; i < barEl.firstElementChild.children.length; i++) {
170169
elementsWidth += barEl.firstElementChild.children[i].clientWidth
171170
}
172171

@@ -206,7 +205,7 @@ function Bar (wrapperEl, config) {
206205
animator.toggle(barEl, 'slide')
207206

208207
// animate body padding
209-
var styles = {}
208+
const styles = {}
210209
styles[isBottomBar ? 'paddingBottom' : 'paddingTop'] = bodyPadding
211210
animator.animate(document.body, styles)
212211
} else {
@@ -236,7 +235,7 @@ function Bar (wrapperEl, config) {
236235
animator.toggle(barEl, 'slide')
237236

238237
// animate body padding
239-
var styles = {}
238+
const styles = {}
240239
styles[isBottomBar ? 'paddingBottom' : 'paddingTop'] = originalBodyPadding
241240
animator.animate(document.body, styles)
242241
} else {
@@ -258,12 +257,16 @@ function Bar (wrapperEl, config) {
258257
return
259258
}
260259

261-
animator.toggle(responseEl, 'fade')
260+
responseEl.style.opacity = '0'
261+
window.setTimeout(() => {
262+
// remove response element so form is usable again
263+
responseEl.parentElement.removeChild(responseEl)
262264

263-
// auto-dismiss bar if we're good!
264-
if (state.submitted && state.success) {
265-
window.setTimeout(function () { hide(true) }, 1000)
266-
}
265+
// hide bar if sign-up was successful
266+
if (state.submitted && state.success) {
267+
hide(true)
268+
}
269+
}, 1000)
267270
}
268271

269272
/**

assets/css/bar.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@
156156
z-index: 100;
157157
top: 0;
158158
left: 0;
159-
width: 100%; }
159+
width: 100%;
160+
transition-duration: 1s; }
160161

161162
.mctb-close {
162163
display: inline-block;

assets/css/bar.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)