You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.8 KiB

3 months ago
  1. import { defineComponent, ref, computed, watch, nextTick, provide, reactive, onActivated, onMounted, onUpdated, openBlock, createElementBlock, normalizeClass, unref, createElementVNode, normalizeStyle, createBlock, resolveDynamicComponent, withCtx, renderSlot, createCommentVNode } from 'vue';
  2. import { useResizeObserver, useEventListener } from '@vueuse/core';
  3. import Bar from './bar2.mjs';
  4. import { scrollbarContextKey } from './constants.mjs';
  5. import { scrollbarProps, scrollbarEmits } from './scrollbar.mjs';
  6. import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
  7. import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
  8. import { addUnit } from '../../../utils/dom/style.mjs';
  9. import { isObject } from '@vue/shared';
  10. import { isNumber } from '../../../utils/types.mjs';
  11. import { debugWarn } from '../../../utils/error.mjs';
  12. const COMPONENT_NAME = "ElScrollbar";
  13. const __default__ = defineComponent({
  14. name: COMPONENT_NAME
  15. });
  16. const _sfc_main = /* @__PURE__ */ defineComponent({
  17. ...__default__,
  18. props: scrollbarProps,
  19. emits: scrollbarEmits,
  20. setup(__props, { expose, emit }) {
  21. const props = __props;
  22. const ns = useNamespace("scrollbar");
  23. let stopResizeObserver = void 0;
  24. let stopResizeListener = void 0;
  25. let wrapScrollTop = 0;
  26. let wrapScrollLeft = 0;
  27. const scrollbarRef = ref();
  28. const wrapRef = ref();
  29. const resizeRef = ref();
  30. const barRef = ref();
  31. const wrapStyle = computed(() => {
  32. const style = {};
  33. if (props.height)
  34. style.height = addUnit(props.height);
  35. if (props.maxHeight)
  36. style.maxHeight = addUnit(props.maxHeight);
  37. return [props.wrapStyle, style];
  38. });
  39. const wrapKls = computed(() => {
  40. return [
  41. props.wrapClass,
  42. ns.e("wrap"),
  43. { [ns.em("wrap", "hidden-default")]: !props.native }
  44. ];
  45. });
  46. const resizeKls = computed(() => {
  47. return [ns.e("view"), props.viewClass];
  48. });
  49. const handleScroll = () => {
  50. var _a;
  51. if (wrapRef.value) {
  52. (_a = barRef.value) == null ? void 0 : _a.handleScroll(wrapRef.value);
  53. wrapScrollTop = wrapRef.value.scrollTop;
  54. wrapScrollLeft = wrapRef.value.scrollLeft;
  55. emit("scroll", {
  56. scrollTop: wrapRef.value.scrollTop,
  57. scrollLeft: wrapRef.value.scrollLeft
  58. });
  59. }
  60. };
  61. function scrollTo(arg1, arg2) {
  62. if (isObject(arg1)) {
  63. wrapRef.value.scrollTo(arg1);
  64. } else if (isNumber(arg1) && isNumber(arg2)) {
  65. wrapRef.value.scrollTo(arg1, arg2);
  66. }
  67. }
  68. const setScrollTop = (value) => {
  69. if (!isNumber(value)) {
  70. debugWarn(COMPONENT_NAME, "value must be a number");
  71. return;
  72. }
  73. wrapRef.value.scrollTop = value;
  74. };
  75. const setScrollLeft = (value) => {
  76. if (!isNumber(value)) {
  77. debugWarn(COMPONENT_NAME, "value must be a number");
  78. return;
  79. }
  80. wrapRef.value.scrollLeft = value;
  81. };
  82. const update = () => {
  83. var _a;
  84. (_a = barRef.value) == null ? void 0 : _a.update();
  85. };
  86. watch(() => props.noresize, (noresize) => {
  87. if (noresize) {
  88. stopResizeObserver == null ? void 0 : stopResizeObserver();
  89. stopResizeListener == null ? void 0 : stopResizeListener();
  90. } else {
  91. ({ stop: stopResizeObserver } = useResizeObserver(resizeRef, update));
  92. stopResizeListener = useEventListener("resize", update);
  93. }
  94. }, { immediate: true });
  95. watch(() => [props.maxHeight, props.height], () => {
  96. if (!props.native)
  97. nextTick(() => {
  98. var _a;
  99. update();
  100. if (wrapRef.value) {
  101. (_a = barRef.value) == null ? void 0 : _a.handleScroll(wrapRef.value);
  102. }
  103. });
  104. });
  105. provide(scrollbarContextKey, reactive({
  106. scrollbarElement: scrollbarRef,
  107. wrapElement: wrapRef
  108. }));
  109. onActivated(() => {
  110. if (wrapRef.value) {
  111. wrapRef.value.scrollTop = wrapScrollTop;
  112. wrapRef.value.scrollLeft = wrapScrollLeft;
  113. }
  114. });
  115. onMounted(() => {
  116. if (!props.native)
  117. nextTick(() => {
  118. update();
  119. });
  120. });
  121. onUpdated(() => update());
  122. expose({
  123. wrapRef,
  124. update,
  125. scrollTo,
  126. setScrollTop,
  127. setScrollLeft,
  128. handleScroll
  129. });
  130. return (_ctx, _cache) => {
  131. return openBlock(), createElementBlock("div", {
  132. ref_key: "scrollbarRef",
  133. ref: scrollbarRef,
  134. class: normalizeClass(unref(ns).b())
  135. }, [
  136. createElementVNode("div", {
  137. ref_key: "wrapRef",
  138. ref: wrapRef,
  139. class: normalizeClass(unref(wrapKls)),
  140. style: normalizeStyle(unref(wrapStyle)),
  141. tabindex: _ctx.tabindex,
  142. onScroll: handleScroll
  143. }, [
  144. (openBlock(), createBlock(resolveDynamicComponent(_ctx.tag), {
  145. id: _ctx.id,
  146. ref_key: "resizeRef",
  147. ref: resizeRef,
  148. class: normalizeClass(unref(resizeKls)),
  149. style: normalizeStyle(_ctx.viewStyle),
  150. role: _ctx.role,
  151. "aria-label": _ctx.ariaLabel,
  152. "aria-orientation": _ctx.ariaOrientation
  153. }, {
  154. default: withCtx(() => [
  155. renderSlot(_ctx.$slots, "default")
  156. ]),
  157. _: 3
  158. }, 8, ["id", "class", "style", "role", "aria-label", "aria-orientation"]))
  159. ], 46, ["tabindex"]),
  160. !_ctx.native ? (openBlock(), createBlock(Bar, {
  161. key: 0,
  162. ref_key: "barRef",
  163. ref: barRef,
  164. always: _ctx.always,
  165. "min-size": _ctx.minSize
  166. }, null, 8, ["always", "min-size"])) : createCommentVNode("v-if", true)
  167. ], 2);
  168. };
  169. }
  170. });
  171. var Scrollbar = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "scrollbar.vue"]]);
  172. export { Scrollbar as default };
  173. //# sourceMappingURL=scrollbar2.mjs.map