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

Commit 2ed2d82

Browse files
Elliott Marquezcopybara-github
authored andcommitted
refactor(linear-progress): remove ts resize observer type polyfill
fixes mwc: #2047 PiperOrigin-RevId: 352883271
1 parent 5268222 commit 2ed2d82

File tree

8 files changed

+81
-29
lines changed

8 files changed

+81
-29
lines changed

packages/mdc-linear-progress/adapter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
* https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
3030
*/
3131

32+
import {MDCResizeObserver, MDCResizeObserverCallback} from './types';
33+
3234
export interface MDCLinearProgressAdapter {
3335
addClass(className: string): void;
3436
/**
@@ -42,7 +44,8 @@ export interface MDCLinearProgressAdapter {
4244
* root element with the given callback. `null` if `ResizeObserver` is not
4345
* implemented or polyfilled.
4446
*/
45-
attachResizeObserver(callback: ResizeObserverCallback): ResizeObserver|null;
47+
attachResizeObserver(callback: MDCResizeObserverCallback): MDCResizeObserver
48+
|null;
4649
forceLayout(): void;
4750
setBufferBarStyle(styleProperty: string, value: string): void;
4851
setPrimaryBarStyle(styleProperty: string, value: string): void;

packages/mdc-linear-progress/component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {MDCComponent} from '@material/base/component';
2525
import {MDCProgressIndicator} from '@material/progress-indicator/component';
2626
import {MDCLinearProgressAdapter} from './adapter';
2727
import {MDCLinearProgressFoundation} from './foundation';
28+
import {WithMDCResizeObserver} from './types';
2829

2930
export class MDCLinearProgress extends
3031
MDCComponent<MDCLinearProgressFoundation> implements MDCProgressIndicator {
@@ -96,9 +97,10 @@ export class MDCLinearProgress extends
9697
setStyle: (name: string, value: string) => {
9798
(this.root as HTMLElement).style.setProperty(name, value);
9899
},
99-
attachResizeObserver: (callback: ResizeObserverCallback) => {
100-
if (window.ResizeObserver) {
101-
const ro = new ResizeObserver(callback);
100+
attachResizeObserver: (callback) => {
101+
const RO = (window as unknown as WithMDCResizeObserver).ResizeObserver;
102+
if (RO) {
103+
const ro = new RO(callback);
102104
ro.observe(this.root);
103105
return ro;
104106
}

packages/mdc-linear-progress/foundation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {MDCProgressIndicatorFoundation} from '@material/progress-indicator/found
2727

2828
import {MDCLinearProgressAdapter} from './adapter';
2929
import {animationDimensionPercentages as percents, cssClasses, strings} from './constants';
30+
import {MDCResizeObserver} from './types';
3031

3132
export class MDCLinearProgressFoundation extends
3233
MDCFoundation<MDCLinearProgressAdapter> implements
@@ -58,7 +59,7 @@ export class MDCLinearProgressFoundation extends
5859
private isDeterminate!: boolean;
5960
private progress!: number;
6061
private buffer!: number;
61-
private observer: ResizeObserver|null = null;
62+
private observer: MDCResizeObserver|null = null;
6263

6364
constructor(adapter?: Partial<MDCLinearProgressAdapter>) {
6465
super({...MDCLinearProgressFoundation.defaultAdapter, ...adapter});

packages/mdc-linear-progress/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ export * from './adapter';
2525
export * from './component';
2626
export * from './constants';
2727
export * from './foundation';
28+
export * from './types';

packages/mdc-linear-progress/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
"tslib": "^1.9.3"
2828
},
2929
"devDependencies": {
30-
"@types/resize-observer-browser": "^0.1.3"
3130
},
3231
"publishConfig": {
3332
"access": "public"

packages/mdc-linear-progress/test/component.test.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@
2424

2525
import {animationDimensionPercentages as percentages} from '../../mdc-linear-progress/constants';
2626
import {MDCLinearProgress, MDCLinearProgressFoundation} from '../../mdc-linear-progress/index';
27+
import {MDCResizeObserver, MDCResizeObserverCallback, MDCResizeObserverEntry, WithMDCResizeObserver} from '../../mdc-linear-progress/types';
2728
import {emitEvent} from '../../../testing/dom/events';
2829
import {createMockFoundation} from '../../../testing/helpers/foundation';
2930
import {setUpMdcTestEnvironment} from '../../../testing/helpers/setup';
3031

3132
interface WithObserverFoundation {
32-
foundation: {observer: null|ResizeObserver};
33+
foundation: {observer: null|MDCResizeObserver};
3334
}
3435

36+
const RO = (window as unknown as WithMDCResizeObserver).ResizeObserver;
37+
3538
const roundPixelsToTwoDecimals = (val: string) => {
3639
const numberVal = Number(val.split('px')[0]);
3740
return Math.floor(numberVal * 100) / 100;
@@ -59,7 +62,7 @@ function getFixture() {
5962
return el;
6063
}
6164

62-
const originalResizeObserver = window.ResizeObserver;
65+
const originalResizeObserver = RO;
6366

6467
function setupTest(hasMockFoundation = false) {
6568
const root = getFixture();
@@ -156,11 +159,13 @@ describe('MDCLinearProgress', () => {
156159
if (root) {
157160
document.body.removeChild(root);
158161
}
159-
window.ResizeObserver = originalResizeObserver;
162+
(window as unknown as WithMDCResizeObserver).ResizeObserver =
163+
originalResizeObserver;
160164
});
161165

162166
it('will not error if there is no resize observer', () => {
163-
(window.ResizeObserver as unknown as null) = null;
167+
((window as unknown as WithMDCResizeObserver).ResizeObserver as unknown as
168+
null) = null;
164169
component = setupTest().component;
165170

166171
const foundation =
@@ -171,7 +176,7 @@ describe('MDCLinearProgress', () => {
171176
});
172177

173178
it('will update size on resize', async () => {
174-
if (!window.ResizeObserver) {
179+
if (!RO) {
175180
// skip tests on IE which wouldn't run these anyway
176181
return;
177182
}
@@ -183,7 +188,7 @@ describe('MDCLinearProgress', () => {
183188
class MockObserver {
184189
observedElement: HTMLElement|null = null;
185190

186-
constructor(public callback: ResizeObserverCallback) {
191+
constructor(public callback: MDCResizeObserverCallback) {
187192
this.callback = callback;
188193
}
189194

@@ -198,12 +203,13 @@ describe('MDCLinearProgress', () => {
198203
triggerResize(width: number) {
199204
const fakeEntry = {contentRect: {width}};
200205
this.callback(
201-
[fakeEntry as unknown as ResizeObserverEntry],
202-
this as unknown as ResizeObserver);
206+
[fakeEntry as unknown as MDCResizeObserverEntry],
207+
this as unknown as MDCResizeObserver);
203208
}
204209
}
205210

206-
window.ResizeObserver = MockObserver as unknown as typeof ResizeObserver;
211+
(window as unknown as WithMDCResizeObserver).ResizeObserver =
212+
MockObserver as unknown as typeof RO;
207213
({root, component} = setupTest());
208214
document.body.appendChild(root);
209215
component.determinate = false;

packages/mdc-linear-progress/test/foundation.test.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323

2424
import {animationDimensionPercentages as percentages} from '../../mdc-linear-progress/constants';
2525
import {MDCLinearProgressFoundation} from '../../mdc-linear-progress/foundation';
26+
import {WithMDCResizeObserver} from '../../mdc-linear-progress/types';
2627
import {checkNumTimesSpyCalledWithArgs, verifyDefaultAdapter} from '../../../testing/helpers/foundation';
2728
import {setUpFoundationTest} from '../../../testing/helpers/setup';
2829

2930
const {cssClasses, strings} = MDCLinearProgressFoundation;
3031

32+
const RO = (window as unknown as WithMDCResizeObserver).ResizeObserver;
33+
3134
const multiplyPercentages = (multipler: number) => {
3235
return {
3336
PRIMARY_HALF: percentages.PRIMARY_HALF * multipler,
@@ -94,7 +97,7 @@ describe('MDCLinearProgressFoundation', () => {
9497
it('#setDeterminate false updates custom props', () => {
9598
const {foundation, mockAdapter} = setupTest();
9699
mockAdapter.getWidth.and.returnValue(100);
97-
mockAdapter.attachResizeObserver.and.returnValue(window.ResizeObserver);
100+
mockAdapter.attachResizeObserver.and.returnValue(RO);
98101
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
99102
.and.returnValue(false);
100103
foundation.init();
@@ -103,7 +106,7 @@ describe('MDCLinearProgressFoundation', () => {
103106
foundation.setDeterminate(false);
104107
expect(foundation.getDeterminate()).toBe(false);
105108

106-
if (!window.ResizeObserver) {
109+
if (!RO) {
107110
expect(mockAdapter.setStyle).toHaveBeenCalledTimes(0);
108111
return;
109112
}
@@ -212,40 +215,36 @@ describe('MDCLinearProgressFoundation', () => {
212215
const {foundation, mockAdapter} = setupTest();
213216
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
214217
.and.returnValue(true);
215-
mockAdapter.attachResizeObserver.and.returnValue(window.ResizeObserver);
218+
mockAdapter.attachResizeObserver.and.returnValue(RO);
216219
foundation.init();
217-
expect(mockAdapter.setStyle)
218-
.toHaveBeenCalledTimes(window.ResizeObserver ? 10 : 0);
220+
expect(mockAdapter.setStyle).toHaveBeenCalledTimes(RO ? 10 : 0);
219221
});
220222

221223
it('#calculateAndSetDimensions called only on setDeterminate(false)', () => {
222224
const {foundation, mockAdapter} = setupTest();
223225
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
224226
.and.returnValue(true);
225-
mockAdapter.attachResizeObserver.and.returnValue(window.ResizeObserver);
227+
mockAdapter.attachResizeObserver.and.returnValue(RO);
226228
foundation.init();
227-
expect(mockAdapter.setStyle)
228-
.toHaveBeenCalledTimes(window.ResizeObserver ? 10 : 0);
229+
expect(mockAdapter.setStyle).toHaveBeenCalledTimes(RO ? 10 : 0);
229230

230231
foundation.setDeterminate(true);
231232

232-
expect(mockAdapter.setStyle)
233-
.toHaveBeenCalledTimes(window.ResizeObserver ? 10 : 0);
233+
expect(mockAdapter.setStyle).toHaveBeenCalledTimes(RO ? 10 : 0);
234234

235235
foundation.setDeterminate(false);
236236

237-
expect(mockAdapter.setStyle)
238-
.toHaveBeenCalledTimes(window.ResizeObserver ? 20 : 0);
237+
expect(mockAdapter.setStyle).toHaveBeenCalledTimes(RO ? 20 : 0);
239238
});
240239

241240
it('#calculateAndSetDimensions restarts animation with a forced reflow',
242241
() => {
243242
const {foundation, mockAdapter} = setupTest();
244243
mockAdapter.hasClass.withArgs(cssClasses.INDETERMINATE_CLASS)
245244
.and.returnValue(true);
246-
mockAdapter.attachResizeObserver.and.returnValue(window.ResizeObserver);
245+
mockAdapter.attachResizeObserver.and.returnValue(RO);
247246
foundation.init();
248-
if (window.ResizeObserver) {
247+
if (RO) {
249248
expect(mockAdapter.addClass)
250249
.toHaveBeenCalledWith(cssClasses.ANIMATION_READY_CLASS);
251250
expect(mockAdapter.forceLayout).toHaveBeenCalledTimes(1);

packages/mdc-linear-progress/types.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* @license
3+
* Copyright 2021 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+
// Opt-in resize observer types
25+
26+
export interface MDCResizeObserverEntry {
27+
contentRect: DOMRectReadOnly;
28+
}
29+
30+
export interface MDCResizeObserver {
31+
new(callback: MDCResizeObserverCallback): MDCResizeObserver;
32+
disconnect(): void;
33+
observe(target: Element): void;
34+
}
35+
36+
export interface WithMDCResizeObserver {
37+
ResizeObserver: MDCResizeObserver;
38+
}
39+
40+
export type MDCResizeObserverCallback =
41+
(entries: MDCResizeObserverEntry[], observer: MDCResizeObserver) => void;

0 commit comments

Comments
 (0)