semantic-ui-react-scrollbar
Version:
Horizontal and vertical scroll bar for Semantic UI React
352 lines (279 loc) • 15.8 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _semanticUiReact = require("semantic-ui-react");
var _styles = _interopRequireDefault(require("./styles"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(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, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
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 } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var Scrollbar =
/*#__PURE__*/
function (_React$Component) {
_inherits(Scrollbar, _React$Component);
function Scrollbar(props) {
var _this;
_classCallCheck(this, Scrollbar);
_this = _possibleConstructorReturn(this, _getPrototypeOf(Scrollbar).call(this, props));
_defineProperty(_assertThisInitialized(_this), "state", {
currentScrollPosition: 0
});
_defineProperty(_assertThisInitialized(_this), "scrollbarRef", void 0);
_defineProperty(_assertThisInitialized(_this), "trackRef", void 0);
_defineProperty(_assertThisInitialized(_this), "thumbRef", void 0);
_defineProperty(_assertThisInitialized(_this), "trackWidth", void 0);
_defineProperty(_assertThisInitialized(_this), "trackLeft", void 0);
_defineProperty(_assertThisInitialized(_this), "thumbWidth", void 0);
_defineProperty(_assertThisInitialized(_this), "trackHeight", void 0);
_defineProperty(_assertThisInitialized(_this), "trackTop", void 0);
_defineProperty(_assertThisInitialized(_this), "thumbHeight", void 0);
_defineProperty(_assertThisInitialized(_this), "onKeyDown", function (keyboardEvent) {
var _this$props = _this.props,
allowArrowKeys = _this$props.allowArrowKeys,
allowCtrlOrMetaArrowKeys = _this$props.allowCtrlOrMetaArrowKeys,
allowHomeAndEndKeys = _this$props.allowHomeAndEndKeys,
changeScrollPosition = _this$props.changeScrollPosition,
maxScrollPosition = _this$props.maxScrollPosition,
orientation = _this$props.orientation,
pageSize = _this$props.pageSize;
var isKeyHandled = false;
if (window.getComputedStyle(_this.scrollbarRef.current).getPropertyValue('visibility') === 'visible') {
if (!keyboardEvent.ctrlKey && !keyboardEvent.metaKey && allowArrowKeys && (orientation === 'horizontal' && keyboardEvent.code === 'ArrowLeft' || orientation === 'vertical' && keyboardEvent.code === 'ArrowUp')) {
_this.scrollLeftOrUp();
isKeyHandled = true;
} else if (!keyboardEvent.ctrlKey && !keyboardEvent.metaKey && allowArrowKeys && (orientation === 'horizontal' && keyboardEvent.code === 'ArrowRight' || orientation === 'vertical' && keyboardEvent.code === 'ArrowDown')) {
_this.scrollRightOrDown();
isKeyHandled = true;
} else if (allowHomeAndEndKeys && keyboardEvent.code === 'Home') {
_this.setState({
currentScrollPosition: 0
});
changeScrollPosition(0);
isKeyHandled = true;
} else if (allowHomeAndEndKeys && keyboardEvent.code === 'End') {
_this.setState({
currentScrollPosition: maxScrollPosition
});
changeScrollPosition(maxScrollPosition);
isKeyHandled = true;
} else if (allowCtrlOrMetaArrowKeys && (keyboardEvent.ctrlKey || keyboardEvent.metaKey) && (orientation === 'horizontal' && keyboardEvent.code === 'ArrowLeft' || orientation === 'vertical' && keyboardEvent.code === 'ArrowUp')) {
_this.setState(function (_ref) {
var currentScrollPosition = _ref.currentScrollPosition;
if (currentScrollPosition - pageSize >= 0) {
changeScrollPosition(currentScrollPosition - pageSize);
return {
currentScrollPosition: currentScrollPosition - pageSize
};
}
changeScrollPosition(0);
return {
currentScrollPosition: 0
};
});
isKeyHandled = true;
} else if (allowCtrlOrMetaArrowKeys && (keyboardEvent.ctrlKey || keyboardEvent.metaKey) && (orientation === 'horizontal' && keyboardEvent.code === 'ArrowRight' || orientation === 'vertical' && keyboardEvent.code === 'ArrowDown')) {
_this.setState(function (_ref2) {
var currentScrollPosition = _ref2.currentScrollPosition;
if (currentScrollPosition + pageSize <= maxScrollPosition) {
changeScrollPosition(currentScrollPosition + pageSize);
return {
currentScrollPosition: currentScrollPosition + pageSize
};
}
changeScrollPosition(maxScrollPosition);
return {
currentScrollPosition: maxScrollPosition
};
});
isKeyHandled = true;
}
}
if (isKeyHandled) {
keyboardEvent.preventDefault();
keyboardEvent.stopPropagation();
}
});
_defineProperty(_assertThisInitialized(_this), "updateThumbPosition", function (event) {
event.stopPropagation();
var _this$props2 = _this.props,
changeScrollPosition = _this$props2.changeScrollPosition,
maxScrollPosition = _this$props2.maxScrollPosition,
orientation = _this$props2.orientation;
var currentScrollPosition;
if (orientation === 'horizontal') {
currentScrollPosition = Math.round((event.pageX - _this.trackLeft - _this.thumbWidth / 2) / (_this.trackWidth - _this.thumbWidth) * maxScrollPosition);
} else {
currentScrollPosition = Math.round((event.pageY - _this.trackTop - _this.thumbHeight / 2) / (_this.trackHeight - _this.thumbHeight) * maxScrollPosition);
}
currentScrollPosition = Math.min(maxScrollPosition, Math.max(0, currentScrollPosition));
_this.setState({
currentScrollPosition: currentScrollPosition
});
changeScrollPosition(currentScrollPosition);
});
_defineProperty(_assertThisInitialized(_this), "endThumbDrag", function (event) {
event.stopPropagation();
window.removeEventListener('mousemove', _this.updateThumbPosition);
window.removeEventListener('mouseup', _this.endThumbDrag);
});
_defineProperty(_assertThisInitialized(_this), "startThumbDrag", function (event) {
event.stopPropagation();
window.addEventListener('mousemove', _this.updateThumbPosition);
window.addEventListener('mouseup', _this.endThumbDrag);
});
_defineProperty(_assertThisInitialized(_this), "scrollLeftOrUp", function (event) {
var changeScrollPosition = _this.props.changeScrollPosition;
if (event) {
event.stopPropagation();
}
_this.setState(function (_ref3) {
var currentScrollPosition = _ref3.currentScrollPosition;
if (currentScrollPosition > 0) {
changeScrollPosition(currentScrollPosition - 1);
return {
currentScrollPosition: currentScrollPosition - 1
};
}
return {
currentScrollPosition: currentScrollPosition
};
});
});
_defineProperty(_assertThisInitialized(_this), "scrollRightOrDown", function (event) {
var _this$props3 = _this.props,
maxScrollPosition = _this$props3.maxScrollPosition,
changeScrollPosition = _this$props3.changeScrollPosition;
if (event) {
event.stopPropagation();
}
_this.setState(function (_ref4) {
var currentScrollPosition = _ref4.currentScrollPosition;
if (currentScrollPosition < maxScrollPosition) {
changeScrollPosition(currentScrollPosition + 1);
return {
currentScrollPosition: currentScrollPosition + 1
};
}
return {
currentScrollPosition: currentScrollPosition
};
});
});
_this.scrollbarRef = React.createRef();
_this.trackRef = React.createRef();
_this.thumbRef = React.createRef();
_this.trackWidth = 0;
_this.trackLeft = 0;
_this.thumbWidth = 0;
_this.trackHeight = 0;
_this.trackTop = 0;
_this.thumbHeight = 0;
return _this;
}
_createClass(Scrollbar, [{
key: "componentDidMount",
value: function componentDidMount() {
this.trackWidth = this.trackRef.current.offsetWidth;
this.trackLeft = this.trackRef.current.getBoundingClientRect().left;
this.thumbWidth = this.thumbRef.current.offsetWidth;
this.trackHeight = this.trackRef.current.offsetHeight;
this.trackTop = this.trackRef.current.getBoundingClientRect().top;
this.thumbHeight = this.thumbRef.current.offsetHeight;
document.addEventListener('keydown', this.onKeyDown);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
document.removeEventListener('keydown', this.onKeyDown);
}
}, {
key: "render",
value: function render() {
var _this$props4 = this.props,
className = _this$props4.className,
maxScrollPosition = _this$props4.maxScrollPosition,
orientation = _this$props4.orientation;
var currentScrollPosition = this.state.currentScrollPosition;
var marginLeft = 0;
var marginTop = 0;
if (orientation === 'horizontal') {
marginLeft = "".concat(this.trackWidth ? currentScrollPosition * (this.trackWidth - this.thumbWidth) / maxScrollPosition : 0, "px");
} else {
marginTop = "".concat(this.trackHeight ? currentScrollPosition * (this.trackHeight - this.thumbHeight) / maxScrollPosition : 0, "px");
}
var thumbStyle = _objectSpread({}, _styles.default.thumb, {
marginLeft: marginLeft,
marginTop: marginTop
});
return React.createElement("div", {
className: className,
ref: this.scrollbarRef,
style: _styles.default.scrollbar[orientation]
}, React.createElement("div", {
className: "button",
style: _styles.default.button,
onClick: this.scrollLeftOrUp
}, React.createElement(_semanticUiReact.Icon, {
name: orientation === 'horizontal' ? 'caret left' : 'caret up',
style: {
margin: 0
}
})), React.createElement("div", {
className: "track",
style: _styles.default.track[orientation],
ref: this.trackRef,
onClick: this.updateThumbPosition
}, React.createElement("div", {
className: "thumb",
style: thumbStyle,
ref: this.thumbRef,
onMouseDown: this.startThumbDrag
})), React.createElement("div", {
className: "button",
style: _styles.default.button,
onClick: this.scrollRightOrDown
}, React.createElement(_semanticUiReact.Icon, {
name: orientation === 'horizontal' ? 'caret right' : 'caret down',
style: {
margin: 0
}
})));
}
}]);
return Scrollbar;
}(React.Component);
exports.default = Scrollbar;
_defineProperty(Scrollbar, "propTypes", {
allowArrowKeys: _propTypes.default.bool,
allowCtrlOrMetaArrowKeys: _propTypes.default.bool,
allowHomeAndEndKeys: _propTypes.default.bool,
changeScrollPosition: _propTypes.default.func.isRequired,
className: _propTypes.default.string,
maxScrollPosition: _propTypes.default.number.isRequired,
orientation: _propTypes.default.oneOf(['horizontal', 'vertical']),
pageSize: _propTypes.default.number
});
_defineProperty(Scrollbar, "defaultProps", {
allowArrowKeys: true,
allowCtrlOrMetaArrowKeys: false,
allowHomeAndEndKeys: false,
className: undefined,
orientation: 'horizontal',
pageSize: 1
});
//# sourceMappingURL=Scrollbar.js.map
;