Skip to content

Commit 2a8fa8d

Browse files
committed
fix: panel css transition
1 parent 611cfa3 commit 2a8fa8d

File tree

2 files changed

+53
-40
lines changed

2 files changed

+53
-40
lines changed

src/js/components/panels.js

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const defaults = {
77
type: 'field',
88
}
99

10+
const getTransition = val => ({ transform: `translateX(${val}px)` })
11+
1012
/**
1113
* Edit and control sliding panels
1214
*/
@@ -201,7 +203,8 @@ export default class Panels {
201203
const firstControlNav = this.labels.querySelector('.panel-labels').firstChild
202204
const siblingGroups = this.currentPanel.parentElement.childNodes
203205
let index = indexOfNode(this.currentPanel, groupParent)
204-
let offset = {}
206+
let offset = { nav: 0, panel: 0 }
207+
let lastOffset = { ...offset }
205208

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

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

221-
const translateX = offset => {
222-
if (_this.panelDisplay !== 'tabbed') {
223-
firstControlNav.style.transform = `translateX(-${offset.nav}px)`
224-
} else {
220+
const translateX = (offset, reset) => {
221+
if (_this.panelDisplay === 'tabbed') {
225222
firstControlNav.removeAttribute('style')
223+
const { transform } = getTransition(offset.panel)
224+
groupParent.style.transform = transform
225+
return null
226+
}
227+
228+
const panelQueue = [getTransition(lastOffset.panel), getTransition(offset.panel)]
229+
const navQueue = [getTransition(lastOffset.nav), getTransition(offset.nav)]
230+
231+
if (reset) {
232+
const [panelStart] = panelQueue
233+
const [navStart] = navQueue
234+
panelQueue.push(panelStart)
235+
navQueue.push(navStart)
236+
}
237+
238+
const animationOptions = {
239+
easing: 'ease-in-out',
240+
duration: 150,
241+
fill: 'forwards',
242+
}
243+
244+
const panelTransition = groupParent.animate(panelQueue, animationOptions)
245+
firstControlNav.animate(navQueue, animationOptions)
246+
const handleFinish = () => {
247+
this.panelsWrap.style.height = dom.getStyle(this.currentPanel, 'height')
248+
panelTransition.removeEventListener('finish', handleFinish)
249+
if (!reset) {
250+
lastOffset = offset
251+
}
226252
}
227-
groupParent.style.transform = `translateX(-${offset.panel}px)`
253+
panelTransition.addEventListener('finish', handleFinish)
228254
}
229255

230256
action.refresh = newIndex => {
@@ -234,8 +260,8 @@ export default class Panels {
234260
}
235261
_this.resizePanels()
236262
offset = {
237-
nav: firstControlNav.offsetWidth * index,
238-
panel: groupParent.offsetWidth * index,
263+
nav: -firstControlNav.offsetWidth * index,
264+
panel: -groupParent.offsetWidth * index,
239265
}
240266
translateX(offset)
241267
}
@@ -247,27 +273,19 @@ export default class Panels {
247273
action.nextGroup = () => {
248274
const newIndex = index + 1
249275
if (newIndex !== siblingGroups.length) {
276+
const curPanel = groupChange(newIndex)
250277
offset = {
251-
nav: firstControlNav.offsetWidth * newIndex,
252-
panel: groupParent.offsetWidth * newIndex,
278+
nav: -firstControlNav.offsetWidth * newIndex,
279+
panel: -curPanel.offsetLeft,
253280
}
254281
translateX(offset)
255-
groupChange(newIndex)
256282
index++
257283
} else {
258-
const origOffset = {
259-
nav: firstControlNav.style.transform,
260-
panel: groupParent.style.transform,
261-
}
262284
offset = {
263-
nav: firstControlNav.offsetWidth * index + 10,
264-
panel: groupParent.offsetWidth * index + 10,
285+
nav: lastOffset.nav - 8,
286+
panel: lastOffset.panel - 8,
265287
}
266-
translateX(offset)
267-
setTimeout(() => {
268-
firstControlNav.style.transform = origOffset.nav
269-
groupParent.style.transform = origOffset.panel
270-
}, 150)
288+
translateX(offset, true)
271289
}
272290

273291
return this.currentPanel
@@ -276,22 +294,19 @@ export default class Panels {
276294
action.prevGroup = () => {
277295
if (index !== 0) {
278296
const newIndex = index - 1
297+
const curPanel = groupChange(newIndex)
279298
offset = {
280-
nav: firstControlNav.offsetWidth * newIndex,
281-
panel: groupParent.offsetWidth * newIndex,
299+
nav: -firstControlNav.offsetWidth * newIndex,
300+
panel: -curPanel.offsetLeft,
282301
}
283302
translateX(offset)
284-
groupChange(newIndex)
285303
index--
286304
} else {
287-
const curTranslate = [firstControlNav.style.transform, groupParent.style.transform]
288-
const nudge = 'translateX(10px)'
289-
firstControlNav.style.transform = nudge
290-
groupParent.style.transform = nudge
291-
setTimeout(() => {
292-
firstControlNav.style.transform = curTranslate[0]
293-
groupParent.style.transform = curTranslate[1]
294-
}, 150)
305+
offset = {
306+
nav: 8,
307+
panel: 8,
308+
}
309+
translateX(offset, true)
295310
}
296311
}
297312

src/sass/components/_panels.scss

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171

7272
.panels {
7373
white-space: nowrap;
74-
transition-property: transform, height;
74+
transition-property: height;
7575
transition-duration: 150ms;
7676
transition-timing-function: ease-in-out;
7777
will-change: transform;
@@ -85,8 +85,6 @@
8585
text-align: center;
8686

8787
div {
88-
transition: transform 150ms ease-in-out;
89-
will-change: transform;
9088
white-space: nowrap;
9189
}
9290
}

0 commit comments

Comments
 (0)