I was skimming the Ionic Worldwide Slack channel and saw this post:
Hi all,
i stuck on mixing an `ion-icon` with an `floating label` like in this the *Angular Material Bootstrap Inputs Demo* (https://mdbootstrap.com/angular/components/#).I tried this:
< ion-item> < ion-icon slot="start" name="bulb" color="ownred">< /ion-icon> < ion-label position="floating">name< /ion-label> < ion-input type="text" [(ngModel)]="newItem.name">< /ion-input> < /ion-item>
But this will center the icon and not align it on bottom, like in the shown picture.
Do you have any idea?
This got me thinking how to create this. My work machine was being updated to Mojave, so I had a few moments to play around with a solution.
The one I came up with was to break the problem into two parts. I figured that I needed to isolate the icon from the label and input set, so I turned to the ion-grid component to handle that. By using the align properties of Flexbox, I could position the elements how I needed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ion-header> | |
<ion-toolbar> | |
<ion-title> | |
Ionic Blank | |
</ion-title> | |
</ion-toolbar> | |
</ion-header> | |
<ion-content padding> | |
<ion-grid> | |
<ion-row align-items-end> | |
<ion-col size="1" align-self-bottom> | |
<ion-icon name="mail" size="large" [ngClass]="{'activeInputIcon': isLabelActive}"></ion-icon> | |
</ion-col> | |
<ion-col> | |
<ion-item> | |
<ion-label position="floating">Floating Label</ion-label> | |
<ion-input (ionBlur)="toggleIcon($event)" (ionFocus)="toggleIcon($event)"></ion-input> | |
</ion-item> | |
</ion-col> | |
</ion-row> | |
</ion-grid> | |
</ion-content> |
In playing with the reference that was supplied, When the input received focus, the icon also changed color. To make that action happen, I add to event handlers on the input for the ionBlur and ionFocus events.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
@Component({ | |
selector: 'app-home', | |
templateUrl: 'home.page.html', | |
styleUrls: ['home.page.scss'], | |
}) | |
export class HomePage { | |
isLabelActive: Boolean = false; | |
constructor () { } | |
toggleIcon(evt) { | |
this.isLabelActive = !this.isLabelActive; | |
} | |
} |
The handler for those events simply toggles a boolean. I use that variable’s state to trigger the CSS class state using [ngClass]=“{‘activeInputIcon’: isLabelActive}” on the ion-icon. So, when the isLabelActive is true, the activeInputIcon class is applied.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.activeInputIcon { | |
color:#3880ff; | |
} |
With that, we have rebuilt a sample of the combined icon and floating label input.
Now, this sample is very basic. It does not handle multiple inputs so you will need to create a solution to solve that problem. For a bigger challenge, this entire element could be wrapped into a custom component…but I will leave that up to someone else.
You must be logged in to post a comment.