transition.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // @ts-nocheck
  2. import { requestAnimationFrame } from '../common/utils';
  3. import { isObj } from '../common/validator';
  4. const getClassNames = (name) => ({
  5. enter: `van-${name}-enter van-${name}-enter-active enter-class enter-active-class`,
  6. 'enter-to': `van-${name}-enter-to van-${name}-enter-active enter-to-class enter-active-class`,
  7. leave: `van-${name}-leave van-${name}-leave-active leave-class leave-active-class`,
  8. 'leave-to': `van-${name}-leave-to van-${name}-leave-active leave-to-class leave-active-class`,
  9. });
  10. export function transition(showDefaultValue) {
  11. return Behavior({
  12. properties: {
  13. customStyle: String,
  14. // @ts-ignore
  15. show: {
  16. type: Boolean,
  17. value: showDefaultValue,
  18. observer: 'observeShow',
  19. },
  20. // @ts-ignore
  21. duration: {
  22. type: null,
  23. value: 300,
  24. },
  25. name: {
  26. type: String,
  27. value: 'fade',
  28. },
  29. },
  30. data: {
  31. type: '',
  32. inited: false,
  33. display: false,
  34. },
  35. ready() {
  36. if (this.data.show === true) {
  37. this.observeShow(true, false);
  38. }
  39. },
  40. methods: {
  41. observeShow(value, old) {
  42. if (value === old) {
  43. return;
  44. }
  45. value ? this.enter() : this.leave();
  46. },
  47. enter() {
  48. if (this.enterFinishedPromise)
  49. return;
  50. this.enterFinishedPromise = new Promise((resolve) => {
  51. const { duration, name } = this.data;
  52. const classNames = getClassNames(name);
  53. const currentDuration = isObj(duration) ? duration.enter : duration;
  54. if (this.status === 'enter') {
  55. return;
  56. }
  57. this.status = 'enter';
  58. this.$emit('before-enter');
  59. requestAnimationFrame(() => {
  60. if (this.status !== 'enter') {
  61. return;
  62. }
  63. this.$emit('enter');
  64. this.setData({
  65. inited: true,
  66. display: true,
  67. classes: classNames.enter,
  68. currentDuration,
  69. });
  70. requestAnimationFrame(() => {
  71. if (this.status !== 'enter') {
  72. return;
  73. }
  74. this.transitionEnded = false;
  75. this.setData({ classes: classNames['enter-to'] });
  76. resolve();
  77. });
  78. });
  79. });
  80. },
  81. leave() {
  82. if (!this.enterFinishedPromise)
  83. return;
  84. this.enterFinishedPromise.then(() => {
  85. if (!this.data.display) {
  86. return;
  87. }
  88. const { duration, name } = this.data;
  89. const classNames = getClassNames(name);
  90. const currentDuration = isObj(duration) ? duration.leave : duration;
  91. this.status = 'leave';
  92. this.$emit('before-leave');
  93. requestAnimationFrame(() => {
  94. if (this.status !== 'leave') {
  95. return;
  96. }
  97. this.$emit('leave');
  98. this.setData({
  99. classes: classNames.leave,
  100. currentDuration,
  101. });
  102. requestAnimationFrame(() => {
  103. if (this.status !== 'leave') {
  104. return;
  105. }
  106. this.transitionEnded = false;
  107. setTimeout(() => {
  108. this.onTransitionEnd();
  109. this.enterFinishedPromise = null;
  110. }, currentDuration);
  111. this.setData({ classes: classNames['leave-to'] });
  112. });
  113. });
  114. });
  115. },
  116. onTransitionEnd() {
  117. if (this.transitionEnded) {
  118. return;
  119. }
  120. this.transitionEnded = true;
  121. this.$emit(`after-${this.status}`);
  122. const { show, display } = this.data;
  123. if (!show && display) {
  124. this.setData({ display: false });
  125. }
  126. },
  127. },
  128. });
  129. }