vue-popper.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import PopperJS from './popper';
  2. import { PopupManager } from 'vue-popup';
  3. /**
  4. * @param {HTMLElement} [reference=$refs.reference] - The reference element used to position the popper.
  5. * @param {HTMLElement} [popper=$refs.popper] - The HTML element used as popper, or a configuration used to generate the popper.
  6. * @param {String} [placement=button] - Placement of the popper accepted values: top(-start, -end), right(-start, -end), bottom(-start, -right), left(-start, -end)
  7. * @param {Number} [offset=0] - Amount of pixels the popper will be shifted (can be negative).
  8. * @param {Boolean} [visible=false] Visibility of the popup element.
  9. * @param {Boolean} [visible-arrow=false] Visibility of the arrow, no style.
  10. */
  11. export default {
  12. props: {
  13. placement: {
  14. type: String,
  15. default: 'bottom'
  16. },
  17. boundariesPadding: {
  18. type: Number,
  19. default: 5
  20. },
  21. reference: Object,
  22. popper: Object,
  23. offset: {
  24. default: 0
  25. },
  26. value: Boolean,
  27. visibleArrow: Boolean,
  28. transition: String,
  29. appendToBody: {
  30. type: Boolean,
  31. default: true
  32. },
  33. options: {
  34. type: Object,
  35. default() {
  36. return {
  37. gpuAcceleration: false
  38. };
  39. }
  40. }
  41. },
  42. data() {
  43. return {
  44. showPopper: false
  45. };
  46. },
  47. watch: {
  48. value: {
  49. immediate: true,
  50. handler(val) {
  51. this.showPopper = val;
  52. this.$emit('input', val);
  53. }
  54. },
  55. showPopper(val) {
  56. val ? this.updatePopper() : this.destroyPopper();
  57. this.$emit('input', val);
  58. }
  59. },
  60. methods: {
  61. createPopper() {
  62. if (!/^(top|bottom|left|right)(-start|-end)?$/g.test(this.placement)) {
  63. return;
  64. }
  65. const options = this.options;
  66. const popper = this.popperElm = this.popperElm || this.popper || this.$refs.popper;
  67. const reference = this.referenceElm = this.referenceElm || this.reference || this.$refs.reference || this.$slots.reference[0].elm;
  68. if (!popper || !reference) return;
  69. if (this.visibleArrow) this.appendArrow(popper);
  70. if (this.appendToBody) document.body.appendChild(this.popperElm);
  71. if (this.popperJS && this.popperJS.hasOwnProperty('destroy')) {
  72. this.popperJS.destroy();
  73. }
  74. options.placement = this.placement;
  75. options.offset = this.offset;
  76. this.popperJS = new PopperJS(reference, popper, options);
  77. this.popperJS.onCreate(_ => {
  78. this.$emit('created', this);
  79. this.resetTransformOrigin();
  80. });
  81. this.popperJS._popper.style.zIndex = PopupManager.nextZIndex();
  82. },
  83. updatePopper() {
  84. this.popperJS ? this.popperJS.update() : this.createPopper();
  85. },
  86. doDestroy() {
  87. if (this.showPopper || !this.popperJS) return;
  88. this.popperJS.destroy();
  89. this.popperJS = null;
  90. },
  91. destroyPopper() {
  92. if (this.popperJS) {
  93. this.resetTransformOrigin();
  94. }
  95. },
  96. resetTransformOrigin() {
  97. let placementMap = { top: 'bottom', bottom: 'top', left: 'right', right: 'left' };
  98. let placement = this.popperJS._popper.getAttribute('x-placement').split('-')[0];
  99. let origin = placementMap[placement];
  100. this.popperJS._popper.style.transformOrigin = ['top', 'bottom'].indexOf(placement) > -1 ? `center ${ origin }` : `${ origin } center`;
  101. },
  102. appendArrow(element) {
  103. let hash;
  104. if (this.appended) {
  105. return;
  106. }
  107. this.appended = true;
  108. for (let item in element.attributes) {
  109. if (/^_v-/.test(element.attributes[item].name)) {
  110. hash = element.attributes[item].name;
  111. break;
  112. }
  113. }
  114. const arrow = document.createElement('div');
  115. if (hash) {
  116. arrow.setAttribute(hash, '');
  117. }
  118. arrow.setAttribute('x-arrow', '');
  119. arrow.className = 'popper__arrow';
  120. element.appendChild(arrow);
  121. }
  122. },
  123. beforeDestroy() {
  124. this.doDestroy();
  125. this.popperElm &&
  126. document.body.contains(this.popperElm) &&
  127. document.body.removeChild(this.popperElm);
  128. }
  129. };