transition.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. methods: {
  37. observeShow(value, old) {
  38. if (value === old) {
  39. return;
  40. }
  41. value ? this.enter() : this.leave();
  42. },
  43. enter() {
  44. const { duration, name } = this.data;
  45. const classNames = getClassNames(name);
  46. const currentDuration = isObj(duration) ? duration.enter : duration;
  47. this.status = 'enter';
  48. this.$emit('before-enter');
  49. requestAnimationFrame(() => {
  50. this.checkStatus('enter');
  51. this.$emit('enter');
  52. this.setData({
  53. inited: true,
  54. display: true,
  55. classes: classNames.enter,
  56. currentDuration,
  57. });
  58. requestAnimationFrame(() => {
  59. this.checkStatus('enter');
  60. this.transitionEnded = false;
  61. this.setData({ classes: classNames['enter-to'] });
  62. });
  63. });
  64. },
  65. leave() {
  66. if (!this.data.display) {
  67. return;
  68. }
  69. const { duration, name } = this.data;
  70. const classNames = getClassNames(name);
  71. const currentDuration = isObj(duration) ? duration.leave : duration;
  72. this.status = 'leave';
  73. this.$emit('before-leave');
  74. requestAnimationFrame(() => {
  75. this.checkStatus('leave');
  76. this.$emit('leave');
  77. this.setData({
  78. classes: classNames.leave,
  79. currentDuration,
  80. });
  81. requestAnimationFrame(() => {
  82. this.checkStatus('leave');
  83. this.transitionEnded = false;
  84. setTimeout(() => this.onTransitionEnd(), currentDuration);
  85. this.setData({ classes: classNames['leave-to'] });
  86. });
  87. });
  88. },
  89. checkStatus(status) {
  90. if (status !== this.status) {
  91. throw new Error(`incongruent status: ${status}`);
  92. }
  93. },
  94. onTransitionEnd() {
  95. if (this.transitionEnded) {
  96. return;
  97. }
  98. this.transitionEnded = true;
  99. this.$emit(`after-${this.status}`);
  100. const { show, display } = this.data;
  101. if (!show && display) {
  102. this.setData({ display: false });
  103. }
  104. },
  105. },
  106. });
  107. }