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

Commit

Permalink
feat: Injectable MdcRipple + EventRegistry provider (#181)
Browse files Browse the repository at this point in the history
Remove `MdcRippleModule` and refactor `MdcRipple` into an `@Injectable` class. Developers no longer need to include a ripple import with each component that required it.

BREAKING CHANGE: `MdcRippleModule` was removed, please update your code accordingly.

Closes #164
  • Loading branch information
trimox authored Sep 15, 2017
1 parent 74a626c commit 99923e7
Show file tree
Hide file tree
Showing 25 changed files with 216 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/lib/button/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NgModule } from '@angular/core';
import { NgModule } from '@angular/core';

import { MdcButtonComponent } from './button.component';

Expand Down
4 changes: 2 additions & 2 deletions src/lib/checkbox/checkbox.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ export const MD_CHECKBOX_CONTROL_VALUE_ACCESSOR: Provider = {
encapsulation: ViewEncapsulation.None,
providers: [
MD_CHECKBOX_CONTROL_VALUE_ACCESSOR,
MdcRipple
MdcRipple,
EventRegistry,
]
})

export class MdcCheckboxComponent implements AfterViewInit, OnDestroy {
private _uniqueId: string = `mdc-checkbox-${++nextUniqueId}`;

Expand Down
3 changes: 2 additions & 1 deletion src/lib/dialog/dialog.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ export class MdcDialogButtonDirective extends MdcButtonComponent {
</mdc-dialog-surface>
<mdc-dialog-backdrop></mdc-dialog-backdrop>
`,
encapsulation: ViewEncapsulation.None
encapsulation: ViewEncapsulation.None,
providers: [EventRegistry],
})
export class MdcDialogComponent implements AfterViewInit, OnDestroy {
private focusTrap_: {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/drawer/persistent/drawer-persistent.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export class MdcPersistentDrawerSelectedDirective {
<ng-content></ng-content>
</mdc-persistent-drawer-nav>
`,
encapsulation: ViewEncapsulation.None
encapsulation: ViewEncapsulation.None,
providers: [EventRegistry],
})
export class MdcPersistentDrawerComponent implements AfterViewInit, OnDestroy {
@Output() opened: EventEmitter<void> = new EventEmitter<void>();
Expand Down
3 changes: 2 additions & 1 deletion src/lib/drawer/temporary/drawer-temporary.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ export class MdcTemporaryDrawerSelectedDirective {
<ng-content></ng-content>
</mdc-temporary-drawer-nav>
`,
encapsulation: ViewEncapsulation.None
encapsulation: ViewEncapsulation.None,
providers: [EventRegistry],
})
export class MdcTemporaryDrawerComponent implements AfterViewInit, OnDestroy {
@Output() opened: EventEmitter<void> = new EventEmitter<void>();
Expand Down
3 changes: 2 additions & 1 deletion src/lib/form-field/form-field.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { MDCFormFieldAdapter } from './form-field-adapter';
import { MDCFormFieldFoundation } from '@material/form-field';

@Directive({
selector: 'mdc-form-field'
selector: 'mdc-form-field',
providers: [EventRegistry],
})
export class MdcFormFieldDirective implements AfterContentInit, OnDestroy {
@Input() alignEnd: boolean = false;
Expand Down
3 changes: 2 additions & 1 deletion src/lib/icon-toggle/icon-toggle.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export const MD_ICON_TOGGLE_CONTROL_VALUE_ACCESSOR: Provider = {
encapsulation: ViewEncapsulation.None,
providers: [
MD_ICON_TOGGLE_CONTROL_VALUE_ACCESSOR,
MdcRipple
MdcRipple,
EventRegistry,
],
})
export class MdcIconToggleComponent implements AfterViewInit, OnChanges, OnDestroy {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/menu/menu.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ export class MdcMenuDividerComponent { }
<ng-content select="mdc-menu-item, mdc-menu-divider"></ng-content>
</ul>
`,
encapsulation: ViewEncapsulation.None
encapsulation: ViewEncapsulation.None,
providers: [EventRegistry],
})
export class MdcMenuComponent implements AfterViewInit, OnChanges, OnDestroy {
private previousFocus_: any;
Expand Down
2 changes: 0 additions & 2 deletions src/lib/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { MdcListModule } from './list/index';
import { MdcMaterialIconModule } from './material-icon/index';
import { MdcMenuModule } from './menu/index';
import { MdcRadioModule } from './radio/index';
import { MdcRippleModule } from './ripple/index';
import { MdcSnackbarModule } from './snackbar/index';
import { MdcSwitchModule } from './switch/index';
import { MdcTabModule } from './tabs/index';
Expand All @@ -38,7 +37,6 @@ const MATERIAL_MODULES = [
MdcMaterialIconModule,
MdcMenuModule,
MdcRadioModule,
MdcRippleModule,
MdcSnackbarModule,
MdcSwitchModule,
MdcTabModule,
Expand Down
3 changes: 1 addition & 2 deletions src/lib/radio/radio.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,9 @@ export const MD_RADIO_CONTROL_VALUE_ACCESSOR: Provider = {
encapsulation: ViewEncapsulation.None,
providers: [
MD_RADIO_CONTROL_VALUE_ACCESSOR,
MdcRipple
MdcRipple,
]
})

export class MdcRadioComponent implements AfterViewInit, OnDestroy {
@Input() id: string = `mdc-radio-${++nextElId_}`;
get inputId(): string {
Expand Down
12 changes: 0 additions & 12 deletions src/lib/ripple/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
import { NgModule } from '@angular/core';

import { MdcRipple } from './ripple.directive';
import { EventRegistry } from '../common/event-registry';

@NgModule({
exports: [MdcRipple],
declarations: [MdcRipple],
providers: [EventRegistry]
})
export class MdcRippleModule { }

export * from './ripple.directive';
32 changes: 16 additions & 16 deletions src/lib/ripple/ripple.directive.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import {
Directive,
ElementRef,
Injectable,
OnDestroy,
Renderer2,
} from '@angular/core';
import { EventRegistry } from '../common/event-registry';
import { toBoolean } from '../common';
import { toBoolean, isBrowser } from '../common';

import { MDCRippleAdapter } from './ripple-adapter';
import { supportsCssVariables } from '@material/ripple/util';
import { MDCRippleFoundation } from '@material/ripple';

@Directive({
selector: '[mdc-ripple]',
providers: [EventRegistry]
})
@Injectable()
export class MdcRipple implements OnDestroy {
interactionListenerFn: () => void;
resizeListenerFn: () => void;
private disabled_: boolean;

unbounded: boolean;
Expand Down Expand Up @@ -44,20 +42,22 @@ export class MdcRipple implements OnDestroy {
this._renderer.removeClass(this._root.nativeElement, className);
},
registerInteractionHandler: (evtType: string, handler: EventListener) => {
if (this._root) {
this._registry.listen_(this._renderer, evtType, handler, this._root);
}
this.resizeListenerFn = this._renderer.listen(this._root.nativeElement, evtType, handler);
},
deregisterInteractionHandler: (evtType: string, handler: EventListener) => {
this._registry.unlisten_(evtType, handler);
if (this.interactionListenerFn) {
this.interactionListenerFn();
}
},
registerResizeHandler: (handler: EventListener) => {
if (this._root) {
this._registry.listen_(this._renderer, 'resize', handler, this._root);
if (isBrowser()) {
this.resizeListenerFn = this._renderer.listen(window, 'resize', handler);
}
},
deregisterResizeHandler: (handler: EventListener) => {
this._registry.unlisten_('resize', handler);
if (isBrowser() && this.resizeListenerFn) {
this.resizeListenerFn();
}
},
updateCssVariable: (varName: string, value: string) => {
if (this._root) {
Expand Down Expand Up @@ -93,8 +93,8 @@ export class MdcRipple implements OnDestroy {

constructor(
private _renderer: Renderer2,
private _root: ElementRef,
private _registry: EventRegistry) { }
private _root: ElementRef) {
}

ngOnDestroy() {
this._foundation.destroy();
Expand Down
3 changes: 2 additions & 1 deletion src/lib/snackbar/snackbar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import { getCorrectEventName } from '@material/animation';
<button mdc-snackbar-action-button></button>
</mdc-snackbar-action-wrapper>
`,
encapsulation: ViewEncapsulation.None
encapsulation: ViewEncapsulation.None,
providers: [EventRegistry]
})
export class MdcSnackbarComponent implements AfterViewInit, OnDestroy {
@Input() alignStart: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/switch/switch.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ let nextElId_ = 0;
encapsulation: ViewEncapsulation.None,
providers: [
MD_SWITCH_CONTROL_VALUE_ACCESSOR,
MdcRipple
MdcRipple,
]
})
export class MdcSwitchComponent implements OnChanges {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/tabs/tab-bar-scroller/tab-bar-scroller.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export class MdcTabBarScrollFrameDirective implements AfterContentInit {
}

@Directive({
selector: '[mdc-tab-bar-scroller], mdc-tab-bar-scroller'
selector: '[mdc-tab-bar-scroller], mdc-tab-bar-scroller',
providers: [EventRegistry],
})
export class MdcTabBarScrollerDirective implements AfterViewInit, OnDestroy {
@Input() direction: 'ltr' | 'rtl' = 'ltr';
Expand Down
3 changes: 2 additions & 1 deletion src/lib/tabs/tab-bar/tab-bar.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import { MDCTabBarAdapter } from './tab-bar-adapter';
import { MDCTabBarFoundation } from '@material/tabs';

@Directive({
selector: '[mdc-tab-bar], mdc-tab-bar'
selector: '[mdc-tab-bar], mdc-tab-bar',
providers: [EventRegistry],
})
export class MdcTabBarDirective {
private tabBarIndicator: HTMLElement;
Expand Down
5 changes: 4 additions & 1 deletion src/lib/tabs/tab/tab.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export class MdcTabIconTextDirective {
@Component({
selector: '[mdc-tab], mdc-tab',
template: '<ng-content></ng-content>',
providers: [MdcRipple],
providers: [
MdcRipple,
EventRegistry
],
encapsulation: ViewEncapsulation.None
})
export class MdcTabComponent implements AfterViewInit, OnDestroy {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/textfield/textarea.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ export const MD_TEXTAREA_CONTROL_VALUE_ACCESSOR: Provider = {
(focus)="onFocus()"></textarea>
<mdc-textfield-label [attr.for]="id" *ngIf="label">{{label}}</mdc-textfield-label>
`,
providers: [MD_TEXTAREA_CONTROL_VALUE_ACCESSOR]
providers: [
MD_TEXTAREA_CONTROL_VALUE_ACCESSOR,
EventRegistry
],
})
export class MdcTextareaComponent extends MdcTextfieldComponent {
@Input() multiline: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/lib/textfield/textfield-box.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class MdcTextfieldBottomLineDirective {
providers: [
MD_TEXTFIELD_BOX_CONTROL_VALUE_ACCESSOR,
MdcRipple,
EventRegistry,
]
})
export class MdcTextfieldBoxComponent extends MdcTextfieldComponent {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/textfield/textfield.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ export class MdcTextfieldLabelDirective {
<mdc-textfield-label [attr.for]="id">{{label}}</mdc-textfield-label>
`,
encapsulation: ViewEncapsulation.None,
providers: [MD_TEXTFIELD_CONTROL_VALUE_ACCESSOR],
providers: [
MD_TEXTFIELD_CONTROL_VALUE_ACCESSOR,
EventRegistry,
],
})
export class MdcTextfieldComponent implements AfterViewInit, OnDestroy, ControlValueAccessor {
private type_ = 'text';
Expand Down
3 changes: 2 additions & 1 deletion src/lib/toolbar/toolbar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import { MDCToolbarFoundation } from '@material/toolbar';
@Component({
selector: 'mdc-toolbar',
template: '<ng-content></ng-content>',
encapsulation: ViewEncapsulation.None
encapsulation: ViewEncapsulation.None,
providers: [EventRegistry],
})
export class MdcToolbarComponent implements AfterViewInit, OnDestroy {
@Input() flexible: boolean;
Expand Down
5 changes: 2 additions & 3 deletions test/unit/button/button.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { Component, DebugElement } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';

import { MdcButtonComponent, MdcButtonModule, MdcRippleModule } from '../../../src/lib/public_api';
import { MdcButtonComponent, MdcButtonModule } from '../../../src/lib/public_api';

describe('MdcButtonComponent', () => {
let fixture: ComponentFixture<any>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdcButtonModule, MdcRippleModule],
imports: [MdcButtonModule],
declarations: [
SimpleButton,
HrefButton,
Expand Down Expand Up @@ -151,7 +151,6 @@ describe('MdcButtonComponent', () => {
[unelevated]="isUnelevated"
[stroked]="isStroked">
</button>
<a href="http://www.google.com" mdc-button [disabled]="isDisabled">Link</a>
`,
})
class SimpleButton {
Expand Down
4 changes: 2 additions & 2 deletions test/unit/checkbox/checkbox.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { async, ComponentFixture, fakeAsync, flushMicrotasks, TestBed } from '@a
import { FormControl, FormsModule, NgModel, ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';

import { MdcCheckboxComponent, MdcCheckboxModule, MdcRippleModule } from '../../../src/lib/public_api';
import { MdcCheckboxComponent, MdcCheckboxModule } from '../../../src/lib/public_api';

describe('MdcCheckboxComponent', () => {
let fixture: ComponentFixture<any>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdcCheckboxModule, MdcRippleModule, FormsModule, ReactiveFormsModule],
imports: [MdcCheckboxModule, FormsModule, ReactiveFormsModule],
declarations: [
SingleCheckbox,
CheckboxWithAriaLabelledby,
Expand Down
Loading

0 comments on commit 99923e7

Please sign in to comment.