Skip to content

Commit

Permalink
feat(addon-doc): adds keybindings to the search field (#8830)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheKenkei authored Sep 11, 2024
1 parent 38d3def commit 8d03673
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
18 changes: 16 additions & 2 deletions projects/addon-doc/components/navigation/navigation.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ChangeDetectorRef,
Component,
inject,
ViewChild,
} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {FormControl, ReactiveFormsModule} from '@angular/forms';
Expand Down Expand Up @@ -34,8 +35,7 @@ import {TuiScrollbar} from '@taiga-ui/core/components/scrollbar';
import {TuiTextfield} from '@taiga-ui/core/components/textfield';
import {TUI_COMMON_ICONS} from '@taiga-ui/core/tokens';
import {TuiAccordion} from '@taiga-ui/kit/components/accordion';
import type {TuiInputComponent} from '@taiga-ui/legacy/components/input';
import {TuiInputModule} from '@taiga-ui/legacy/components/input';
import {TuiInputComponent, TuiInputModule} from '@taiga-ui/legacy/components/input';
import {TuiTextfieldControllerModule} from '@taiga-ui/legacy/directives/textfield-controller';
import {PolymorpheusOutlet, PolymorpheusTemplate} from '@taiga-ui/polymorpheus';
import {combineLatest, filter, map, switchMap, take} from 'rxjs';
Expand Down Expand Up @@ -79,9 +79,13 @@ import {TuiDocScrollIntoViewLink} from './scroll-into-view.directive';
providers: NAVIGATION_PROVIDERS,
host: {
'[class._open]': 'menuOpen',
'(window:keydown)': 'onFocusSearch($event)',
},
})
export class TuiDocNavigation {
@ViewChild(TuiInputComponent, {static: true})
private readonly searchInput?: TuiInputComponent;

private readonly router = inject(Router);
private readonly doc = inject(DOCUMENT);

Expand Down Expand Up @@ -165,6 +169,16 @@ export class TuiDocNavigation {
this.openActivePageGroup();
}

protected onFocusSearch(event: KeyboardEvent): void {
if (
event.code === 'Slash' &&
!(this.doc.activeElement instanceof HTMLInputElement)
) {
this.searchInput?.nativeFocusableElement?.focus();
event.preventDefault();
}
}

@tuiPure
private filterItems(
items: ReadonlyArray<readonly TuiDocRoutePage[]>,
Expand Down
11 changes: 11 additions & 0 deletions projects/addon-doc/components/navigation/navigation.style.less
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@
::ng-deep tui-icon {
border: 0.25rem solid transparent;
}

.t-search-text {
display: flex;
flex-direction: row;
justify-content: space-between;

.t-slash {
block-size: 1rem;
font-size: 0.5rem;
}
}
}

.t-navigation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
[tuiTextfieldIconLeft]="docIcons.search"
[tuiTextfieldLabelOutside]="true"
>
{{ searchText }}
<div class="t-search-text">
{{ searchText }}
<code class="t-slash">/</code>
</div>
<ng-container *ngIf="canOpen">
<tui-data-list *tuiDataList>
<tui-opt-group
Expand Down

0 comments on commit 8d03673

Please sign in to comment.