stremio-web/src/common/NavBar/NavBarButton/NavBarButton.js
2019-05-16 18:40:23 +03:00

39 lines
1.4 KiB
JavaScript

const React = require('react');
const PropTypes = require('prop-types');
const classnames = require('classnames');
const UrlUtils = require('url');
const Icon = require('stremio-icons/dom');
const { Input } = require('stremio-navigation');
const useLocationHash = require('../../useLocationHash');
const styles = require('./styles');
const NavBarButton = React.memo(({ className, icon, label, href, onClick }) => {
const locationHash = useLocationHash();
const active = React.useMemo(() => {
if (typeof href !== 'string') {
return false;
}
const { pathname: locationPathname } = UrlUtils.parse(locationHash.slice(1));
const { pathname: hrefPathname } = UrlUtils.parse(href.slice(1));
return locationPathname === hrefPathname;
}, [href, locationHash]);
return (
<Input className={classnames(className, styles['nav-bar-button-container'], { 'active': active })} tabIndex={-1} type={typeof onClick === 'function' ? 'button' : 'link'} href={href} onClick={onClick}>
<Icon className={styles['icon']} icon={icon} />
<div className={styles['label']}>{label}</div>
</Input>
);
});
NavBarButton.displayName = 'NavBarButton';
NavBarButton.propTypes = {
className: PropTypes.string,
icon: PropTypes.string,
label: PropTypes.string,
href: PropTypes.string,
onClick: PropTypes.func
};
module.exports = NavBarButton;