UNPKG

2.19 kBJavaScriptView Raw
1import React from 'react';
2import PropTypes from 'prop-types';
3import classNames from 'classnames';
4import { mapToCssModules, tagPropType } from './utils';
5
6const propTypes = {
7 children: PropTypes.node,
8 /** Add custom class */
9 className: PropTypes.string,
10 /** Theme the navbar by adding a background color */
11 color: PropTypes.string,
12 /** Use any of the responsive containers to change how wide the content in your navbar is presented. */
13 container: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
14 /** Change underlying component's CSS base class name */
15 cssModule: PropTypes.object,
16 /** This prop is passed if the background is dark, to make the text lighter */
17 dark: PropTypes.bool,
18 /** Determine if to show toggler button */
19 expand: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]),
20 /** Make the navbar fixed at the top */
21 fixed: PropTypes.string,
22 /** Add `.navbar-light` class */
23 light: PropTypes.bool,
24 role: PropTypes.string,
25 /** Use `position: sticky` which isn't fully supported in every browser */
26 sticky: PropTypes.string,
27 /** Set a custom element for this component */
28 tag: tagPropType,
29};
30
31const getExpandClass = (expand) => {
32 if (expand === false) {
33 return false;
34 }
35 if (expand === true || expand === 'xs') {
36 return 'navbar-expand';
37 }
38
39 return `navbar-expand-${expand}`;
40};
41
42function Navbar(props) {
43 const {
44 expand = false,
45 className,
46 cssModule,
47 light,
48 dark,
49 fixed,
50 sticky,
51 color,
52 container = 'fluid',
53 tag: Tag = 'nav',
54 children,
55 ...attributes
56 } = props;
57
58 const classes = mapToCssModules(
59 classNames(className, 'navbar', getExpandClass(expand), {
60 'navbar-light': light,
61 'navbar-dark': dark,
62 [`bg-${color}`]: color,
63 [`fixed-${fixed}`]: fixed,
64 [`sticky-${sticky}`]: sticky,
65 }),
66 cssModule,
67 );
68
69 const containerClass =
70 container && container === true ? 'container' : `container-${container}`;
71
72 return (
73 <Tag {...attributes} className={classes}>
74 {container ? <div className={containerClass}>{children}</div> : children}
75 </Tag>
76 );
77}
78
79Navbar.propTypes = propTypes;
80
81export default Navbar;