Skip to content

Commit

Permalink
fix: panel css transition
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinchappell committed Feb 24, 2020
1 parent 611cfa3 commit 2a8fa8d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 40 deletions.
89 changes: 52 additions & 37 deletions src/js/components/panels.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const defaults = {
type: 'field',
}

const getTransition = val => ({ transform: `translateX(${val}px)` })

/**
* Edit and control sliding panels
*/
Expand Down Expand Up @@ -201,7 +203,8 @@ export default class Panels {
const firstControlNav = this.labels.querySelector('.panel-labels').firstChild
const siblingGroups = this.currentPanel.parentElement.childNodes
let index = indexOfNode(this.currentPanel, groupParent)
let offset = {}
let offset = { nav: 0, panel: 0 }
let lastOffset = { ...offset }

const groupChange = newIndex => {
const labels = this.labels.querySelector('.panel-labels div').children
Expand All @@ -211,20 +214,43 @@ export default class Panels {
this.currentPanel.classList.add('active-panel')

labels[newIndex].classList.add('active-tab')
this.panelsWrap.style.height = dom.getStyle(this.currentPanel, 'height')
// if (this.opts.type === 'field') {
// this.slideToggle.style.height = 'auto'
// }
return this.currentPanel
}

const translateX = offset => {
if (_this.panelDisplay !== 'tabbed') {
firstControlNav.style.transform = `translateX(-${offset.nav}px)`
} else {
const translateX = (offset, reset) => {
if (_this.panelDisplay === 'tabbed') {
firstControlNav.removeAttribute('style')
const { transform } = getTransition(offset.panel)
groupParent.style.transform = transform
return null
}

const panelQueue = [getTransition(lastOffset.panel), getTransition(offset.panel)]
const navQueue = [getTransition(lastOffset.nav), getTransition(offset.nav)]

if (reset) {
const [panelStart] = panelQueue
const [navStart] = navQueue
panelQueue.push(panelStart)
navQueue.push(navStart)
}

const animationOptions = {
easing: 'ease-in-out',
duration: 150,
fill: 'forwards',
}

const panelTransition = groupParent.animate(panelQueue, animationOptions)
firstControlNav.animate(navQueue, animationOptions)
const handleFinish = () => {
this.panelsWrap.style.height = dom.getStyle(this.currentPanel, 'height')
panelTransition.removeEventListener('finish', handleFinish)
if (!reset) {
lastOffset = offset
}
}
groupParent.style.transform = `translateX(-${offset.panel}px)`
panelTransition.addEventListener('finish', handleFinish)
}

action.refresh = newIndex => {
Expand All @@ -234,8 +260,8 @@ export default class Panels {
}
_this.resizePanels()
offset = {
nav: firstControlNav.offsetWidth * index,
panel: groupParent.offsetWidth * index,
nav: -firstControlNav.offsetWidth * index,
panel: -groupParent.offsetWidth * index,
}
translateX(offset)
}
Expand All @@ -247,27 +273,19 @@ export default class Panels {
action.nextGroup = () => {
const newIndex = index + 1
if (newIndex !== siblingGroups.length) {
const curPanel = groupChange(newIndex)
offset = {
nav: firstControlNav.offsetWidth * newIndex,
panel: groupParent.offsetWidth * newIndex,
nav: -firstControlNav.offsetWidth * newIndex,
panel: -curPanel.offsetLeft,
}
translateX(offset)
groupChange(newIndex)
index++
} else {
const origOffset = {
nav: firstControlNav.style.transform,
panel: groupParent.style.transform,
}
offset = {
nav: firstControlNav.offsetWidth * index + 10,
panel: groupParent.offsetWidth * index + 10,
nav: lastOffset.nav - 8,
panel: lastOffset.panel - 8,
}
translateX(offset)
setTimeout(() => {
firstControlNav.style.transform = origOffset.nav
groupParent.style.transform = origOffset.panel
}, 150)
translateX(offset, true)
}

return this.currentPanel
Expand All @@ -276,22 +294,19 @@ export default class Panels {
action.prevGroup = () => {
if (index !== 0) {
const newIndex = index - 1
const curPanel = groupChange(newIndex)
offset = {
nav: firstControlNav.offsetWidth * newIndex,
panel: groupParent.offsetWidth * newIndex,
nav: -firstControlNav.offsetWidth * newIndex,
panel: -curPanel.offsetLeft,
}
translateX(offset)
groupChange(newIndex)
index--
} else {
const curTranslate = [firstControlNav.style.transform, groupParent.style.transform]
const nudge = 'translateX(10px)'
firstControlNav.style.transform = nudge
groupParent.style.transform = nudge
setTimeout(() => {
firstControlNav.style.transform = curTranslate[0]
groupParent.style.transform = curTranslate[1]
}, 150)
offset = {
nav: 8,
panel: 8,
}
translateX(offset, true)
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/sass/components/_panels.scss
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@

.panels {
white-space: nowrap;
transition-property: transform, height;
transition-property: height;
transition-duration: 150ms;
transition-timing-function: ease-in-out;
will-change: transform;
Expand All @@ -85,8 +85,6 @@
text-align: center;

div {
transition: transform 150ms ease-in-out;
will-change: transform;
white-space: nowrap;
}
}
Expand Down

0 comments on commit 2a8fa8d

Please sign in to comment.