Building a triangle toggler in YUI

by Dave Dash 25Feb08

If someone knows the more common name for triangle-toggle menu's similar to this. Let me know.

There is a widget where an element toggles the display of a secondary set of elements. The toggle shows an arrow pointing down or right depending on the visibility of the elements. I wanted to build that in YUI.

The problem is two-fold:

  1. Build a triangle that toggles from right to down.
  2. Show and hide content.

However, with some clever CSS we can do this all in a single class change on an element.

Build that triangle

There's a number of ways to show or hide this triangle. Because it is flexible, I'm going to opt for using a background image. Also to save on HTTP requests, I'll use a sprite-d image. This may get merged with other icons on the system.

The particular site I'm working on uses a black background with white text, therefore a white triangle toggle seems appropriate.

sprite.png

This is what I use. I leave a little space, because the background will only clip horizontally based on the size of the element.

Add the HTML/CSS

The following HTML:

and the following CSS:

Provide the two states we require for the triangle. If you are new to sprites, rather than changing the background image entirely we just shift the background image up or down appropriately to show a new background.

The Javascript

The javascript is rather simple, but we put in some magical tricks here and there.

If you're unfamiliar with this style of Javascript, here's what's going on. Everything is done in the MA namespace as to not conflict with other javascript.

The toggler is fairly generic and expects a similar HTML structure for any toggle-able element. That means this code only needs to be written once, and anytime we use the toggle class, toggling in this fashion will occur.

We only run the init function. init says when the DOM is available then run the setup function. setup adds an event handler to doc4. doc4 just happens to be the id we use on our body tag.

Note that we're listening for clicks everywhere. This means we have to define only one event handler, regardless of how many toggle-able items there are. The event handler checks to make sure we clicked on a relevant element and then applies the toggle function to the grandparent element (switching the class from off to on as appropriate).

Note that this style of event handling means you need to carefully apply your class names. Also note, that this code could be optimized a little bit more, I haven't put this code into production, and therefore haven't optimized it for the YUI compressor.

Try this out if you want, I'm sure it can be trimmed down overtime. If you have trouble with it, let me know.

-d


Where am I?

This is a single entry in the weblog.

"Building a triangle toggler in YUI" is filed under spindrop. It was published in February 2008.

February 2008
M T W T F S S
« Jan   Mar »
 123
45678910
11121314151617
18192021222324
2526272829  

Tags

&& &&

need more help

If you found our tutorials and articles to be useful, but are still looking for more hands on help, consider hiring us. Find out more about how Spindrop can help you.

 

5 Responses to “Building a triangle toggler in YUI”


  1. 1 Dan Nelson Posted March 14th, 2008 - 12:57 pm

    I’ve always seen it called a “twistie”:

    http://www-03.ibm.com/support/techdocs/atsmastr.nsf/Web/TwHelp

  2. 2 David dB Posted March 16th, 2008 - 4:06 pm

    It’s called a disclosure triangle on Mac OS X (and 9, 8, 7).

  3. 3 dolsen Posted March 21st, 2008 - 1:26 pm

    In your example you have `#hide_me`. However, if I have multiple toggle-able items on my page I would have multiple items with `id=”hide_me”`. This is bad. Is there something I am not understanding?

  4. 4 Dave Dash Posted March 21st, 2008 - 2:15 pm

    dolsen,

    Good catch,

    I’ve changed my code examples to show that it can be class="hide_me". The id is irrelevant for the most part.

  5. 5 Michael Posted October 10th, 2009 - 3:09 pm

    Well done!

    I was trying to do this with YUI 3 but could not find anything to go on… so I dropped back to YUI2 and your code has helped a great deal.

    I am using this with the module container on the YUI site just to reveal an optional paragraph on my page. So much code for such a simple function.

    Thanks again.

Further Help

If you require more hands on assistance, we do offer affordable hands on support.

Leave a Reply


Comment guidelines: No spamming, no profanity, and no flaming. Inappropriate comments will be deleted outright.