Skip to content

Commit

Permalink
perf(searchbar): searchbar animation is disabled by default
Browse files Browse the repository at this point in the history
it can be reenabled by`<ion-searchbar animated="true">`

fixes ionic-team#6023
  • Loading branch information
manucorporat committed Sep 22, 2016
1 parent b29dfdb commit e788cd4
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 67 deletions.
78 changes: 43 additions & 35 deletions src/components/searchbar/searchbar.ios.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ $searchbar-ios-toolbar-input-background: rgba(0, 0, 0, .08) !default;

background-repeat: no-repeat;
background-size: $searchbar-ios-input-search-icon-size;

transition: $searchbar-ios-input-transition;
}


Expand All @@ -87,8 +85,6 @@ $searchbar-ios-toolbar-input-background: rgba(0, 0, 0, .08) !default;

color: $searchbar-ios-input-text-color;
background-color: $searchbar-ios-input-background-color;

transition: $searchbar-ios-input-transition;
}


Expand Down Expand Up @@ -119,47 +115,34 @@ $searchbar-ios-toolbar-input-background: rgba(0, 0, 0, .08) !default;

flex-shrink: 0;

margin-right: -100%;
margin-left: 0;
padding: 0;
padding-left: 8px;

height: 30px;

cursor: pointer;

opacity: 0;

transform: translate3d(0, 0, 0);
transition: $searchbar-ios-cancel-transition;

pointer-events: none;
}

.searchbar-ios.searchbar-show-cancel .searchbar-ios-cancel {
display: block;
}


// Searchbar Left Aligned (iOS only)
// -----------------------------------------

.searchbar-ios.searchbar-left-aligned .searchbar-search-icon {
margin-left: 0;
}
.searchbar-ios.searchbar-left-aligned {
.searchbar-search-icon {
margin-left: 0;
}

.searchbar-ios.searchbar-left-aligned .searchbar-input {
padding-left: 30px;
.searchbar-input {
padding-left: 30px;
}
}


// Searchbar Has Focus
// -----------------------------------------

.searchbar-ios.searchbar-has-focus .searchbar-ios-cancel {
opacity: 1;

pointer-events: auto;
.searchbar-ios.searchbar-show-cancel.searchbar-has-focus .searchbar-ios-cancel {
display: block;
}


Expand All @@ -169,18 +152,18 @@ $searchbar-ios-toolbar-input-background: rgba(0, 0, 0, .08) !default;
.toolbar .searchbar-ios {
border-bottom-width: 0;
background: transparent;
}

.toolbar .searchbar-ios .searchbar-input {
background: $searchbar-ios-toolbar-input-background;
}
.searchbar-input {
background: $searchbar-ios-toolbar-input-background;
}

.toolbar .searchbar-ios .searchbar-ios-cancel {
padding: 0;
}
.searchbar-ios-cancel {
padding: 0;
}

.toolbar .searchbar-ios.searchbar-has-focus .searchbar-ios-cancel {
padding-left: 8px;
&.searchbar-has-focus .searchbar-ios-cancel {
padding-left: 8px;
}
}


Expand Down Expand Up @@ -225,3 +208,28 @@ $searchbar-ios-toolbar-input-background: rgba(0, 0, 0, .08) !default;
}

}

// Searchbar animation
// -----------------------------------------

.searchbar-ios[animated=true] {
.searchbar-search-icon,
.searchbar-input {
transition: $searchbar-ios-input-transition;
}

.searchbar-ios-cancel {
display: block;
opacity: 0;
margin-right: -100%;
transition: $searchbar-ios-cancel-transition;
transform: translate3d(0, 0, 0);
pointer-events: none;
}

&.searchbar-has-focus .searchbar-ios-cancel {
pointer-events: auto;
opacity: 1;
}

}
1 change: 1 addition & 0 deletions src/components/searchbar/searchbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ ion-searchbar {
.searchbar-has-value.searchbar-has-focus .searchbar-clear-icon {
display: block;
}

76 changes: 46 additions & 30 deletions src/components/searchbar/searchbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { NgControl } from '@angular/forms';

import { Config } from '../../config/config';
import { Ion } from '../ion';
import { isPresent } from '../../util/util';
import { raf } from '../../util/dom';
import { isPresent, isTrueProperty } from '../../util/util';
import { Debouncer } from '../../util/debouncer';


Expand Down Expand Up @@ -42,13 +43,15 @@ import { Debouncer } from '../../util/debouncer';
'[class.searchbar-has-value]': '_value',
'[class.searchbar-active]': '_isActive',
'[class.searchbar-show-cancel]': 'showCancelButton',
'[class.searchbar-left-aligned]': 'shouldAlignLeft()'
'[class.searchbar-left-aligned]': '_shouldAlignLeft'
},
encapsulation: ViewEncapsulation.None
})
export class Searchbar extends Ion {
_value: string|number = '';
_shouldBlur: boolean = true;
_shouldAlignLeft: boolean = true;
_isCancelVisible: boolean = false;
_isActive: boolean = false;
_searchbarInput: ElementRef;
_debouncer: Debouncer = new Debouncer(250);
Expand Down Expand Up @@ -115,6 +118,11 @@ export class Searchbar extends Ion {
*/
@Input() type: string = 'search';

/**
* @input {string|boolean} Set the input's spellcheck property. Values: `true`, `false`. Default `false`.
*/
@Input() animated: string | boolean = false;

/**
* @output {event} When the Searchbar input has changed including cleared.
*/
Expand Down Expand Up @@ -217,8 +225,8 @@ export class Searchbar extends Ion {
* @private
* After View Checked position the elements
*/
ngAfterViewChecked() {
this.positionElements();
ngAfterContentInit() {
raf(() => this.positionElements());
}

/**
Expand All @@ -227,26 +235,30 @@ export class Searchbar extends Ion {
* based on the input value and if it is focused. (ios only)
*/
positionElements() {
if (this._config.get('mode') !== 'ios') return;
let isAnimated = isTrueProperty(this.animated);
let prevAlignLeft = this._shouldAlignLeft;
this._shouldAlignLeft = (!isAnimated || (this._value && this._value.toString().trim() !== '') || this._sbHasFocus === true);

// Position the input placeholder & search icon
if (this._searchbarInput && this._searchbarIcon) {
this.positionInputPlaceholder(this._searchbarInput.nativeElement, this._searchbarIcon.nativeElement);
if (this._config.get('mode') !== 'ios') {
return;
}

// Position the cancel button
if (this._cancelButton && this._cancelButton.nativeElement) {
this.positionCancelButton(this._cancelButton.nativeElement);
if (prevAlignLeft !== this._shouldAlignLeft) {
this.positionPlaceholder();
}
if (isAnimated) {
this.positionCancelButton();
}
}

/**
* @private
* Calculates the amount of padding/margin left for the elements
* in order to center them based on the placeholder width
*/
positionInputPlaceholder(inputEle: HTMLElement, iconEle: HTMLElement) {
if (this.shouldAlignLeft()) {
positionPlaceholder() {
let inputEle = this._searchbarInput.nativeElement;
let iconEle = this._searchbarIcon.nativeElement;
if (!this._searchbarInput || !this._searchbarIcon) {
return;
}

if (this._shouldAlignLeft) {
inputEle.removeAttribute('style');
iconEle.removeAttribute('style');
} else {
Expand All @@ -273,23 +285,23 @@ export class Searchbar extends Ion {
* @private
* Show the iOS Cancel button on focus, hide it offscreen otherwise
*/
positionCancelButton(cancelButtonEle: HTMLElement) {
if (cancelButtonEle.offsetWidth > 0) {
if (this._sbHasFocus) {
cancelButtonEle.style.marginRight = '0';
positionCancelButton() {
if (!this._cancelButton || !this._cancelButton.nativeElement) {
return;
}
let showShowCancel = this._sbHasFocus;
if (showShowCancel !== this._isCancelVisible) {
let cancelStyleEle = this._cancelButton.nativeElement;
let cancelStyle = cancelStyleEle.style;
this._isCancelVisible = showShowCancel;
if (showShowCancel) {
cancelStyle.marginRight = '0';
} else {
cancelButtonEle.style.marginRight = -cancelButtonEle.offsetWidth + 'px';
cancelStyle.marginRight = -cancelStyleEle.offsetWidth + 'px';
}
}
}

/**
* @private
* Align the input placeholder left on focus or if a value exists
*/
shouldAlignLeft() {
return ( (this._value && this._value.toString().trim() !== '') || this._sbHasFocus === true );
}

/**
* @private
Expand Down Expand Up @@ -399,4 +411,8 @@ export class Searchbar extends Ion {
registerOnTouched(fn: () => {}): void {
this.onTouched = fn;
}

focus() {
this.getNativeElement().focus();
}
}
3 changes: 3 additions & 0 deletions src/components/searchbar/test/basic/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<h5 padding-left> Search - Default </h5>
<ion-searchbar [(ngModel)]="defaultSearch" showCancelButton debounce="500" (ionInput)="triggerInput($event)" (ionBlur)="inputBlurred($event)" (ionFocus)="inputFocused($event)" (ionCancel)="onCancelSearchbar($event)" (ionClear)="onClearSearchbar($event)"></ion-searchbar>

<h5 padding-left> Search - Animated </h5>
<ion-searchbar animated="true" showCancelButton debounce="500" (ionInput)="triggerInput($event)" (ionBlur)="inputBlurred($event)" (ionFocus)="inputFocused($event)" (ionCancel)="onCancelSearchbar($event)" (ionClear)="onClearSearchbar($event)"></ion-searchbar>

<p padding-left>
defaultSearch: <b>{{ defaultSearch }}</b>
</p>
Expand Down
5 changes: 3 additions & 2 deletions src/components/searchbar/test/nav/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

<ion-content>

<ion-searchbar (ionInput)="getItems($event)"></ion-searchbar>

<form>
<ion-searchbar (ionInput)="getItems($event)"></ion-searchbar>
</form>
<ion-list>
<button ion-item *ngFor="let item of items" (click)="showDetail(item)" class="e2eSearchbarNavItem">
{{ item }}
Expand Down

0 comments on commit e788cd4

Please sign in to comment.