Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you force Vue to show falsey attributes using v-bind?

<button class="navbar__dropdown-toggle" :aria-expanded="active"></button>

I am trying to bind aria-expanded to the state of a dropdown menu. Unfortunately, when using a screen reader (for the visually impaired), dictation doesn't indicate that it is a non-expanded dropdown menu since falsey attributes are removed.

Is there any way of forcing a falsey attribute to remain?

like image 373
matchai Avatar asked Jul 20 '17 02:07

matchai


2 Answers

Looks like from the W3C spec, the value of aria-expanded can be true or false. So in your template you can do this.

<button class="navbar__dropdown-toggle" :aria-expanded="active ? 'true' : 'false'">expanded</button>

Working example.

like image 137
Bert Avatar answered Sep 21 '22 12:09

Bert


Assuming your boolean has a value, I prefer using toString():

<button class="navbar__dropdown-toggle" :aria-expanded="active.toString()"></button>

It is short and the intent is perhaps clearer than the other examples.

like image 45
The sleeping wombat Avatar answered Sep 17 '22 12:09

The sleeping wombat