when i click on a link to navigate to certain page , if i press the tab key, focus starts on a browser elements (search bar), not at the top of a new page like a visually-impaired user would expect even i set the tab-index value?
but it works fine when i reload the page
this is my page component
@Component({
selector: 'app-rental-contracts-page',
template: '<router-outlet></router-outlet>',
host: {
class:'lc-grid__item--wrapper'
}
})
export class RentalContractsPageComponent implements OnInit {
constructor() {
}
ngOnInit() {
}
}
and this my module
const routes: Routes = [
{
path: '',
component: RentalContractsPageComponent,
children: [
{
path: '',
component: RentalContractsListPageComponent,
data: { title: "Baux" },
},
{
path: 'term/:term',
component: RentalContractsListPageComponent
},
{
path: 'create',
component: CreateRentalContractPageComponent
},
{
path: 'create/:ownerId',
component: CreateRentalContractPageComponent
}
,
{
path: 'create/:ownerId/:partId',
component: CreateRentalContractPageComponent
}
]
}
];
@NgModule({
declarations: [RentalContractsPageComponent, CreateRentalContractPageComponent, RentalContractsListPageComponent],
imports: [
CommonModule,
RouterModule.forChild(routes),
TranslateModule,
NgbModule,
FormsModule,
ContractsModule.forRoot(environment),
]
})
export class RentalContractsPageModule { }
2
Answers
When you load a component, ensure you have a
ViewChild
on the element that you want to focus, this can also be a plain div tagWhen you focus, the tabIndex will continue from the point of focus, is my solution for this issue!
HTML
TS
Create an element that has a
tabindex
of-1
. Because the value is-1
, it won’t be focusable viatab
, but it can be focusable via JavaScript. Also note, any non-standard element (input, button, textarea, etc.) with atabindex
attribute also must also have arole
attribute to be valid so that a screen reader knows how it should interact with the element.Here is a working example.
Lets create a reusable directive to do this we will call it autofocus to have it work similar to a native HTML element. We will also add two inputs/bindings one for a custom
tabindex
which we will default to-1
, and one for a custom role which we will default toregion
.Note: Since you can have only one
activeElement
per page, if you use more than one on the same page, then the last directive created will be the focused element.Next, lets import it into our component and add the autofocus attribute to it.
Here is how you can (optionally) style your input elements. I disabled styling on all elements except for the input in this example:
* {outline: none;}
:focus-visible