|
| 1 | +<script>{ |
| 2 | + "title": "Using the classes Option", |
| 3 | + "level": "advanced" |
| 4 | +}</script> |
| 5 | + |
| 6 | +As of the 1.12 release, the jQuery UI widget factory includes a means of managing CSS class names through the [`classes` option](http://api.jqueryui.com/jquery.widget/#option-classes). This article will give you an overview of how the `classes` option works, and discuss what you can do with it. |
| 7 | + |
| 8 | +## Syntax overview |
| 9 | + |
| 10 | +The `classes` option is used to map structural class names to theme-related class names that you define. To see what this means let's look at an example. The code below uses the `classes` option to create a red dialog: |
| 11 | + |
| 12 | +``` |
| 13 | +<style> |
| 14 | + .custom-red { background: red; } |
| 15 | +</style> |
| 16 | +<script> |
| 17 | + var dialog = $( "<div>Red</div>" ).dialog({ |
| 18 | + classes: { |
| 19 | + "ui-dialog": "custom-red" |
| 20 | + } |
| 21 | + }); |
| 22 | +</script> |
| 23 | +``` |
| 24 | + |
| 25 | +Here, the presentational `custom-red` class name is associated with the structural `ui-dialog` class name. Now, whenever the dialog applies the `ui-dialog` class name, it will also add a `custom-red` class name. However, something other than adding the `custom-red` class has happened here, which isn't immediately obvious. This code also *removes* the existing default value which was `"ui-corner-all"`. You can associate multiple class names by including multiple space-delimited class names in the object's value. For instance the following code creates a dialog that is red and still has rounded corners: |
| 26 | + |
| 27 | +``` |
| 28 | +<style> |
| 29 | + .custom-red { background: red; } |
| 30 | +</style> |
| 31 | +<script> |
| 32 | + var dialog = $( "<div>Big and red</div>" ).dialog({ |
| 33 | + classes: { |
| 34 | + "ui-dialog": "ui-corner-all custom-red" |
| 35 | + } |
| 36 | + }); |
| 37 | +</script> |
| 38 | +``` |
| 39 | + |
| 40 | +*Note: To get a full list of the class names you can use with the `classes` option, check the API documentation for the jQuery UI widget you're interested in. For example, here's the list of classes for the dialog width: <http://api.jqueryui.com/dialog/#theming>.* |
| 41 | + |
| 42 | +The `classes` option works like any other widget factory option, which means all the widget factory option mechanisms still apply. For instance, the following code uses the [`option()` method](http://api.jqueryui.com/jquery.widget/#method-option) to remove all class names currently associated with the `ui-dialog` class name: |
| 43 | + |
| 44 | +``` |
| 45 | +dialog.dialog( "option", "classes.ui-dialog", null ); |
| 46 | +``` |
| 47 | + |
| 48 | +And the following creates a [widget extension](http://learn.jquery.com/jquery-ui/widget-factory/extending-widgets/) that automatically associates the `custom-red` class with the `ui-dialog` class: |
| 49 | + |
| 50 | +``` |
| 51 | +<style> |
| 52 | + .custom-red { background: red; } |
| 53 | +</style> |
| 54 | +<script> |
| 55 | + $.widget( "custom.dialog", $.ui.dialog, { |
| 56 | + options: { |
| 57 | + classes: { |
| 58 | + "ui-dialog": "ui-corner-all custom-red custom-big" |
| 59 | + } |
| 60 | + } |
| 61 | + }); |
| 62 | + $( "<div>Big and red</div>" ).dialog(); |
| 63 | +</script> |
| 64 | +``` |
| 65 | + |
| 66 | +As an added benefit, the widget factory also removes any class names specified in the `classes` option when the widget is destroyed. |
| 67 | + |
| 68 | +## Theming |
| 69 | + |
| 70 | +As the previous examples show, the `classes` option provides a quick way to associate theme-related class names with the structural class names used within a widget. This approach works for simple cases, but it can also be used to adapt third-party themes to work with widget-factory-built widgets. For example, if you're using [Bootstrap](http://getbootstrap.com/) and jQuery UI together, you can use the following code to create a jQuery UI dialog that uses Bootstrap's theming: |
| 71 | + |
| 72 | +``` |
| 73 | +$.extend( $.ui.dialog.prototype.options.classes, { |
| 74 | + "ui-dialog": "modal-content", |
| 75 | + "ui-dialog-titlebar": "modal-header", |
| 76 | + "ui-dialog-title": "modal-title", |
| 77 | + "ui-dialog-titlebar-close": "close", |
| 78 | + "ui-dialog-content": "modal-body", |
| 79 | + "ui-dialog-buttonpane": "modal-footer" |
| 80 | +}); |
| 81 | +``` |
| 82 | + |
| 83 | +For more examples of this approach, check out [Alexander Schmitz's repo](https://github.com/arschmitz/jqueryui-bootstrap-adapter) that adapts jQuery UI to work with Boostrap using the `classes` option. |
| 84 | + |
| 85 | +## Conclusion |
| 86 | + |
| 87 | +The introduction of the `classes` option takes us one step further in the split between structural and theme-related classes, making it easier than ever to make jQuery UI widgets match the look and feel of your existing site. At the same time, this allows jQuery UI to be used alongside other CSS frameworks, just like jQuery can be used alongside other JavaScript frameworks. |
0 commit comments