/
Navbar.js
203 lines (178 loc) · 5.82 KB
/
Navbar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import classNames from 'classnames';
import React from 'react';
import PropTypes from 'prop-types';
import { useUncontrolled } from 'uncontrollable';
import createWithBsPrefix from './utils/createWithBsPrefix';
import NavbarBrand from './NavbarBrand';
import NavbarCollapse from './NavbarCollapse';
import NavbarToggle from './NavbarToggle';
import { useBootstrapPrefix } from './ThemeProvider';
import NavbarContext from './NavbarContext';
import SelectableContext from './SelectableContext';
const propTypes = {
/** @default 'navbar' */
bsPrefix: PropTypes.string,
/**
* The general visual variant a the Navbar.
* Use in combination with the `bg` prop, `background-color` utilities,
* or your own background styles.
*
* @type {('light'|'dark')}
*/
variant: PropTypes.string,
/**
* The breakpoint, below which, the Navbar will collapse.
* When `true` the Navbar will always be expanded regardless of screen size.
*/
expand: PropTypes.oneOf([true, 'sm', 'md', 'lg', 'xl']).isRequired,
/**
* A convenience prop for adding `bg-*` utility classes since they are so commonly used here.
* `light` and `dark` are common choices but any `bg-*` class is supported, including any custom ones you might define.
*
* Pairs nicely with the `variant` prop.
*/
bg: PropTypes.string,
/**
* Create a fixed navbar along the top or bottom of the screen, that scrolls with the
* page. A convenience prop for the `fixed-*` positioning classes.
*/
fixed: PropTypes.oneOf(['top', 'bottom']),
/**
* Position the navbar at the top or bottom of the viewport,
* but only after scrolling past it. . A convenience prop for the `sticky-*` positioning classes.
*
* __Not supported in <= IE11 and other older browsers without a polyfill__
*/
sticky: PropTypes.oneOf(['top', 'bottom']),
/**
* Set a custom element for this component.
*/
as: PropTypes.elementType,
/**
* A callback fired when the `<Navbar>` body collapses or expands. Fired when
* a `<Navbar.Toggle>` is clicked and called with the new `expanded`
* boolean value.
*
* @controllable expanded
*/
onToggle: PropTypes.func,
/**
* A callback fired when a descendant of a child `<Nav>` is selected. Should
* be used to execute complex closing or other miscellaneous actions desired
* after selecting a descendant of `<Nav>`. Does nothing if no `<Nav>` or `<Nav>`
* descendants exist. The callback is called with an eventKey, which is a
* prop from the selected `<Nav>` descendant, and an event.
*
* ```js
* function (
* eventKey: mixed,
* event?: SyntheticEvent
* )
* ```
*
* For basic closing behavior after all `<Nav>` descendant onSelect events in
* mobile viewports, try using collapseOnSelect.
*
* Note: If you are manually closing the navbar using this `OnSelect` prop,
* ensure that you are setting `expanded` to false and not *toggling* between
* true and false.
*/
onSelect: PropTypes.func,
/**
* Toggles `expanded` to `false` after the onSelect event of a descendant of a
* child `<Nav>` fires. Does nothing if no `<Nav>` or `<Nav>` descendants exist.
*
* Manually controlling `expanded` via the onSelect callback is recommended instead,
* for more complex operations that need to be executed after
* the `select` event of `<Nav>` descendants.
*/
collapseOnSelect: PropTypes.bool,
/**
* Controls the visiblity of the navbar body
*
* @controllable onToggle
*/
expanded: PropTypes.bool,
/**
* The ARIA role for the navbar, will default to 'navigation' for
* Navbars whose `as` is something other than `<nav>`.
*
* @default 'navigation'
*/
role: PropTypes.string,
};
const defaultProps = {
expand: true,
variant: 'light',
collapseOnSelect: false,
};
const Navbar = React.forwardRef((props, ref) => {
let {
bsPrefix,
expand,
variant,
bg,
fixed,
sticky,
className,
children,
// Need to define the default "as" during prop destructuring to be compatible with styled-components github.com/react-bootstrap/react-bootstrap/issues/3595
as: Component = 'nav',
expanded,
onToggle,
onSelect,
collapseOnSelect,
...controlledProps
} = useUncontrolled(props, {
expanded: 'onToggle',
});
bsPrefix = useBootstrapPrefix(bsPrefix, 'navbar');
const handleCollapse = (...args) => {
if (onSelect) onSelect(...args);
if (collapseOnSelect && expanded) {
onToggle(false);
}
};
const handleToggle = () => {
onToggle(!expanded);
};
// will result in some false positives but that seems better
// than false negatives. strict `undefined` check allows explicit
// "nulling" of the role if the user really doesn't want one
if (controlledProps.role === undefined && Component !== 'nav') {
controlledProps.role = 'navigation';
}
let expandClass = `${bsPrefix}-expand`;
if (typeof expand === 'string') expandClass = `${expandClass}-${expand}`;
return (
<NavbarContext.Provider value={{ handleToggle, bsPrefix, expanded }}>
<SelectableContext.Provider value={handleCollapse}>
<Component
ref={ref}
{...controlledProps}
className={classNames(
className,
bsPrefix,
expand && expandClass,
variant && `${bsPrefix}-${variant}`,
bg && `bg-${bg}`,
sticky && `sticky-${sticky}`,
fixed && `fixed-${fixed}`,
)}
>
{children}
</Component>
</SelectableContext.Provider>
</NavbarContext.Provider>
);
});
Navbar.propTypes = propTypes;
Navbar.defaultProps = defaultProps;
Navbar.displayName = 'Navbar';
Navbar.Brand = NavbarBrand;
Navbar.Toggle = NavbarToggle;
Navbar.Collapse = NavbarCollapse;
Navbar.Text = createWithBsPrefix('navbar-text', {
Component: 'span',
});
export default Navbar;