element-plus
Version:
A Component Library for Vue 3
183 lines (180 loc) • 6.31 kB
JavaScript
import { defineComponent, ref, computed, watch, nextTick, provide, reactive, onMounted, onUpdated, resolveComponent, openBlock, createElementBlock, normalizeClass, createElementVNode, normalizeStyle, createBlock, resolveDynamicComponent, withCtx, renderSlot, createCommentVNode } from 'vue';
import { isNumber, useResizeObserver, useEventListener } from '@vueuse/core';
import '../../../utils/index.mjs';
import '../../../tokens/index.mjs';
import '../../../hooks/index.mjs';
import Bar from './bar2.mjs';
import { scrollbarProps, scrollbarEmits } from './scrollbar.mjs';
import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
import { addUnit } from '../../../utils/dom/style.mjs';
import { isObject } from '@vue/shared';
import { debugWarn } from '../../../utils/error.mjs';
import { scrollbarContextKey } from '../../../tokens/scrollbar.mjs';
const _sfc_main = defineComponent({
name: "ElScrollbar",
components: {
Bar
},
props: scrollbarProps,
emits: scrollbarEmits,
setup(props, { emit }) {
const ns = useNamespace("scrollbar");
let stopResizeObserver = void 0;
let stopResizeListener = void 0;
const scrollbar$ = ref();
const wrap$ = ref();
const resize$ = ref();
const sizeWidth = ref("0");
const sizeHeight = ref("0");
const barRef = ref();
const moveX = ref(0);
const moveY = ref(0);
const ratioY = ref(1);
const ratioX = ref(1);
const SCOPE = "ElScrollbar";
const GAP = 4;
const style = computed(() => {
const style2 = {};
if (props.height)
style2.height = addUnit(props.height);
if (props.maxHeight)
style2.maxHeight = addUnit(props.maxHeight);
return [props.wrapStyle, style2];
});
const handleScroll = () => {
var _a;
if (wrap$.value) {
(_a = barRef.value) == null ? void 0 : _a.handleScroll(wrap$.value);
emit("scroll", {
scrollTop: wrap$.value.scrollTop,
scrollLeft: wrap$.value.scrollLeft
});
}
};
function scrollTo(arg1, arg2) {
if (isObject(arg1)) {
wrap$.value.scrollTo(arg1);
} else if (isNumber(arg1) && isNumber(arg2)) {
wrap$.value.scrollTo(arg1, arg2);
}
}
const setScrollTop = (value) => {
if (!isNumber(value)) {
debugWarn(SCOPE, "value must be a number");
return;
}
wrap$.value.scrollTop = value;
};
const setScrollLeft = (value) => {
if (!isNumber(value)) {
debugWarn(SCOPE, "value must be a number");
return;
}
wrap$.value.scrollLeft = value;
};
const update = () => {
if (!wrap$.value)
return;
const offsetHeight = wrap$.value.offsetHeight - GAP;
const offsetWidth = wrap$.value.offsetWidth - GAP;
const originalHeight = offsetHeight ** 2 / wrap$.value.scrollHeight;
const originalWidth = offsetWidth ** 2 / wrap$.value.scrollWidth;
const height = Math.max(originalHeight, props.minSize);
const width = Math.max(originalWidth, props.minSize);
ratioY.value = originalHeight / (offsetHeight - originalHeight) / (height / (offsetHeight - height));
ratioX.value = originalWidth / (offsetWidth - originalWidth) / (width / (offsetWidth - width));
sizeHeight.value = height + GAP < offsetHeight ? `${height}px` : "";
sizeWidth.value = width + GAP < offsetWidth ? `${width}px` : "";
};
watch(() => props.noresize, (noresize) => {
if (noresize) {
stopResizeObserver == null ? void 0 : stopResizeObserver();
stopResizeListener == null ? void 0 : stopResizeListener();
} else {
;
({ stop: stopResizeObserver } = useResizeObserver(resize$, update));
stopResizeListener = useEventListener("resize", update);
}
}, { immediate: true });
watch(() => [props.maxHeight, props.height], () => {
if (!props.native)
nextTick(() => {
var _a;
update();
if (wrap$.value) {
(_a = barRef.value) == null ? void 0 : _a.handleScroll(wrap$.value);
}
});
});
provide(scrollbarContextKey, reactive({
scrollbarElement: scrollbar$,
wrapElement: wrap$
}));
onMounted(() => {
if (!props.native)
nextTick(() => update());
});
onUpdated(() => update());
return {
ns,
scrollbar$,
wrap$,
resize$,
barRef,
moveX,
moveY,
ratioX,
ratioY,
sizeWidth,
sizeHeight,
style,
update,
handleScroll,
scrollTo,
setScrollTop,
setScrollLeft
};
}
});
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
const _component_bar = resolveComponent("bar");
return openBlock(), createElementBlock("div", {
ref: "scrollbar$",
class: normalizeClass(_ctx.ns.b())
}, [
createElementVNode("div", {
ref: "wrap$",
class: normalizeClass([
_ctx.wrapClass,
_ctx.ns.e("wrap"),
{ [_ctx.ns.em("wrap", "hidden-default")]: !_ctx.native }
]),
style: normalizeStyle(_ctx.style),
onScroll: _cache[0] || (_cache[0] = (...args) => _ctx.handleScroll && _ctx.handleScroll(...args))
}, [
(openBlock(), createBlock(resolveDynamicComponent(_ctx.tag), {
ref: "resize$",
class: normalizeClass([_ctx.ns.e("view"), _ctx.viewClass]),
style: normalizeStyle(_ctx.viewStyle)
}, {
default: withCtx(() => [
renderSlot(_ctx.$slots, "default")
]),
_: 3
}, 8, ["class", "style"]))
], 38),
!_ctx.native ? (openBlock(), createBlock(_component_bar, {
key: 0,
ref: "barRef",
height: _ctx.sizeHeight,
width: _ctx.sizeWidth,
always: _ctx.always,
"ratio-x": _ctx.ratioX,
"ratio-y": _ctx.ratioY
}, null, 8, ["height", "width", "always", "ratio-x", "ratio-y"])) : createCommentVNode("v-if", true)
], 2);
}
var Scrollbar = /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render], ["__file", "/home/runner/work/element-plus/element-plus/packages/components/scrollbar/src/scrollbar.vue"]]);
export { Scrollbar as default };
//# sourceMappingURL=scrollbar2.mjs.map