forked from react-bootstrap/react-bootstrap
/
Navbar.tsx
229 lines (205 loc) · 6.92 KB
/
Navbar.tsx
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import classNames from 'classnames';
import * as React from 'react';
import { useCallback, useMemo } from 'react';
import PropTypes from 'prop-types';
import SelectableContext from '@restart/ui/SelectableContext';
import { SelectCallback } from '@restart/ui/types';
import { useUncontrolled } from 'uncontrollable';
import createWithBsPrefix from './createWithBsPrefix';
import NavbarBrand from './NavbarBrand';
import NavbarCollapse from './NavbarCollapse';
import NavbarToggle from './NavbarToggle';
import NavbarOffcanvas from './NavbarOffcanvas';
import { useBootstrapPrefix } from './ThemeProvider';
import NavbarContext, { NavbarContextType } from './NavbarContext';
import { BsPrefixProps, BsPrefixRefForwardingComponent } from './helpers';
const NavbarText = createWithBsPrefix('navbar-text', {
Component: 'span',
});
export interface NavbarProps
extends BsPrefixProps,
Omit<React.HTMLAttributes<HTMLElement>, 'onSelect'> {
variant?: 'light' | 'dark' | string;
expand?: boolean | string | 'sm' | 'md' | 'lg' | 'xl' | 'xxl';
bg?: string;
fixed?: 'top' | 'bottom';
sticky?: 'top';
onToggle?: (expanded: boolean) => void;
onSelect?: SelectCallback;
collapseOnSelect?: boolean;
expanded?: boolean;
}
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.oneOfType([PropTypes.bool, PropTypes.string]).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 of the viewport, but only after scrolling past it.
* A convenience prop for the `sticky-top` positioning class.
*
* __Not supported in <= IE11 and other older browsers without a polyfill__
*/
sticky: PropTypes.oneOf(['top']),
/**
* 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' as const,
collapseOnSelect: false,
};
const Navbar: BsPrefixRefForwardingComponent<'nav', NavbarProps> =
React.forwardRef<HTMLElement, NavbarProps>((props, ref) => {
const {
bsPrefix: initialBsPrefix,
expand,
variant,
bg,
fixed,
sticky,
className,
// 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',
});
const bsPrefix = useBootstrapPrefix(initialBsPrefix, 'navbar');
const handleCollapse = useCallback<SelectCallback>(
(...args) => {
onSelect?.(...args);
if (collapseOnSelect && expanded) {
onToggle?.(false);
}
},
[onSelect, collapseOnSelect, expanded, onToggle],
);
// 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}`;
const navbarContext = useMemo<NavbarContextType>(
() => ({
onToggle: () => onToggle?.(!expanded),
bsPrefix,
expanded: !!expanded,
expand,
}),
[bsPrefix, expanded, expand, onToggle],
);
return (
<NavbarContext.Provider value={navbarContext}>
<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}`,
)}
/>
</SelectableContext.Provider>
</NavbarContext.Provider>
);
});
Navbar.propTypes = propTypes;
Navbar.defaultProps = defaultProps;
Navbar.displayName = 'Navbar';
export default Object.assign(Navbar, {
Brand: NavbarBrand,
Collapse: NavbarCollapse,
Offcanvas: NavbarOffcanvas,
Text: NavbarText,
Toggle: NavbarToggle,
});