transition.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import TComponent from '../common/component';
  2. import config from '../common/config';
  3. import { classNames } from '../common/utils';
  4. const { prefix } = config;
  5. const name = `${prefix}-transition`;
  6. TComponent({
  7. options: {
  8. styleIsolation: 'shared',
  9. },
  10. properties: {
  11. visible: {
  12. type: Boolean,
  13. observer(current, prev) {
  14. if (this.inited && current !== prev) {
  15. if (current) {
  16. this.enter();
  17. }
  18. else {
  19. this.leave();
  20. }
  21. }
  22. },
  23. },
  24. destroyOnHide: Boolean,
  25. appear: Boolean,
  26. customClass: String,
  27. name: {
  28. type: String,
  29. value: name,
  30. },
  31. durations: {
  32. type: Number,
  33. optionalTypes: [Array],
  34. },
  35. },
  36. data: {
  37. dataVisible: false,
  38. transitionClass: '',
  39. transitionDurations: 300,
  40. className: '',
  41. },
  42. lifetimes: {
  43. created() {
  44. this.status = '';
  45. this.transitionT = 0;
  46. },
  47. attached() {
  48. this.setClass();
  49. this.durations = this.getDurations();
  50. if (this.data.appear && this.data.visible) {
  51. this.enter();
  52. }
  53. else if (this.data.visible) {
  54. this.setData({
  55. dataVisible: true,
  56. });
  57. }
  58. this.inited = true;
  59. },
  60. detached() {
  61. clearTimeout(this.transitionT);
  62. },
  63. },
  64. observers: {
  65. 'customClass, transitionClass'() {
  66. this.setClass();
  67. },
  68. },
  69. methods: {
  70. setClass() {
  71. const { customClass, transitionClass } = this.data;
  72. const className = classNames(customClass, transitionClass);
  73. this.setData({
  74. className,
  75. });
  76. },
  77. getDurations() {
  78. const { durations } = this.data;
  79. if (Array.isArray(durations)) {
  80. return durations.map((item) => Number(item));
  81. }
  82. return [Number(durations), Number(durations)];
  83. },
  84. enter() {
  85. const { name } = this.data;
  86. const [duration] = this.durations;
  87. this.status = 'entering';
  88. this.setData({
  89. transitionClass: `${name}-enter ${name}-enter-active`,
  90. dataVisible: true,
  91. });
  92. setTimeout(() => {
  93. this.setData({
  94. transitionClass: `${name}-enter ${name}-enter-active ${name}-enter-to`,
  95. });
  96. }, 30);
  97. if (typeof duration === 'number' && duration > 0) {
  98. this.transitionT = setTimeout(this.entered.bind(this), duration + 30);
  99. }
  100. },
  101. entered() {
  102. this.customDuration = false;
  103. clearTimeout(this.transitionT);
  104. this.status = 'entered';
  105. this.setData({
  106. transitionClass: '',
  107. });
  108. },
  109. leave() {
  110. const { name } = this.data;
  111. const [, duration] = this.durations;
  112. this.status = 'leaving';
  113. this.setData({
  114. transitionClass: `${name}-leave ${name}-leave-active`,
  115. });
  116. setTimeout(() => {
  117. this.setData({
  118. transitionClass: [`${name}-leave ${name}-leave-active ${name}-leave-to`],
  119. });
  120. }, 30);
  121. if (typeof duration === 'number' && duration > 0) {
  122. this.customDuration = true;
  123. this.transitionT = setTimeout(this.leaved.bind(this), duration + 30);
  124. }
  125. },
  126. leaved() {
  127. this.customDuration = false;
  128. clearTimeout(this.transitionT);
  129. this.status = 'leaved';
  130. this.setData({
  131. transitionClass: '',
  132. dataVisible: false,
  133. });
  134. },
  135. onTransitionEnd() {
  136. if (this.customDuration) {
  137. return;
  138. }
  139. clearTimeout(this.transitionT);
  140. if (this.status === 'entering' && this.data.visible) {
  141. this.entered();
  142. }
  143. else if (this.status === 'leaving' && !this.data.visible) {
  144. this.leaved();
  145. }
  146. },
  147. },
  148. });
  149. //# sourceMappingURL=transition.js.map