index.mustache revision e2f644c80c2428330032af20658279d458938f20
402N/A<div class="intro">
402N/A <p>The Button component for YUI 3 is a light-weight, Y.Base wrapper around DOM elements you'd like treated like a button. This includes things like look &amp; feel, state management, and accessibility.</p>
402N/A</div>
402N/A
402N/A{{>getting-started}}
402N/A
402N/A<h2>Using Button</h2>
402N/A<h3>Quick Start</h3>
402N/A<p>There are two ways to create a Y.Button instance.</p>
402N/A
402N/A```
402N/A// Or, use an existing node
402N/Avar button = new Y.Button({
402N/A srcNode: "#myButton"
402N/A})
402N/A```
402N/A
402N/AYou can also create an unattached node and
402N/A```
402N/A// Dynamically create the node
2899N/Avar button = new Y.Button();
402N/Avar node = button.getNode();
814N/AY.one('#container').append(node);
814N/A```
814N/A
1780N/A<p>In either case, you will receive a Y.Button instance that contains a few attributes, and wraps around a Y.Node instance. The markup generated will look something similar to:</p>
814N/A
402N/A```
402N/A<button id="myButton" class="yui3-button" role="button"
402N/A aria-selected="false" aria-pressed="false">My Button</button>
402N/A```
402N/A
402N/A<p>At this point, you can modify `button`'s attributes, manipulate its state, or listen for change events.</p>
618N/A
402N/A<h3>Events</h3>
402N/A
402N/A<p>Most commonly with button elements, you want to listen for any clicks that occur. That can be achieved with something like this...</p>
844N/A
844N/A```
402N/Avar button = new Y.Button({
1407N/A srcNode: "#myButton"
402N/A});
2899N/A
2899N/Abutton.on('click', function(){
814N/A alert("Hello!");
814N/A});
402N/A```
402N/A
402N/A<p>You can also pass in groups of events at creation:</p>
402N/A
402N/A```
402N/Avar button = new Y.Button({
402N/A srcNode: '#myButton',
402N/A type: 'toggle',
402N/A on: {
402N/A click: function(){
402N/A Y.log('This will fire when the button is clicked.')
402N/A },
402N/A focus: function(){
402N/A Y.log('The button is now focused')
402N/A },
402N/A selectedChange: function(){
402N/A Y.log('This will fire when the toggled state changed')
402N/A }
402N/A }
402N/A})
402N/A```
402N/A
402N/A<p>Any event handlers can also be assigned by using the `on` property in the configuration object. Valid events include any <a href="">DOM events</a>, as well as Y.Button attribute change events (e.g. 'selectedChange').</p>
402N/A
402N/A<h3>Benefits</h3>
402N/A<p>So what do you get from Y.Button over just creating your own, via `new Y.Node.create('<button></button>')`</p>
402N/A<ul>
402N/A <li><strong>Accessibility</strong> - Your buttons automatically create and manage their own ARIA states. This includes `aria-selected` and `aria-pressed`. Y.Button instances also automatically get the `role='button'` attribute to properly identify their purpose (as a button) to screen readers even if they are not a `<button>` or `<input type="button">` element.</li>
402N/A<li><strong>State management</strong> - Y.Button instances automatically apply classes, such as yui3-button-selected, yui3-button-disabled, and yui3-button-focused, which are useful for styling purposes. Also, assigning a type of 'toggle' will fire a 'selectedChange' event only when it's state changes from selected to unselected, or vice-versa, eliminating the classical case of listening for 'click' and then checking to see if the state changed.</li>
402N/A</ul>
402N/A
402N/A<p><em>Note: In the future, Y.Button instances will be capable of being assigned to groups and managed via a new ButtonGroup module.</em></p>
402N/A
402N/A<h3>Styling</h3>
402N/A<p>YUI's Button component was designed with the idea in mind that you may only want button styles, no JS functionality. Instead of `use('button')`, you can just include the `cssbuttons` sub-module. This can be done either dynamically by `use('cssbuttons')` or statically with a <link> tag. Including this module will load in a stylesheet consisting of the following classes:</p>
402N/A<ul>
402N/A <li>yui3-button</li>
402N/A <li>yui3-button:hover</li>
402N/A <li>yui3-button:active</li>
402N/A <li>yui3-button-disabled</li>
402N/A</ul>
402N/A
402N/A<p>These styles are designed to progressively enhance. In legacy browsers, you'll get styles that appear a bit nicer than native buttons, and in modern browsers you'll get buttons using the latest styles that CSS3 has to offer.</p>
402N/A
402N/A
402N/A
459N/A
402N/A
402N/A<h3>Events</h3>
402N/A<table>
402N/A <thead>
402N/A <tr>
402N/A <th>Event</th>
402N/A <th>Description</th>
402N/A </tr>
402N/A </thead>
402N/A <tbody>
402N/A <tr>
402N/A <td>`selectedChange`</td>
402N/A <td>
402N/A
402N/A </td>
402N/A </tr>
402N/A <tr>
402N/A <td>`disabledChange`</td>
402N/A <td>
402N/A
402N/A </td>
402N/A </tr>
402N/A <tr>
402N/A <td>`typeChange`</td>
402N/A <td>
402N/A
402N/A </td>
402N/A </tr>
402N/A <tr>
402N/A <td>`labelChange`</td>
402N/A <td>
402N/A
402N/A </td>
402N/A </tr>
402N/A </tbody>
402N/A</table>
402N/A
402N/A
402N/A<h3>Configuration</h3>
402N/A<table>
402N/A <thead>
402N/A <tr>
402N/A <th>Event</th>
402N/A <th>Description</th>
402N/A </tr>
402N/A </thead>
402N/A <tbody>
402N/A <tr>
402N/A <td>`srcNode`</td>
402N/A <td>
402N/A The source node
402N/A </td>
402N/A </tr>
402N/A <tr>
402N/A <td>`label`</td>
402N/A <td>
402N/A The textual representation of the element
402N/A </td>
402N/A </tr>
402N/A <tr>
402N/A <td>`on`</td>
402N/A <td>
402N/A Any single event, or group of events
402N/A </td>
402N/A </tr>
402N/A <tr>
402N/A <td>`disabled`</td>
402N/A <td>
402N/A Whether or not the button should be disabled by default
402N/A </td>
402N/A </tr>
402N/A <tr>
402N/A <td>`selected`</td>
402N/A <td>
402N/A Whether or not the button should be selected by default
402N/A </td>
402N/A </tr>
402N/A </tbody>
402N/A</table>