mirror of
https://github.com/bitwarden/browser
synced 2025-12-13 06:43:35 +00:00
[CL-601] Replace default reset button to enable it in more browsers (#14974)
* bb/pm-19497/replace default reset button to enable it in more browsers * address feedback: add ngClass; improve accessibility * add signals for form hover and input focus; compute showResetButton * fix(style): [CL-601] Improve CSS per reviewer comments Signed-off-by: Ben Brooks <bbrooks@bitwarden.com> * fix: [CL-601] add ngForm; remove standalone attributes Signed-off-by: Ben Brooks <bbrooks@bitwarden.com> * fix: [CL-601] add translation strings Signed-off-by: Ben Brooks <bbrooks@bitwarden.com> * fix: [CL-601] Use message key in aria label Signed-off-by: Ben Brooks <bbrooks@bitwarden.com> * fix: [CL-601] Remove unnecessary aria-hidden attribute Signed-off-by: Ben Brooks <bbrooks@bitwarden.com> * fix: [CL-601] Remove unecessary ngForm attributes Signed-off-by: Ben Brooks <bbrooks@bitwarden.com> * fix: [CL-601] Add storybook description Signed-off-by: Ben Brooks <bbrooks@bitwarden.com> * fix: [CL-601] Match main for recent signal input changs Signed-off-by: Ben Brooks <bbrooks@bitwarden.com> --------- Signed-off-by: Ben Brooks <bbrooks@bitwarden.com>
This commit is contained in:
@@ -547,6 +547,9 @@
|
|||||||
"searchVault": {
|
"searchVault": {
|
||||||
"message": "Search vault"
|
"message": "Search vault"
|
||||||
},
|
},
|
||||||
|
"resetSearch": {
|
||||||
|
"message": "Reset search"
|
||||||
|
},
|
||||||
"edit": {
|
"edit": {
|
||||||
"message": "Edit"
|
"message": "Edit"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -41,6 +41,9 @@
|
|||||||
"searchVault": {
|
"searchVault": {
|
||||||
"message": "Search vault"
|
"message": "Search vault"
|
||||||
},
|
},
|
||||||
|
"resetSearch": {
|
||||||
|
"message": "Reset search"
|
||||||
|
},
|
||||||
"addItem": {
|
"addItem": {
|
||||||
"message": "Add item"
|
"message": "Add item"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -629,6 +629,9 @@
|
|||||||
"searchGroups": {
|
"searchGroups": {
|
||||||
"message": "Search groups"
|
"message": "Search groups"
|
||||||
},
|
},
|
||||||
|
"resetSearch": {
|
||||||
|
"message": "Reset search"
|
||||||
|
},
|
||||||
"allItems": {
|
"allItems": {
|
||||||
"message": "All items"
|
"message": "All items"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
/**
|
/**
|
||||||
* Tailwind doesn't have a good way to style search-cancel-button.
|
* 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 {
|
bit-search input[type="search"]::-webkit-search-cancel-button {
|
||||||
-webkit-appearance: none;
|
-webkit-appearance: none;
|
||||||
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");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
<label class="tw-sr-only" [for]="id">{{ "search" | i18n }}</label>
|
<form
|
||||||
<div class="tw-relative tw-flex tw-items-center">
|
role="search"
|
||||||
|
(mouseenter)="isFormHovered.set(true)"
|
||||||
|
(mouseleave)="isFormHovered.set(false)"
|
||||||
|
class="tw-relative tw-flex tw-items-center tw-w-full"
|
||||||
|
>
|
||||||
|
<label class="tw-sr-only" [for]="id">{{ "search" | i18n }}</label>
|
||||||
<label
|
<label
|
||||||
[for]="id"
|
[for]="id"
|
||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
class="tw-absolute tw-left-2 tw-z-20 !tw-mb-0 tw-cursor-text"
|
class="tw-absolute tw-start-2 tw-z-20 !tw-mb-0 tw-cursor-text"
|
||||||
>
|
>
|
||||||
<i class="bwi bwi-search bwi-fw tw-text-muted"></i>
|
<i class="bwi bwi-search bwi-fw tw-text-muted"></i>
|
||||||
</label>
|
</label>
|
||||||
@@ -14,10 +19,23 @@
|
|||||||
[id]="id"
|
[id]="id"
|
||||||
[placeholder]="placeholder() ?? ('search' | i18n)"
|
[placeholder]="placeholder() ?? ('search' | i18n)"
|
||||||
class="tw-ps-9"
|
class="tw-ps-9"
|
||||||
|
name="searchText"
|
||||||
[ngModel]="searchText"
|
[ngModel]="searchText"
|
||||||
(ngModelChange)="onChange($event)"
|
(ngModelChange)="onChange($event)"
|
||||||
(blur)="onTouch()"
|
(focus)="isInputFocused.set(true)"
|
||||||
|
(blur)="isInputFocused.set(false); onTouch()"
|
||||||
[disabled]="disabled()"
|
[disabled]="disabled()"
|
||||||
[attr.autocomplete]="autocomplete()"
|
[attr.autocomplete]="autocomplete()"
|
||||||
/>
|
/>
|
||||||
</div>
|
<button
|
||||||
|
*ngIf="searchText && showResetButton()"
|
||||||
|
[ngClass]="{
|
||||||
|
'tw-opacity-0': !showResetButton(),
|
||||||
|
'tw-bg-text-muted': showResetButton(),
|
||||||
|
}"
|
||||||
|
class="bw-reset-btn tw-size-6 tw-absolute hover:tw-bg-text-main tw-end-2 tw-z-20 !tw-mb-0 tw-cursor-pointer"
|
||||||
|
type="reset"
|
||||||
|
[attr.aria-label]="'resetSearch' | i18n"
|
||||||
|
(click)="clearSearch()"
|
||||||
|
></button>
|
||||||
|
</form>
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// FIXME: Update this file to be type safe and remove this and next line
|
// FIXME: Update this file to be type safe and remove this and next line
|
||||||
// @ts-strict-ignore
|
// @ts-strict-ignore
|
||||||
import { Component, ElementRef, ViewChild, input, model } from "@angular/core";
|
import { NgIf, NgClass } from "@angular/common";
|
||||||
|
import { Component, ElementRef, ViewChild, input, model, signal, computed } from "@angular/core";
|
||||||
import {
|
import {
|
||||||
ControlValueAccessor,
|
ControlValueAccessor,
|
||||||
NG_VALUE_ACCESSOR,
|
NG_VALUE_ACCESSOR,
|
||||||
@@ -16,6 +17,9 @@ import { FocusableElement } from "../shared/focusable-element";
|
|||||||
|
|
||||||
let nextId = 0;
|
let nextId = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Do not nest Search components inside another `<form>`, as they already contain their own standalone `<form>` element for searching.
|
||||||
|
*/
|
||||||
@Component({
|
@Component({
|
||||||
selector: "bit-search",
|
selector: "bit-search",
|
||||||
templateUrl: "./search.component.html",
|
templateUrl: "./search.component.html",
|
||||||
@@ -30,7 +34,7 @@ let nextId = 0;
|
|||||||
useExisting: SearchComponent,
|
useExisting: SearchComponent,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
imports: [InputModule, ReactiveFormsModule, FormsModule, I18nPipe],
|
imports: [InputModule, ReactiveFormsModule, FormsModule, I18nPipe, NgIf, NgClass],
|
||||||
})
|
})
|
||||||
export class SearchComponent implements ControlValueAccessor, FocusableElement {
|
export class SearchComponent implements ControlValueAccessor, FocusableElement {
|
||||||
private notifyOnChange: (v: string) => void;
|
private notifyOnChange: (v: string) => void;
|
||||||
@@ -43,6 +47,11 @@ export class SearchComponent implements ControlValueAccessor, FocusableElement {
|
|||||||
// Use `type="text"` for Safari to improve rendering performance
|
// Use `type="text"` for Safari to improve rendering performance
|
||||||
protected inputType = isBrowserSafariApi() ? ("text" as const) : ("search" as const);
|
protected inputType = isBrowserSafariApi() ? ("text" as const) : ("search" as const);
|
||||||
|
|
||||||
|
protected isInputFocused = signal(false);
|
||||||
|
protected isFormHovered = signal(false);
|
||||||
|
|
||||||
|
protected showResetButton = computed(() => this.isInputFocused() || this.isFormHovered());
|
||||||
|
|
||||||
readonly disabled = model<boolean>();
|
readonly disabled = model<boolean>();
|
||||||
readonly placeholder = input<string>();
|
readonly placeholder = input<string>();
|
||||||
readonly autocomplete = input<string>();
|
readonly autocomplete = input<string>();
|
||||||
@@ -52,11 +61,20 @@ export class SearchComponent implements ControlValueAccessor, FocusableElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onChange(searchText: string) {
|
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) {
|
if (this.notifyOnChange != undefined) {
|
||||||
this.notifyOnChange(searchText);
|
this.notifyOnChange(searchText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle the reset button click
|
||||||
|
clearSearch() {
|
||||||
|
this.searchText = "";
|
||||||
|
if (this.notifyOnChange) {
|
||||||
|
this.notifyOnChange("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onTouch() {
|
onTouch() {
|
||||||
if (this.notifyOnTouch != undefined) {
|
if (this.notifyOnTouch != undefined) {
|
||||||
this.notifyOnTouch();
|
this.notifyOnTouch();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Meta, Canvas, Source, Primary, Controls, Title } from "@storybook/addon-docs";
|
import { Meta, Canvas, Source, Primary, Controls, Title, Description } from "@storybook/addon-docs";
|
||||||
|
|
||||||
import * as stories from "./search.stories";
|
import * as stories from "./search.stories";
|
||||||
|
|
||||||
@@ -9,6 +9,7 @@ import { SearchModule } from "@bitwarden/components";
|
|||||||
```
|
```
|
||||||
|
|
||||||
<Title>Search field</Title>
|
<Title>Search field</Title>
|
||||||
|
<Description />
|
||||||
|
|
||||||
<Primary />
|
<Primary />
|
||||||
<Controls />
|
<Controls />
|
||||||
|
|||||||
Reference in New Issue
Block a user