Skip to content

Commit bfceabd

Browse files
committed
refactor(searchbar): update padding based on icons
1 parent f47153f commit bfceabd

File tree

2 files changed

+75
-33
lines changed

2 files changed

+75
-33
lines changed

core/src/components/searchbar/searchbar.ionic.scss

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
@use "../../themes/ionic/ionic.globals.scss" as globals;
22
@use "searchbar.common";
33

4-
$leading-icon-padding: calc(globals.$ion-scale-400 + globals.$ion-space-200);
5-
$clear-button-padding: calc(globals.$ion-scale-400 + globals.$ion-space-200);
6-
74
// Ionic Searchbar
85
// --------------------------------------------------
96

@@ -60,10 +57,6 @@ $clear-button-padding: calc(globals.$ion-scale-400 + globals.$ion-space-200);
6057
contain: strict;
6158
}
6259

63-
:host(:not(.searchbar-should-show-clear)) {
64-
$clear-button-padding: #{globals.$ion-scale-0};
65-
}
66-
6760
// Searchbar Cancel Icon
6861
// -----------------------------------------
6962

@@ -132,7 +125,7 @@ $clear-button-padding: calc(globals.$ion-scale-400 + globals.$ion-space-200);
132125
* the size of the leading icon (search or cancel),
133126
* and the gap between the icon and the input.
134127
*/
135-
padding-inline-start: calc(9px + $leading-icon-padding);
128+
padding-inline-start: calc(9px + globals.$ion-scale-400 + globals.$ion-space-200);
136129
}
137130

138131
// Searchbar States
@@ -189,6 +182,42 @@ $clear-button-padding: calc(globals.$ion-scale-400 + globals.$ion-space-200);
189182

190183
/* Small */
191184
:host(.searchbar-size-small) .searchbar-input {
185+
/**
186+
* Padding start is based on
187+
* desired padding from design,
188+
* no leading icons.
189+
*
190+
* Padding end is based on
191+
* desired padding from design,
192+
* no trailing icons.
193+
*/
194+
@include globals.padding-horizontal(globals.$ion-space-300);
195+
196+
height: globals.$ion-scale-1000;
197+
}
198+
199+
/* Small with Leading Icons */
200+
:host(.searchbar-size-small.searchbar-should-show-search-icon) .searchbar-input,
201+
:host(.searchbar-size-small.searchbar-should-show-cancel) .searchbar-input {
202+
/**
203+
* Padding start is based on
204+
* desired padding from design,
205+
* the size of the leading icon (search or cancel),
206+
* and the gap between the icon and the input.
207+
*
208+
* Padding end is based on
209+
* desired padding from design,
210+
* no trailing icons.
211+
*/
212+
@include globals.padding-horizontal(
213+
calc(globals.$ion-space-300 + globals.$ion-scale-400 + globals.$ion-space-200),
214+
globals.$ion-space-300
215+
);
216+
}
217+
218+
/* Small with Leading Icons and Trailing Icons */
219+
:host(.searchbar-size-small.searchbar-should-show-search-icon.searchbar-should-show-clear) .searchbar-input,
220+
:host(.searchbar-size-small.searchbar-should-show-cancel.searchbar-should-show-clear) .searchbar-input {
192221
/**
193222
* Padding start is based on
194223
* desired padding from design,
@@ -200,12 +229,25 @@ $clear-button-padding: calc(globals.$ion-scale-400 + globals.$ion-space-200);
200229
* the size of the trailing icon (clear),
201230
* and the gap between the icon and the input.
202231
*/
232+
@include globals.padding-horizontal(calc(globals.$ion-space-300 + globals.$ion-scale-400 + globals.$ion-space-200));
233+
}
234+
235+
/* Small with Trailing Icons */
236+
:host(.searchbar-size-small.searchbar-should-show-clear) .searchbar-input {
237+
/**
238+
* Padding start is based on
239+
* desired padding from design,
240+
* no leading icons.
241+
*
242+
* Padding end is based on
243+
* desired padding from design,
244+
* the size of the trailing icon (clear),
245+
* and the gap between the icon and the input.
246+
*/
203247
@include globals.padding-horizontal(
204-
calc(globals.$ion-space-300 + $leading-icon-padding),
205-
calc(globals.$ion-space-300 + $clear-button-padding)
248+
globals.$ion-space-300,
249+
calc(globals.$ion-space-300 + globals.$ion-scale-400 + globals.$ion-space-200)
206250
);
207-
208-
height: globals.$ion-scale-1000;
209251
}
210252

211253
:host(.searchbar-size-small) .searchbar-search-icon,
@@ -232,13 +274,7 @@ $clear-button-padding: calc(globals.$ion-scale-400 + globals.$ion-space-200);
232274
* the size of the trailing icon (clear),
233275
* and the gap between the icon and the input.
234276
*/
235-
@debug "Leading icon padding is equal to #{$leading-icon-padding}";
236-
@debug "Clear button padding is equal to #{$clear-button-padding}";
237-
238-
@include globals.padding-horizontal(
239-
calc(globals.$ion-space-400 + $leading-icon-padding),
240-
calc(globals.$ion-space-400 + $clear-button-padding)
241-
);
277+
@include globals.padding-horizontal(calc(globals.$ion-space-400 + globals.$ion-scale-400 + globals.$ion-space-200));
242278

243279
height: globals.$ion-scale-1200;
244280
}
@@ -257,8 +293,6 @@ $clear-button-padding: calc(globals.$ion-scale-400 + globals.$ion-space-200);
257293
/* Large */
258294
:host(.searchbar-size-large) .searchbar-search-icon,
259295
:host(.searchbar-size-large) .searchbar-cancel-button {
260-
$leading-icon-padding: #{calc(globals.$ion-scale-500 + globals.$ion-space-200)};
261-
262296
width: globals.$ion-scale-500;
263297
height: globals.$ion-scale-500;
264298
}
@@ -274,7 +308,7 @@ $clear-button-padding: calc(globals.$ion-scale-400 + globals.$ion-space-200);
274308
* the size of the leading icon (search or cancel),
275309
* and the gap between the icon and the input.
276310
*/
277-
padding-inline-start: calc(9px + $leading-icon-padding);
311+
padding-inline-start: calc(9px + globals.$ion-scale-500 + globals.$ion-space-200);
278312
}
279313

280314
:host(.searchbar-size-large) .searchbar-input {
@@ -292,8 +326,8 @@ $clear-button-padding: calc(globals.$ion-scale-400 + globals.$ion-space-200);
292326
* and the gap between the icon and the input.
293327
*/
294328
@include globals.padding-horizontal(
295-
calc(globals.$ion-space-500 + $leading-icon-padding),
296-
calc(globals.$ion-space-500 + $clear-button-padding)
329+
calc(globals.$ion-space-500 + globals.$ion-scale-500 + globals.$ion-space-200),
330+
calc(globals.$ion-space-500 + globals.$ion-scale-400 + globals.$ion-space-200)
297331
);
298332

299333
height: globals.$ion-scale-1400;

core/src/components/searchbar/searchbar.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,14 @@ export class Searchbar implements ComponentInterface {
602602
return this.getValue() !== '';
603603
}
604604

605+
private shouldShowSearchIcon(): boolean {
606+
if (this.searchIcon === false || this.searchIcon === 'false') {
607+
return false;
608+
}
609+
610+
return true;
611+
}
612+
605613
/**
606614
* Determines whether or not the cancel button should be visible onscreen.
607615
* Cancel button should be shown if one of two conditions applies:
@@ -702,9 +710,10 @@ export class Searchbar implements ComponentInterface {
702710
* Otherwise, use the icon set in the config.
703711
* If no icon is set in the config, use the default icon.
704712
*/
705-
get searchbarSearchIcon(): string | boolean {
706-
// Return the icon if it is explicitly set
707-
if (this.searchIcon != null) {
713+
get searchbarSearchIcon(): string {
714+
// Return the icon if it is explicitly set.
715+
// If the icon is set to a boolean or string 'true', use the default icon.
716+
if (this.searchIcon != null && this.searchIcon !== 'true' && typeof this.searchIcon !== 'boolean') {
708717
return this.searchIcon;
709718
}
710719

@@ -791,8 +800,7 @@ export class Searchbar implements ComponentInterface {
791800
'searchbar-has-value': this.hasValue(),
792801
'searchbar-left-aligned': this.shouldAlignLeft,
793802
'searchbar-has-focus': this.focused,
794-
'searchbar-has-leading-icons':
795-
theme === 'ionic' && (this.searchIcon !== false || this.showCancelButton !== 'never'),
803+
'searchbar-should-show-search-icon': this.shouldShowSearchIcon(),
796804
'searchbar-should-show-clear': this.shouldShowClearButton(),
797805
'searchbar-should-show-cancel': this.shouldShowCancelButton(),
798806
[`searchbar-shape-${shape}`]: shape !== undefined,
@@ -826,16 +834,16 @@ export class Searchbar implements ComponentInterface {
826834

827835
{(theme === 'md' || theme === 'ionic') && cancelButton}
828836

829-
{this.searchIcon !== false ? (
837+
{this.shouldShowSearchIcon() && (
830838
<ion-icon
831839
aria-hidden="true"
832840
icon={searchbarSearchIcon}
833841
lazy={false}
834842
class="searchbar-search-icon"
835843
></ion-icon>
836-
) : null}
844+
)}
837845

838-
{this.shouldShowClearButton() ? (
846+
{this.shouldShowClearButton() && (
839847
<button
840848
aria-label="reset"
841849
type="button"
@@ -858,7 +866,7 @@ export class Searchbar implements ComponentInterface {
858866
className="searchbar-clear-icon"
859867
></ion-icon>
860868
</button>
861-
) : null}
869+
)}
862870
</div>
863871
{theme === 'ios' && cancelButton}
864872
</Host>

0 commit comments

Comments
 (0)