MMenu, short for Mobile Menu, is a JavaScript plugin for creating an app look-alike off-canvas (sidebar) menu fully integrated with J1 Template. The plugin turns a basic HTML menu structure into a fully functional sidebar menu with sliding submenus.
In the J1 Template, MMenu is the engine behind all mobile menus and drawers: the main navigation on small screens, the table of contents (TOC) drawer, and the optional sidebar drawer. You do not need to write any JavaScript to use these menus. All menus are created automatically by the J1 Navigator module and are configured by simple settings in a YAML configuration file.
| J1 Template use the free variant MMenu Light of version v3.1.1. |
10-30 Minutes to read
Getting started
The plugin MMenu knows its way around a bit more complex HTML structures as well, so you probably don’t need to change any markup if you’re migrating from another script or plugin. All the plugin needs is a simple HTML list structure built from <ul />, <li /> and <a /> (or <span />) elements.
After the plugin is invoked, there are two add-ons available for the menu: navigation and offcanvas.
navigation()-
Creates the navigation with a fixed navbar and sliding submenus from
<nav />,<ul />,<li />and<a />(or<span />) elements. Submenus slide in from the right when a parent item is tapped, and a back button in the navbar brings the user back to the parent panel. offcanvas()-
Creates an off-canvas drawer. This drawer can be populated with any kind of content. A drawer slides in from the left or right side of the screen and closes when the user taps the darkened area (the backdrop) next to it.
| Both add-ons |
How MMenu is used in J1 Template
Good news first: you do not need to write any JavaScript code to use mobile menus in the J1 Template. The template ships with a ready-to-use integration, and all menus are configured by simple settings in a YAML configuration file.
The integration consists of three building blocks:
- The J1 adapter (
mmenu.js) -
A small piece of JavaScript, called an adapter, that connects the MMenu Light plugin with the J1 Template. The adapter reads your YAML settings, loads the menu content, and creates all configured menu instances automatically when a page is loaded.
- The YAML configuration (
navigator.yml) -
All mobile menus are part of the J1 Navigator module. Their settings live in the file
_data/modules/navigator.ymlunder the keynav_mmenu. Here you enable or disable menus, set the drawer position, the menu title, the color theme, and more. - The HTML data files
-
The content of each menu (the link structure) is not part of the page itself. It is stored in separate HTML data files, for example
/assets/data/mmenu/index.htmlfor the main navigation. The adapter loads these files in the background (via AJAX) when a page is opened.
Menu types
The J1 integration knows two types of mobile menus. The type is set for each menu instance by the config key content.type:
navigation-
A full mobile navigation menu with sliding submenus. The J1 Template uses this type for the main site navigation on small screens. It is opened and closed by the hamburger button in the navigation bar.
drawer-
A simple content drawer without submenus. The J1 Template uses this type for the table of contents (TOC) drawer and the optional sidebar drawer. A drawer can hold any HTML content, for example the TOC of the current page or info boxes about the site.
Using the plugin standalone
If you want to use MMenu Light outside of the J1 integration, for example on a custom page, the plugin can be invoked manually with a few lines of JavaScript:
<script>
document.addEventListener(
"DOMContentLoaded", () => {
const menu = new MmenuLight(
document.querySelector( "#my-menu" ),
"(max-width: 600px)"
);
const navigator = menu.navigation({
// options
});
const drawer = menu.offcanvas({
// options
});
}
);
</script>The first argument passed to MmenuLight is the HTML element that should be turned into a mobile menu. The second argument is a media query string: the menu is only active as long as the media query matches, for example on screens with a width of 600 pixels or less.
Options
The mmenu light plugin and both the navigation and offcanvas add-ons provide a set of options for customizing your menu. An option is a simple key/value pair. The default values can be overridden by passing new values to the plugin or to the add-on methods.
In the J1 Template, you normally do not pass these options in JavaScript yourself. Instead, you set them in the YAML configuration file navigator.yml, and the J1 adapter passes them to the plugin for you. See the section J1 YAML configuration below for all available settings.
<script>
document.addEventListener(
"DOMContentLoaded", () => {
const menu = new MmenuLight(
document.querySelector( "#my-menu" ),
"(max-width: 600px)"
);
const navigator = menu.navigation({
// options
});
const drawer = menu.offcanvas({
// options
});
}
);
</script>MMenu Plugin
The plugin itself accepts two options. Both are passed when a new MmenuLight instance is created.
| Option | Datatype | Default | Description |
|---|---|---|---|
| HTMLElement |
| The HTML element you want to turn into an off-canvas menu. .Example |
| String |
| MediaQuery for when the menu should be enabled. The default value |
Navigation add-on
The navigation add-on turns the HTML list structure into a navigation menu with a fixed navbar and sliding submenus.
| Option | Datatype | Default | Description |
|---|---|---|---|
| String |
| Classname to use for finding the selected |
| Boolean |
| Whether or not to use sliding submenus (forced to be |
| String |
| The title above the main panel. For subpanels the text in its parent menu item is used. |
| String |
| Color scheme to use. Possible values: |
Offcanvas add-on
The offcanvas add-on places the menu in a drawer that slides in from the side of the screen.
| Option | Datatype | Default | Description |
|---|---|---|---|
| String |
| The position for the menu. Possible values: |
J1 YAML configuration
In the J1 Template, all mobile menu settings are part of the Navigator module. The configuration is split into two files:
- Default settings
-
_data/modules/defaults/navigator.yml— the factory defaults shipped with the template. Do not change this file; it documents all available options and their default values. - User settings
-
_data/modules/navigator.yml— your personal settings. Every value you set here overrides the corresponding default value.
All mobile menu settings live under the key nav_mmenu.
Default settings (plugin and add-ons)
The default settings map directly to the plugin and add-on options described above. They apply to all menu instances.
nav_mmenu:
enabled: false
nav_main_menu: navigator_nav_menu
nav_quicklinks: quicklinks
mmenu_plugin:
node: "null"
mediaQuery: all
max_width: 100000
mmenu_navigator:
selected: Selected
slidingSubmenus: true
title: Navigation
theme: dark
mmenu_drawer:
position: right| Setting | Datatype | Default | Description |
|---|---|---|---|
| Boolean |
| Enables or disables the mobile menus of the Navigator module as a whole. Set this key to |
| String |
| The |
| String |
| The |
| String |
| Maps to the plugin option |
| String |
| Maps to the plugin option |
| Number |
| Maximum screen width (in pixels) for which the menus are created. The very large default value practically means no limit. The adapter builds the media query string |
| String |
| Maps to the navigation add-on option |
| Boolean |
| Maps to the navigation add-on option |
| String |
| Maps to the navigation add-on option |
| String |
| Maps to the navigation add-on option |
| String |
| Maps to the offcanvas add-on option |
Instance settings (menus)
Each mobile menu is configured as one entry in the list menus of your user settings. The J1 Template ships with three preconfigured instances: the main navigation, the TOC drawer, and the (disabled) sidebar drawer.
nav_mmenu:
enabled: true
menus:
# NAVIGATION Main
- menu:
enabled: true
xhr_container_id: navigator_nav_mmenu
xhr_data_element: menu_mmenu
xhr_data_path: /assets/data/mmenu/index.html
drawer:
position: right
content:
id: menu_mmenu
type: navigation
title: Navigation
theme: dark
button: "#mmenu-button"
button_activated: always
# DRAWER TOC
- menu:
enabled: true
xhr_container_id: navigator_toc_mmenu
xhr_data_element: toc_mmenu
xhr_data_path: /assets/data/mmenu_toc/index.html
drawer:
position: right
content:
id: toc_mmenu
type: drawer
title: Table of Contents
theme: dark
button: "#open_mmenu_toc"
button_activated: "js-toc-content"| Setting | Datatype | Default | Description |
|---|---|---|---|
| Boolean |
| Enables or disables this menu instance. |
| String | none | The |
| String | none | The |
| String | none | The path to the HTML data file that provides the menu content, for example |
| String |
| The side of the screen from which this drawer slides in. Possible values: |
| String | none | The |
| String | none | The type of the menu instance. Possible values: |
| String | none | The title of the menu shown in the navbar of the panel. |
| String |
| Color scheme for the menu. Possible values: |
| String | none | A CSS selector for the button (or element) that opens this menu, for example |
| String |
| Controls when the open button is active. The value |
Styling (CSS)
It’s pretty easy to change the styling for the mmenu light plugin: just override some of the CSS values and variables. A CSS variable (also called a custom property) is a named value, like --mm-ocd-width, that is used at several places inside the plugin styles. If you change the variable, all styles that use it change automatically.
.mm {
background: #ffe;
color: #330;
--mm-spn-item-height: 46px;
--mm-ocd-max-width: 500px;
}| In the J1 Template, the base styles of the plugin are provided by the file |
Navigation add-on
The navigation panels use the CSS class prefix mm-spn (sliding panel navigation). Colors are set by simple CSS values, sizes by CSS variables.
| Value | Datatype | Default | Description |
|---|---|---|---|
| Color |
| Background-color for the menu. The default value applies to the |
| Color |
| Color for the text and borders in the menu. The default value applies to the |
| Variables | Datatype | Default | Description |
|---|---|---|---|
| CSS value |
| Height for menu items. |
| CSS value |
| Indent for menu items. |
| CSS value |
| Line-height for text in the menu. |
Offcanvas add-on
The off-canvas drawer uses the CSS class prefix mm-ocd (off-canvas drawer). Its position and size are controlled by CSS values and variables.
| Value | Datatype | Default | Description |
|---|---|---|---|
| CSS value |
| Position relative to the top of the page. The J1 Uno theme sets the value to |
| CSS value |
| Position relative to the bottom of the page. |
| Variables | Datatype | Default | Description |
|---|---|---|---|
| CSS value |
| Width for the menu. |
| CSS value |
| Minimal width for the menu. |
| CSS value |
| Maximum width for the menu. |
J1 theme customizations
The J1 Uno theme file mmenu.css adds a set of overrides to adapt the plugin styles to the look of the template. The most important ones are:
- Dark drawer colors
-
The drawer content (
.mm-ocd__content) and the navigation panels (.mm-spn—dark) use a black background and light gray text to match the dark J1 mobile navigation. - Menu title
-
The menu title in the navbar is aligned to the left and shown in a larger font size (
x-large). - Hover highlighting
-
Menu items are highlighted with a dark gray background (
#424242) when the user moves the pointer over them. - Navbar offset
-
On small screens (below
992px), the drawer gets a top offset of48px, so it opens below the fixed navigation bar of the template. - Sidebar widgets
-
The class
mm-sideand its child classes (widget,heading,link) style the content boxes of the sidebar and TOC drawers.
API
After invoking the add-ons, an API is available for each add-on. With this API, you can invoke the add-on methods to control the plugin manually, for example to open a drawer when a button is clicked.
<script>
document.addEventListener(
"DOMContentLoaded", () => {
const menu = new MmenuLight(
document.querySelector( "#my-menu" )
);
const navigator = menu.navigation();
const drawer = menu.offcanvas();
navigator.openPanel(
document.querySelector( "#my-ul" )
);
document.querySelector( 'a[href="#my-menu"]' )
.addEventListener( 'click', ( evnt ) => {
evnt.preventDefault();
drawer.open();
});
}
);
</script>Navigation add-on
The API of the navigation add-on provides one method to control the panels of the menu.
openPanel
Invoke this method to open a panel in the menu. A panel is one level of the menu, technically a <ul /> element. Opening a panel of a submenu slides that submenu into view.
| Arguments | Datatype | Default | Description |
|---|---|---|---|
| HTMLElement |
| Panel (UL) to open. |
Off-Canvas add-on
Creates an off-canvas drawer. This drawer can be populated with any kind of content. The API of the offcanvas add-on provides two methods to open and close the drawer.
| Method | Datatype | Default | Description |
|---|---|---|---|
| NA | NA | Invoke this method to open the menu. Example |
| NA | NA | Invoke this method to close the menu. Example |
J1 adapter API
The J1 adapter j1.adapter.mmenu wraps the plugin API and manages all configured menu instances for you. Normally you do not call the adapter methods yourself; they are invoked automatically by the J1 core at page load. The methods are documented here for completeness and for troubleshooting.
| Method | Arguments | Returns | Description |
|---|---|---|---|
|
| NA | The adapter initializer. Reads the module options from the YAML configuration, waits until the J1 core has finished and the page content is visible, and then starts the menu loader |
|
| NA | Loads the HTML content of all enabled menu instances from their configured HTML data files ( |
|
| NA | Creates an |
|
| Boolean | Receives and processes messages sent by other J1 modules, for example the notification |
|
| NA | Sets the current processing state of the module. Used states are: |
| NA | String | Returns the current processing state of the module. |
| You can check the state of the mobile menu module at any time from the browser console by calling |