Skip to content

Commit

Permalink
fix(ripple): Expose focus/blur handlers (#2905)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiomkar committed Jun 8, 2018
1 parent 14cb0bf commit 31d81ad
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/mdc-ripple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ Method Signature | Description
`activate() => void` | Proxies to the foundation's `activate` method
`deactivate() => void` | Proxies to the foundation's `deactivate` method
`layout() => void` | Proxies to the foundation's `layout` method
`handleFocus() => void` | Handles focus event on the ripple surface
`handleBlur() => void` | Handles blur event on the ripple surface

### `MDCRippleAdapter`

Expand Down
18 changes: 12 additions & 6 deletions packages/mdc-ripple/foundation.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,10 @@ class MDCRippleFoundation extends MDCFoundation {
this.deactivateHandler_ = (e) => this.deactivate_(e);

/** @private {function(?Event=)} */
this.focusHandler_ = () => requestAnimationFrame(
() => this.adapter_.addClass(MDCRippleFoundation.cssClasses.BG_FOCUSED)
);
this.focusHandler_ = () => this.handleFocus();

/** @private {function(?Event=)} */
this.blurHandler_ = () => requestAnimationFrame(
() => this.adapter_.removeClass(MDCRippleFoundation.cssClasses.BG_FOCUSED)
);
this.blurHandler_ = () => this.handleBlur();

/** @private {!Function} */
this.resizeHandler_ = () => this.layout();
Expand Down Expand Up @@ -587,6 +583,16 @@ class MDCRippleFoundation extends MDCFoundation {
this.adapter_.removeClass(UNBOUNDED);
}
}

handleFocus() {
requestAnimationFrame(() =>
this.adapter_.addClass(MDCRippleFoundation.cssClasses.BG_FOCUSED));
}

handleBlur() {
requestAnimationFrame(() =>
this.adapter_.removeClass(MDCRippleFoundation.cssClasses.BG_FOCUSED));
}
}

export default MDCRippleFoundation;
20 changes: 20 additions & 0 deletions test/unit/mdc-ripple/mdc-ripple.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import td from 'testdouble';
import {MDCRipple} from '../../../packages/mdc-ripple';
import {cssClasses} from '../../../packages/mdc-ripple/constants';
import * as util from '../../../packages/mdc-ripple/util';
import {createMockRaf} from '../helpers/raf';

suite('MDCRipple');

Expand Down Expand Up @@ -222,3 +223,22 @@ test('adapter#getWindowPageOffset returns page{X,Y}Offset as {x,y} respectively'
y: window.pageYOffset,
});
});

test(`handleFocus() adds class ${cssClasses.BG_FOCUSED}`, () => {
const raf = createMockRaf();
const {root, component} = setupTest();
component.foundation_.handleFocus();
raf.flush();
assert.isTrue(root.classList.contains(cssClasses.BG_FOCUSED));
raf.restore();
});

test(`handleBlur() removes class ${cssClasses.BG_FOCUSED}`, () => {
const raf = createMockRaf();
const {root, component} = setupTest();
root.classList.add(cssClasses.BG_FOCUSED);
component.foundation_.handleBlur();
raf.flush();
assert.isFalse(root.classList.contains(cssClasses.BG_FOCUSED));
raf.restore();
});

0 comments on commit 31d81ad

Please sign in to comment.