Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(addon-doc): adds keybindings to the search field #8830

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -15,11 +16,7 @@ import {
RouterLinkActive,
Scroll,
} from '@angular/router';
import {
TUI_DOC_ICONS,
TUI_DOC_PAGE_LOADED,
TUI_DOC_SEARCH_TEXT,
} from '@taiga-ui/addon-doc/tokens';
import {TUI_DOC_ICONS, TUI_DOC_PAGE_LOADED} from '@taiga-ui/addon-doc/tokens';
import type {TuiDocRoutePage, TuiDocRoutePages} from '@taiga-ui/addon-doc/types';
import {tuiTransliterateKeyboardLayout} from '@taiga-ui/addon-doc/utils';
import {TuiSidebarDirective} from '@taiga-ui/addon-mobile/directives/sidebar';
Expand All @@ -34,8 +31,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 +75,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 All @@ -90,7 +90,6 @@ export class TuiDocNavigation {
protected readonly sidebar = inject(TuiSidebarDirective, {optional: true});
protected readonly labels = inject(NAVIGATION_LABELS);
protected readonly items = inject(NAVIGATION_ITEMS);
protected readonly searchText = inject(TUI_DOC_SEARCH_TEXT);
protected readonly activatedRoute = inject(ActivatedRoute);
protected readonly docIcons = inject(TUI_DOC_ICONS);
protected readonly icons = inject(TUI_COMMON_ICONS);
Expand Down Expand Up @@ -165,6 +164,16 @@ export class TuiDocNavigation {
this.openActivePageGroup();
}

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

@tuiPure
private filterItems(
items: ReadonlyArray<readonly TuiDocRoutePage[]>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
::ng-deep tui-icon {
border: 0.25rem solid transparent;
}

.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,11 @@
[tuiTextfieldIconLeft]="docIcons.search"
[tuiTextfieldLabelOutside]="true"
>
{{ searchText }}
<span>
Type
<code class="slash">/</code>
to search
</span>
<ng-container *ngIf="canOpen">
<tui-data-list *tuiDataList>
<tui-opt-group
Expand Down
Loading