forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmorph.js
More file actions
978 lines (851 loc) · 33.5 KB
/
morph.js
File metadata and controls
978 lines (851 loc) · 33.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
import { getElement } from './dom.js'
import { isObject } from './is'
let id = 0
let offsetBase = void 0
function getAbsolutePosition (el, resize) {
if (offsetBase === void 0) {
offsetBase = document.createElement('div')
offsetBase.style.cssText = 'position: absolute; left: 0; top: 0'
document.body.appendChild(offsetBase)
}
const boundingRect = el.getBoundingClientRect()
const baseRect = offsetBase.getBoundingClientRect()
const { marginLeft, marginRight, marginTop, marginBottom } = window.getComputedStyle(el)
const marginH = parseInt(marginLeft, 10) + parseInt(marginRight, 10)
const marginV = parseInt(marginTop, 10) + parseInt(marginBottom, 10)
return {
left: boundingRect.left - baseRect.left,
top: boundingRect.top - baseRect.top,
width: boundingRect.right - boundingRect.left,
height: boundingRect.bottom - boundingRect.top,
widthM: boundingRect.right - boundingRect.left + (resize === true ? 0 : marginH),
heightM: boundingRect.bottom - boundingRect.top + (resize === true ? 0 : marginV),
marginH: resize === true ? marginH : 0,
marginV: resize === true ? marginV : 0
}
}
function getAbsoluteSize (el) {
return {
width: el.scrollWidth,
height: el.scrollHeight
}
}
// firefox rulez
const styleEdges = [ 'Top', 'Right', 'Bottom', 'Left' ]
const styleBorderRadiuses = [ 'borderTopLeftRadius', 'borderTopRightRadius', 'borderBottomRightRadius', 'borderBottomLeftRadius' ]
const reStyleSkipKey = /-block|-inline|block-|inline-/
const reStyleSkipRule = /(-block|-inline|block-|inline-).*:/
function getComputedStyle (el, props) {
const style = window.getComputedStyle(el)
const fixed = {}
for (let i = 0; i < props.length; i++) {
const prop = props[i]
if (style[prop] === '') {
if (prop === 'cssText') {
const styleLen = style.length
let val = ''
for (let i = 0; i < styleLen; i++) {
if (reStyleSkipKey.test(style[i]) !== true) {
val += style[i] + ': ' + style[style[i]] + '; '
}
}
fixed[prop] = val
}
else if ([ 'borderWidth', 'borderStyle', 'borderColor' ].indexOf(prop) > -1) {
const suffix = prop.replace('border', '')
let val = ''
for (let j = 0; j < styleEdges.length; j++) {
const subProp = 'border' + styleEdges[j] + suffix
val += style[subProp] + ' '
}
fixed[prop] = val
}
else if (prop === 'borderRadius') {
let val1 = ''
let val2 = ''
for (let j = 0; j < styleBorderRadiuses.length; j++) {
const val = style[styleBorderRadiuses[j]].split(' ')
val1 += val[0] + ' '
val2 += (val[1] === void 0 ? val[0] : val[1]) + ' '
}
fixed[prop] = val1 + '/ ' + val2
}
else {
fixed[prop] = style[prop]
}
}
else {
if (prop === 'cssText') {
fixed[prop] = style[prop]
.split(';')
.filter(val => reStyleSkipRule.test(val) !== true)
.join(';')
}
else {
fixed[prop] = style[prop]
}
}
}
return fixed
}
const zIndexPositions = ['absolute', 'fixed', 'relative', 'sticky']
function getMaxZIndex (elStart) {
let el = elStart
let maxIndex = 0
while (el !== null && el !== document) {
const { position, zIndex } = window.getComputedStyle(el)
const zIndexNum = Number(zIndex)
if (
zIndexNum > maxIndex &&
(el === elStart || zIndexPositions.includes(position) === true)
) {
maxIndex = zIndexNum
}
el = el.parentNode
}
return maxIndex
}
function normalizeElements (opts) {
return {
from: opts.from,
to: opts.to !== void 0
? opts.to
: opts.from
}
}
function normalizeOptions (options) {
if (typeof options === 'number') {
options = {
duration: options
}
}
else if (typeof options === 'function') {
options = {
onEnd: options
}
}
return {
...options,
waitFor: options.waitFor === void 0 ? 0 : options.waitFor,
duration: isNaN(options.duration) === true ? 300 : parseInt(options.duration, 10),
easing: typeof options.easing === 'string' && options.easing.length > 0 ? options.easing : 'ease-in-out',
delay: isNaN(options.delay) === true ? 0 : parseInt(options.delay, 10),
fill: typeof options.fill === 'string' && options.fill.length > 0 ? options.fill : 'none',
resize: options.resize === true,
useCSS: options.useCSS === true,
hideFromClone: options.hideFromClone === true,
keepToClone: options.keepToClone === true,
tween: options.tween === true,
tweenFromOpacity: isNaN(options.tweenFromOpacity) === true ? 0.6 : parseFloat(options.tweenFromOpacity),
tweenToOpacity: isNaN(options.tweenToOpacity) === true ? 0.5 : parseFloat(options.tweenToOpacity)
}
}
function isValidElement (element) {
return element &&
element.ownerDocument === document &&
element.parentNode !== null
}
export default function morph (_options) {
let cancel = () => false
let cancelStatus = false
let endElementTo = true
const elements = normalizeElements(_options)
const options = normalizeOptions(_options)
const elFrom = getElement(elements.from)
if (isValidElement(elFrom) !== true) {
// we return a cancel function that return false, meaning the cancel function failed
return cancel
}
// we clean other morphs running on this element
typeof elFrom.qMorphCancel === 'function' && elFrom.qMorphCancel()
let animationFromClone = void 0
let animationFromTween = void 0
let animationToClone = void 0
let animationTo = void 0
const elFromParent = elFrom.parentNode
const elFromNext = elFrom.nextElementSibling
// we get the dimensions and characteristics
// of the parent of the initial element before changes
const elFromPosition = getAbsolutePosition(elFrom, options.resize)
const {
width: elFromParentWidthBefore,
height: elFromParentHeightBefore
} = getAbsoluteSize(elFromParent)
const {
borderWidth: elFromBorderWidth,
borderStyle: elFromBorderStyle,
borderColor: elFromBorderColor,
borderRadius: elFromBorderRadius,
backgroundColor: elFromBackground,
transform: elFromTransform,
position: elFromPositioningType,
cssText: elFromCssText
} = getComputedStyle(elFrom, [ 'borderWidth', 'borderStyle', 'borderColor', 'borderRadius', 'backgroundColor', 'transform', 'position', 'cssText' ])
const elFromClassSaved = elFrom.classList.toString()
const elFromStyleSaved = elFrom.style.cssText
// we make a clone of the initial element and
// use it to display until the final element is ready
// and to change the occupied space during animation
const elFromClone = elFrom.cloneNode(true)
const elFromTween = options.tween === true ? elFrom.cloneNode(true) : void 0
if (elFromTween !== void 0) {
elFromTween.className = elFromTween.classList.toString().split(' ').filter(c => /^bg-/.test(c) === false).join(' ')
}
// if the initial element is not going to be removed do not show the placeholder
options.hideFromClone === true && elFromClone.classList.add('q-morph--internal')
// prevent interaction with placeholder
elFromClone.setAttribute('aria-hidden', 'true')
elFromClone.style.transition = 'none'
elFromClone.style.animation = 'none'
elFromClone.style.pointerEvents = 'none'
elFromParent.insertBefore(elFromClone, elFromNext)
// we mark the element with its cleanup function
elFrom.qMorphCancel = () => {
cancelStatus = true
// we clean the clone of the initial element
elFromClone.remove()
elFromTween !== void 0 && elFromTween.remove()
options.hideFromClone === true && elFromClone.classList.remove('q-morph--internal')
// we remove the cleanup function from the element
elFrom.qMorphCancel = void 0
}
// will be called after Vue catches up with the changes done by _options.onToggle() function
const calculateFinalState = () => {
const elTo = getElement(elements.to)
if (cancelStatus === true || isValidElement(elTo) !== true) {
typeof elFrom.qMorphCancel === 'function' && elFrom.qMorphCancel()
return
}
// we clean other morphs running on this element
elFrom !== elTo && typeof elTo.qMorphCancel === 'function' && elTo.qMorphCancel()
// we hide the final element and the clone of the initial element
// we don't hide the final element if we want both it and the animated one visible
options.keepToClone !== true && elTo.classList.add('q-morph--internal')
elFromClone.classList.add('q-morph--internal')
// we get the dimensions of the parent of the initial element after changes
// the difference is how much we should animate the clone
const {
width: elFromParentWidthAfter,
height: elFromParentHeightAfter
} = getAbsoluteSize(elFromParent)
// we get the dimensions of the parent of the final element before changes
const {
width: elToParentWidthBefore,
height: elToParentHeightBefore
} = getAbsoluteSize(elTo.parentNode)
// then we show the clone of the initial element if we don't want it hidden
options.hideFromClone !== true && elFromClone.classList.remove('q-morph--internal')
// we mark the element with its cleanup function
elTo.qMorphCancel = () => {
cancelStatus = true
// we clean the clone of the initial element
elFromClone.remove()
elFromTween !== void 0 && elFromTween.remove()
options.hideFromClone === true && elFromClone.classList.remove('q-morph--internal')
// we show the final element
options.keepToClone !== true && elTo.classList.remove('q-morph--internal')
// we remove the cleanup function from the elements
elFrom.qMorphCancel = void 0
elTo.qMorphCancel = void 0
}
// will be called after waitFor (give time to render the final element)
const animate = () => {
if (cancelStatus === true) {
typeof elTo.qMorphCancel === 'function' && elTo.qMorphCancel()
return
}
// now the animation starts, so we only need the clone
// of the initial element as a spacer
// we also hide it to calculate the dimensions of the
// parent of the final element after the changes
if (options.hideFromClone !== true) {
elFromClone.classList.add('q-morph--internal')
elFromClone.innerHTML = ''
elFromClone.style.left = 0
elFromClone.style.right = 'unset'
elFromClone.style.top = 0
elFromClone.style.bottom = 'unset'
elFromClone.style.transform = 'none'
}
// we show the final element
if (options.keepToClone !== true) {
elTo.classList.remove('q-morph--internal')
}
// we get the dimensions of the parent of the final element after changes
// the difference is how much we should animate the clone
const elToParent = elTo.parentNode
const {
width: elToParentWidthAfter,
height: elToParentHeightAfter
} = getAbsoluteSize(elToParent)
const elToClone = elTo.cloneNode(options.keepToClone)
elToClone.setAttribute('aria-hidden', 'true')
if (options.keepToClone !== true) {
elToClone.style.left = 0
elToClone.style.right = 'unset'
elToClone.style.top = 0
elToClone.style.bottom = 'unset'
elToClone.style.transform = 'none'
elToClone.style.pointerEvents = 'none'
}
elToClone.classList.add('q-morph--internal')
// if elFrom is the same as elTo the next element is elFromClone
const elToNext = elTo === elFrom && elFromParent === elToParent ? elFromClone : elTo.nextElementSibling
elToParent.insertBefore(elToClone, elToNext)
const {
borderWidth: elToBorderWidth,
borderStyle: elToBorderStyle,
borderColor: elToBorderColor,
borderRadius: elToBorderRadius,
backgroundColor: elToBackground,
transform: elToTransform,
position: elToPositioningType,
cssText: elToCssText
} = getComputedStyle(elTo, [ 'borderWidth', 'borderStyle', 'borderColor', 'borderRadius', 'backgroundColor', 'transform', 'position', 'cssText' ])
const elToClassSaved = elTo.classList.toString()
const elToStyleSaved = elTo.style.cssText
// we set the computed styles on the element (to be able to remove classes)
elTo.style.cssText = elToCssText
elTo.style.transform = 'none'
elTo.style.animation = 'none'
elTo.style.transition = 'none'
// we strip the background classes (background color can no longer be animated if !important is used)
elTo.className = elToClassSaved.split(' ').filter(c => /^bg-/.test(c) === false).join(' ')
const elToPosition = getAbsolutePosition(elTo, options.resize)
const deltaX = elFromPosition.left - elToPosition.left
const deltaY = elFromPosition.top - elToPosition.top
const scaleX = elFromPosition.width / (elToPosition.width > 0 ? elToPosition.width : 10)
const scaleY = elFromPosition.height / (elToPosition.height > 0 ? elToPosition.height : 100)
const elFromParentWidthDiff = elFromParentWidthBefore - elFromParentWidthAfter
const elFromParentHeightDiff = elFromParentHeightBefore - elFromParentHeightAfter
const elToParentWidthDiff = elToParentWidthAfter - elToParentWidthBefore
const elToParentHeightDiff = elToParentHeightAfter - elToParentHeightBefore
const elFromCloneWidth = Math.max(elFromPosition.widthM, elFromParentWidthDiff)
const elFromCloneHeight = Math.max(elFromPosition.heightM, elFromParentHeightDiff)
const elToCloneWidth = Math.max(elToPosition.widthM, elToParentWidthDiff)
const elToCloneHeight = Math.max(elToPosition.heightM, elToParentHeightDiff)
const elSharedSize = elFrom === elTo &&
[ 'absolute', 'fixed' ].includes(elToPositioningType) === false &&
[ 'absolute', 'fixed' ].includes(elFromPositioningType) === false
// if the final element has fixed position or if a parent
// has fixed position we need to animate it as fixed
let elToNeedsFixedPosition = elToPositioningType === 'fixed'
let parent = elToParent
while (elToNeedsFixedPosition !== true && parent !== document) {
elToNeedsFixedPosition = window.getComputedStyle(parent).position === 'fixed'
parent = parent.parentNode
}
// we show the spacer for the initial element
if (options.hideFromClone !== true) {
elFromClone.style.display = 'block'
elFromClone.style.flex = '0 0 auto'
elFromClone.style.opacity = 0
elFromClone.style.minWidth = 'unset'
elFromClone.style.maxWidth = 'unset'
elFromClone.style.minHeight = 'unset'
elFromClone.style.maxHeight = 'unset'
elFromClone.classList.remove('q-morph--internal')
}
// we show the spacer for the final element
if (options.keepToClone !== true) {
elToClone.style.display = 'block'
elToClone.style.flex = '0 0 auto'
elToClone.style.opacity = 0
elToClone.style.minWidth = 'unset'
elToClone.style.maxWidth = 'unset'
elToClone.style.minHeight = 'unset'
elToClone.style.maxHeight = 'unset'
}
elToClone.classList.remove('q-morph--internal')
// we apply classes specified by user
if (typeof options.classes === 'string') {
elTo.className += ' ' + options.classes
}
// we apply styles specified by user
if (typeof options.style === 'string') {
elTo.style.cssText += ' ' + options.style
}
else if (isObject(options.style) === true) {
for (const prop in options.style) {
elTo.style[prop] = options.style[prop]
}
}
const elFromZIndex = getMaxZIndex(elFromClone)
const elToZIndex = getMaxZIndex(elTo)
// we position the morphing element
// if we use fixed position for the final element we need to adjust for scroll
const documentScroll = elToNeedsFixedPosition === true
? document.documentElement
: { scrollLeft: 0, scrollTop: 0 }
elTo.style.position = elToNeedsFixedPosition === true ? 'fixed' : 'absolute'
elTo.style.left = `${elToPosition.left - documentScroll.scrollLeft}px`
elTo.style.right = 'unset'
elTo.style.top = `${elToPosition.top - documentScroll.scrollTop}px`
elTo.style.margin = 0
if (options.resize === true) {
elTo.style.minWidth = 'unset'
elTo.style.maxWidth = 'unset'
elTo.style.minHeight = 'unset'
elTo.style.maxHeight = 'unset'
elTo.style.overflow = 'hidden'
elTo.style.overflowX = 'hidden'
elTo.style.overflowY = 'hidden'
}
document.body.appendChild(elTo)
if (elFromTween !== void 0) {
elFromTween.style.cssText = elFromCssText
elFromTween.style.transform = 'none'
elFromTween.style.animation = 'none'
elFromTween.style.transition = 'none'
elFromTween.style.position = elTo.style.position
elFromTween.style.left = `${elFromPosition.left - documentScroll.scrollLeft}px`
elFromTween.style.right = 'unset'
elFromTween.style.top = `${elFromPosition.top - documentScroll.scrollTop}px`
elFromTween.style.margin = 0
elFromTween.style.pointerEvents = 'none'
if (options.resize === true) {
elFromTween.style.minWidth = 'unset'
elFromTween.style.maxWidth = 'unset'
elFromTween.style.minHeight = 'unset'
elFromTween.style.maxHeight = 'unset'
elFromTween.style.overflow = 'hidden'
elFromTween.style.overflowX = 'hidden'
elFromTween.style.overflowY = 'hidden'
}
document.body.appendChild(elFromTween)
}
const commonCleanup = aborted => {
// we put the element back in it's place
// and restore the styles and classes
if (elFrom === elTo && endElementTo !== true) {
elTo.style.cssText = elFromStyleSaved
elTo.className = elFromClassSaved
}
else {
elTo.style.cssText = elToStyleSaved
elTo.className = elToClassSaved
}
elToClone.parentNode === elToParent && elToParent.insertBefore(elTo, elToClone)
// we clean the spacers
elFromClone.remove()
elToClone.remove()
elFromTween !== void 0 && elFromTween.remove()
// cancel will be no longer available
cancel = () => false
elFrom.qMorphCancel = void 0
elTo.qMorphCancel = void 0
// we are ready
if (typeof options.onEnd === 'function') {
options.onEnd(endElementTo === true ? 'to' : 'from', aborted === true)
}
}
if (options.useCSS !== true && typeof elTo.animate === 'function') {
const resizeFrom = options.resize === true
? {
transform: `translate(${deltaX}px, ${deltaY}px)`,
width: `${elFromCloneWidth}px`,
height: `${elFromCloneHeight}px`
}
: {
transform: `translate(${deltaX}px, ${deltaY}px) scale(${scaleX}, ${scaleY})`
}
const resizeTo = options.resize === true
? {
width: `${elToCloneWidth}px`,
height: `${elToCloneHeight}px`
}
: {}
const resizeFromTween = options.resize === true
? {
width: `${elFromCloneWidth}px`,
height: `${elFromCloneHeight}px`
}
: {}
const resizeToTween = options.resize === true
? {
transform: `translate(${-1 * deltaX}px, ${-1 * deltaY}px)`,
width: `${elToCloneWidth}px`,
height: `${elToCloneHeight}px`
}
: {
transform: `translate(${-1 * deltaX}px, ${-1 * deltaY}px) scale(${1 / scaleX}, ${1 / scaleY})`
}
const tweenFrom = elFromTween !== void 0
? { opacity: options.tweenToOpacity }
: { backgroundColor: elFromBackground }
const tweenTo = elFromTween !== void 0
? { opacity: 1 }
: { backgroundColor: elToBackground }
animationTo = elTo.animate([
{
margin: 0,
borderWidth: elFromBorderWidth,
borderStyle: elFromBorderStyle,
borderColor: elFromBorderColor,
borderRadius: elFromBorderRadius,
zIndex: elFromZIndex,
transformOrigin: '0 0',
...resizeFrom,
...tweenFrom
},
{
margin: 0,
borderWidth: elToBorderWidth,
borderStyle: elToBorderStyle,
borderColor: elToBorderColor,
borderRadius: elToBorderRadius,
zIndex: elToZIndex,
transformOrigin: '0 0',
transform: elToTransform,
...resizeTo,
...tweenTo
}
], {
duration: options.duration,
easing: options.easing,
fill: options.fill,
delay: options.delay
})
animationFromTween = elFromTween === void 0 ? void 0 : elFromTween.animate([
{
opacity: options.tweenFromOpacity,
margin: 0,
borderWidth: elFromBorderWidth,
borderStyle: elFromBorderStyle,
borderColor: elFromBorderColor,
borderRadius: elFromBorderRadius,
zIndex: elFromZIndex,
transformOrigin: '0 0',
transform: elFromTransform,
...resizeFromTween
},
{
opacity: 0,
margin: 0,
borderWidth: elToBorderWidth,
borderStyle: elToBorderStyle,
borderColor: elToBorderColor,
borderRadius: elToBorderRadius,
zIndex: elToZIndex,
transformOrigin: '0 0',
...resizeToTween
}
], {
duration: options.duration,
easing: options.easing,
fill: options.fill,
delay: options.delay
})
animationFromClone = options.hideFromClone === true || elSharedSize === true ? void 0 : elFromClone.animate([
{
margin: `${elFromParentHeightDiff < 0 ? elFromParentHeightDiff / 2 : 0}px ${elFromParentWidthDiff < 0 ? elFromParentWidthDiff / 2 : 0}px`,
width: `${elFromCloneWidth + elFromPosition.marginH}px`,
height: `${elFromCloneHeight + elFromPosition.marginV}px`
},
{
margin: 0,
width: 0,
height: 0
}
], {
duration: options.duration,
easing: options.easing,
fill: options.fill,
delay: options.delay
})
animationToClone = options.keepToClone === true ? void 0 : elToClone.animate([
elSharedSize === true
? {
margin: `${elFromParentHeightDiff < 0 ? elFromParentHeightDiff / 2 : 0}px ${elFromParentWidthDiff < 0 ? elFromParentWidthDiff / 2 : 0}px`,
width: `${elFromCloneWidth + elFromPosition.marginH}px`,
height: `${elFromCloneHeight + elFromPosition.marginV}px`
}
: {
margin: 0,
width: 0,
height: 0
},
{
margin: `${elToParentHeightDiff < 0 ? elToParentHeightDiff / 2 : 0}px ${elToParentWidthDiff < 0 ? elToParentWidthDiff / 2 : 0}px`,
width: `${elToCloneWidth + elToPosition.marginH}px`,
height: `${elToCloneHeight + elToPosition.marginV}px`
}
], {
duration: options.duration,
easing: options.easing,
fill: options.fill,
delay: options.delay
})
const cleanup = abort => {
animationFromClone !== void 0 && animationFromClone.cancel()
animationFromTween !== void 0 && animationFromTween.cancel()
animationToClone !== void 0 && animationToClone.cancel()
animationTo.cancel()
animationTo.removeEventListener('finish', cleanup)
animationTo.removeEventListener('cancel', cleanup)
commonCleanup(abort)
// we clean the animations
animationFromClone = void 0
animationFromTween = void 0
animationToClone = void 0
animationTo = void 0
}
elFrom.qMorphCancel = () => {
elFrom.qMorphCancel = void 0
cancelStatus = true
cleanup()
}
elTo.qMorphCancel = () => {
elTo.qMorphCancel = void 0
cancelStatus = true
cleanup()
}
animationTo.addEventListener('finish', cleanup)
animationTo.addEventListener('cancel', cleanup)
cancel = abort => {
// we are not in a morph that we can cancel
if (cancelStatus === true || animationTo === void 0) {
return false
}
if (abort === true) {
cleanup(true)
return true
}
endElementTo = endElementTo !== true
animationFromClone !== void 0 && animationFromClone.reverse()
animationFromTween !== void 0 && animationFromTween.reverse()
animationToClone !== void 0 && animationToClone.reverse()
animationTo.reverse()
return true
}
}
else {
const qAnimId = `q-morph-anim-${++id}`
const style = document.createElement('style')
const resizeFrom = options.resize === true
? `
transform: translate(${deltaX}px, ${deltaY}px);
width: ${elFromCloneWidth}px;
height: ${elFromCloneHeight}px;
`
: `transform: translate(${deltaX}px, ${deltaY}px) scale(${scaleX}, ${scaleY});`
const resizeTo = options.resize === true
? `
width: ${elToCloneWidth}px;
height: ${elToCloneHeight}px;
`
: ''
const resizeFromTween = options.resize === true
? `
width: ${elFromCloneWidth}px;
height: ${elFromCloneHeight}px;
`
: ''
const resizeToTween = options.resize === true
? `
transform: translate(${-1 * deltaX}px, ${-1 * deltaY}px);
width: ${elToCloneWidth}px;
height: ${elToCloneHeight}px;
`
: `transform: translate(${-1 * deltaX}px, ${-1 * deltaY}px) scale(${1 / scaleX}, ${1 / scaleY});`
const tweenFrom = elFromTween !== void 0
? `opacity: ${options.tweenToOpacity};`
: `background-color: ${elFromBackground};`
const tweenTo = elFromTween !== void 0
? 'opacity: 1;'
: `background-color: ${elToBackground};`
const keyframesFromTween = elFromTween === void 0
? ''
: `
@keyframes ${qAnimId}-from-tween {
0% {
opacity: ${options.tweenFromOpacity};
margin: 0;
border-width: ${elFromBorderWidth};
border-style: ${elFromBorderStyle};
border-color: ${elFromBorderColor};
border-radius: ${elFromBorderRadius};
z-index: ${elFromZIndex};
transform-origin: 0 0;
transform: ${elFromTransform};
${resizeFromTween}
}
100% {
opacity: 0;
margin: 0;
border-width: ${elToBorderWidth};
border-style: ${elToBorderStyle};
border-color: ${elToBorderColor};
border-radius: ${elToBorderRadius};
z-index: ${elToZIndex};
transform-origin: 0 0;
${resizeToTween}
}
}
`
const keyframesFrom = options.hideFromClone === true || elSharedSize === true
? ''
: `
@keyframes ${qAnimId}-from {
0% {
margin: ${elFromParentHeightDiff < 0 ? elFromParentHeightDiff / 2 : 0}px ${elFromParentWidthDiff < 0 ? elFromParentWidthDiff / 2 : 0}px;
width: ${elFromCloneWidth + elFromPosition.marginH}px;
height: ${elFromCloneHeight + elFromPosition.marginV}px;
}
100% {
margin: 0;
width: 0;
height: 0;
}
}
`
const keyframeToStart = elSharedSize === true
? `
margin: ${elFromParentHeightDiff < 0 ? elFromParentHeightDiff / 2 : 0}px ${elFromParentWidthDiff < 0 ? elFromParentWidthDiff / 2 : 0}px;
width: ${elFromCloneWidth + elFromPosition.marginH}px;
height: ${elFromCloneHeight + elFromPosition.marginV}px;
`
: `
margin: 0;
width: 0;
height: 0;
`
const keyframesTo = options.keepToClone === true
? ''
: `
@keyframes ${qAnimId}-to {
0% {
${keyframeToStart}
}
100% {
margin: ${elToParentHeightDiff < 0 ? elToParentHeightDiff / 2 : 0}px ${elToParentWidthDiff < 0 ? elToParentWidthDiff / 2 : 0}px;
width: ${elToCloneWidth + elToPosition.marginH}px;
height: ${elToCloneHeight + elToPosition.marginV}px;
}
}
`
style.innerHTML = `
@keyframes ${qAnimId} {
0% {
margin: 0;
border-width: ${elFromBorderWidth};
border-style: ${elFromBorderStyle};
border-color: ${elFromBorderColor};
border-radius: ${elFromBorderRadius};
background-color: ${elFromBackground};
z-index: ${elFromZIndex};
transform-origin: 0 0;
${resizeFrom}
${tweenFrom}
}
100% {
margin: 0;
border-width: ${elToBorderWidth};
border-style: ${elToBorderStyle};
border-color: ${elToBorderColor};
border-radius: ${elToBorderRadius};
background-color: ${elToBackground};
z-index: ${elToZIndex};
transform-origin: 0 0;
transform: ${elToTransform};
${resizeTo}
${tweenTo}
}
}
${keyframesFrom}
${keyframesFromTween}
${keyframesTo}
`
document.head.appendChild(style)
let animationDirection = 'normal'
elFromClone.style.animation = `${options.duration}ms ${options.easing} ${options.delay}ms ${animationDirection} ${options.fill} ${qAnimId}-from`
if (elFromTween !== void 0) {
elFromTween.style.animation = `${options.duration}ms ${options.easing} ${options.delay}ms ${animationDirection} ${options.fill} ${qAnimId}-from-tween`
}
elToClone.style.animation = `${options.duration}ms ${options.easing} ${options.delay}ms ${animationDirection} ${options.fill} ${qAnimId}-to`
elTo.style.animation = `${options.duration}ms ${options.easing} ${options.delay}ms ${animationDirection} ${options.fill} ${qAnimId}`
const cleanup = evt => {
if (evt === Object(evt) && evt.animationName !== qAnimId) {
return
}
elTo.removeEventListener('animationend', cleanup)
elTo.removeEventListener('animationcancel', cleanup)
commonCleanup()
// we clean the animations
style.remove()
}
elFrom.qMorphCancel = () => {
elFrom.qMorphCancel = void 0
cancelStatus = true
cleanup()
}
elTo.qMorphCancel = () => {
elTo.qMorphCancel = void 0
cancelStatus = true
cleanup()
}
elTo.addEventListener('animationend', cleanup)
elTo.addEventListener('animationcancel', cleanup)
cancel = abort => {
// we are not in a morph that we can cancel
if (cancelStatus === true || !elTo || !elFromClone || !elToClone) {
return false
}
if (abort === true) {
cleanup()
return true
}
endElementTo = endElementTo !== true
animationDirection = animationDirection === 'normal' ? 'reverse' : 'normal'
elFromClone.style.animationDirection = animationDirection
elFromTween.style.animationDirection = animationDirection
elToClone.style.animationDirection = animationDirection
elTo.style.animationDirection = animationDirection
return true
}
}
}
if (
options.waitFor > 0 ||
options.waitFor === 'transitionend' ||
(options.waitFor === Object(options.waitFor) && typeof options.waitFor.then === 'function')
) {
const delayPromise = options.waitFor > 0
? new Promise(resolve => setTimeout(resolve, options.waitFor))
: (
options.waitFor === 'transitionend'
? new Promise(resolve => {
const timer = setTimeout(() => {
endFn()
}, 400)
const endFn = ev => {
clearTimeout(timer)
if (elTo) {
elTo.removeEventListener('transitionend', endFn)
elTo.removeEventListener('transitioncancel', endFn)
}
resolve()
}
elTo.addEventListener('transitionend', endFn)
elTo.addEventListener('transitioncancel', endFn)
})
: options.waitFor
)
delayPromise
.then(animate)
.catch(() => {
typeof elTo.qMorphCancel === 'function' && elTo.qMorphCancel()
})
}
else {
animate()
}
}
typeof _options.onToggle === 'function' && _options.onToggle()
requestAnimationFrame(calculateFinalState)
// we return the cancel function
// returns:
// false if the cancel cannot be performed (the morph ended already or has not started)
// true else
return abort => cancel(abort)
}