Skip to content
This repository was archived by the owner on Jan 13, 2025. It is now read-only.

Commit b8b1988

Browse files
authored
feat(top-app-bar): Convert JS to TypeScript (#4397)
Refs #4225
1 parent f8ba48f commit b8b1988

File tree

11 files changed

+271
-326
lines changed

11 files changed

+271
-326
lines changed

packages/mdc-top-app-bar/adapter.js renamed to packages/mdc-top-app-bar/adapter.ts

Lines changed: 22 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,87 +21,67 @@
2121
* THE SOFTWARE.
2222
*/
2323

24-
/* eslint no-unused-vars: [2, {"args": "none"}] */
24+
import {EventType, SpecificEventListener} from '@material/base/types';
2525

2626
/**
27-
* Adapter for MDC Top App Bar
28-
*
29-
* Defines the shape of the adapter expected by the foundation. Implement this
30-
* adapter to integrate the Top App Bar into your framework. See
31-
* https://github.com/material-components/material-components-web/blob/master/docs/authoring-components.md
32-
* for more information.
33-
*
34-
* @record
27+
* Defines the shape of the adapter expected by the foundation.
28+
* Implement this adapter for your framework of choice to delegate updates to
29+
* the component in your framework of choice. See architecture documentation
30+
* for more details.
31+
* https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
3532
*/
36-
class MDCTopAppBarAdapter {
33+
interface MDCTopAppBarAdapter {
3734
/**
3835
* Adds a class to the root Element.
39-
* @param {string} className
4036
*/
41-
addClass(className) {}
37+
addClass(className: string): void;
4238

4339
/**
4440
* Removes a class from the root Element.
45-
* @param {string} className
4641
*/
47-
removeClass(className) {}
42+
removeClass(className: string): void;
4843

4944
/**
5045
* Returns true if the root Element contains the given class.
51-
* @param {string} className
52-
* @return {boolean}
5346
*/
54-
hasClass(className) {}
47+
hasClass(className: string): boolean;
5548

5649
/**
5750
* Sets the specified inline style property on the root Element to the given value.
58-
* @param {string} property
59-
* @param {string} value
6051
*/
61-
setStyle(property, value) {}
52+
setStyle(property: string, value: string): void;
6253

6354
/**
6455
* Gets the height of the top app bar.
65-
* @return {number}
6656
*/
67-
getTopAppBarHeight() {}
57+
getTopAppBarHeight(): number;
6858

6959
/**
7060
* Registers an event handler on the navigation icon element for a given event.
71-
* @param {string} type
72-
* @param {function(!Event): undefined} handler
7361
*/
74-
registerNavigationIconInteractionHandler(type, handler) {}
62+
registerNavigationIconInteractionHandler<K extends EventType>(type: K, handler: SpecificEventListener<K>): void;
7563

7664
/**
7765
* Deregisters an event handler on the navigation icon element for a given event.
78-
* @param {string} type
79-
* @param {function(!Event): undefined} handler
8066
*/
81-
deregisterNavigationIconInteractionHandler(type, handler) {}
67+
deregisterNavigationIconInteractionHandler<K extends EventType>(type: K, handler: SpecificEventListener<K>): void;
8268

8369
/**
8470
* Emits an event when the navigation icon is clicked.
8571
*/
86-
notifyNavigationIconClicked() {}
72+
notifyNavigationIconClicked(): void;
8773

88-
/** @param {function(!Event)} handler */
89-
registerScrollHandler(handler) {}
74+
registerScrollHandler(handler: SpecificEventListener<'scroll'>): void;
9075

91-
/** @param {function(!Event)} handler */
92-
deregisterScrollHandler(handler) {}
76+
deregisterScrollHandler(handler: SpecificEventListener<'scroll'>): void;
9377

94-
/** @param {function(!Event)} handler */
95-
registerResizeHandler(handler) {}
78+
registerResizeHandler(handler: SpecificEventListener<'resize'>): void;
9679

97-
/** @param {function(!Event)} handler */
98-
deregisterResizeHandler(handler) {}
80+
deregisterResizeHandler(handler: SpecificEventListener<'resize'>): void;
9981

100-
/** @return {number} */
101-
getViewportScrollY() {}
82+
getViewportScrollY(): number;
10283

103-
/** @return {number} */
104-
getTotalActionItems() {}
84+
getTotalActionItems(): number;
10585
}
10686

107-
export default MDCTopAppBarAdapter;
87+
export {MDCTopAppBarAdapter as default, MDCTopAppBarAdapter};

packages/mdc-top-app-bar/constants.js renamed to packages/mdc-top-app-bar/constants.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,19 @@
2121
* THE SOFTWARE.
2222
*/
2323

24-
/** @enum {string} */
2524
const cssClasses = {
2625
FIXED_CLASS: 'mdc-top-app-bar--fixed',
2726
FIXED_SCROLLED_CLASS: 'mdc-top-app-bar--fixed-scrolled',
2827
SHORT_CLASS: 'mdc-top-app-bar--short',
29-
SHORT_HAS_ACTION_ITEM_CLASS: 'mdc-top-app-bar--short-has-action-item',
3028
SHORT_COLLAPSED_CLASS: 'mdc-top-app-bar--short-collapsed',
29+
SHORT_HAS_ACTION_ITEM_CLASS: 'mdc-top-app-bar--short-has-action-item',
3130
};
3231

33-
/** @enum {number} */
3432
const numbers = {
3533
DEBOUNCE_THROTTLE_RESIZE_TIME_MS: 100,
3634
MAX_TOP_APP_BAR_HEIGHT: 128,
3735
};
3836

39-
/** @enum {string} */
4037
const strings = {
4138
ACTION_ITEM_SELECTOR: '.mdc-top-app-bar__action-item',
4239
NAVIGATION_EVENT: 'MDCTopAppBar:nav',
@@ -45,4 +42,4 @@ const strings = {
4542
TITLE_SELECTOR: '.mdc-top-app-bar__title',
4643
};
4744

48-
export {strings, cssClasses, numbers};
45+
export {cssClasses, numbers, strings};

packages/mdc-top-app-bar/fixed/foundation.js renamed to packages/mdc-top-app-bar/fixed/foundation.ts

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,25 @@
2121
* THE SOFTWARE.
2222
*/
2323

24+
import {MDCTopAppBarAdapter} from '../adapter';
2425
import {cssClasses} from '../constants';
25-
import MDCTopAppBarAdapter from '../adapter';
26-
import MDCTopAppBarFoundation from '../foundation';
26+
import {MDCTopAppBarFoundation} from '../standard/foundation';
2727

28-
/**
29-
* @extends {MDCTopAppBarFoundation<!MDCFixedTopAppBarFoundation>}
30-
* @final
31-
*/
3228
class MDCFixedTopAppBarFoundation extends MDCTopAppBarFoundation {
3329
/**
34-
* @param {!MDCTopAppBarAdapter} adapter
30+
* State variable for the previous scroll iteration top app bar state
3531
*/
36-
constructor(adapter) {
32+
private wasScrolled_ = false;
33+
34+
/* istanbul ignore next: optional argument is not a branch statement */
35+
constructor(adapter?: Partial<MDCTopAppBarAdapter>) {
3736
super(adapter);
38-
/** State variable for the previous scroll iteration top app bar state */
39-
this.wasScrolled_ = false;
4037

4138
this.scrollHandler_ = () => this.fixedScrollHandler_();
4239
}
4340

44-
init() {
45-
super.init();
46-
this.adapter_.registerScrollHandler(this.scrollHandler_);
47-
}
48-
49-
destroy() {
50-
super.destroy();
51-
this.adapter_.deregisterScrollHandler(this.scrollHandler_);
52-
}
53-
5441
/**
55-
* Scroll handler for applying/removing the modifier class
56-
* on the fixed top app bar.
42+
* Scroll handler for applying/removing the modifier class on the fixed top app bar.
5743
*/
5844
fixedScrollHandler_() {
5945
const currentScroll = this.adapter_.getViewportScrollY();
@@ -72,4 +58,4 @@ class MDCFixedTopAppBarFoundation extends MDCTopAppBarFoundation {
7258
}
7359
}
7460

75-
export default MDCFixedTopAppBarFoundation;
61+
export {MDCFixedTopAppBarFoundation as default, MDCFixedTopAppBarFoundation};

packages/mdc-top-app-bar/foundation.js

Lines changed: 0 additions & 99 deletions
This file was deleted.

0 commit comments

Comments
 (0)