@enact/moonstone
Version:
Large-screen/TV support library for Enact, containing a variety of UI components.
281 lines (278 loc) • 14.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "ScrollThumb", {
enumerable: true,
get: function get() {
return _UiScrollThumb["default"];
}
});
exports["default"] = exports.ScrollbarBase = exports.Scrollbar = void 0;
var _classnames = _interopRequireDefault(require("classnames"));
var _util = require("@enact/core/util");
var _resolution = _interopRequireDefault(require("@enact/ui/resolution"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _react = require("react");
var _reactDom = _interopRequireDefault(require("react-dom"));
var _UiScrollThumb = _interopRequireDefault(require("./UiScrollThumb"));
var _UiScrollbarModule = _interopRequireDefault(require("./UiScrollbar.module.css"));
var _jsxRuntime = require("react/jsx-runtime");
var _excluded = ["childRenderer", "className", "corner", "css", "vertical"];
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); }
var minThumbSize = 18,
// Size in pixels
nop = function nop() {},
thumbHidingDelay = 400; // in milliseconds
/*
* Set CSS Varaible value.
*
* @method
* @param {Node} element - Node.
* @param {String} variable - CSS Variable property.
* @param {String} value - CSS Variable value.
*/
var setCSSVariable = function setCSSVariable(element, variable, value) {
_reactDom["default"].findDOMNode(element).style.setProperty(variable, value); // eslint-disable-line react/no-find-dom-node
};
/**
* An unstyled base component for a scroll bar. It is used in {@link moonstone/UiScrollable.Scrollable|Scrollable}.
*
* @class ScrollbarBase
* @memberof moonstone/UiScrollable
* @ui
* @private
*/
var ScrollbarBase = exports.ScrollbarBase = /*#__PURE__*/function (_PureComponent) {
_inherits(ScrollbarBase, _PureComponent);
var _super = _createSuper(ScrollbarBase);
function ScrollbarBase(props) {
var _this;
_classCallCheck(this, ScrollbarBase);
_this = _super.call(this, props);
_this.minThumbSizeRatio = 0;
_this.ignoreMode = false;
_this.update = function (bounds) {
var vertical = _this.props.vertical,
clientWidth = bounds.clientWidth,
clientHeight = bounds.clientHeight,
scrollWidth = bounds.scrollWidth,
scrollHeight = bounds.scrollHeight,
scrollLeft = bounds.scrollLeft,
scrollTop = bounds.scrollTop,
clientSize = vertical ? clientHeight : clientWidth,
scrollSize = vertical ? scrollHeight : scrollWidth,
scrollOrigin = vertical ? scrollTop : scrollLeft,
thumbSizeRatioBase = clientSize / scrollSize,
scrollThumbPositionRatio = scrollOrigin / (scrollSize - clientSize),
scrollThumbSizeRatio = Math.max(_this.minThumbSizeRatio, Math.min(1, thumbSizeRatioBase));
setCSSVariable(_this.thumbRef.current, '--scrollbar-size-ratio', scrollThumbSizeRatio);
setCSSVariable(_this.thumbRef.current, '--scrollbar-progress-ratio', scrollThumbPositionRatio);
};
_this.showThumb = function () {
_this.hideThumbJob.stop();
_reactDom["default"].findDOMNode(_this.thumbRef.current).classList.add(_this.props.css.thumbShown); // eslint-disable-line react/no-find-dom-node
};
_this.startHidingThumb = function () {
_this.hideThumbJob.start();
};
_this.hideThumb = function () {
_reactDom["default"].findDOMNode(_this.thumbRef.current).classList.remove(_this.props.css.thumbShown); // eslint-disable-line react/no-find-dom-node
};
_this.hideThumbJob = new _util.Job(_this.hideThumb.bind(_assertThisInitialized(_this)), thumbHidingDelay);
_this.calculateMetrics = function () {
var primaryDimenstion = _this.props.vertical ? 'clientHeight' : 'clientWidth';
var trackSize;
if (_this.props.clientSize) {
trackSize = _this.props.clientSize[primaryDimenstion];
} else {
trackSize = _this.containerRef.current[primaryDimenstion];
}
_this.minThumbSizeRatio = _resolution["default"].scale(minThumbSize) / trackSize;
};
_this.getContainerRef = function () {
return _this.containerRef;
};
_this.containerRef = /*#__PURE__*/(0, _react.createRef)();
_this.thumbRef = /*#__PURE__*/(0, _react.createRef)();
return _this;
}
_createClass(ScrollbarBase, [{
key: "componentDidMount",
value: function componentDidMount() {
this.calculateMetrics();
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate() {
this.calculateMetrics();
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.hideThumbJob.stop();
}
}, {
key: "render",
value: function render() {
var _this$props = this.props,
childRenderer = _this$props.childRenderer,
className = _this$props.className,
corner = _this$props.corner,
css = _this$props.css,
vertical = _this$props.vertical,
rest = _objectWithoutProperties(_this$props, _excluded),
containerClassName = (0, _classnames["default"])(className, css.scrollbar, corner ? css.corner : null, vertical ? css.vertical : css.horizontal);
delete rest.clientSize;
return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread(_objectSpread({}, rest), {}, {
className: containerClassName,
ref: this.containerRef,
children: childRenderer({
getContainerRef: this.getContainerRef,
thumbRef: this.thumbRef
})
}));
}
}]);
return ScrollbarBase;
}(_react.PureComponent);
/**
* An unstyled scroll bar. It is used in {@link moonstone/UiScrollable.Scrollable|Scrollable}.
*
* @class Scrollbar
* @memberof moonstone/UiScrollable
* @ui
* @private
*/
ScrollbarBase.displayName = 'ui:Scrollbar';
ScrollbarBase.propTypes = /** @lends moonstone/UiScrollable.Scrollbar.prototype */{
/**
* The render function for child.
*
* @type {Function}
* @required
* @private
*/
childRenderer: _propTypes["default"].func.isRequired,
/**
* Client size of the container; valid values are an object that has `clientWidth` and `clientHeight`.
*
* @type {Object}
* @property {Number} clientHeight The client height of the list.
* @property {Number} clientWidth The client width of the list.
* @public
*/
clientSize: _propTypes["default"].shape({
clientHeight: _propTypes["default"].number.isRequired,
clientWidth: _propTypes["default"].number.isRequired
}),
/**
* If `true`, add the corner between vertical and horizontal scrollbars.
*
* @type {Booelan}
* @default false
* @public
*/
corner: _propTypes["default"].bool,
/**
* Customizes the component by mapping the supplied collection of CSS class names to the
* corresponding internal Elements and states of this component.
*
* The following classes are supported:
*
* * `scrollbar` - The scrollbar component class
*
* @type {Object}
* @public
*/
css: _propTypes["default"].object,
/**
* If `true`, the scrollbar will be oriented vertically.
*
* @type {Boolean}
* @default true
* @public
*/
vertical: _propTypes["default"].bool
};
ScrollbarBase.defaultProps = {
childRenderer: nop,
// eslint-disable-line react/default-props-match-prop-types
corner: false,
css: _UiScrollbarModule["default"],
vertical: true
};
var Scrollbar = exports.Scrollbar = /*#__PURE__*/function (_Component) {
_inherits(Scrollbar, _Component);
var _super2 = _createSuper(Scrollbar);
function Scrollbar() {
var _this2;
_classCallCheck(this, Scrollbar);
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_this2 = _super2.call.apply(_super2, [this].concat(args));
_this2.setApi = function (ref) {
if (ref) {
var getContainerRef = ref.getContainerRef,
showThumb = ref.showThumb,
startHidingThumb = ref.startHidingThumb,
uiUpdate = ref.update;
_this2.getContainerRef = getContainerRef;
_this2.showThumb = showThumb;
_this2.startHidingThumb = startHidingThumb;
_this2.update = uiUpdate;
}
};
return _this2;
}
_createClass(Scrollbar, [{
key: "render",
value: function render() {
var vertical = this.props.vertical;
return /*#__PURE__*/(0, _jsxRuntime.jsx)(ScrollbarBase, _objectSpread(_objectSpread({}, this.props), {}, {
ref: this.setApi,
childRenderer: function childRenderer(_ref) {
var thumbRef = _ref.thumbRef;
// eslint-disable-line react/jsx-no-bind
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_UiScrollThumb["default"], {
ref: thumbRef,
vertical: vertical
}, "thumb");
}
}));
}
}]);
return Scrollbar;
}(_react.Component);
Scrollbar.propTypes = /** @lends moonstone/UiScrollable.Scrollbar.prototype */{
/**
* If `true`, the scrollbar will be oriented vertically.
*
* @type {Boolean}
* @default true
* @public
*/
vertical: _propTypes["default"].bool
};
Scrollbar.defaultProps = {
vertical: true
};
var _default = exports["default"] = Scrollbar;