@aplus-frontend/ui
Version:
131 lines (130 loc) • 4.25 kB
JavaScript
import { defineComponent as V, ref as f, toRef as p, watch as W, watchEffect as z, unref as o, createElementBlock as A, openBlock as N, normalizeStyle as U, normalizeClass as b, createElementVNode as E } from "vue";
import "../config-provider/index.mjs";
import { useTrack as X } from "./hooks/use-track.mjs";
import { ensureRangeValue as L } from "./utils/index.mjs";
import Y from "./style/index.mjs";
import { useNamespace as I } from "../config-provider/hooks/use-namespace.mjs";
const K = /* @__PURE__ */ V({
__name: "internal",
props: {
direction: { default: "vertical" },
scroll: { default: 0 },
barLength: { default: 35 },
width: {},
barColor: {},
trackColor: {},
trackWidth: {},
disabled: { type: Boolean, default: !1 },
showTrack: { type: Boolean, default: !0 },
trackSpeed: { default: 2 },
onScrollStart: {},
onScroll: {},
onScrollEnd: {}
},
setup(C, { expose: D }) {
const t = C, m = f(), i = f(), l = f(), { b: M, e: k, m: B, em: c, cssVar: P } = I("scroll-bar"), R = Y("scroll-bar"), n = f(t.scroll), { handleMouseDown: v } = X({
currentScroll: n,
track: i,
bar: l,
type: p(t, "direction"),
trackSpeed: p(t, "trackSpeed"),
barLength: p(t, "barLength"),
disabled: p(t, "disabled"),
onDown: (e) => {
t.onScrollStart?.(e);
},
onUp: (e) => {
t.onScrollEnd?.(e);
},
onScroll: (e) => {
t.onScroll?.(e);
}
});
W(
() => t.scroll,
(e) => {
n.value = e;
}
), z(() => {
if (!o(l)) return;
const e = `${(100 - t.barLength) * n.value / t.barLength}%`, r = `${t.barLength}%`;
t.direction === "vertical" ? (l.value.style.height = r, l.value.style.transform = `translate3d(0, ${e}, 0)`) : (l.value.style.width = r, l.value.style.transform = `translate3d(${e}, 0, 0)`);
});
let h, s, d;
function T(e) {
if (t.disabled || e.button !== 0) return;
e.preventDefault(), e.stopPropagation();
const r = o(i), a = o(l);
if (!r || !a)
return;
document.addEventListener("pointermove", g), document.addEventListener("pointerup", S);
const u = r.getBoundingClientRect(), y = a.getBoundingClientRect();
t.direction === "vertical" ? (h = u.height, s = y.top - u.top, d = e.clientY) : (h = u.width, s = y.left - u.left, d = e.clientX), t.onScrollStart?.(o(n));
}
function g(e) {
e.stopPropagation(), e.preventDefault();
let r;
t.direction === "vertical" ? r = s + e.clientY - d : r = s + e.clientX - d;
const a = r / h / (100 - t.barLength) * 1e4;
n.value = L(a), t.onScroll?.(o(n));
}
function S(e) {
e.preventDefault(), document.removeEventListener("pointermove", g), document.removeEventListener("pointerup", S), t.onScrollEnd?.(o(n));
}
function w(e) {
e.cancelable && (e.stopPropagation(), e.preventDefault());
}
function $(e) {
Math.abs(o(n) - e) <= 1e-3 || (n.value = L(e));
}
return D({
currentScroll: n,
container: m,
bar: l,
track: i,
scroll: $
}), (e, r) => (N(), A("div", {
ref_key: "container",
ref: m,
class: b([o(M)(), o(B)(e.direction), o(R)]),
style: U(
o(P)(
{
"bar-color": e.barColor,
"track-color": e.trackColor,
"bar-width": e.width ? `${e.width}px` : void 0,
"track-width": e.trackWidth ? `${e.trackWidth}px` : void 0
},
!1
)
)
}, [
E("div", {
ref_key: "track",
ref: i,
class: b([
o(k)("track"),
e.disabled ? o(c)("track", "disabled") : null,
e.showTrack ? null : o(c)("track", "hidden")
]),
onPointerdown: r[0] || (r[0] = //@ts-ignore
(...a) => o(v) && o(v)(...a)),
onTouchstart: w
}, null, 34),
E("div", {
ref_key: "bar",
ref: l,
class: b([
o(k)("bar"),
o(c)("bar", e.direction),
e.disabled ? o(c)("bar", "disabled") : null
]),
onPointerdown: T,
onTouchstart: w
}, null, 34)
], 6));
}
});
export {
K as default
};