Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(material/core): ripple coords when trigger is not the container #29020

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

enten
Copy link
Contributor

@enten enten commented May 8, 2024

This pull request is a proposal to fix coordinates emitted from the trigger element when it's different than the container element.

Actual behavior

When a pointer down event (mousedown: MouseEvent or touchstart: TouchEvent) is emitted from the trigger element, its clientX and clientY properties are used to call RippleRender::fadeInRipple.

In case where the trigger element is the ripple container element, clientX and clientY are inside the container dom rect.

But when the trigger element is different than the ripple container element (a trigger element can be set by using matRippleTrigger input), clientX and clientY are outside the container dom rect.

Expeded behavior

In case where the trigger element is different than the ripple container element, using clientX and clientY properties and calculating an equivalent point in the container element before calling RippleRender::fadeInRipple.

Reproduction

StackBlitz link: https://stackblitz.com/edit/components-issue-gtvxae

  • The div "Ripple Container" is the ripple container: <div matRipple matRippleRadius="10" [matRippleTrigger]="trigger"></div>
  • The "Trigger Ripple" button is the ripple trigger: <button #trigger>Trigger Button</button>
  • When "Trigger Ripple" button is clicked, the ripple element (red dot in screenshot below) is positionned outside the ripple container dom rect (the ripple element is positionned inside the trigger dom rect)
  • When "Trigger Ripple with expected behavior" button is clicked, the clientX and clientY properties are fixed and the ripple element (green dot in screenshot below) is positionned correctly inside the ripple container dom rect

image

  /** Fix coordinates emitted from the trigger element when is not the container element. */
  private _fixCoordinatesEmittedFromTrigger(x: number, y: number): [number, number] {
    if (this._triggerElement && this._triggerElement !== this._containerElement) {
      const containerRect = (this._containerRect =
        this._containerRect || this._containerElement.getBoundingClientRect());
      const triggerRect = (this._triggerRect =
        this._triggerRect || this._triggerElement.getBoundingClientRect());
      x = ((x - triggerRect.left) / triggerRect.width) * containerRect.width + containerRect.left;
      y = ((y - triggerRect.top) / triggerRect.height) * containerRect.height + containerRect.top;
    }
    return [x, y];
  }
}

@enten enten requested a review from devversion as a code owner May 8, 2024 19:06
@enten enten force-pushed the ripple-coords-when-trigger-is-not-the-container branch from 783d885 to cd59496 Compare May 9, 2024 19:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant