u-drawer.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <view v-show="visible">
  3. <view
  4. class="u-drawer__overlay"
  5. :style="{ zIndex: zIndex }"
  6. @click.stop="handleOverlayClick"
  7. >
  8. </view>
  9. <view
  10. class="u-drawer"
  11. :class="{
  12. 'u-drawer--left': placement === 'left',
  13. 'u-drawer--right': placement === 'right'
  14. }"
  15. :style="{ zIndex: zIndex + 1, width: width, [placement]: `-${translate}` }"
  16. @transitionend="handleTransitionEnd"
  17. >
  18. <view class="u-drawer__content">
  19. <slot></slot>
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. import props from './props.js'
  26. export default {
  27. name: 'u-drawer',
  28. mixins: [props],
  29. // props: {
  30. // //
  31. // visible: {
  32. // type: Boolean,
  33. // default: false,
  34. // },
  35. // mask: {
  36. // type: Boolean,
  37. // default: true,
  38. // },
  39. // width: {
  40. // type: Number,
  41. // default: 300,
  42. // },
  43. // placement: {
  44. // type: String,
  45. // default: 'left',
  46. // validator: (value) => ['left', 'right'].includes(value),
  47. // },
  48. // // transition: {
  49. // // type: String,
  50. // // default: 'u-slide-down',
  51. // // },
  52. // zIndex: {
  53. // type: Number,
  54. // default: 100,
  55. // },
  56. // },
  57. data() {
  58. return {
  59. translate: 0,
  60. };
  61. },
  62. watch: {
  63. show: {
  64. immediate: true,
  65. handler(value) {
  66. if (value) {
  67. this.openDrawer();
  68. } else {
  69. this.closeDrawer();
  70. }
  71. },
  72. },
  73. },
  74. methods: {
  75. openDrawer() {
  76. this.$emit('open');
  77. this.translate = this.width;
  78. },
  79. closeDrawer() {
  80. this.$emit('close');
  81. this.translate = 0;
  82. },
  83. handleOverlayClick() {
  84. // console.log('this.mask',this.mask)
  85. if (this.mask) {
  86. this.$emit('update:visible', false);
  87. }
  88. },
  89. handleTransitionEnd() {
  90. if (!this.visible) {
  91. this.translate = 0;
  92. }
  93. },
  94. },
  95. };
  96. </script>
  97. <style scoped>
  98. .u-drawer {
  99. position: fixed;
  100. top: 0;
  101. bottom: 0;
  102. background-color: #fff;
  103. box-shadow: 2px 0 4px rgba(0, 0, 0, 0.1);
  104. overflow: hidden;
  105. }
  106. .u-drawer--left {
  107. left: 0;
  108. }
  109. .u-drawer--right {
  110. right: 0;
  111. }
  112. .u-drawer__content {
  113. height: 100%;
  114. overflow-y: auto;
  115. }
  116. .u-drawer__overlay {
  117. position: fixed;
  118. top: 0;
  119. left: 0;
  120. bottom: 0;
  121. right: 0;
  122. background-color: rgba(0, 0, 0, 0.5);
  123. }
  124. </style>