Search code examples
javascriptreactjsionic4

Ionic 4: ion-chip activatable / clickable by default; how to turn off?


Using Ionic 4 with React, I have the following code:

<IonChip color="success">
  <IonIcon icon={thumbsUp} />
  <IonLabel>{this.state.numCorrect}</IonLabel>
</IonChip>

This by default is rendering the following DOM content in my browser:

<ion-chip color="success" class="ion-color ion-color-success ios ion-activatable hydrated">
  <ion-icon role="img" class="ios hydrated"> ... </ion-icon>
  <ion-label class="sc-ion-label-ios-h sc-ion-label-ios-s ios hydrated">0</ion-label>
</ion-chip>

As you can see, the ion-activatable class is being automatically added to the chip component, which is causing it to appear clickable and show the Material Design ripple effect, etc. But I don't want this component to be clickable (I am using it just as an indicator component). Can I disable this class somehow?

The online documentation for Ionic 4 doesn't offer any advice on this.


Solution

  • If you look at the implemetation of the ion-chip Webcomponent, you can see that the ion-activatable class is added by default (you can only modify the class and the events after the component is rendered):

    export class Chip implements ComponentInterface {
      /**
       * The color to use from your application's color palette.
       * Default options are: `"primary"`, `"secondary"`, `"tertiary"`, `"success"`, `"warning"`, `"danger"`, `"light"`, `"medium"`, and `"dark"`.
       * For more information on colors, see [theming](/docs/theming/basics).
       */
      @Prop() color?: Color;
    
      /**
       * Display an outline style button.
       */
      @Prop() outline = false;
    
      render() {
        const mode = getIonMode(this);
    
        return (
          <Host
            class={{
              ...createColorClasses(this.color),
              [mode]: true,
              'chip-outline': this.outline,
              'ion-activatable': true,
            }}
          >
            <slot></slot>
            {mode === 'md' && <ion-ripple-effect></ion-ripple-effect>}
          </Host>
        );
      }
    }
    

    so a simple workaround you can do, is to prevent all click, state and cursor options on the ion-chip element with the pointer-events CSS property (and only do <IonChip mode="ios"> if you don't want the ripple efect to be rendered):

    ion-chip {
      pointer-events: none;
    }