Stop the ion-fab-list from automatically closing when an element is selected within it

I'm having trouble getting a form to stay visible when a fab is clicked in my Ionic 4 app. Every time I click on a fab or another component within the ion-fab-list, the ion-fab-list automatically closes. How can I prevent this from happening and keep the form open?

I've attempted using @ViewChild, but it hasn't been successful. One thing I've noticed about the @ViewChild decorator is that it requires two parameters, which seems different from other solutions I've seen.

form.component.ts

import { Component, OnInit, ViewChild } from '@angular/core';
import { NavController, IonFab } from '@ionic/angular'

@Component({
  selector: 'app-form',
  templateUrl: './form.component.html',
  styleUrls: ['./form.component.scss'],
})
export class FormComponent implements OnInit {
  isFabListOpen = false

  toggleFab(): void {
    this.isFabListOpen = !this.isFabListOpen
    console.log('lol')
  }

  constructor() {}

  ngOnInit() {}

}

form.component.scss

ion-fab {
  position: fixed;
  left: 84%;
  float: right;
}

ion-fab-list {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  width: 350px;
  right: 130px;
}

form.component.html

<ion-fab horizontal="end" vertical="bottom" slot="fixed" [activated]="isFabListOpen">
  <ion-fab-button color="light" (click)="toggleFab()">
    <ion-icon name="arrow-dropleft"></ion-icon>
  </ion-fab-button>
  <ion-fab-list side="top" [hidden]="isFabListOpen">
    <!-- <ion-fab-button color="light">
      <ion-icon name="logo-facebook"></ion-icon>
    </ion-fab-button>
    <ion-fab-button color="light">
      <ion-icon name="logo-twitter"></ion-icon>
    </ion-fab-button>
    <ion-fab-button color="light">
      <ion-icon name="logo-vimeo"></ion-icon>
    </ion-fab-button> -->
    <form action="">
      <ion-item>
        <ion-label>quote</ion-label>
        <ion-input type="text" placeholder="Awesome Input"></ion-input>
      </ion-item>
      <ion-item>
        <ion-label>author</ion-label>
        <ion-input type="text" placeholder="Awesome Input"></ion-input>
      </ion-item>
      <ion-item>
        <ion-label>author</ion-label>
        <ion-input type="text" placeholder="Awesome Input"></ion-input>
      </ion-item>
      <ion-item>
        <ion-label>day</ion-label>
        <ion-datetime displayFormat="DD MM YY" placeholder="Leave empty to select today"></ion-datetime>
      </ion-item>
      <ion-button>
        Save
      </ion-button>
    </form>
  </ion-fab-list>
</ion-fab>


The fab list should still remain even when clicking on the fabs or components but instead close when clicked.

Answer №1

If you click fast, you can instantly restore another fabulous look with the same information as before. It'll seem like it never left! 😊

Answer №2

Eliminate the use of toggleFab() function on the button as it is redundant when using ion-fab-button. The functionality of opening and closing remains the same without the need for an extra function call. Additionally, you can omit the activated attribute unless specifically required elsewhere since its default value is false.

  1. Clicking on ion-fab-button updates isFabListOpen & activated to true
  2. Fab opens
  3. toggleFab() triggers and toggles
    this.isFabListOpen = !this.isFabListOpen
  4. isFabListOpen & activated are then set to false
  5. Modal is closed

Answer №3

Here is an example of how I used JavaScript to achieve this:

<ion-fab class="remote_controller" vertical="bottom" horizontal="center" slot="fixed">
  <ion-fab-button>
    <ion-icon name="game-controller"></ion-icon>
  </ion-fab-button>
  <ion-fab-list side="top">
    <ion-fab-button id="up" onclick="stop_close(this)">
      <ion-icon name="caret-up"></ion-icon>
    </ion-fab-button>
  </ion-fab-list>
  <ion-fab-list side="bottom">
    <ion-fab-button id="down" onclick="stop_close(this)">
      <ion-icon name="caret-down"></ion-icon>
    </ion-fab-button>
  </ion-fab-list>
  <ion-fab-list side="start">
    <ion-fab-button id="left" onclick="stop_close(this)">
      <ion-icon name="caret-back"></ion-icon>
    </ion-fab-button>
  </ion-fab-list>
  <ion-fab-list side="end">
    <ion-fab-button id="right" onclick="stop_close(this)">
      <ion-icon name="caret-forward"></ion-icon>
      </ion-icon>
    </ion-fab-button>
  </ion-fab-list>
</ion-fab>

Additionally, I included the following script:

<script>
  function stop_close(event){
    $('.remote_controller')[0].activated = false;

    //perform actions based on the button
  }
</script>

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

Show information retrieved from one API request within another API request

Currently, I am in the process of retrieving data from the Youtube API by utilizing 2 separate requests. One request is used to fetch a list of videos, while the other request provides details for each individual video. The initial request successfully di ...

How can you notify a component, via a service, that an event has occurred using Subject or BehaviorSubject?

For my Angular 10 application, I created a service to facilitate communication between components: export class CommunicationService { private messageSubject = new Subject<Message>(); sendMessage(code: MessageCode, data?: any) { this.messag ...

calculating the dynamic height of a document from top to bottom using Angular

Is there a way to dynamically calculate the height of each page from top to bottom in Angular? The syntax that works in JavaScript seems to give an error in Angular. console.log( (document.height !== undefined) ? document.height : document.body.offsetHeigh ...

Is it necessary to verify the apiKey or does the authentication of the user suffice for an HTTPS callable function?

I'm interested in creating a cloud function that can be executed from both the client and the backend. After exploring the documentation, I learned that an HTTPS callable function will automatically include user authentication data, accessible through ...

Extension for VS Code that displays the properties of a model

Is there a VS Code extension available that allows me to view the properties of a model directly from the HTML markup? For instance, when typing ngModel and then pressing the dot (.) key, it would display its properties similar to how it works in a TypeScr ...

Angular 4 HTTP Requests Failing to Retrieve JSON Data

I am currently working with the following method in my Typescript: allPowerPlants(onlyActive: boolean = false, page: number = 1): PowerPlant[] { const params: string = [ `onlyActive=${onlyActive}`, `page=${page}` ].join('&&apo ...

Potential issue with Jhipster: loading of data could be experiencing delays

I've encountered an issue with getting my chart to display properly. I am working on creating a chart using ng2-charts, where I retrieve data from a service and then display it on the chart. The problem arises when the data is not correctly displayed ...

Delay in Angular routing

As I am still learning Angular, I am trying to familiarize myself with its routing functionality. In my click-through wizard, the final step includes a 'Complete' button that should post to the database and then route the user to a specific page ...

arrange elements by their relationship with parents and children using typescript and angular

Here is a list that needs to be sorted by parent and child relationships: 0: {id: 7, name: "333", code: "333", type: 3, hasParent: true, parentId: 4} 1: {id: 6, name: "dfgdfg", code: "dfgdfg", type: 3, hasParent: false, parentId: null} 2: {id: 5, name: ...

Comparison between typings and @types in the NPM scope

There are different approaches when it comes to handling TypeScript definitions. In some cases, the typings tool is used, as seen in projects like angular/angular2-seed. Alternatively, some projects use scoped NPM packages with the prefix @types, complete ...

What is the most efficient way to modify a list within another list variable in Angular without impacting the parent list?

Here is a data list that I am working with: subscriberDataList -> wipEligibilityList -> dependentList dependentList -> wipEligibilityList -> eligibilityList wipEligibilityList[0] -> status - ...

Having difficulty implementing a versatile helper using Typescript in a React application

Setting up a generic for a Text Input helper has been quite challenging for me. I encountered an error when the Helper is used (specifically on the e passed to props.handleChange) <TextInput hiddenLabel={true} name={`${id}-number`} labelText=" ...

The Angular dependency provider is failing to supply the requested alternative class

I am currently in the process of writing unit tests for the doc-manager.component component. This particular component relies on the DocService, but I want to swap it with instances of MockedDocService within my tests. By leveraging alternative class prov ...

Unable to utilize console.log and alert functions within the Next.js application

I'm currently facing a problem in my Next.js application where the console.log and alert functions are not functioning as intended. Despite checking the code, browser settings, and environment thoroughly, pinpointing the root cause of the issue remain ...

How to Programmatically Assign Bootstrap Class to an Element Using Angular 2

I am working on a page with several input boxes that need to be flagged for certain criteria. <div class="form-group"> <label class="d-block">Allowance Type</label> <div class="input-group"> ...

Struggling to incorporate generics into a Typescript method without sacrificing the typing of object keys

Currently, I am working on a method in Typescript that is responsible for extracting allowable property types from an object of a constrained generic type. The scenario involves a type called ParticipantBase which consists of properties like first: string ...

Unexpected token error in TypeScript: Syntax mistake spotted in code

Being new to Angular, I understand that mastering TypeScript is crucial for becoming a skilled Angular developer. Therefore, I created this simple program: function loge(messag){ console.log(messag); } var message:string; message = "Hi"; loge(messa ...

Typescript Routing Issue - This call does not match any overloads

Need assistance with redirecting to a sign-up page upon button click. Currently encountering a 'no overload matches this call' error in TypeScript. Have tried researching the issue online, but it's quite broad, and being new to Typescript ma ...

What is the reason behind Typescript errors vanishing after including onchange in the code?

When using VSCode with appropriate settings enabled, the error would be displayed in the following .html file: <!DOCTYPE html> <html> <body> <div> <select> </select> </div> <script&g ...

Creating a dynamic selection in Angular without duplicate values

How can I prevent repetition of values when creating a dynamic select based on an object fetched from a database? Below is the HTML code: <router-outlet></router-outlet> <hr> <div class="row"> <div class="col-xs-12"> & ...