UNPKG

chayns-components

Version:

A set of beautiful React components for developing chayns® applications.

156 lines (153 loc) 5.64 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.default = void 0; var _clsx = _interopRequireDefault(require("clsx")); var _propTypes = _interopRequireDefault(require("prop-types")); var _react = _interopRequireWildcard(require("react")); var _withPolyfill = _interopRequireDefault(require("react-resize-detector/build/withPolyfill")); var _ScrollViewHelper = _interopRequireDefault(require("./ScrollViewHelper")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /** * @component */ /** * A scrollable container with a custom scrollbar that looks great on every * device. */ class ScrollView extends _react.Component { constructor(props) { super(props); this.handleRefreshScrollView = () => { if (this.scrollView) { this.scrollView.refresh(); } }; this.state = { contentWidth: 0 }; this.scrollTo = this.scrollTo.bind(this); this.setContentWidth = this.setContentWidth.bind(this); } componentDidMount() { const { contentWidth } = this.state; this.scrollView = new _ScrollViewHelper.default(this.node, { wrapper: this.wrapper, content: this.content, bar: this.bar }); if (contentWidth === 0) { this.setContentWidth(); } } componentDidUpdate() { this.handleRefreshScrollView(); } setContentWidth() { this.setState({ contentWidth: this.content.getBoundingClientRect().width - this.children.getBoundingClientRect().width + 1 }); } scrollTo() { if (this.content) { this.content.scrollTo(...arguments); } } render() { const { style, className, children, scrollElementId, scrollElementRef, onScroll } = this.props; const { contentWidth } = this.state; const classNames = (0, _clsx.default)('cc__scroll-view', className, chayns.env.isMobile && 'cc__scroll-view--mobile'); return /*#__PURE__*/_react.default.createElement("div", { ref: ref => { this.node = ref; }, style: style, className: classNames }, /*#__PURE__*/_react.default.createElement("div", { className: "cc__scroll-view__wrapper", ref: ref => { this.wrapper = ref; } }, /*#__PURE__*/_react.default.createElement("div", { style: { width: `calc( 100% + ${contentWidth}px)`, maxHeight: style && style.maxHeight ? style.maxHeight : undefined, height: style && style.height ? style.height : undefined, overflowY: 'scroll' }, className: "cc__scroll-view__content", ref: ref => { this.content = ref; if (scrollElementRef) { scrollElementRef(ref); } }, onScroll: onScroll, id: scrollElementId }, /*#__PURE__*/_react.default.createElement("div", { className: "cc__scroll-view__children", ref: ref => { this.children = ref; } }, children, /*#__PURE__*/_react.default.createElement(_withPolyfill.default, { handleHeight: true, onResize: this.handleRefreshScrollView })))), /*#__PURE__*/_react.default.createElement("div", { className: "cc__scroll-view__scrollbar", ref: ref => { this.bar = ref; } })); } } exports.default = ScrollView; ScrollView.propTypes = { /** * The contents of the `ScrollView`. */ children: _propTypes.default.oneOfType([_propTypes.default.node, _propTypes.default.arrayOf(_propTypes.default.node)]), /** * A React style object that will be applied to the outer-most container. * Specify a `height` or `maxHeight` here. */ style: _propTypes.default.objectOf(_propTypes.default.oneOfType([_propTypes.default.number, _propTypes.default.string])), /** * A classname string that will be applied to the outer-most container. */ className: _propTypes.default.string, /** * A HTML id that will be applied to the scroll container. */ scrollElementId: _propTypes.default.string, /** * A function that receives the reference to the scroll container element as * its first argument. */ scrollElementRef: _propTypes.default.func, /** * The `onScroll`-listener for the scroll container. */ onScroll: _propTypes.default.func }; ScrollView.defaultProps = { children: null, style: undefined, className: undefined, scrollElementId: undefined, scrollElementRef: null, onScroll: null }; ScrollView.displayName = 'ScrollView'; //# sourceMappingURL=ScrollView.js.map