# How to use

The Toolbar plugin allows for several methods to register your button, using the `Prism.plugins.toolbar.registerButton` function.

The simplest method is through the HTML API. Add a `data-label` attribute to the `pre` element, and the Toolbar plugin will read the value of that attribute and append a label to the code snippet.

    <pre data-src="plugins/toolbar/prism-toolbar.js" data-label="Hello World!"></pre>

If you want to provide arbitrary HTML to the label, create a `template` element with the HTML you want in the label, and provide the `template` element's `id` to `data-label`. The Toolbar plugin will use the template's content for the button. You can also use to declare your event handlers inline:

    <pre data-src="plugins/toolbar/prism-toolbar.js" data-label="my-label-button"></pre>

    <template id="my-label-button"><button onclick="console.log('This is an inline-handler');">My button</button></template>

For more flexibility, the Toolbar exposes a JavaScript function that can be used to register new buttons or labels to the Toolbar, `Prism.plugins.toolbar.registerButton`.

The function accepts a key for the button and an object with a `text` property string and an optional `onClick` function or a `url` string. The `onClick` function will be called when the button is clicked, while the `url` property will be set to the anchor tag's `href`.

    Prism.plugins.toolbar.registerButton('hello-world', {
    	text: 'Hello World!', // required
    	onClick: function (env) { // optional
    		alert('This code snippet is written in ' + env.language + '.');
    	}
    });

See how the above code registers the `Hello World!` button? You can use this in your plugins to register your own buttons with the toolbar.

If you need more control, you can provide a function to `registerButton` that returns either a `span`, `a`, or `button` element.

    Prism.plugins.toolbar.registerButton('select-code', function(env) {
    	var button = document.createElement('button');
    	button.innerHTML = 'Select Code';

    	button.addEventListener('click', function () {
    		// Source: http://stackoverflow.com/a/11128179/2757940
    		if (document.body.createTextRange) { // ms
    			var range = document.body.createTextRange();
    			range.moveToElementText(env.element);
    			range.select();
    		} else if (window.getSelection) { // moz, opera, webkit
    			var selection = window.getSelection();
    			var range = document.createRange();
    			range.selectNodeContents(env.element);
    			selection.removeAllRanges();
    			selection.addRange(range);
    		}
    	});

    	return button;
    });

The above function creates the Select Code button you see, and when you click it, the code gets highlighted.

By default, the buttons will be added to the code snippet in the order they were registered. If more control over the order is needed, an HTML attribute can be added to the `body` tag with a comma-separated string indicating the order.

    <body data-toolbar-order="select-code,hello-world,label">