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

Commit 87a5e8e

Browse files
authored
feat(animation): Convert JS to TypeScript (#4271)
Refs #4225
1 parent 66c8d81 commit 87a5e8e

File tree

13 files changed

+270
-196
lines changed

13 files changed

+270
-196
lines changed

packages/mdc-animation/README.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,54 @@ Function | Description
9595
These functions handle prefixing across various browsers
9696

9797
```js
98-
import {getCorrectEventName} from '@material/animation';
98+
import {getCorrectEventName, StandardJsEventType} from '@material/animation';
9999

100-
const eventToListenFor = getCorrectEventName(window, 'animationstart');
100+
const eventToListenFor = getCorrectEventName(window, StandardJsEventType.ANIMATION_START);
101101
```
102102

103103
Method Signature | Description
104104
--- | ---
105-
`getCorrectEventName(windowObj, eventType)` | Returns a JavaScript event name, prefixed if necessary
106-
`getCorrectPropertyName(windowObj, eventType)` | Returns a CSS property name, prefixed if necessary
105+
`getCorrectEventName(windowObj: Window, eventType: StandardJsEventType) => StandardJsEventType \| PrefixedJsEventType` | Returns a JavaScript event name, prefixed if necessary
106+
`getCorrectPropertyName(windowObj: Window, cssProperty: StandardCssPropertyName) => StandardCssPropertyName \| PrefixedCssPropertyName` | Returns a CSS property name, prefixed if necessary
107+
108+
#### `StandardCssPropertyName`
109+
110+
Enum passed to and returned by `getCorrectPropertyName()`.
111+
112+
Key | Value
113+
--- | ---
114+
`ANIMATION` | `animation`
115+
`TRANSFORM` | `transform`
116+
`TRANSITION` | `transition`
117+
118+
#### `PrefixedCssPropertyName`
119+
120+
Enum returned by `getCorrectPropertyName()`.
121+
122+
Key | Value
123+
--- | ---
124+
`WEBKIT_ANIMATION` | `-webkit-animation`
125+
`WEBKIT_TRANSFORM` | `-webkit-transform`
126+
`WEBKIT_TRANSITION` | `-webkit-transition`
127+
128+
#### `StandardJsEventType`
129+
130+
Enum passed to and returned by `getCorrectEventName()`.
131+
132+
Key | Value
133+
--- | ---
134+
`ANIMATION_END` | `animationend`
135+
`ANIMATION_ITERATION` | `animationiteration`
136+
`ANIMATION_START` | `animationstart`
137+
`TRANSITION_END` | `transitionend`
138+
139+
#### `PrefixedJsEventType`
140+
141+
Enum returned by `getCorrectEventName()`.
142+
143+
Key | Value
144+
--- | ---
145+
`WEBKIT_ANIMATION_END` | `webkitAnimationEnd`
146+
`WEBKIT_ANIMATION_ITERATION` | `webkitAnimationIteration`
147+
`WEBKIT_ANIMATION_START` | `webkitAnimationStart`
148+
`WEBKIT_TRANSITION_END` | `webkitTransitionEnd`

packages/mdc-animation/index.js

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

packages/mdc-animation/index.ts

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/**
2+
* @license
3+
* Copyright 2016 Google Inc.
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
enum StandardCssPropertyName {
25+
ANIMATION = 'animation',
26+
TRANSFORM = 'transform',
27+
TRANSITION = 'transition',
28+
}
29+
30+
enum PrefixedCssPropertyName {
31+
WEBKIT_ANIMATION = '-webkit-animation',
32+
WEBKIT_TRANSFORM = '-webkit-transform',
33+
WEBKIT_TRANSITION = '-webkit-transition',
34+
}
35+
36+
enum StandardJsEventType {
37+
ANIMATION_END = 'animationend',
38+
ANIMATION_ITERATION = 'animationiteration',
39+
ANIMATION_START = 'animationstart',
40+
TRANSITION_END = 'transitionend',
41+
}
42+
43+
enum PrefixedJsEventType {
44+
WEBKIT_ANIMATION_END = 'webkitAnimationEnd',
45+
WEBKIT_ANIMATION_ITERATION = 'webkitAnimationIteration',
46+
WEBKIT_ANIMATION_START = 'webkitAnimationStart',
47+
WEBKIT_TRANSITION_END = 'webkitTransitionEnd',
48+
}
49+
50+
interface CssVendorPropertyMap {
51+
[key: string]: CssVendorProperty;
52+
}
53+
54+
interface JsVendorPropertyMap {
55+
[key: string]: JsVendorProperty;
56+
}
57+
58+
interface CssVendorProperty {
59+
prefixed: PrefixedCssPropertyName;
60+
standard: StandardCssPropertyName;
61+
}
62+
63+
interface JsVendorProperty {
64+
cssProperty: StandardCssPropertyName;
65+
prefixed: PrefixedJsEventType;
66+
standard: StandardJsEventType;
67+
}
68+
69+
const transformStyleProperties = ['transform', 'WebkitTransform', 'MozTransform', 'OTransform', 'MSTransform'];
70+
71+
// Destructure enum members to make their usages more readable.
72+
const {WEBKIT_ANIMATION, WEBKIT_TRANSFORM, WEBKIT_TRANSITION} = PrefixedCssPropertyName;
73+
const {ANIMATION, TRANSFORM, TRANSITION} = StandardCssPropertyName;
74+
const {
75+
WEBKIT_ANIMATION_END,
76+
WEBKIT_ANIMATION_ITERATION,
77+
WEBKIT_ANIMATION_START,
78+
WEBKIT_TRANSITION_END,
79+
} = PrefixedJsEventType;
80+
const {ANIMATION_END, ANIMATION_ITERATION, ANIMATION_START, TRANSITION_END} = StandardJsEventType;
81+
82+
const cssPropertyNameMap: CssVendorPropertyMap = {
83+
[ANIMATION]: {
84+
prefixed: WEBKIT_ANIMATION,
85+
standard: ANIMATION,
86+
},
87+
[TRANSFORM]: {
88+
prefixed: WEBKIT_TRANSFORM,
89+
standard: TRANSFORM,
90+
},
91+
[TRANSITION]: {
92+
prefixed: WEBKIT_TRANSITION,
93+
standard: TRANSITION,
94+
},
95+
};
96+
97+
const jsEventTypeMap: JsVendorPropertyMap = {
98+
[ANIMATION_END]: {
99+
cssProperty: ANIMATION,
100+
prefixed: WEBKIT_ANIMATION_END,
101+
standard: ANIMATION_END,
102+
},
103+
[ANIMATION_ITERATION]: {
104+
cssProperty: ANIMATION,
105+
prefixed: WEBKIT_ANIMATION_ITERATION,
106+
standard: ANIMATION_ITERATION,
107+
},
108+
[ANIMATION_START]: {
109+
cssProperty: ANIMATION,
110+
prefixed: WEBKIT_ANIMATION_START,
111+
standard: ANIMATION_START,
112+
},
113+
[TRANSITION_END]: {
114+
cssProperty: TRANSITION,
115+
prefixed: WEBKIT_TRANSITION_END,
116+
standard: TRANSITION_END,
117+
},
118+
};
119+
120+
function isWindow(windowObj: Window): boolean {
121+
return Boolean(windowObj.document) && typeof windowObj.document.createElement === 'function';
122+
}
123+
124+
function getCorrectPropertyName(windowObj: Window, cssProperty: StandardCssPropertyName):
125+
StandardCssPropertyName | PrefixedCssPropertyName {
126+
if (isWindow(windowObj) && cssProperty in cssPropertyNameMap) {
127+
const el = windowObj.document.createElement('div');
128+
const {standard, prefixed} = cssPropertyNameMap[cssProperty];
129+
const isStandard = standard in el.style;
130+
return isStandard ? standard : prefixed;
131+
}
132+
return cssProperty;
133+
}
134+
135+
function getCorrectEventName(windowObj: Window, eventType: StandardJsEventType):
136+
StandardJsEventType | PrefixedJsEventType {
137+
if (isWindow(windowObj) && eventType in jsEventTypeMap) {
138+
const el = windowObj.document.createElement('div');
139+
const {standard, prefixed, cssProperty} = jsEventTypeMap[eventType];
140+
const isStandard = cssProperty in el.style;
141+
return isStandard ? standard : prefixed;
142+
}
143+
return eventType;
144+
}
145+
146+
export {
147+
PrefixedCssPropertyName,
148+
StandardCssPropertyName,
149+
PrefixedJsEventType,
150+
StandardJsEventType,
151+
getCorrectEventName,
152+
getCorrectPropertyName,
153+
transformStyleProperties,
154+
};

packages/mdc-checkbox/index.js

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

24-
import {getCorrectEventName} from '@material/animation/index';
24+
import {StandardJsEventType, getCorrectEventName} from '@material/animation/index.ts';
2525
import MDCComponent from '@material/base/component';
2626
/* eslint-disable no-unused-vars */
2727
import {MDCSelectionControlState, MDCSelectionControl} from '@material/selection-control/index';
@@ -33,6 +33,8 @@ import {getMatchesProperty} from '@material/ripple/util';
3333
/** @const {!Array<string>} */
3434
const CB_PROTO_PROPS = ['checked', 'indeterminate'];
3535

36+
const {ANIMATION_END} = StandardJsEventType;
37+
3638
/**
3739
* @extends MDCComponent<!MDCCheckboxFoundation>
3840
* @implements {MDCSelectionControl}
@@ -69,7 +71,7 @@ class MDCCheckbox extends MDCComponent {
6971
this.handleChange_ = () => this.foundation_.handleChange();
7072
this.handleAnimationEnd_= () => this.foundation_.handleAnimationEnd();
7173
this.nativeCb_.addEventListener('change', this.handleChange_);
72-
this.listen(getCorrectEventName(window, 'animationend'), this.handleAnimationEnd_);
74+
this.listen(getCorrectEventName(window, ANIMATION_END), this.handleAnimationEnd_);
7375
this.installPropertyChangeHooks_();
7476
}
7577

@@ -191,7 +193,7 @@ class MDCCheckbox extends MDCComponent {
191193
destroy() {
192194
this.ripple_.destroy();
193195
this.nativeCb_.removeEventListener('change', this.handleChange_);
194-
this.unlisten(getCorrectEventName(window, 'animationend'), this.handleAnimationEnd_);
196+
this.unlisten(getCorrectEventName(window, ANIMATION_END), this.handleAnimationEnd_);
195197
this.uninstallPropertyChangeHooks_();
196198
super.destroy();
197199
}

0 commit comments

Comments
 (0)