primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Issue in Carousel component. Click events or href does not work on touch on mobile devices

Open sukhminderx opened this issue 7 years ago • 11 comments

I'm submitting a ... (check one with "x")

[x] bug report => Search github for a similar issue or PR before submitting
[ ] feature request => Please check if request is not on the roadmap already https://github.com/primefaces/primeng/wiki/Roadmap
[ ] support request => Please do not submit support request here, instead see http://forum.primefaces.org/viewforum.php?f=35

Plunkr Case (Bug Reports) Please demonstrate your case at stackblitz by using the issue template below. Issues without a test case have much less possibility to be reviewd in detail and assisted.

https://stackblitz.com/github/primefaces/primeng-issue-template

Current behavior

I added a simple carousel component. In the content of the carousel, I added anchor tag with href that navigates to other website. In desktop, the click events work and the navigation is successful. But in mobile, the touch events work for slide process and the click for href is not executed.

Expected behavior

In mobile, if the content of the carousel contains the href, it should be navigate on the touch/click of the anchor /button.

Minimal reproduction of the problem with instructions

The app can be seen here: https://llqiwwpyb.github.stackblitz.io/

What is the motivation / use case for changing the behavior?

There can be anchors or button in carousel content and mobile devices should execute the click events of the content.

Please tell us about your environment:

  • Angular version: 8.X

8.X

  • PrimeNG version: 8.X

8.X

  • Browser: [all | Chrome XX | Firefox XX | IE XX | Safari XX | Mobile Chrome XX | Android X.X Web Browser | iOS XX Safari | iOS XX UIWebView | iOS XX WKWebView ]

all. I did on chrome

  • Language: [all | TypeScript X.X | ES6/7 | ES5] TypeScript

  • Node (for AoT issues): node --version =

sukhminderx avatar Nov 07 '19 06:11 sukhminderx

Hi, My team also encountered this problem. The problem lies here, in carousel.js:410:

Carousel.prototype.changePageOnTouch = function (e, diff) {
        if (diff < 0) {
            this.navForward(e);
        }
        else {
            this.navBackward(e);
        }
    };

Easiest way is to comment out if statements to remove defect and make carousel work properly. Removal of this function requires to remove/change logic in Carousel.prototype.onTouchEnd method.

Edit: Of course to hotfix this in your project you can just overwrite this function e.g. in ngAfterViewInit hook :)

Eldrisch avatar Nov 26 '19 14:11 Eldrisch

@Eldrisch , will this work if I override the changePageOnTouch in my code??

sukhminderx avatar Nov 26 '19 14:11 sukhminderx

@Eldrisch , will this work if I override the changePageOnTouch in my code??

Yeah, should work, you can do it via ViewChild for exact element or just override whole prototype for every Carousel element in ngOnInit or constructor

Eldrisch avatar Nov 26 '19 15:11 Eldrisch

worked.. thanks! Closing this.

sukhminderx avatar Dec 13 '19 09:12 sukhminderx

Hi @sukhminderx , @Eldrisch,

Can you please post an example for the fix provided? I am not sure how to "override" the changePageOnTouch() in my component.

P.S: I am new to Angular ecosystem. Thanks!

m-r-arvind avatar Jan 02 '20 16:01 m-r-arvind

Hello @biscuitboy, try it this way:

import { ElementRef, NgZone } from '@angular/core';
import { Carousel } from 'primeng/carousel';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent extends Carousel {

  constructor(public el:ElementRef,
    public zone: NgZone) {
    super(el,zone);
    Carousel.prototype.changePageOnTouch = (e,diff) => {}
}

Hope this will help. :)

wizluo avatar Feb 11 '20 19:02 wizluo

@sukhminderx Could you please reopen? I know the issue is fixed for you, but it's still there for others to rediscover (IMO #8377 is a duplicate) and yet it somehow got into 9.0 changelog as fixed. While @tavadur's code is a great workaround, I believe there is more that can be done.

MarcinListkowski avatar Feb 12 '20 16:02 MarcinListkowski

Hello @biscuitboy, try it this way:

import { ElementRef, NgZone } from '@angular/core';
import { Carousel } from 'primeng/carousel';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent extends Carousel {

  constructor(public el:ElementRef,
    public zone: NgZone) {
    super(el,zone);
    Carousel.prototype.changePageOnTouch = (e,diff) => {}
}

Hope this will help. :)

This worked but now the swipe gesture on carousel is not working.

murtuzamacdev avatar Dec 31 '21 11:12 murtuzamacdev

For anyone wondering how to fix this without breaking the swiping of the carousel:

Use your Carousel with the CarouselModule as usual. Import Carousel as well. In the constructor set Carousel.prototype.onTouchMove = (e) => {};

A Code example with a standalone component would be:

import { Carousel, CarouselModule } from 'primeng/carousel';

@Component({
  selector: 'app-my-component',
  standalone: true,
  imports: [
    CarouselModule,
  ],
  templateUrl: './my-app.component.html',
  styleUrls: ['./my-app.component.scss']
})
export class MyComponent {
   constructor() {
    Carousel.prototype.onTouchMove = (e) => {};  //Overwrites function for all Carousels
  }
}

xSentry avatar Mar 15 '24 22:03 xSentry

In my case, none of the fixes mentioned worked. I needed to disable all touch events on the carousel. This helped:

import { Carousel } from 'primeng/carousel';

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  standalone: false
})
export class AppComponent {
   constructor() {
    //Overwrites functions for all Carousels
    Carousel.prototype.onTouchStart = () => {};
    Carousel.prototype.onTouchMove = () => {};
    Carousel.prototype.onTouchEnd = () => {};   
  }
}

pziacek avatar Dec 23 '25 16:12 pziacek

It is possible to also use directive to target all carousel instances in the app. Create directive e.g. carousel-touch-fix.directive.ts

import { Directive, Self, Optional, OnInit } from "@angular/core";
import { Carousel } from "primeng/carousel";

@Directive({
  selector: "p-carousel", // Targets all instances of p-carousel
  standalone: false
})
export class CarouselTouchFixDirective implements OnInit {
  constructor(@Self() @Optional() private carousel: Carousel) {}

  ngOnInit() {
    if (!this.carousel) return;

    // override onTouchStart of the carousel
    this.carousel.onTouchStart = (event) => {
      const INTERACTIVE_SELECTOR = 'button, a, img, div, input, select, textarea, [role="button"]';

      // Get the actual element that was touched (e.g., button)
      const target = event.target as HTMLElement;

      // Find the nearest "meaningful" interactive parent (or the element itself)
      const interactiveElement = target.closest(INTERACTIVE_SELECTOR) as HTMLElement;

      // If we found one, trigger the click
      if (interactiveElement) {
        interactiveElement.click();
      }
    };

    // Reassigning to an empty function kills the default internal logic
    this.carousel.onTouchMove = () => {};
    this.carousel.onTouchEnd = () => {};
  }
}

Then import it in the app.module.ts

import { CarouselTouchFixDirective } from "./carousel-touch-fix.directive";

@NgModule({
  declarations: [
    ...
    CarouselTouchFixDirective
  ],
  imports: [],
  exports: [
    ....
    CarouselTouchFixDirective
  ]
})

This directive will be applied automatically to all carousel elements in the app. You don’t have to change your existing HTML code.

pziacek avatar Dec 23 '25 16:12 pziacek