From bc835375c6212a61c36a76250a583e5c7db70d24 Mon Sep 17 00:00:00 2001 From: bensbits91 Date: Tue, 27 May 2025 15:42:57 -0700 Subject: [PATCH] bb/pm-19497/replace default reset button to enable it in more browsers --- .../src/search/search.component.css | 18 +++--- .../src/search/search.component.html | 56 ++++++++++++------- .../components/src/search/search.component.ts | 16 +++++- 3 files changed, 59 insertions(+), 31 deletions(-) diff --git a/libs/components/src/search/search.component.css b/libs/components/src/search/search.component.css index 35304438a88..6233de2a3ac 100644 --- a/libs/components/src/search/search.component.css +++ b/libs/components/src/search/search.component.css @@ -1,19 +1,17 @@ /** * Tailwind doesn't have a good way to style search-cancel-button. + * Hide the default reset button that only appears in some browsers. */ bit-search input[type="search"]::-webkit-search-cancel-button { -webkit-appearance: none; appearance: none; - height: 21px; - width: 21px; - margin: 0; - cursor: pointer; - background-repeat: no-repeat; - mask-image: url("./close-button.svg"); - -webkit-mask-image: url("./close-button.svg"); - background-color: rgba(var(--color-text-muted)); } -bit-search input[type="search"]::-webkit-search-cancel-button:hover { - background-color: rgba(var(--color-text-main)); +/** + * Style our custom reset button that works in all common browsers. + * Tailwind CSS does not natively support mask-image or -webkit-mask-image utilities (but can be extended if needed). + */ +.bw-reset-btn { + mask-image: url("./close-button.svg"); + -webkit-mask-image: url("./close-button.svg"); } diff --git a/libs/components/src/search/search.component.html b/libs/components/src/search/search.component.html index 5bb25425e57..2ba4ef525af 100644 --- a/libs/components/src/search/search.component.html +++ b/libs/components/src/search/search.component.html @@ -1,23 +1,39 @@
- - +
+ + + +
diff --git a/libs/components/src/search/search.component.ts b/libs/components/src/search/search.component.ts index 7edf3b1d60a..ffd8dc6947e 100644 --- a/libs/components/src/search/search.component.ts +++ b/libs/components/src/search/search.component.ts @@ -1,5 +1,6 @@ // FIXME: Update this file to be type safe and remove this and next line // @ts-strict-ignore +import { NgIf } from "@angular/common"; import { Component, ElementRef, Input, ViewChild } from "@angular/core"; import { ControlValueAccessor, @@ -31,7 +32,7 @@ let nextId = 0; }, ], standalone: true, - imports: [InputModule, ReactiveFormsModule, FormsModule, I18nPipe], + imports: [InputModule, ReactiveFormsModule, FormsModule, I18nPipe, NgIf], }) export class SearchComponent implements ControlValueAccessor, FocusableElement { private notifyOnChange: (v: string) => void; @@ -43,6 +44,10 @@ export class SearchComponent implements ControlValueAccessor, FocusableElement { protected searchText: string; // Use `type="text"` for Safari to improve rendering performance protected inputType = isBrowserSafariApi() ? ("text" as const) : ("search" as const); + // Optional: Track focus and hover state of the input to emulate the hiding/showing of the native reset button + protected isInputFocused = false; + protected isInputHovered = false; + protected resetHovered = false; @Input() disabled: boolean; @Input() placeholder: string; @@ -53,11 +58,20 @@ export class SearchComponent implements ControlValueAccessor, FocusableElement { } onChange(searchText: string) { + this.searchText = searchText; // update the model when the input changes (so we can use it with *ngIf in the template) if (this.notifyOnChange != undefined) { this.notifyOnChange(searchText); } } + // Handle the reset button click + clearSearch() { + this.searchText = ""; + if (this.notifyOnChange) { + this.notifyOnChange(""); + } + } + onTouch() { if (this.notifyOnTouch != undefined) { this.notifyOnTouch();