@enact/ui
Version:
A collection of simplified unstyled cross-platform UI components for Enact
251 lines (248 loc) • 11.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.ScrollerBasic = void 0;
var _propTypes = _interopRequireDefault(require("@enact/core/internal/prop-types"));
var _platform = require("@enact/core/platform");
var _classnames = _interopRequireDefault(require("classnames"));
var _propTypes2 = _interopRequireDefault(require("prop-types"));
var _react = require("react");
var _ScrollerModule = _interopRequireDefault(require("./Scroller.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["className", "style"];
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
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(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
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); }
function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); } /**
* An unstyled base scroller component.
*
* @class ScrollerBasic
* @memberof ui/Scroller
* @ui
* @public
*/
var ScrollerBasic = exports.ScrollerBasic = /*#__PURE__*/function (_Component) {
function ScrollerBasic() {
var _this;
_classCallCheck(this, ScrollerBasic);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this = _callSuper(this, ScrollerBasic, [].concat(args));
_this.scrollBounds = {
clientWidth: 0,
clientHeight: 0,
scrollWidth: 0,
scrollHeight: 0,
maxLeft: 0,
maxTop: 0
};
_this.scrollPos = {
top: 0,
left: 0
};
_this.getScrollBounds = function () {
return _this.scrollBounds;
};
_this.getRtlPositionX = function (x) {
if (_this.props.rtl) {
return _platform.platform.chrome < 85 ? _this.scrollBounds.maxLeft - x : -x; // Chrome lower than 85 has a bug with RTL scrollLeft
}
return x;
};
_this.getNodePosition = function (node) {
var _node$getBoundingClie = node.getBoundingClientRect(),
nodeLeft = _node$getBoundingClie.left,
nodeTop = _node$getBoundingClie.top,
nodeHeight = _node$getBoundingClie.height,
nodeWidth = _node$getBoundingClie.width,
_this$props$scrollCon = _this.props.scrollContentRef.current.getBoundingClientRect(),
containerLeft = _this$props$scrollCon.left,
containerTop = _this$props$scrollCon.top,
_this$props$scrollCon2 = _this.props.scrollContentRef.current,
scrollLeft = _this$props$scrollCon2.scrollLeft,
scrollTop = _this$props$scrollCon2.scrollTop,
left = _this.isHorizontal() ? scrollLeft + nodeLeft - containerLeft : null,
top = _this.isVertical() ? scrollTop + nodeTop - containerTop : null;
return {
left: left,
top: top,
width: nodeWidth,
height: nodeHeight
};
};
_this.isVertical = function () {
return _this.props.direction !== 'horizontal';
};
_this.isHorizontal = function () {
return _this.props.direction !== 'vertical';
};
_this.syncClientSize = function () {
var node = _this.props.scrollContentRef.current;
if (!node) {
return false;
}
var clientWidth = node.clientWidth,
clientHeight = node.clientHeight,
_this2 = _this,
scrollBounds = _this2.scrollBounds;
if (clientWidth !== scrollBounds.clientWidth || clientHeight !== scrollBounds.clientHeight) {
_this.calculateMetrics();
return true;
}
return false;
};
return _this;
}
_inherits(ScrollerBasic, _Component);
return _createClass(ScrollerBasic, [{
key: "componentDidMount",
value: function componentDidMount() {
this.calculateMetrics();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps) {
this.calculateMetrics();
if (this.props.isVerticalScrollbarVisible && !prevProps.isVerticalScrollbarVisible) {
this.forceUpdate();
}
}
}, {
key: "setScrollPosition",
value:
// scrollMode 'translate'
function setScrollPosition(x, y) {
var node = this.props.scrollContentRef.current;
if (this.isVertical()) {
node.scrollTop = y;
this.scrollPos.top = y;
}
if (this.isHorizontal()) {
node.scrollLeft = this.getRtlPositionX(x);
this.scrollPos.left = x;
}
}
// scrollMode 'native'
}, {
key: "scrollToPosition",
value: function scrollToPosition(left, top, behavior) {
this.props.scrollContentRef.current.scrollTo({
left: this.getRtlPositionX(left),
top: top,
behavior: behavior
});
}
// scrollMode 'native'
}, {
key: "didScroll",
value: function didScroll(x, y) {
this.scrollPos.left = x;
this.scrollPos.top = y;
}
}, {
key: "calculateMetrics",
value: function calculateMetrics() {
var scrollBounds = this.scrollBounds,
_this$props$scrollCon3 = this.props.scrollContentRef.current,
scrollWidth = _this$props$scrollCon3.scrollWidth,
scrollHeight = _this$props$scrollCon3.scrollHeight,
clientWidth = _this$props$scrollCon3.clientWidth,
clientHeight = _this$props$scrollCon3.clientHeight;
scrollBounds.scrollWidth = scrollWidth;
scrollBounds.scrollHeight = scrollHeight;
scrollBounds.clientWidth = clientWidth;
scrollBounds.clientHeight = clientHeight;
scrollBounds.maxLeft = Math.max(0, scrollWidth - clientWidth);
scrollBounds.maxTop = Math.max(0, scrollHeight - clientHeight);
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
className = _this$props.className,
style = _this$props.style,
rest = _objectWithoutProperties(_this$props, _excluded),
mergedStyle = Object.assign({}, style, {
overflowX: this.isHorizontal() ? 'auto' : 'hidden',
overflowY: this.isVertical() ? 'auto' : 'hidden'
});
delete rest.cbScrollTo;
delete rest.direction;
delete rest.isHorizontalScrollbarVisible;
delete rest.isVerticalScrollbarVisible;
delete rest.rtl;
delete rest.scrollContainerContainsDangerously;
delete rest.scrollContentRef;
delete rest.scrollMode;
delete rest.setThemeScrollContentHandle;
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread(_objectSpread({}, rest), {}, {
className: (0, _classnames["default"])(className, _ScrollerModule["default"].scroller),
ref: this.props.scrollContentRef,
style: mergedStyle
}));
}
}]);
}(_react.Component);
ScrollerBasic.displayName = 'ui:ScrollerBasic';
ScrollerBasic.propTypes = /** @lends ui/Scroller.ScrollerBasic.prototype */{
children: _propTypes2["default"].node.isRequired,
/**
* Callback method of scrollTo.
* Normally, `useScroll` should set this value.
*
* @type {Function}
* @private
*/
cbScrollTo: _propTypes2["default"].func,
/**
* Direction of the scroller.
*
* Valid values are:
* * `'both'`,
* * `'horizontal'`, and
* * `'vertical'`.
*
* @type {String}
* @default 'both'
* @public
*/
direction: _propTypes2["default"].oneOf(['both', 'horizontal', 'vertical']),
/**
* Prop to check context value if Scrollbar exists or not.
*
* @type {Boolean}
* @private
*/
isVerticalScrollbarVisible: _propTypes2["default"].bool,
/**
* `true` if RTL, `false` if LTR.
*
* @type {Boolean}
* @private
*/
rtl: _propTypes2["default"].bool,
/**
* Ref for scroll content
*
* @type {Object|Function}
* @private
*/
scrollContentRef: _propTypes["default"].ref
};
var _default = exports["default"] = ScrollerBasic;