Dynamically transition through different content panes.
The Switcher component consists of a number of toggles and their related content items. Add the uk-switcher
attribute to a list element that contains the toggles. Add the .uk-switcher
class to the element containing the content items.
By default, the element with the .uk-switcher
class has to succeed the toggle directly in order for the switcher to function. If you need it to be nested in another element, for example when using a grid, add the connect: SELECTOR
option to the uk-switcher
attribute and select the element containing the items for switching.
Typically, the switcher toggles are styled through other components, some of which will be shown here.
<!-- This is the nav containing the toggling elements -->
<ul uk-switcher>
<li><a href="#"></a></li>
</ul>
<!-- This is the container of the content items -->
<div class="uk-switcher">
<div></div>
</div>
In this example we are using the Subnav component.
<ul class="uk-subnav uk-subnav-pill" uk-switcher>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
<div class="uk-switcher uk-margin">
<div>Hello!</div>
<div>Hello again!</div>
<div>Bazinga!</div>
</div>
In some cases you want to switch to another content section from within the active content. This is possible using the uk-switcher-item
attribute. To target the items, you need to set the attribute to the number of the respective content items.
Setting the attribute to next
and previous
will switch to the adjacent items.
<div class="uk-switcher uk-margin">
<div><a href="#" uk-switcher-item="0"></a></div>
<div><a href="#" uk-switcher-item="1"></a></div>
<div><a href="#" uk-switcher-item="next"></a></div>
<div><a href="#" uk-switcher-item="previous"></a></div>
</div>
<ul class="uk-subnav uk-subnav-pill" uk-switcher>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
<div class="uk-switcher uk-margin">
<div>Hello! <a href="#" uk-switcher-item="2">Switch to item 3</a></div>
<div>Hello again! <a href="#" uk-switcher-item="next">Next item</a></div>
<div>Bazinga! <a href="#" uk-switcher-item="previous">Previous item</a></div>
</div>
It is also possible to connect multiple content containers. Just add the connect
parameter to the uk-switcher
attribute and use a selector that applies to all items.
<!-- This is the nav containing the toggling elements -->
<ul uk-switcher="connect: .my-class">…</ul>
<!-- These are the containers of the content items -->
<div class="uk-switcher my-class">…</div>
<div class="uk-switcher my-class">…</div>
<ul class="uk-subnav uk-subnav-pill" uk-switcher="connect: .switcher-container">
<li><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
<h4>Container 1</h4>
<div class="uk-switcher switcher-container uk-margin">
<div>Hello!</div>
<div>Hello again!</div>
<div>Bazinga!</div>
</div>
<h4>Container 2</h4>
<div class="uk-switcher switcher-container uk-margin">
<div>Hello! The first item.</div>
<div>Hello again! The second item.</div>
<div>Bazinga! The third item.</div>
</div>
You can apply all animations from the Animation component to switcher items. To do so, add the animation
parameter with the relevant class to the uk-switcher
attribute.
<ul uk-switcher="animation: uk-animation-fade">…</ul>
<ul class="uk-subnav uk-subnav-pill" uk-switcher="animation: uk-animation-fade">
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
<div class="uk-switcher uk-margin">
<div>Hello!</div>
<div>Hello again!</div>
<div>Bazinga!</div>
</div>
You can also apply multiple animations from the Animation component. That way you can add different in and out animations.
<ul uk-switcher="animation: uk-animation-slide-left-medium, uk-animation-slide-right-medium">…</ul>
<ul class="uk-subnav uk-subnav-pill" uk-switcher="animation: uk-animation-slide-left-medium, uk-animation-slide-right-medium">
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
<div class="uk-switcher uk-margin">
<div>Hello!</div>
<div>Hello again!</div>
<div>Bazinga!</div>
</div>
The switcher is easily applied to the Subnav component.
<!-- This is the subnav containing the toggling elements -->
<ul class="uk-subnav uk-subnav-pill" uk-switcher>…</ul>
<!-- This is the container of the content items -->
<div class="uk-switcher"></div>
<ul class="uk-subnav uk-subnav-pill" uk-switcher>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
<div class="uk-switcher uk-margin">
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<div>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur, sed do eiusmod.</div>
</div>
As an exception to the rule, add the uk-tab
attribute instead of uk-switcher
to the tabbed navigation to combine the switcher with the Tab component.
<!-- This is the subnav containing the toggling elements -->
<ul uk-tab>…</ul>
<!-- This is the container of the content items -->
<ul class="uk-switcher">…</ul>
<ul uk-tab>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
<div class="uk-switcher uk-margin">
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<div>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur, sed do eiusmod.</div>
</div>
Use the Grid and Width components to display content correctly with a vertical tabbed navigation.
<div uk-grid>
<div class="uk-width-auto">
<ul class="uk-tab-left" uk-tab="connect: #my-id">…</ul>
</div>
<div class="uk-width-expand">
<div id="my-id" class="uk-switcher">…</div>
</div>
</div>
<div class="uk-child-width-1-2@s" uk-grid>
<div>
<div uk-grid>
<div class="uk-width-auto@m">
<ul class="uk-tab-left" uk-tab="connect: #component-tab-left; animation: uk-animation-fade">
<li><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div class="uk-width-expand@m">
<div id="component-tab-left" class="uk-switcher">
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<div>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur, sed do eiusmod.</div>
</div>
</div>
</div>
</div>
<div>
<div uk-grid>
<div class="uk-width-auto@m uk-flex-last@m">
<ul class="uk-tab-right" uk-tab="connect: #component-tab-right; animation: uk-animation-fade">
<li><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div class="uk-width-expand@m">
<div id="component-tab-right" class="uk-switcher">
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<div>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur, sed do eiusmod.</div>
</div>
</div>
</div>
</div>
</div>
The switcher can also be applied to buttons or button groups from the Button component. Just add the switcher attribute to a block around a group of buttons or to the element with the .uk-button-group
class.
<!-- This is a switcher using a number of buttons -->
<div uk-switcher="toggle: > *">
<button class="uk-button uk-button-default" type="button"></button>
<button class="uk-button uk-button-default" type="button"></button>
</div>
<div class="uk-switcher">…</div>
<div uk-switcher="animation: uk-animation-fade; toggle: > *">
<button class="uk-button uk-button-default" type="button">Item</button>
<button class="uk-button uk-button-default" type="button">Item</button>
<button class="uk-button uk-button-default" type="button">Item</button>
</div>
<div class="uk-switcher uk-margin">
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<div>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur, sed do eiusmod.</div>
</div>
Note Since this example does not wrap the buttons into list items the clickable elements which trigger the content switching has to be changed by using the toggle
option.
To apply the switcher to the Nav component, add the uk-switcher
attribute to the nav <ul>
element. Use the Grid and Width components to arrange nav and content in a grid layout.
<div uk-grid>
<div class="uk-width-small">
<ul class="uk-nav uk-nav-default" uk-switcher="connect: #my-id">…</ul>
</div>
<div class="uk-width-expand">
<div id="my-id" class="uk-switcher">…</div>
</div>
</div>
<div uk-grid>
<div class="uk-width-small@m">
<ul class="uk-nav uk-nav-default" uk-switcher="connect: #component-nav; animation: uk-animation-fade">
<li><a href="#">Active</a></li>
<li><a href="#">Item</a></li>
<li><a href="#">Item</a></li>
</ul>
</div>
<div class="uk-width-expand@m">
<div id="component-nav" class="uk-switcher">
<div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</div>
<div>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</div>
<div>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur, sed do eiusmod.</div>
</div>
</div>
</div>
Any of these options can be applied to the component attribute. Separate multiple options with a semicolon. Learn more
Option | Value | Default | Description |
---|---|---|---|
connect |
CSS selector | ~.uk-switcher |
Related items container. By default succeeding elements with class 'uk-switcher'. |
toggle |
CSS selector | > * > :first-child |
Select the clickable elements which trigger content switching. |
itemNav |
CSS selector | false |
Related nav container. By default, nav items are found in related items container only. |
active |
Number | 0 |
Active index on init. Providing a negative number indicates a position starting from the end of the set. |
animation |
String | false |
Space-separated names of animations. Comma-separated for animation out. |
duration |
Number | 200 |
The animation duration. |
swiping |
Boolean | true |
Use swiping. |
followFocus |
Boolean | false |
Selection follows focus automatically. |
connect
is the Primary option and its key may be omitted, if it's the only option in the attribute value.
<span uk-switcher=".switcher-container"></span>
Learn more about JavaScript components.
UIkit.switcher(element, options);
The following events will be triggered on the connected items of the elements with this component attached:
Name | Description |
---|---|
beforeshow |
Fires before an item is shown. Can prevent showing by calling preventDefault() on the event. |
show |
Fires after an item is shown. |
shown |
Fires after the item's show animation has been completed. |
beforehide |
Fires before an item is hidden. Can prevent hiding by calling preventDefault() on the event. |
hide |
Fires after an item's hide animation has started. |
hidden |
Fires after an item is hidden. |
The following methods are available for the component:
UIkit.switcher(element).show(index);
Shows the Switcher item with given index.
Name | Type | Default | Description |
---|---|---|---|
index |
String, Number, Node | 0 | Switcher item to show. 0 based index. |
The Switcher component adheres to the Tab WAI-ARIA design pattern and automatically sets the appropriate WAI-ARIA roles, states and properties.
tablist
role, and if it is a Nav component, the aria-orientation
state is set to vertical
. presentation
role. tab
role, the aria-selected
state and the aria-controls
property set to the ID of the respective content item. presentation
role.tabpanel
role and the aria-labelledby
property set to the ID of the respective toggle item.The toggle navigation can be accessed through the keyboard using the following keys.
By default, the Switcher component has the manual activation behavior. This means the corresponding content items will only be activated using the enter or spacekeys. To switch to automatic activation, set follow-focus
to true
. When navigating through toggle items, the corresponding content item will get active automatically.