affix2.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var vue = require('vue');
  4. var core = require('@vueuse/core');
  5. require('../../../utils/index.js');
  6. require('../../../hooks/index.js');
  7. var affix = require('./affix.js');
  8. var pluginVue_exportHelper = require('../../../_virtual/plugin-vue_export-helper.js');
  9. var index = require('../../../hooks/use-namespace/index.js');
  10. var style = require('../../../utils/dom/style.js');
  11. var error = require('../../../utils/error.js');
  12. var scroll = require('../../../utils/dom/scroll.js');
  13. const COMPONENT_NAME = "ElAffix";
  14. const __default__ = vue.defineComponent({
  15. name: COMPONENT_NAME
  16. });
  17. const _sfc_main = /* @__PURE__ */ vue.defineComponent({
  18. ...__default__,
  19. props: affix.affixProps,
  20. emits: affix.affixEmits,
  21. setup(__props, { expose, emit }) {
  22. const props = __props;
  23. const ns = index.useNamespace("affix");
  24. const target = vue.shallowRef();
  25. const root = vue.shallowRef();
  26. const scrollContainer = vue.shallowRef();
  27. const { height: windowHeight } = core.useWindowSize();
  28. const {
  29. height: rootHeight,
  30. width: rootWidth,
  31. top: rootTop,
  32. bottom: rootBottom,
  33. update: updateRoot
  34. } = core.useElementBounding(root, { windowScroll: false });
  35. const targetRect = core.useElementBounding(target);
  36. const fixed = vue.ref(false);
  37. const scrollTop = vue.ref(0);
  38. const transform = vue.ref(0);
  39. const rootStyle = vue.computed(() => {
  40. return {
  41. height: fixed.value ? `${rootHeight.value}px` : "",
  42. width: fixed.value ? `${rootWidth.value}px` : ""
  43. };
  44. });
  45. const affixStyle = vue.computed(() => {
  46. if (!fixed.value)
  47. return {};
  48. const offset = props.offset ? style.addUnit(props.offset) : 0;
  49. return {
  50. height: `${rootHeight.value}px`,
  51. width: `${rootWidth.value}px`,
  52. top: props.position === "top" ? offset : "",
  53. bottom: props.position === "bottom" ? offset : "",
  54. transform: transform.value ? `translateY(${transform.value}px)` : "",
  55. zIndex: props.zIndex
  56. };
  57. });
  58. const update = () => {
  59. if (!scrollContainer.value)
  60. return;
  61. scrollTop.value = scrollContainer.value instanceof Window ? document.documentElement.scrollTop : scrollContainer.value.scrollTop || 0;
  62. if (props.position === "top") {
  63. if (props.target) {
  64. const difference = targetRect.bottom.value - props.offset - rootHeight.value;
  65. fixed.value = props.offset > rootTop.value && targetRect.bottom.value > 0;
  66. transform.value = difference < 0 ? difference : 0;
  67. } else {
  68. fixed.value = props.offset > rootTop.value;
  69. }
  70. } else if (props.target) {
  71. const difference = windowHeight.value - targetRect.top.value - props.offset - rootHeight.value;
  72. fixed.value = windowHeight.value - props.offset < rootBottom.value && windowHeight.value > targetRect.top.value;
  73. transform.value = difference < 0 ? -difference : 0;
  74. } else {
  75. fixed.value = windowHeight.value - props.offset < rootBottom.value;
  76. }
  77. };
  78. const handleScroll = () => {
  79. updateRoot();
  80. emit("scroll", {
  81. scrollTop: scrollTop.value,
  82. fixed: fixed.value
  83. });
  84. };
  85. vue.watch(fixed, (val) => emit("change", val));
  86. vue.onMounted(() => {
  87. var _a;
  88. if (props.target) {
  89. target.value = (_a = document.querySelector(props.target)) != null ? _a : void 0;
  90. if (!target.value)
  91. error.throwError(COMPONENT_NAME, `Target is not existed: ${props.target}`);
  92. } else {
  93. target.value = document.documentElement;
  94. }
  95. scrollContainer.value = scroll.getScrollContainer(root.value, true);
  96. updateRoot();
  97. });
  98. core.useEventListener(scrollContainer, "scroll", handleScroll);
  99. vue.watchEffect(update);
  100. expose({
  101. update,
  102. updateRoot
  103. });
  104. return (_ctx, _cache) => {
  105. return vue.openBlock(), vue.createElementBlock("div", {
  106. ref_key: "root",
  107. ref: root,
  108. class: vue.normalizeClass(vue.unref(ns).b()),
  109. style: vue.normalizeStyle(vue.unref(rootStyle))
  110. }, [
  111. vue.createElementVNode("div", {
  112. class: vue.normalizeClass({ [vue.unref(ns).m("fixed")]: fixed.value }),
  113. style: vue.normalizeStyle(vue.unref(affixStyle))
  114. }, [
  115. vue.renderSlot(_ctx.$slots, "default")
  116. ], 6)
  117. ], 6);
  118. };
  119. }
  120. });
  121. var Affix = /* @__PURE__ */ pluginVue_exportHelper["default"](_sfc_main, [["__file", "/home/runner/work/element-plus/element-plus/packages/components/affix/src/affix.vue"]]);
  122. exports["default"] = Affix;
  123. //# sourceMappingURL=affix2.js.map