transition.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. observer: 'observeDuration',
  25. },
  26. name: {
  27. type: String,
  28. value: 'fade',
  29. },
  30. },
  31. data: {
  32. type: '',
  33. inited: false,
  34. display: false,
  35. },
  36. ready() {
  37. if (this.data.show === true) {
  38. this.observeShow(true, false);
  39. }
  40. },
  41. methods: {
  42. observeShow(value, old) {
  43. if (value === old) {
  44. return;
  45. }
  46. value ? this.enter() : this.leave();
  47. },
  48. enter() {
  49. const { duration, name } = this.data;
  50. const classNames = getClassNames(name);
  51. const currentDuration = isObj(duration) ? duration.enter : duration;
  52. this.status = 'enter';
  53. this.$emit('before-enter');
  54. requestAnimationFrame(() => {
  55. if (this.status !== 'enter') {
  56. return;
  57. }
  58. this.$emit('enter');
  59. this.setData({
  60. inited: true,
  61. display: true,
  62. classes: classNames.enter,
  63. currentDuration,
  64. });
  65. requestAnimationFrame(() => {
  66. if (this.status !== 'enter') {
  67. return;
  68. }
  69. this.transitionEnded = false;
  70. this.setData({ classes: classNames['enter-to'] });
  71. });
  72. });
  73. },
  74. leave() {
  75. if (!this.data.display) {
  76. return;
  77. }
  78. const { duration, name } = this.data;
  79. const classNames = getClassNames(name);
  80. const currentDuration = isObj(duration) ? duration.leave : duration;
  81. this.status = 'leave';
  82. this.$emit('before-leave');
  83. requestAnimationFrame(() => {
  84. if (this.status !== 'leave') {
  85. return;
  86. }
  87. this.$emit('leave');
  88. this.setData({
  89. classes: classNames.leave,
  90. currentDuration,
  91. });
  92. requestAnimationFrame(() => {
  93. if (this.status !== 'leave') {
  94. return;
  95. }
  96. this.transitionEnded = false;
  97. setTimeout(() => this.onTransitionEnd(), currentDuration);
  98. this.setData({ classes: classNames['leave-to'] });
  99. });
  100. });
  101. },
  102. onTransitionEnd() {
  103. if (this.transitionEnded) {
  104. return;
  105. }
  106. this.transitionEnded = true;
  107. this.$emit(`after-${this.status}`);
  108. const { show, display } = this.data;
  109. if (!show && display) {
  110. this.setData({ display: false });
  111. }
  112. },
  113. },
  114. });
  115. }