Build a tabs component that displays one panel of content at a time depending on the active tab element. Some HTML is provided for you as example content.
ids, data attributes, replacing some tags, etc.) and use client-side rendering instead.1import { Component } from '@angular/core';23@Component({4 selector: 'app-root',5 templateUrl: './app.component.html',6})7export class AppComponent {8 tabs = [9 {10 value: 'html',11 label: 'HTML',12 panel:13 'The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser.',14 },15 {16 value: 'css',17 label: 'CSS',18 panel:19 'Cascading Style Sheets is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML.',20 },21 {22 value: 'javascript',23 label: 'JavaScript',24 panel:25 'JavaScript, often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS.',26 },27 ];28}
Implementing a basic (not fully accessible) Tabs component in Angular is quite simple due to the fact that only one state value is needed: the currently active tab item. Angular also helps to keep the state and the UI in sync, which is more troublesome to do in Vanilla JavaScript.
For simplicity's sake, we'll create a Tabs component where the state is managed within the Tabs component. During interviews, do clarify with your interviewer if they prefer to manage state within the component or outside.
Part of the complexity of building a component is designing its API. This Angular component accepts two @Input values:
items: A list of item objects. Each item is an object with the fields:
value: A unique identifier for the tab item.label: The text label to show in the tab item.panel: The contents to show in the tab panel when the item is active.defaultValue: The default tab item/panel to show. In case the defaultValue is not provided, we'll use the first item as the value. This is assuming that items is non-empty.ngOnInit initializes the component's local value, click bindings call setValue, and Angular class/hidden bindings render the active tab and panel. A controlled Angular API could instead accept value as an input and emit changes with an @Output, rather than owning the selection internally. This component creates no resources that require lifecycle cleanup.
Accessibility is an important factor for making good Tabs components. The ARIA Authoring Practices Guide for Tabs has a long list of guidelines for the ARIA roles, states, and properties to add to the various elements of a tab. Tabs II and Tabs III will focus on improving the accessibility of the Tabs component.
console.log() statements will appear here.