Welcome to Atalasoft Community Sign in | Help

Converting YUI ButtonGroups into Image Buttons

ButtonGroups in YUI are fairly quick to implement, have a nice look, and act the way most people expect.  I’ve been surprised to find out that there are no options available to use images in place of the text on the Buttons inside the ButtonGroup.

In our most recent AJAX Photo Viewer Demo, I decided to use a ButtonGroup for the main mouse tool controls, and Bill Bither suggested that I use icons instead of text.

I didn’t want to throw away what I’d done so far, since the demo was pretty much done at that point.  I poked around YUI’s documentation a bit and came up with simple function that can convert a YUI Button into an image only button, much like a button on a tool bar.  These converted YUI Buttons still maintain the same original properties internally, so you can use them interchangeably.

Here’s a quick demonstration:

Here’s what you need:
YUI Dependencies:

<!-- Combo-handled YUI CSS files: -->  
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?2.6.0/build/button/assets/skins/sam/button.css" />
<!-- Combo-handled YUI JS files: -->
<script type="text/javascript" src="http://yui.yahooapis.com/combo?2.6.0/build/yahoo-dom-event/yahoo-dom-event.js&2.6.0/build/element/element-beta-min.js&2.6.0/build/button/button-min.js"></script>

CSS:

/* Our new icon class */
.icon
{
	background-repeat: no-repeat;  
	background-position: 2px 2px;
	cursor: pointer;
	overflow: hidden;
	height: 28px;
	width: 28px;
}

HTML:

<div id="mainContainer">
	<!-- This is where the button bar will be created -->
</div>
<div style="padding-top: 10px;">
	<input type="button" value="Convert to Image Buttons" onclick="return onConvertClick();" />
</div>

Java Script:

var _mainButtonGroup = null;
// An array of arrays containing the label/value of each button, and its corresponding tooltip
// The label of each of these buttons will be used as the base filename for the icon.
var _mainButtonData = [
	['Pan', 'Left click and drag to pan the image.'],
	['Center', 'Left click anywhere on the image to re-center the control\'s position.'],
	['Selection', 'Click and drag to make a rectangular selection. Click outside the selection to remove it.'],
	['Ellipse', 'Click and drag to make a rectangular selection. An ellipse will be rendered inside the selected area.'],
	['Rectangle', 'Click and drag to make a rectangular selection. A rectangle will be rendered inside the selected area.'],
	['Text', 'Click and drag to make a rectangular selection. Sample text will be rendered inside the selected area.'],
	['Image', 'Click and drag to make a rectangular selection. An image will be rendered inside the selected area.'],
	['Zoom', 'Left click anywhere on the image to zoom in, right click to zoom out.'],
	['ZoomSelection', 'Click and drag to make a rectangular selection. The area you select will be zoomed into.']
]
		
// Create the button group when the page is ready
YAHOO.util.Event.onDOMReady(onPageLoad);
function onPageLoad(){
	createButtonGroup();
}
		
function createButtonGroup(){
	// Create new YUI button group on the 'mainContainer' div
	_mainButtonGroup = new YAHOO.widget.ButtonGroup({id:'mainButtonGroup', container: 'mainContainer'});
	// Add some buttons to the group
	for (var i = 0; i < _mainButtonData.length; i++){
		_mainButtonGroup.addButton({label: _mainButtonData[i][0], value: _mainButtonData[i][0] });
	}
			
	// Render the new button group
	_mainButtonGroup.render();
}
		
// Simple demo function that runs the converter function
function onConvertClick(){
	var buttons = _mainButtonGroup.getButtons();
			
	// Loop through the Button array and convert each button
	for (var i = 0; i < buttons.length; i++){
		convertButtonToImageButton(buttons[i], _mainButtonData[i][1]);
	}
}
// Main converter
function convertButtonToImageButton(button, tooltip){
	// We use the value of the the button for the icon name
	var name = button.get('value');
	// Use the icon as a background image
	// It's entirely possible for you to use the CSS sprite method here, by 
	// incrementing the background offset rather than using different names
	button._button.parentNode.style.backgroundImage = 'url(icons/' + name + '.png)';
	// Add our new icon class to the existing classes
	button._button.parentNode.className += ' icon';
	// Add tooltip to the parent node
	button._button.parentNode.title = tooltip;
	// Hide the text of the button
	button._button.style.visibility = 'hidden';
}
Published Wednesday, January 21, 2009 5:28 PM by David Cilley
Filed under: ,

Comments

# Yahoo! User Interface Blog &raquo; In the Wild for January 26, 2009

Anonymous comments are disabled