react-kube
Version:
Kube CSS in React Components
141 lines (121 loc) • 5.49 kB
JavaScript
"use strict";
var _createClass = (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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
var _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; desc = parent = getter = undefined; _again = false; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; continue _function; } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) subClass.__proto__ = superClass; }
var _react = require("react");
var _react2 = _interopRequireDefault(_react);
var _classnames = require("classnames");
var _classnames2 = _interopRequireDefault(_classnames);
var Navigation = (function (_React$Component) {
function Navigation(props) {
_classCallCheck(this, Navigation);
_get(Object.getPrototypeOf(Navigation.prototype), "constructor", this).call(this, props);
this.state = { showFixed: false };
}
_inherits(Navigation, _React$Component);
_createClass(Navigation, [{
key: "componentWillMount",
value: function componentWillMount() {
if (!this.props.id) {
console.error("Please assign an ID to the Navigation component");
}
window.addEventListener("resize", this.onResize.bind(this));
this.onResize();
}
}, {
key: "componentDidMount",
value: function componentDidMount() {
window.addEventListener("scroll", this.handleScroll.bind(this));
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
window.removeEventListener("scroll", this.handleScroll.bind(this));
window.removeEventListener("resize", this.onResize.bind(this));
}
}, {
key: "handleScroll",
value: function handleScroll() {
this.navHeight = _react2["default"].findDOMNode(this.refs[this.props.id]).offsetHeight; //recalculate nav height
this.navHeight += 20;
if (window.pageYOffset > this.navHeight) {
this.setState({
showFixed: true,
collapsed: false
});
} else {
this.setState({
showFixed: false
});
}
}
}, {
key: "handleToggleClick",
value: function handleToggleClick() {
this.setState({
collapsed: !this.state.collapsed
});
}
}, {
key: "onResize",
value: function onResize() {
if (window.innerWidth < 767) {
this.setState({
mobile: true
});
} else {
this.setState({
mobile: false
});
}
}
}, {
key: "render",
value: function render() {
var navClasses = (0, _classnames2["default"])({
"nav-fullwidth": this.props.fullwidth || this.state.showFixed,
"navigation-fixed": this.props.fixed && this.state.showFixed
});
var navWidth = this.props.fullwidth || this.state.showFixed ? "100%" : "auto";
var navStyle = {
width: navWidth
};
var toggleStyle = {
display: this.state.mobile ? "block" : "none"
};
return _react2["default"].createElement(
"div",
{ className: (0, _classnames2["default"])(this.props.className, navClasses), id: this.props.id, ref: this.props.id, style: navStyle },
this.props.responsive ? _react2["default"].createElement(
"div",
{ className: "navigation-toggle", onClick: this.handleToggleClick.bind(this), style: toggleStyle },
_react2["default"].createElement(
"span",
null,
this.props.menuLabel
)
) : null,
!this.state.mobile || this.state.collapsed ? this.props.children : null
);
}
}]);
return Navigation;
})(_react2["default"].Component);
Navigation.propTypes = {
active: _react2["default"].PropTypes.string,
children: _react2["default"].PropTypes.node,
className: _react2["default"].PropTypes.string,
fixed: _react2["default"].PropTypes.bool,
fullwidth: _react2["default"].PropTypes.bool,
id: _react2["default"].PropTypes.string.isRequired,
menuLabel: _react2["default"].PropTypes.string,
pills: _react2["default"].PropTypes.bool,
responsive: _react2["default"].PropTypes.bool,
style: _react2["default"].PropTypes.object,
toggle: _react2["default"].PropTypes.bool,
wrap: _react2["default"].PropTypes.bool
};
Navigation.defaultProps = { wrap: true };
module.exports = Navigation;