UNPKG

simplebar-react

Version:

React component for SimpleBar

118 lines (109 loc) 5.66 kB
/** * simplebar-react - v3.3.2 * React component for SimpleBar * https://grsmto.github.io/simplebar/ * * Made by Adrien Denat * Under MIT License */ import * as React from 'react'; import SimpleBarCore from 'simplebar-core'; /****************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ var __assign = function() { __assign = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; function __rest(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } var SimpleBar = React.forwardRef(function (_a, ref) { var children = _a.children, _b = _a.scrollableNodeProps, scrollableNodeProps = _b === void 0 ? {} : _b, otherProps = __rest(_a, ["children", "scrollableNodeProps"]); var elRef = React.useRef(); var scrollableNodeRef = React.useRef(); var contentNodeRef = React.useRef(); var options = {}; var rest = {}; Object.keys(otherProps).forEach(function (key) { if (Object.prototype.hasOwnProperty.call(SimpleBarCore.defaultOptions, key)) { options[key] = otherProps[key]; } else { rest[key] = otherProps[key]; } }); var classNames = __assign(__assign({}, SimpleBarCore.defaultOptions.classNames), options.classNames); var scrollableNodeFullProps = __assign(__assign({}, scrollableNodeProps), { className: "".concat(classNames.contentWrapper).concat(scrollableNodeProps.className ? " ".concat(scrollableNodeProps.className) : ''), tabIndex: options.tabIndex || SimpleBarCore.defaultOptions.tabIndex, role: 'region', 'aria-label': options.ariaLabel || SimpleBarCore.defaultOptions.ariaLabel }); React.useEffect(function () { var instance; scrollableNodeRef.current = scrollableNodeFullProps.ref ? scrollableNodeFullProps.ref.current : scrollableNodeRef.current; if (elRef.current) { instance = new SimpleBarCore(elRef.current, __assign(__assign(__assign({}, options), (scrollableNodeRef.current && { scrollableNode: scrollableNodeRef.current })), (contentNodeRef.current && { contentNode: contentNodeRef.current }))); if (typeof ref === 'function') { ref(instance); } else if (ref) { ref.current = instance; } } return function () { instance === null || instance === void 0 ? void 0 : instance.unMount(); instance = null; if (typeof ref === 'function') { ref(null); } }; }, []); return (React.createElement("div", __assign({ "data-simplebar": "init", ref: elRef }, rest), React.createElement("div", { className: classNames.wrapper }, React.createElement("div", { className: classNames.heightAutoObserverWrapperEl }, React.createElement("div", { className: classNames.heightAutoObserverEl })), React.createElement("div", { className: classNames.mask }, React.createElement("div", { className: classNames.offset }, typeof children === 'function' ? (children({ scrollableNodeRef: scrollableNodeRef, scrollableNodeProps: __assign(__assign({}, scrollableNodeFullProps), { ref: scrollableNodeRef }), contentNodeRef: contentNodeRef, contentNodeProps: { className: classNames.contentEl, ref: contentNodeRef } })) : (React.createElement("div", __assign({}, scrollableNodeFullProps), React.createElement("div", { className: classNames.contentEl }, children))))), React.createElement("div", { className: classNames.placeholder })), React.createElement("div", { className: "".concat(classNames.track, " ").concat(classNames.horizontal) }, React.createElement("div", { className: classNames.scrollbar })), React.createElement("div", { className: "".concat(classNames.track, " ").concat(classNames.vertical) }, React.createElement("div", { className: classNames.scrollbar })))); }); SimpleBar.displayName = 'SimpleBar'; export { SimpleBar as default };