Skip to content

Commit

Permalink
fix(ripple)!: rename hover methods to event handlers
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 510547677
  • Loading branch information
asyncLiz authored and copybara-github committed Feb 18, 2023
1 parent c955055 commit cde7ca0
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 36 deletions.
4 changes: 2 additions & 2 deletions chips/action/lib/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ export abstract class Action extends ActionElement {
}

protected handlePointerEnter(e: PointerEvent) {
this.ripple?.beginHover(e);
this.ripple?.handlePointerenter(e);
}

override handlePointerLeave(e: PointerEvent) {
super.handlePointerLeave(e);
this.ripple?.endHover();
this.ripple?.handlePointerleave(e);
}

override handlePointerDown(e: PointerEvent) {
Expand Down
4 changes: 2 additions & 2 deletions navigationtab/lib/navigation-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ export class NavigationTab extends ActionElement implements NavigationTabState {
}

protected handlePointerEnter(e: PointerEvent) {
this.ripple.beginHover(e);
this.ripple.handlePointerenter(e);
}

override handlePointerLeave(e: PointerEvent) {
super.handlePointerLeave(e);
this.ripple.endHover();
this.ripple.handlePointerleave(e);
}

protected handleFocus() {
Expand Down
12 changes: 3 additions & 9 deletions ripple/directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class RippleDirective extends Directive {
this.pointerDown(ripple, event as PointerEvent);
break;
case 'pointerenter':
this.pointerEnter(ripple, event as PointerEvent);
ripple.handlePointerenter(event as PointerEvent);
break;
case 'pointerleave':
this.pointerLeave(ripple, event as PointerEvent);
Expand Down Expand Up @@ -266,15 +266,9 @@ class RippleDirective extends Directive {
}
}

private pointerEnter(ripple: Ripple, ev: PointerEvent) {
if (this.shouldReactToEvent(ripple, ev, true)) {
ripple.beginHover(ev);
}
}

private pointerLeave(ripple: Ripple, ev: PointerEvent) {
if (this.shouldReactToEvent(ripple, ev, true)) {
ripple.endHover();
ripple.handlePointerleave(ev);
// release a held mouse or pen press that moves outside the element
if (!this.isTouch(ev) && this.state !== State.INACTIVE) {
this.endPress(ripple);
Expand All @@ -291,4 +285,4 @@ class RippleDirective extends Directive {
* keyboard interaction, pass `true` to enable press animations on Enter or
* Spacebar
*/
export const ripple = directive(RippleDirective);
export const ripple = directive(RippleDirective);
29 changes: 24 additions & 5 deletions ripple/lib/ripple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ export class Ripple extends LitElement {
private growAnimation?: Animation;
private delayedEndPressHandle?: number;

beginHover(hoverEvent?: Event) {
if ((hoverEvent as PointerEvent)?.pointerType !== 'touch') {
this.hovered = true;
handlePointerenter(event: PointerEvent) {
if (this.isTouch(event) || !this.shouldReactToEvent(event)) {
return;
}

this.hovered = true;
}

endHover() {
handlePointerleave(event: PointerEvent) {
if (!this.shouldReactToEvent(event)) {
return;
}

this.hovered = false;
}

Expand Down Expand Up @@ -95,7 +101,7 @@ export class Ripple extends LitElement {

protected override update(changedProps: PropertyValues<this>) {
if (changedProps.has('disabled') && this.disabled) {
this.endHover();
this.hovered = false;
this.endFocus();
this.endPress();
}
Expand Down Expand Up @@ -192,4 +198,17 @@ export class Ripple extends LitElement {
fill: ANIMATION_FILL
});
}

/**
* Returns `true` if
* - the ripple element is enabled
* - the pointer is primary for the input type
*/
private shouldReactToEvent(event: PointerEvent) {
return !this.disabled && event.isPrimary;
}

private isTouch({pointerType}: PointerEvent) {
return pointerType === 'touch';
}
}
16 changes: 10 additions & 6 deletions ripple/lib/ripple_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,28 @@ describe('ripple', () => {
expect(surface).not.toHaveClass(RippleStateClasses.FOCUSED);
});

it('sets hover class on beginHover()', async () => {
element.beginHover();
it('sets hover class on handlePointerenter()', async () => {
element.handlePointerenter(
new PointerEvent('pointerenter', {isPrimary: true}));
await element.updateComplete;

expect(surface).toHaveClass(RippleStateClasses.HOVERED);
});

it('removes hover class on endHover()', async () => {
element.beginHover();
it('removes hover class on handlePointerleave()', async () => {
element.handlePointerenter(
new PointerEvent('pointerenter', {isPrimary: true}));
await element.updateComplete;
element.endHover();
element.handlePointerleave(
new PointerEvent('pointerleave', {isPrimary: true}));
await element.updateComplete;

expect(surface).not.toHaveClass(RippleStateClasses.HOVERED);
});

it('stops hovering when disabled', async () => {
element.beginHover();
element.handlePointerenter(
new PointerEvent('pointerenter', {isPrimary: true}));
await element.updateComplete;
element.disabled = true;
await element.updateComplete;
Expand Down
4 changes: 2 additions & 2 deletions segmentedbutton/lib/segmented-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ export class SegmentedButton extends ActionElement {
}

protected handlePointerEnter(e: PointerEvent) {
this.ripple.beginHover(e);
this.ripple.handlePointerenter(e);
}

override handlePointerLeave(e: PointerEvent) {
super.handlePointerLeave(e);
this.ripple.endHover();
this.ripple.handlePointerleave(e);
}

protected handleFocus() {
Expand Down
23 changes: 13 additions & 10 deletions slider/lib/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,13 @@ export class Slider extends LitElement {
};

return html`
<div
<div
class="container ${classMap(containerClasses)}"
style=${styleMap(containerStyles)}
>
${when(this.allowRange, () => this.renderInput(inputAProps))}
${this.renderInput(inputBProps)}
${this.renderTrack()}
${this.renderTrack()}
<div class="handleContainerPadded">
<div class="handleContainerBlock">
<div class="handleContainer ${classMap(handleContainerClasses)}">
Expand Down Expand Up @@ -416,7 +416,7 @@ export class Slider extends LitElement {
this.allowRange ? (lesser ? 'Lesser' : 'Greater') : ''}">
<div class="handleNub"><md-elevation shadow></md-elevation></div>
${when(this.withLabel, () => this.renderLabel(label))}
</slot>
${when(showRipple, () => this.renderRipple(id))}
${when(focusRequested, () => this.renderFocusRing(showFocus))}
Expand All @@ -440,16 +440,16 @@ export class Slider extends LitElement {
})}"
@focus=${this.handleFocus}
@blur=${this.handleBlur}
@pointerdown=${this.handleDown}
@pointerup=${this.handleUp}
@pointerdown=${this.handleDown}
@pointerup=${this.handleUp}
@pointerenter=${this.handleEnter}
@pointermove=${this.handleMove}
@pointerleave=${this.handleLeave}
@input=${this.handleInput}
@input=${this.handleInput}
@change=${this.handleChange}
.disabled=${this.disabled}
.min=${String(this.min)}
.max=${String(this.max)}
.min=${String(this.min)}
.max=${String(this.max)}
.step=${String(this.step)}
.value=${String(value)}
.tabIndex=${lesser ? 1 : 0}
Expand Down Expand Up @@ -484,10 +484,13 @@ export class Slider extends LitElement {
if (!rippleEl) {
return;
}
// TODO(b/269799771): improve slider ripple connection
if (hovering) {
rippleEl.beginHover();
rippleEl.handlePointerenter(
new PointerEvent('pointerenter', {isPrimary: true}));
} else {
rippleEl.endHover();
rippleEl.handlePointerleave(
new PointerEvent('pointerleave', {isPrimary: true}));
}
}

Expand Down

0 comments on commit cde7ca0

Please sign in to comment.