Semantic Markup

Using Proper HTML Elements and Attributes

A common trap when structuring html is using too many divs and marking them up with classes or ids to indicate their role, for example:

  1. @Component({
  2. selector: 'ngc2-app'
  3. template: `
  4. <div class="header">
  5. <div class="navigation">
  6. <div class="item"><a [routerLink]="['']"><img src="https://angular.io/resources/images/logos/angular2/angular.svg" width="40"></a></div>
  7. <div class="item"><a [routerLink]="['services']">Services</a></div>
  8. <div class="item"><a [routerLink]="['process']">Process</a></div>
  9. <div class="item"><a [routerLink]="['work']">Work</a></div>
  10. </div>
  11. </div>
  12. <router-outlet class="page-content">
  13. </router-outlet>
  14. `,
  15. })

View Example

While it might be obvious to someone reading the HTML or using the application what the purpose of each element is, the class names don't have any semantic meaning for browsers and screen readers. We can give them more information by using the proper HTML elements instead.

  1. @Component({
  2. selector: 'ngc2-app',
  3. template: `
  4. <header>
  5. <nav>
  6. <ul>
  7. <li><a [routerLink]="['']"><img src="https://angular.io/resources/images/logos/angular2/angular.svg" width="40" alt="Angular logo"></a></li>
  8. <li><a [routerLink]="['services']">Services</a></li>
  9. <li><a [routerLink]="['process']">Process</a></li>
  10. <li><a [routerLink]="['work']">Work</a></li>
  11. </ul>
  12. </nav>
  13. </header>
  14. <main aria-live="polite">
  15. <router-outlet>
  16. </router-outlet>
  17. </main>
  18. `
  19. })

View Example

Here, we use the header element instead of a div, which lets the browser know that the elements within provide information about the site as a whole rather than about the specific page.

We replace another div with the nav element, which lets the browser know the elements within are related to accessing different parts of the page or site.

We also nest router-outlet within a main element, which tells the browser that the content loaded into the router outlet is the main content of the page.

There are a couple of new attributes on different elements as well to give the browser even more information. The alt attribute has been added to the image to let the browser know that it's a logo image. There's also an aria-live attribute on the main element.

This attribute is part of larger spec known as Accessible Rich Internet Applications (WAI-ARIA) which we'll go over in detail. This is something that lets screen readers know that the content within the main tag will be updated on the client-side after the page has loaded and needs to be watched for updates.

Roles and ARIA

The ARIA spec was created as a way for content authors a way to provide additional context to the semantics of their application rather than just details on how to render the content. This allows assistive technology to understand what's going on inside an application and relay that information in a structured and streamlined format for users with disabilities.

One of the main concepts in the ARIA spec is the role. A role defines what the purpose of an html element is within the context of that document or application. Roles are defined by adding an attribute to an html element ie. role="main" or are defined by default depending on the html element.

Some examples of roles are list, button or navigation which are the default roles of ul, button and nav respectively. Sometimes however, you may not want or be able to use the standard html element to represent these objects in your application, for example, you may want to create your own button component with it's own distinct logic. In this case you can make use of the role attribute:

  1. @Component({
  2. selector: 'ngc2-notification-button',
  3. template: `
  4. <span>{{label}}</span>
  5. `,
  6. styles: [`
  7. :host {
  8. display: flex;
  9. width: 80px;
  10. height: 80px;
  11. justify-content: center;
  12. align-items: center;
  13. background-color: yellow;
  14. border-radius: 40px;
  15. }
  16. :host:hover {
  17. cursor: pointer;
  18. }
  19. `]
  20. })
  21. export class NotificationButtonComponent {
  22. @Input()
  23. message = 'Alert!';
  24. @Input()
  25. label = 'Notify';
  26. constructor(private notification: NotificationService) { }
  27. @HostListener('click', [])
  28. notify() {
  29. this.notification.notify(this.message)
  30. }
  31. }

View Example

This lets you create a component that has the same semantics as a button element to screen readers and browsers, but now have the opportunity to fully control the styling of that component as well as inject your own custom logic.

ARIA attributes

Some native HTML tags have attributes that providers extra context on what's being displayed on the browser. For example, the img tag's alt attribute lets the reader know what is being shown using a short description.

However, native tags don't cover all cases. This is where ARIA fits in. ARIA attributes can provide context on what roles specific elements have in the application or on how elements within the document relate to each other.

One example of this is modals. Native modals provided by different platforms such as web browsers often have limited customization options, which can make for a poor experience. This necessitates the creation of custom modals.

A modal component can be given the role of dialog or alertdialog to let the browser know that that component is acting as a modal. The modal component template can use the ARIA attributes aria-labelledby and aria-described to describe to readers what the title and purpose of the modal is.

app.component.ts

  1. @Component({
  2. selector: 'ngc2-app',
  3. template: `
  4. <ngc2-notification-button
  5. message="Hello!"
  6. label="Greeting"
  7. role="button">
  8. </ngc2-notification-button>
  9. <ngc2-modal
  10. [title]="modal.title"
  11. [description]="modal.description"
  12. [visible]="modal.visible"
  13. (close)="modal.close()">
  14. </ngc2-modal>
  15. `
  16. })
  17. export class AppComponent {
  18. constructor(private modal: ModalService) { }
  19. }

notification-button.component.ts

  1. @Component({
  2. selector: 'ngc2-modal',
  3. template: `
  4. <div
  5. role="dialog"
  6. aria-labelledby="modal-title"
  7. aria-describedby="modal-description">
  8. <div id="modal-title">{{title}}</div>
  9. <p id="modal-description">{{description}}</p>
  10. <button (click)="close.emit()">OK</button>
  11. </div>
  12. `
  13. })
  14. export class ModalComponent {
  15. ...
  16. }

View Example

ARIA tags can enhance the accessibility of an application, but should by no means be the only accessibility consideration. More information is available in the WAI-ARIA specification.

原文: https://angular-2-training-book.rangle.io/handout/a11y/key-concerns/semantic-markup.html