Skip to content
This repository has been archived by the owner on Oct 7, 2020. It is now read-only.

fix: Replace @material/dom with SSR compatible code #2106

Merged
merged 8 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/checkbox/checkbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {Platform, supportsPassiveEventListeners} from '@angular/cdk/platform';
import {fromEvent, Subject} from 'rxjs';
import {takeUntil, filter} from 'rxjs/operators';

import {matches} from '@material/dom/ponyfill';
import {matches} from '@angular-mdc/web/dom';
import {MDCRippleFoundation, MDCRippleAdapter} from '@material/ripple';
import {MDCCheckboxFoundation, MDCCheckboxAdapter} from '@material/checkbox';

Expand Down
2 changes: 1 addition & 1 deletion packages/data-table/data-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
MDCDataTableRow,
} from './data-table.directives';

import {closest} from '@material/dom/ponyfill';
import {closest} from '@angular-mdc/web/dom';
import {
strings,
MDCDataTableRowSelectionChangedEventDetail,
Expand Down
2 changes: 1 addition & 1 deletion packages/dialog/dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import {MdcDialogRef} from './dialog-ref';
import {MdcDialogConfig} from './dialog-config';

import {closest, matches} from '@material/dom/ponyfill';
import {closest, matches} from '@angular-mdc/web/dom';
import {MDCDialogFoundation, MDCDialogAdapter, strings, util} from '@material/dialog';

const LAYOUT_EVENTS = ['resize', 'orientationchange'];
Expand Down
1 change: 1 addition & 0 deletions packages/dom/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public-api';
42 changes: 42 additions & 0 deletions packages/dom/ponyfill.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

const hasNativeClosest = typeof Element !== 'undefined' && !!Element.prototype.closest;

/** IE 11 compatible closest implementation that is able to start from non-Element Nodes. */
export function closest(element: EventTarget | Element | null | undefined, selector: string):
Element | null {
if (!(element instanceof Node)) {
return null;
}

let curr: any | null = element;
while (curr != null && !(curr instanceof Element)) {
curr = curr.parentNode;
}

return curr && (hasNativeClosest ?
curr.closest(selector) : polyfillClosest(curr, selector)) as Element | null;
}

/** Polyfill for browsers without Element.closest. */
function polyfillClosest(element: Element, selector: string): Element | null {
let curr: Node | null = element;
while (curr != null && !(curr instanceof Element && matches(curr, selector))) {
curr = curr.parentNode;
}

return (curr || null) as Element | null;
}

/** IE 11 compatible matches implementation. */
export function matches(element: Element, selector: string): boolean {
return element.matches ?
element.matches(selector) :
(element as any)['msMatchesSelector'](selector);
}
1 change: 1 addition & 0 deletions packages/dom/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ponyfill';
15 changes: 15 additions & 0 deletions packages/dom/tsconfig-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "../tsconfig-build",
"files": [
"public-api.ts",
"../typings.d.ts"
],
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
"strictMetadataEmit": true,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular-mdc/web/dom",
"skipTemplateCodegen": true,
"fullTemplateTypeCheck": true
}
}
2 changes: 1 addition & 1 deletion packages/list/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {MDCComponent} from '@angular-mdc/web/base';

import {MdcListItem, MdcListSelectionChange, MDC_LIST_PARENT_COMPONENT} from './list-item';

import {matches} from '@material/dom/ponyfill';
import {matches} from '@angular-mdc/web/dom';
import {cssClasses, strings, MDCListFoundation, MDCListAdapter} from '@material/list';

/** Change event that is being fired whenever the selected state of an option changes. */
Expand Down
2 changes: 1 addition & 1 deletion packages/menu/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {takeUntil} from 'rxjs/operators';
import {MdcList, MdcListItem, MdcListItemAction} from '@angular-mdc/web/list';
import {MdcMenuSurfaceBase} from '@angular-mdc/web/menu-surface';

import {closest} from '@material/dom/ponyfill';
import {closest} from '@angular-mdc/web/dom';
import {
cssClasses,
DefaultFocusState,
Expand Down
1 change: 1 addition & 0 deletions packages/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from '@angular-mdc/web/checkbox';
export * from '@angular-mdc/web/chips';
export * from '@angular-mdc/web/data-table';
export * from '@angular-mdc/web/dialog';
export * from '@angular-mdc/web/dom';
export * from '@angular-mdc/web/drawer';
export * from '@angular-mdc/web/elevation';
export * from '@angular-mdc/web/fab';
Expand Down
2 changes: 1 addition & 1 deletion packages/ripple/ripple.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {supportsPassiveEventListeners} from '@angular/cdk/platform';

import {EventType, SpecificEventListener} from '@material/base';
import {matches} from '@material/dom/ponyfill';
import {matches} from '@angular-mdc/web/dom';
import {supportsCssVariables} from '@material/ripple/util';
import {MDCRippleFoundation, MDCRippleAdapter} from '@material/ripple';

Expand Down
2 changes: 1 addition & 1 deletion packages/ripple/ripple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {coerceBooleanProperty} from '@angular/cdk/coercion';
import {supportsPassiveEventListeners} from '@angular/cdk/platform';

import {MdcRipple, MDCRippleCapableSurface} from './ripple.service';
import {matches} from '@material/dom/ponyfill';
import {matches} from '@angular-mdc/web/dom';
import {MDCRippleFoundation, MDCRippleAdapter} from '@material/ripple';

@Directive({
Expand Down
1 change: 0 additions & 1 deletion packages/select/select.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
AfterContentInit,
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Expand Down
2 changes: 1 addition & 1 deletion packages/switch/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {coerceBooleanProperty} from '@angular/cdk/coercion';

import {MDCSwitchFoundation, MDCSwitchAdapter} from '@material/switch';
import {MDCRippleAdapter, MDCRippleFoundation} from '@material/ripple';
import {matches} from '@material/dom/ponyfill';
import {matches} from '@angular-mdc/web/dom';

import {MDCComponent} from '@angular-mdc/web/base';
import {MdcRipple, MDCRippleCapableSurface} from '@angular-mdc/web/ripple';
Expand Down
2 changes: 1 addition & 1 deletion packages/tab-scroller/tab-scroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {filter, takeUntil} from 'rxjs/operators';

import {MDCComponent} from '@angular-mdc/web/base';

import {matches} from '@material/dom/ponyfill';
import {matches} from '@angular-mdc/web/dom';
import {MDCTabScrollerFoundation, MDCTabScrollerAdapter, util} from '@material/tab-scroller';

/** Possible alignments for tab scroller content. */
Expand Down
2 changes: 1 addition & 1 deletion test/karma.webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ module.exports = {
loader: 'istanbul-instrumenter-loader',
options: { esModules: true },
enforce: 'post',
exclude: /(node_modules|base|testing|\.test\.ts$)/
exclude: /(node_modules|base|testing|dom|\.test\.ts$)/
}
]
}
Expand Down
2 changes: 0 additions & 2 deletions tools/package-tools/rollup-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ export const rollupGlobals = {
'@material/base': 'mdc.base',
'@material/checkbox': 'mdc.checkbox',
'@material/chips': 'mdc.chips',
'@material/dom': 'mdc.dom',
'@material/dom/ponyfill': 'mdc.dom.ponyfill',
'@material/dialog': 'mdc.dialog',
'@material/data-table': 'mdc.dataTable',
'@material/drawer': 'mdc.drawer',
Expand Down