Incorporate my personalized icons into the button design of ionic 2 actionSheet

I'm struggling to figure out how to incorporate my personal icon into the actionSheet feature of ionic 2/3.

presentActionSheet() {
   let actionSheet = this.actionSheetCtrl.create({
     title: 'Mode',     
     buttons: [
       {
         text: 'Add to Y' ,
         role: 'destructive',
         icon:"my_icon",
         cssClass:"yoy",
         handler: () => {
           console.log('Destructive clicked');
         }
       },   
       {
         text: 'Cancel',
         role: 'cancel',
         handler: () => {
           console.log('Cancel clicked');
        }
      }
     ],

   });

   actionSheet.present();
 }

Any tips on how to include my custom icons in the actionsheet button options?

Answer №1

The documentation provided by Ionic does not offer a built-in solution for this particular issue. Nevertheless, there is a workaround using a custom class.

   let actionSheet = this.actionSheetCtrl.create({
     title: 'Mode',     
     buttons: [
       {
         text: 'Add to Y',
         // Setting a custom class for displaying the icon
         cssClass: "class_used_to_set_icon",
         handler: () => {
           console.log('Add to Y clicked');
         }
       }
     ],

   });

   actionSheet.present();

You can define the following class in your src/app/app.scss file:

.class_used_to_set_icon {
  // It's recommended to place the icon in src/assets (e.g., ../assets/icon/favicon.ico)
  background-image: url("path/to/your/icon") !important;
  // Properly position the icon
  background-repeat: no-repeat !important;
  background-position: 10px 10px !important;
  // Indent the text to the right of the icon
  padding-left: 70px !important;
}


EDIT
Another approach involves using custom icons from a font library like icomoon:

   let actionSheet = this.actionSheetCtrl.create({
     title: 'Mode',     
     buttons: [
       {
         text: 'Add to Y',
         // Set custom icon class and a class for better display
         cssClass: "icon-drink actionSheet_withIcomoon",
         handler: () => {
           console.log('Add to Y clicked');
         }
       }
     ],

   });

   actionSheet.present();

In your src/app/app.scss file, define the following class:

// Adjust the size of the displayed icon
.actionSheet_withIcomoon { font-size: 2.4rem !important; }
// Correct font dimensions and text positioning
.actionSheet_withIcomoon .button-inner {
  font-family: "Roboto", "Helvetica Neue", sans-serif;
  font-weight: bold;
  padding-left: 52px;
  margin-top: -20px;
  font-size: 1.6rem !important;
}

Answer №2

Discover a workaround by making use of FontAwesome. Check out an example at https://codepen.io/ablewhite/pen/LyZNXZ.

In your scenario, this could look like (after referencing the FontAwesome CSS):

   let actionSheet = this.actionSheetCtrl.create({
     title: 'Mode',     
     buttons: [
       {
         text: '<i class="icon fa fa-plus"></i> Add to Y' ,
         role: 'destructive',
         cssClass: 'yoy',
         handler: () => {
           console.log('Destructive clicked');
         }
       },   
       {
         text: 'Cancel',
         role: 'cancel',
         handler: () => {
           console.log('Cancel clicked');
         }
       }
     ],

   });

If you require icons not found in FontAwesome, apply the same method with a custom font of your own.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with routing in Node.js using Express

This is my first attempt at programming. I am currently in the process of setting up routing in the following way: var indexRouter = require('./routes/index'); var loginRouter = require('./routes/login'); app.use('/', indexRo ...

Updating an array by adding or removing items

I am attempting to create a method for deleting and adding items to an array, but I need easy-to-use delete and add methods since I am unfamiliar with TypeScript. export class NgForComponent implements OnInit { Numbers: number[]; constructor() { ...

Oops! There seems to be an issue: Uncaught promise error - ReferenceError: google is not defined

I've integrated Angular 2 with Google Maps Places Autocomplete in my project, but I encountered an error: ERROR Error: Uncaught (in promise): ReferenceError: google is not defined Here's the HTML snippet: <agm-map id="googleMap"> ...

Comparing setInterval with Observable's timer method, which is the superior choice for setting up timers?

In order to create a timer in Angular, I am currently using Observable as shown below: timer: Observable<any> = Observable.timer(0, 990); timerSubscription: Subscription; this.timerSubscription = this.timer.subscribe((value) => { this. ...

JavaScript code snippet for detecting key presses of 3 specific arrow keys on the document

For this specific action, I must press and hold the left arrow key first, followed by the right arrow key, and then the up arrow key. However, it seems that the up arrow key is not being triggered as expected. It appears that there may be some limitations ...

Fuzzy background when you scroll

My container has an image that blurs when you scroll down the page by 50 pixels. I feel like I've seen this effect somewhere before, but I can't quite recall where. I want the text to remain unaffected and not turn blue. Here is the HTML: <d ...

Using JSON as a variable solely for determining its type and guaranteeing that the import is eliminated during compilation

In my TypeScript backend project with Node as the target runtime, I have a JSON file that is auto-generated within my repository. I use the following code to import the JSON file in order to get the type of the JSON object: import countries from '../g ...

Combining plugin setups and functionalities into a unified script

When it comes to grouping plugin initializations and scripts in a website, I often have a "tools.js" file that contains various functions, click events, and more. I prefer keeping all these script calls centralized in one file for simplicity, organization, ...

Can builtins like DOM globals be explicitly imported?

The present situation includes the utilization of rollup (as well as iife parameters), but I am hesitant about whether it is solely related to rollup or typescript. My objective is to achieve something similar to this: import { document } from "[wherever ...

Incorporating a favicon into a Next.js React project

I'm currently working on integrating a favicon into a Next.js project that was generated using create-next-app. The favicon.png file is stored in the public folder, and I followed the instructions for serving static files outlined here. In my Layout ...

Limiting the display of every item in Angular ngFor

I'm currently working with Angular and I have the following code in my component.html: <div class="card" *ngFor="let item of galleries;" (mouseenter)=" setBackground(item?.image?.fullpath)" (mouseover)="showCount ...

Mastering sorting in AngularJS: ascending or descending, the choice is yours!

I have set up a table view with infinite scroll functionality. The table contains 2000 objects, but only shows 25 at a time. As the user scrolls to the bottom, it loads an additional 25 elements and so on. There is a "V" or "^" button in the header that sh ...

Angular 14: Deleting an item from a FormArray triggers unintended form submission due to Angular animation

After beginning to create animations for my app, I observed that deleting an element from a FormArray triggers a form submission. Custom form controls were developed using ControlValueAccessor, and certain FormGroups are passed through @Inputs. The animati ...

Unable to retrieve class attributes within a function

I have recently started delving into the world of node.js, and I am facing some challenges with a middleware that I created. The purpose of this middleware is to act as an Error handler. However, I am encountering difficulties in accessing properties that ...

Setting up a Web application testing environment on its own

Embarking on my journey in web application development and testing, I am currently involved in a project that requires me to create a standalone environment for testing the web application. The main goal is to ensure the web application is easily testable ...

Prevent automatic suggestions from appearing in Ionic app on Android

Our Ionic v4 (Angular, Cordova) app features form input fields (<ion-input>) like the example below: <ion-input formControlName="firstName" type="text" inputmode="text" autocapitalize="sentences"></ion-input> When running the Android ap ...

Is AngularJS Service a Singleton for Your Application?

As a relative newcomer to angularJS, I've learned that services should be singleton, but for some reason it's not working for me. Here is my service: .factory('TrainingPlan', function($http, SERVER){ var o = { exercises: [], ...

AJAX loading footer content before images are fully loaded

I am a beginner when it comes to ajax and I'm facing an issue where the footer loads before the images, causing the images to overlap the footer. The problem is illustrated in the image below. <!doctype html> <html lang="en"> <head ...

Dynamically setting the height of elements using jQuery with incremental values

My Objective: I am trying to dynamically adjust the height of each li element within a ul.container. The goal is to find the tallest li, add 25px to its height, and then apply that new height to all other li elements in the container. This calculation ne ...

"Mastering the art of traversing through request.body and making necessary updates on an object

As I was reviewing a MERN tutorial, specifically focusing on the "update" route, I came across some interesting code snippets. todoRoutes.route('/update/:id').post(function(req, res) { Todo.findById(req.params.id, function(err, todo) { ...