@prefect9/ui
Version:
UI React components
135 lines (131 loc) • 6.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/web.dom-collections.iterator.js");
var _react = require("react");
var _isType = require("@prefect9/is-type");
var _jsxRuntime = require("react/jsx-runtime");
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
const Scroll = /*#__PURE__*/(0, _react.forwardRef)(function (_ref2, ref) {
let {
children,
className,
actions
} = _ref2;
const _ref = (0, _react.useRef)(null);
const scrollRef = ref !== null && ref !== void 0 ? ref : _ref;
const scrollContainerRef = (0, _react.useRef)(null);
const verticalBarRef = (0, _react.useRef)(null);
const verticalBarSlickRef = (0, _react.useRef)(null);
// Render children
const [elements, setElements] = (0, _react.useState)([]);
(0, _react.useEffect)(() => {
const newElements = [];
const processingChild = child => {
const index = newElements.length;
const element = {};
if (child.props.ref) {
element.ref = child.props.ref;
element.content = /*#__PURE__*/(0, _react.cloneElement)(child, _objectSpread(_objectSpread({}, child.props), {}, {
key: index
}));
} else {
element.content = /*#__PURE__*/(0, _react.cloneElement)(child, _objectSpread(_objectSpread({}, child.props), {}, {
key: index,
ref: _ref3 => element.ref = _ref3
}));
}
newElements.push(element);
};
if ((0, _isType.isArr)(children)) for (let child of children) processingChild(child);else if ((0, _isType.isObj)(children)) processingChild(children);
setElements(newElements);
}, [children]);
// Init resize
const [showVerticalScroll, setShowVerticalScroll] = (0, _react.useState)(false);
(0, _react.useEffect)(() => {
let containerHeight;
let contentHeight;
let verticalBarHeight;
let verticalBarSlickHeight;
const scroll = () => {
if (!scrollContainerRef || !scrollContainerRef.current) return;
if (!verticalBarSlickRef || !verticalBarSlickRef.current) return;
const scrollTop = scrollContainerRef.current.scrollTop;
const barTop = verticalBarHeight * scrollTop / contentHeight;
verticalBarSlickRef.current.style.top = "".concat(barTop, "px");
};
const resize = () => {
if (!scrollRef || !scrollRef.current) return;
if (!scrollContainerRef || !scrollContainerRef.current) return;
if (!verticalBarRef || !verticalBarRef.current) return;
if (!verticalBarSlickRef || !verticalBarSlickRef.current) return;
// reset styles
scrollContainerRef.current.style.maxHeight = 'auto';
verticalBarRef.current.style.display = 'none';
containerHeight = scrollRef.current.getBoundingClientRect().height;
contentHeight = 0;
for (let element of elements) {
if (!element.ref) continue;
contentHeight += element.ref.getBoundingClientRect().height;
}
if (contentHeight > containerHeight) {
scrollContainerRef.current.style.maxHeight = "".concat(containerHeight, "px");
setShowVerticalScroll(true);
verticalBarRef.current.style.display = 'block';
verticalBarHeight = verticalBarRef.current.getBoundingClientRect().height;
verticalBarSlickHeight = verticalBarHeight * containerHeight / contentHeight;
verticalBarSlickRef.current.style.height = "".concat(verticalBarSlickHeight, "px");
} else setShowVerticalScroll(false);
scroll();
};
if (actions && (0, _isType.isObj)(actions.current)) {
actions.current.resize = resize;
actions.current.scrollTop = scrollTop => {
if (!scrollContainerRef || !scrollContainerRef.current) return;
scrollContainerRef.current.scrollTop = scrollTop;
scroll();
};
}
resize();
window.addEventListener('resize', resize, {
passive: true
});
let initedScrollEvent = false;
if (scrollContainerRef.current) {
initedScrollEvent = true;
scrollContainerRef.current.addEventListener('scroll', scroll, {
passive: true
});
}
return () => {
window.removeEventListener('resize', resize, {
passive: true
});
if (!initedScrollEvent) scrollContainerRef.current.removeEventListener('scroll', scroll, {
passive: true
});
};
}, [elements, actions, scrollRef, scrollContainerRef, verticalBarRef, verticalBarSlickRef]);
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
ref: scrollRef,
className: "prefect9-scroll ".concat(className, " ").concat(showVerticalScroll ? 'prefect9-scroll__show-vertical' : ''),
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
ref: scrollContainerRef,
className: "prefect9-scroll__container",
children: elements.map(element => element.content)
}), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
ref: verticalBarRef,
className: "prefect9-scroll__vertical-bar",
children: /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
ref: verticalBarSlickRef
})
})]
});
});
var _default = exports.default = Scroll;