UNPKG

@lahzenegar/video-react

Version:

Video-React is a web video player built from the ground up for an HTML5 world using React library.

284 lines (243 loc) 7.99 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of'); var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf); var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck'); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _createClass2 = require('babel-runtime/helpers/createClass'); var _createClass3 = _interopRequireDefault(_createClass2); var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn'); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _inherits2 = require('babel-runtime/helpers/inherits'); var _inherits3 = _interopRequireDefault(_inherits2); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _classnames = require('classnames'); var _classnames2 = _interopRequireDefault(_classnames); var _react3 = require('@tippyjs/react'); var _react4 = _interopRequireDefault(_react3); var _Menu = require('./Menu'); var _Menu2 = _interopRequireDefault(_Menu); var _MenuItem = require('./MenuItem'); var _MenuItem2 = _interopRequireDefault(_MenuItem); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var propTypes = { inline: _propTypes2.default.bool, items: _propTypes2.default.array, className: _propTypes2.default.string, onSelectItem: _propTypes2.default.func, children: _propTypes2.default.any, selectedIndex: _propTypes2.default.number }; var MenuButton = function (_Component) { (0, _inherits3.default)(MenuButton, _Component); function MenuButton(props, context) { (0, _classCallCheck3.default)(this, MenuButton); var _this = (0, _possibleConstructorReturn3.default)(this, (MenuButton.__proto__ || (0, _getPrototypeOf2.default)(MenuButton)).call(this, props, context)); _this.state = { active: false, activateIndex: props.selectedIndex || 0 }; _this.commitSelection = _this.commitSelection.bind(_this); _this.activateMenuItem = _this.activateMenuItem.bind(_this); _this.handleClick = _this.handleClick.bind(_this); _this.handleFocus = _this.handleFocus.bind(_this); _this.handleBlur = _this.handleBlur.bind(_this); _this.handleUpArrow = _this.handleUpArrow.bind(_this); _this.handleDownArrow = _this.handleDownArrow.bind(_this); _this.handleEscape = _this.handleEscape.bind(_this); _this.handleReturn = _this.handleReturn.bind(_this); _this.handleTab = _this.handleTab.bind(_this); _this.handleKeyPress = _this.handleKeyPress.bind(_this); _this.handleSelectItem = _this.handleSelectItem.bind(_this); _this.handleIndexChange = _this.handleIndexChange.bind(_this); return _this; } // componentDidUpdate(prevProps) { // } (0, _createClass3.default)(MenuButton, [{ key: 'commitSelection', value: function commitSelection(index) { this.setState({ activateIndex: index, active: false }); this.handleIndexChange(index); } }, { key: 'activateMenuItem', value: function activateMenuItem(index) { this.setState({ activateIndex: index }); this.handleIndexChange(index); } }, { key: 'handleIndexChange', value: function handleIndexChange(index) { var onSelectItem = this.props.onSelectItem; onSelectItem(index); } }, { key: 'handleClick', value: function handleClick() { this.setState({ active: !this.state.active }); } }, { key: 'handleFocus', value: function handleFocus() { document.addEventListener('keydown', this.handleKeyPress); } }, { key: 'handleBlur', value: function handleBlur() { this.setState({ active: false }); document.removeEventListener('keydown', this.handleKeyPress); } }, { key: 'handleUpArrow', value: function handleUpArrow(e) { var items = this.props.items; if (this.state.active) { e.preventDefault(); var newIndex = this.state.activateIndex - 1; if (newIndex < 0) { newIndex = items.length ? items.length - 1 : 0; } this.activateMenuItem(newIndex); } } }, { key: 'handleDownArrow', value: function handleDownArrow(e) { var items = this.props.items; if (this.state.active) { e.preventDefault(); var newIndex = this.state.activateIndex + 1; if (newIndex >= items.length) { newIndex = 0; } this.activateMenuItem(newIndex); } } }, { key: 'handleTab', value: function handleTab(e) { if (this.state.active) { e.preventDefault(); this.commitSelection(this.state.activateIndex); } } }, { key: 'handleReturn', value: function handleReturn(e) { e.preventDefault(); if (this.state.active) { this.commitSelection(this.state.activateIndex); } else { this.setState({ active: true }); } } }, { key: 'handleEscape', value: function handleEscape() { this.setState({ active: false, activateIndex: 0 }); } }, { key: 'handleKeyPress', value: function handleKeyPress(event) { // Escape (27) key if (event.which === 27) { this.handleEscape(event); } else if (event.which === 9) { // Tab (9) key this.handleTab(event); } else if (event.which === 13) { // Enter (13) key this.handleReturn(event); } else if (event.which === 38) { // Up (38) key this.handleUpArrow(event); } else if (event.which === 40) { // Down (40) key press this.handleDownArrow(event); } } }, { key: 'handleSelectItem', value: function handleSelectItem(i) { this.commitSelection(i); } }, { key: 'render', value: function render() { var _this2 = this; var _props = this.props, inline = _props.inline, className = _props.className, items = _props.items; var _state = this.state, activateIndex = _state.activateIndex, active = _state.active; return _react2.default.createElement( _react4.default, { interactive: true, trigger: 'click focus', animation: 'shift-away', arrow: false, visible: true, hideOnClick: false, offset: [0, 0], content: _react2.default.createElement( _Menu2.default, null, items.map(function (item, i) { return _react2.default.createElement(_MenuItem2.default, { item: item, index: i, onSelectItem: _this2.handleSelectItem, activateIndex: activateIndex, key: 'item-' + i++ }); }) ) }, _react2.default.createElement( 'div', { className: (0, _classnames2.default)(className, { 'video-react-menu-button-inline': !!inline, 'video-react-menu-button-popup': !inline, 'video-react-menu-button-active': active }, 'video-react-control video-react-button video-react-menu-button'), role: 'button', tabIndex: '0', onClick: this.handleClick, onFocus: this.handleFocus, onBlur: this.handleBlur, ref: function ref(c) { _this2.menuButton = c; } }, this.props.children ) ); } }]); return MenuButton; }(_react.Component); exports.default = MenuButton; MenuButton.propTypes = propTypes; MenuButton.displayName = 'MenuButton';