Styling Buttons With Attributes

Permalink to "Styling Buttons With Attributes"

Now all that's missing is the button functionality. This component should allow the user to provide a single up or down vote rating and give visual feedback to the user. In this step you'll do some setup work:

Then in the following step you'll add the event listeners to handle button clicks and set the vote attribute.

Start by adding the following lines:

index.html

Permalink to "index.html"

In shadow DOM the :host selector refers to the node or custom element that the shadow root is attached to.

In this case, if the vote attribute is "up" (e.g. <rating-element vote="up">) it will turn the thumb-up button green.

If vote is "down" (e.g. <rating-element vote="down">), then it will turn the thumb-down button red.

Now, implement the logic for this by creating a reflecting property / attribute for vote similar to how you implemented rating. Start with the property setter and getter:

Initialize the _vote instance property with null as a class-member property, and in the setter, check if the new value is different. If so, adjust the rating accordingly and, importantly, reflect the vote attribute back to the host with this.setAttribute.

It is not recommended to manipulate rating inside the vote setter in this way.

This is not the most efficient way to update rating, but it's the most convenient way for this tutorial.

Handle "vote" Attribute Changes

Permalink to "Handle "vote" Attribute Changes"

Next, set up the attribute binding for vote:

This is the same process you went through with the rating attribute binding:

Check that this is working by setting the vote attribute to "up" in the browser dev tools console with $0.setAttribute('vote', 'up').