Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Modify User Provided UI with Angular Content Directives

    Isaac MannIsaac Mann
    angularAngular
    5 - 6

    If we’re going to make our toggle accessible, we’ll need to apply certain aria attributes to the control that does the toggling. Instead of making the parent component handle that, we can provide a toggler directive that automatically applies the needed attributes. Content directives allow the user to tell the UI component where to make those modifications.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Here is a toggle component that uses an ng template tag to render its UI. Now the parent component is providing two different user interfaces, one with the switch, and one with this button. You see both of them work just as expected, and they are wired up to the same state.

    We want to make these a little bit more accessible. We're going to add some accessibility features. Give them both a role of switch and make them aria-checked when the ON state is true.

    Now, the problem is that no matter how much you put this in the documentation. Most developers will not remember to put these kinds of tags on the user interface elements.

    We want to do is bundle up all of this common functionality into one toggler directive. Then whenever developer makes their own custom user interface, they just drop this toggler directive on it, and the toggler directive will take care of adding that role attribute and the aria-checked attribute.

    Let's make that directive. We'll set the selector to toggler. First, we'll use a host binding decorator to set the role attribute to switch. Next, we need to have an ON input, so we can know when the toggle is ON or OFF.

    We'll simply add another host binding decorator on that same ON attribute, and we'll bind it to the aria-checked attribute. Now whenever ON is true, aria-checked will be set to true. Whenever ON is false, aria-checked will be set to false.

    Let's go back to the app component here. The only thing we need to change is we need to set this ON attribute on the button for this toggler here. This toggler up here will listen to this ON input that was already there for their switch.

    Now if inspect this button, we see that the role is switch. If we toggle it, we have aria-checked true, and we toggle that OFF, aria-checked false. This toggler directive is doing what is supposed to do.