UNPKG

11.7 kBTypeScriptView Raw
1import { Message } from '@lumino/messaging';
2import { ElementARIAAttrs, ElementDataset, h, VirtualElement } from '@lumino/virtualdom';
3import { Menu } from './menu';
4import { Title } from './title';
5import { Widget } from './widget';
6/**
7 * A widget which displays menus as a canonical menu bar.
8 */
9export declare class MenuBar extends Widget {
10 /**
11 * Construct a new menu bar.
12 *
13 * @param options - The options for initializing the menu bar.
14 */
15 constructor(options?: MenuBar.IOptions);
16 /**
17 * Dispose of the resources held by the widget.
18 */
19 dispose(): void;
20 /**
21 * The renderer used by the menu bar.
22 */
23 readonly renderer: MenuBar.IRenderer;
24 /**
25 * The child menu of the menu bar.
26 *
27 * #### Notes
28 * This will be `null` if the menu bar does not have an open menu.
29 */
30 get childMenu(): Menu | null;
31 /**
32 * The overflow index of the menu bar.
33 */
34 get overflowIndex(): number;
35 /**
36 * The overflow menu of the menu bar.
37 */
38 get overflowMenu(): Menu | null;
39 /**
40 * Get the menu bar content node.
41 *
42 * #### Notes
43 * This is the node which holds the menu title nodes.
44 *
45 * Modifying this node directly can lead to undefined behavior.
46 */
47 get contentNode(): HTMLUListElement;
48 /**
49 * Get the currently active menu.
50 */
51 get activeMenu(): Menu | null;
52 /**
53 * Set the currently active menu.
54 *
55 * #### Notes
56 * If the menu does not exist, the menu will be set to `null`.
57 */
58 set activeMenu(value: Menu | null);
59 /**
60 * Get the index of the currently active menu.
61 *
62 * #### Notes
63 * This will be `-1` if no menu is active.
64 */
65 get activeIndex(): number;
66 /**
67 * Set the index of the currently active menu.
68 *
69 * #### Notes
70 * If the menu cannot be activated, the index will be set to `-1`.
71 */
72 set activeIndex(value: number);
73 /**
74 * A read-only array of the menus in the menu bar.
75 */
76 get menus(): ReadonlyArray<Menu>;
77 /**
78 * Open the active menu and activate its first menu item.
79 *
80 * #### Notes
81 * If there is no active menu, this is a no-op.
82 */
83 openActiveMenu(): void;
84 /**
85 * Add a menu to the end of the menu bar.
86 *
87 * @param menu - The menu to add to the menu bar.
88 *
89 * #### Notes
90 * If the menu is already added to the menu bar, it will be moved.
91 */
92 addMenu(menu: Menu, update?: boolean): void;
93 /**
94 * Insert a menu into the menu bar at the specified index.
95 *
96 * @param index - The index at which to insert the menu.
97 *
98 * @param menu - The menu to insert into the menu bar.
99 *
100 * #### Notes
101 * The index will be clamped to the bounds of the menus.
102 *
103 * If the menu is already added to the menu bar, it will be moved.
104 */
105 insertMenu(index: number, menu: Menu, update?: boolean): void;
106 /**
107 * Remove a menu from the menu bar.
108 *
109 * @param menu - The menu to remove from the menu bar.
110 *
111 * #### Notes
112 * This is a no-op if the menu is not in the menu bar.
113 */
114 removeMenu(menu: Menu, update?: boolean): void;
115 /**
116 * Remove the menu at a given index from the menu bar.
117 *
118 * @param index - The index of the menu to remove.
119 *
120 * #### Notes
121 * This is a no-op if the index is out of range.
122 */
123 removeMenuAt(index: number, update?: boolean): void;
124 /**
125 * Remove all menus from the menu bar.
126 */
127 clearMenus(): void;
128 /**
129 * Handle the DOM events for the menu bar.
130 *
131 * @param event - The DOM event sent to the menu bar.
132 *
133 * #### Notes
134 * This method implements the DOM `EventListener` interface and is
135 * called in response to events on the menu bar's DOM nodes. It
136 * should not be called directly by user code.
137 */
138 handleEvent(event: Event): void;
139 /**
140 * A message handler invoked on a `'before-attach'` message.
141 */
142 protected onBeforeAttach(msg: Message): void;
143 /**
144 * A message handler invoked on an `'after-detach'` message.
145 */
146 protected onAfterDetach(msg: Message): void;
147 /**
148 * A message handler invoked on an `'activate-request'` message.
149 */
150 protected onActivateRequest(msg: Message): void;
151 /**
152 * A message handler invoked on a `'resize'` message.
153 */
154 protected onResize(msg: Widget.ResizeMessage): void;
155 /**
156 * A message handler invoked on an `'update-request'` message.
157 */
158 protected onUpdateRequest(msg: Message): void;
159 /**
160 * Calculate and update the current overflow index.
161 */
162 private _updateOverflowIndex;
163 /**
164 * Handle the `'keydown'` event for the menu bar.
165 *
166 * #### Notes
167 * All keys are trapped except the tab key that is ignored.
168 */
169 private _evtKeyDown;
170 /**
171 * Handle the `'mousedown'` event for the menu bar.
172 */
173 private _evtMouseDown;
174 /**
175 * Handle the `'mousemove'` event for the menu bar.
176 */
177 private _evtMouseMove;
178 /**
179 * Find initial position for the menu based on menubar item position.
180 *
181 * NOTE: this should be called before updating active index to avoid
182 * an additional layout and style invalidation as changing active
183 * index modifies DOM.
184 */
185 private _positionForMenu;
186 /**
187 * Handle the `'focusout'` event for the menu bar.
188 */
189 private _evtFocusOut;
190 /**
191 * Focus an item in the menu bar.
192 *
193 * #### Notes
194 * Does not open the associated menu.
195 */
196 private _focusItemAt;
197 /**
198 * Open the child menu at the active index immediately.
199 *
200 * If a different child menu is already open, it will be closed,
201 * even if there is no active menu.
202 */
203 private _openChildMenu;
204 /**
205 * Close the child menu immediately.
206 *
207 * This is a no-op if a child menu is not open.
208 */
209 private _closeChildMenu;
210 /**
211 * Handle the `aboutToClose` signal of a menu.
212 */
213 private _onMenuAboutToClose;
214 /**
215 * Handle the `menuRequested` signal of a child menu.
216 */
217 private _onMenuMenuRequested;
218 /**
219 * Handle the `changed` signal of a title object.
220 */
221 private _onTitleChanged;
222 private _activeIndex;
223 private _tabFocusIndex;
224 private _forceItemsPosition;
225 private _overflowMenuOptions;
226 private _menus;
227 private _childMenu;
228 private _overflowMenu;
229 private _menuItemSizes;
230 private _overflowIndex;
231}
232/**
233 * The namespace for the `MenuBar` class statics.
234 */
235export declare namespace MenuBar {
236 /**
237 * An options object for creating a menu bar.
238 */
239 interface IOptions {
240 /**
241 * A custom renderer for creating menu bar content.
242 *
243 * The default is a shared renderer instance.
244 */
245 renderer?: IRenderer;
246 /**
247 * Whether to force the position of the menu. The MenuBar forces the
248 * coordinates of its menus by default. With this option you can disable it.
249 *
250 * Setting to `false` will enable the logic which repositions the
251 * coordinates of the menu if it will not fit entirely on screen.
252 *
253 * The default is `true`.
254 */
255 forceItemsPosition?: Menu.IOpenOptions;
256 /**
257 * Whether to add a overflow menu if there's overflow.
258 *
259 * Setting to `true` will enable the logic that creates an overflow menu
260 * to show the menu items that don't fit entirely on the screen.
261 *
262 * The default is `true`.
263 */
264 overflowMenuOptions?: IOverflowMenuOptions;
265 }
266 /**
267 * An object which holds the data to render a menu bar item.
268 */
269 interface IRenderData {
270 /**
271 * The title to be rendered.
272 */
273 readonly title: Title<Widget>;
274 /**
275 * Whether the item is the active item.
276 */
277 readonly active: boolean;
278 /**
279 * Whether the user can tab to the item.
280 */
281 readonly tabbable: boolean;
282 /**
283 * Whether the item is disabled.
284 *
285 * #### Notes
286 * A disabled item cannot be active.
287 * A disabled item cannot be focussed.
288 */
289 readonly disabled?: boolean;
290 readonly onfocus?: (event: FocusEvent) => void;
291 }
292 /**
293 * A renderer for use with a menu bar.
294 */
295 interface IRenderer {
296 /**
297 * Render the virtual element for a menu bar item.
298 *
299 * @param data - The data to use for rendering the item.
300 *
301 * @returns A virtual element representing the item.
302 */
303 renderItem(data: IRenderData): VirtualElement;
304 }
305 /**
306 * The default implementation of `IRenderer`.
307 *
308 * #### Notes
309 * Subclasses are free to reimplement rendering methods as needed.
310 */
311 class Renderer implements IRenderer {
312 /**
313 * Render the virtual element for a menu bar item.
314 *
315 * @param data - The data to use for rendering the item.
316 *
317 * @returns A virtual element representing the item.
318 */
319 renderItem(data: IRenderData): VirtualElement;
320 /**
321 * Render the icon element for a menu bar item.
322 *
323 * @param data - The data to use for rendering the icon.
324 *
325 * @returns A virtual element representing the item icon.
326 */
327 renderIcon(data: IRenderData): VirtualElement;
328 /**
329 * Render the label element for a menu item.
330 *
331 * @param data - The data to use for rendering the label.
332 *
333 * @returns A virtual element representing the item label.
334 */
335 renderLabel(data: IRenderData): VirtualElement;
336 /**
337 * Create the class name for the menu bar item.
338 *
339 * @param data - The data to use for the class name.
340 *
341 * @returns The full class name for the menu item.
342 */
343 createItemClass(data: IRenderData): string;
344 /**
345 * Create the dataset for a menu bar item.
346 *
347 * @param data - The data to use for the item.
348 *
349 * @returns The dataset for the menu bar item.
350 */
351 createItemDataset(data: IRenderData): ElementDataset;
352 /**
353 * Create the aria attributes for menu bar item.
354 *
355 * @param data - The data to use for the aria attributes.
356 *
357 * @returns The aria attributes object for the item.
358 */
359 createItemARIA(data: IRenderData): ElementARIAAttrs;
360 /**
361 * Create the class name for the menu bar item icon.
362 *
363 * @param data - The data to use for the class name.
364 *
365 * @returns The full class name for the item icon.
366 */
367 createIconClass(data: IRenderData): string;
368 /**
369 * Create the render content for the label node.
370 *
371 * @param data - The data to use for the label content.
372 *
373 * @returns The content to add to the label node.
374 */
375 formatLabel(data: IRenderData): h.Child;
376 }
377 /**
378 * The default `Renderer` instance.
379 */
380 const defaultRenderer: Renderer;
381}
382/**
383 * Options for overflow menu.
384 */
385export interface IOverflowMenuOptions {
386 /**
387 * Determines if a overflow menu appears when the menu items overflow.
388 *
389 * Defaults to `true`.
390 */
391 isVisible: boolean;
392 /**
393 * Determines the title of the overflow menu.
394 *
395 * Default: `...`.
396 */
397 title?: string;
398}