transition.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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.enureEnter() : this.enureLeave();
  46. },
  47. enureEnter() {
  48. if (this.enterPromise)
  49. return;
  50. this.enterPromise = new Promise((resolve) => this.enter(resolve));
  51. },
  52. enureLeave() {
  53. const { enterPromise } = this;
  54. if (!enterPromise)
  55. return;
  56. enterPromise
  57. .then(() => new Promise((resolve) => this.leave(resolve)))
  58. .then(() => {
  59. this.enterPromise = null;
  60. });
  61. },
  62. enter(resolve) {
  63. const { duration, name } = this.data;
  64. const classNames = getClassNames(name);
  65. const currentDuration = isObj(duration) ? duration.enter : duration;
  66. if (this.status === 'enter') {
  67. return;
  68. }
  69. this.status = 'enter';
  70. this.$emit('before-enter');
  71. requestAnimationFrame(() => {
  72. if (this.status !== 'enter') {
  73. return;
  74. }
  75. this.$emit('enter');
  76. this.setData({
  77. inited: true,
  78. display: true,
  79. classes: classNames.enter,
  80. currentDuration,
  81. });
  82. requestAnimationFrame(() => {
  83. if (this.status !== 'enter') {
  84. return;
  85. }
  86. this.transitionEnded = false;
  87. this.setData({ classes: classNames['enter-to'] });
  88. resolve();
  89. });
  90. });
  91. },
  92. leave(resolve) {
  93. if (!this.data.display) {
  94. return;
  95. }
  96. const { duration, name } = this.data;
  97. const classNames = getClassNames(name);
  98. const currentDuration = isObj(duration) ? duration.leave : duration;
  99. this.status = 'leave';
  100. this.$emit('before-leave');
  101. requestAnimationFrame(() => {
  102. if (this.status !== 'leave') {
  103. return;
  104. }
  105. this.$emit('leave');
  106. this.setData({
  107. classes: classNames.leave,
  108. currentDuration,
  109. });
  110. requestAnimationFrame(() => {
  111. if (this.status !== 'leave') {
  112. return;
  113. }
  114. this.transitionEnded = false;
  115. setTimeout(() => {
  116. this.onTransitionEnd();
  117. resolve();
  118. }, currentDuration);
  119. this.setData({ classes: classNames['leave-to'] });
  120. });
  121. });
  122. },
  123. onTransitionEnd() {
  124. if (this.transitionEnded) {
  125. return;
  126. }
  127. this.transitionEnded = true;
  128. this.$emit(`after-${this.status}`);
  129. const { show, display } = this.data;
  130. if (!show && display) {
  131. this.setData({ display: false });
  132. }
  133. },
  134. },
  135. });
  136. }