@enact/moonstone
Version:
Large-screen/TV support library for Enact, containing a variety of UI components.
371 lines (368 loc) • 15.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = exports.ScrollerBase = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _platform = require("@enact/core/platform");
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = require("react");
var _UiScrollerModule = _interopRequireDefault(require("./UiScroller.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); } /**
* Unstyled scroller components and behaviors to be customized by a theme or application.
*
* @module moonstone/UiScroller
* @exports ScrollerBase
*/ /**
* An unstyled base scroller component.
*
* In most circumstances, you will want to use the Scrollable version.
*
* @class ScrollerBase
* @memberof moonstone/UiScroller
* @ui
* @public
*/
var ScrollerBase = exports.ScrollerBase = /*#__PURE__*/function (_Component) {
_inherits(ScrollerBase, _Component);
var _super = _createSuper(ScrollerBase);
function ScrollerBase(props) {
var _this;
_classCallCheck(this, ScrollerBase);
_this = _super.call(this, props);
_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.ios || _platform.platform.safari || _platform.platform.chrome >= 85 || _platform.platform.androidChrome >= 85 ? -x : _this.scrollBounds.maxLeft - x;
}
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$containerRef$cu = _this.containerRef.current.getBoundingClientRect(),
containerLeft = _this$containerRef$cu.left,
containerTop = _this$containerRef$cu.top,
_this$containerRef$cu2 = _this.containerRef.current,
scrollLeft = _this$containerRef$cu2.scrollLeft,
scrollTop = _this$containerRef$cu2.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.containerRef = /*#__PURE__*/(0, _react.createRef)();
return _this;
}
_createClass(ScrollerBase, [{
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:
// for Scrollable
function setScrollPosition(x, y) {
var node = this.containerRef.current;
if (this.isVertical()) {
node.scrollTop = y;
this.scrollPos.top = y;
}
if (this.isHorizontal()) {
node.scrollLeft = this.getRtlPositionX(x);
this.scrollPos.left = x;
}
}
// for ScrollableNative
}, {
key: "scrollToPosition",
value: function scrollToPosition(x, y) {
this.containerRef.current.scrollTo(this.getRtlPositionX(x), y);
}
// for ScrollableNative
}, {
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$containerRef$cu3 = this.containerRef.current,
scrollWidth = _this$containerRef$cu3.scrollWidth,
scrollHeight = _this$containerRef$cu3.scrollHeight,
clientWidth = _this$containerRef$cu3.clientWidth,
clientHeight = _this$containerRef$cu3.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;
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread(_objectSpread({}, rest), {}, {
className: (0, _classnames["default"])(className, _UiScrollerModule["default"].scroller),
ref: this.containerRef,
style: mergedStyle
}));
}
}]);
return ScrollerBase;
}(_react.Component);
/**
* A callback function that receives a reference to the `scrollTo` feature.
*
* Once received, the `scrollTo` method can be called as an imperative interface.
*
* The `scrollTo` function accepts the following parameters:
* - {position: {x, y}} - Pixel value for x and/or y position
* - {align} - Where the scroll area should be aligned. Values are:
* `'left'`, `'right'`, `'top'`, `'bottom'`,
* `'topleft'`, `'topright'`, `'bottomleft'`, and `'bottomright'`.
* - {node} - Node to scroll into view
* - {animate} - When `true`, scroll occurs with animation. When `false`, no
* animation occurs.
* - {focus} - When `true`, attempts to focus item after scroll. Only valid when scrolling
* by `index` or `node`.
* > Note: Only specify one of: `position`, `align`, `index` or `node`
*
* Example:
* ```
* // If you set cbScrollTo prop like below;
* cbScrollTo: (fn) => {this.scrollTo = fn;}
* // You can simply call like below;
* this.scrollTo({align: 'top'}); // scroll to the top
* ```
*
* @name cbScrollTo
* @memberof moonstone/UiScroller.ScrollerBase.prototype
* @type {Function}
* @public
*/
/**
* Specifies how to show horizontal scrollbar.
*
* Valid values are:
* * `'auto'`,
* * `'visible'`, and
* * `'hidden'`.
*
* @name horizontalScrollbar
* @memberof moonstone/UiScroller.ScrollerBase.prototype
* @type {String}
* @default 'auto'
* @public
*/
/**
* Prevents scroll by wheeling on the scroller.
*
* @name noScrollByWheel
* @memberof moonstone/UiScroller.ScrollerBase.prototype
* @type {Boolean}
* @default false
* @public
*/
/**
* Called when scrolling.
*
* Passes `scrollLeft` and `scrollTop`.
* It is not recommended to set this prop since it can cause performance degradation.
* Use `onScrollStart` or `onScrollStop` instead.
*
* @name onScroll
* @memberof moonstone/UiScroller.ScrollerBase.prototype
* @type {Function}
* @param {Object} event
* @param {Number} event.scrollLeft Scroll left value.
* @param {Number} event.scrollTop Scroll top value.
* @param {Object} event.moreInfo The object including `firstVisibleIndex` and `lastVisibleIndex` properties.
* @public
*/
/**
* Called when scroll starts.
*
* Passes `scrollLeft` and `scrollTop`.
*
* Example:
* ```
* onScrollStart = ({scrollLeft, scrollTop}) => {
* // do something with scrollLeft and scrollTop
* }
*
* render = () => (
* <Scroller
* ...
* onScrollStart={this.onScrollStart}
* ...
* />
* )
* ```
*
* @name onScrollStart
* @memberof moonstone/UiScroller.ScrollerBase.prototype
* @type {Function}
* @param {Object} event
* @param {Number} event.scrollLeft Scroll left value.
* @param {Number} event.scrollTop Scroll top value.
* @param {Object} event.moreInfo The object including `firstVisibleIndex` and `lastVisibleIndex` properties.
* @public
*/
/**
* Called when scroll stops.
*
* Passes `scrollLeft` and `scrollTop`.
*
* Example:
* ```
* onScrollStop = ({scrollLeft, scrollTop}) => {
* // do something with scrollLeft and scrollTop
* }
*
* render = () => (
* <Scroller
* ...
* onScrollStop={this.onScrollStop}
* ...
* />
* )
* ```
*
* @name onScrollStop
* @memberof moonstone/UiScroller.ScrollerBase.prototype
* @type {Function}
* @param {Object} event
* @param {Number} event.scrollLeft Scroll left value.
* @param {Number} event.scrollTop Scroll top value.
* @param {Object} event.moreInfo The object including `firstVisibleIndex` and `lastVisibleIndex` properties.
* @public
*/
/**
* Specifies how to show vertical scrollbar.
*
* Valid values are:
* * `'auto'`,
* * `'visible'`, and
* * `'hidden'`.
*
* @name verticalScrollbar
* @memberof moonstone/UiScroller.ScrollerBase.prototype
* @type {String}
* @default 'auto'
* @public
*/
ScrollerBase.displayName = 'ui:ScrollerBase';
ScrollerBase.propTypes = /** @lends moonstone/UiScroller.ScrollerBase.prototype */{
children: _propTypes["default"].node.isRequired,
/**
* Callback method of scrollTo.
* Normally, `Scrollable` should set this value.
*
* @type {Function}
* @private
*/
cbScrollTo: _propTypes["default"].func,
/**
* Direction of the scroller.
*
* Valid values are:
* * `'both'`,
* * `'horizontal'`, and
* * `'vertical'`.
*
* @type {String}
* @default 'both'
* @public
*/
direction: _propTypes["default"].oneOf(['both', 'horizontal', 'vertical']),
/**
* Prop to check context value if Scrollbar exists or not.
*
* @type {Boolean}
* @private
*/
isVerticalScrollbarVisible: _propTypes["default"].bool,
/**
* `true` if RTL, `false` if LTR.
*
* @type {Boolean}
* @private
*/
rtl: _propTypes["default"].bool
};
ScrollerBase.defaultProps = {
direction: 'both'
};
var _default = exports["default"] = ScrollerBase;