@enact/ui
Version:
A collection of simplified unstyled cross-platform UI components for Enact
253 lines (250 loc) • 12.7 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(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
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 _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(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 _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } /**
* An unstyled base scroller component.
*
* @class ScrollerBasic
* @memberof ui/Scroller
* @ui
* @public
*/
var ScrollerBasic = exports.ScrollerBasic = /*#__PURE__*/function (_Component) {
_inherits(ScrollerBasic, _Component);
var _super = _createSuper(ScrollerBasic);
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 = _super.call.apply(_super, [this].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,
_assertThisInitialize = _assertThisInitialized(_this),
scrollBounds = _assertThisInitialize.scrollBounds;
if (clientWidth !== scrollBounds.clientWidth || clientHeight !== scrollBounds.clientHeight) {
_this.calculateMetrics();
return true;
}
return false;
};
return _this;
}
_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
}));
}
}]);
return ScrollerBasic;
}(_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;