Responsive menu para Drupal con Sidr

  • 3 Abr 2015
  • Drupal 7

En un sitio con diseño adaptable (responsive web design) el menú es uno de los elementos más importantes a tener en cuenta. A diferencia de simples bloques que mostramos u ocultamos según las dimensiones del dispositivo del cliente, el menú se debe adpatar en forma, color, posición y funcionabilidad. Este artículo trata de cubrir la mayor parte de los requerimientos para la implementación del menú adaptable Sird en Drupal 7 con un subtheme de Bootstrap.

Objetivos

Hacer que el menú se convierta en un icono para abrir y cerrar el menú

Hacer que el menú Sird se cierre cuando se supera un determinado ancho

Instalar módulos y librerías

Instalar el módulo responsive menus como cualquier otro módulo de Drupal. Descargar, descomprimir e instalar. Luego, descargar el plugin de jQuery Sidr y descomprimirla de modo que el módulo pueda acceder al archivo en sites/all/libraries/sidr/jquery.sidr.min.js

Configurar responsive menu

Ir a admin/config/user-interface/responsive_menus y configurar los siguientes parámetros:

Responsive menu style

Sidr

CSS selectors for which menu to responsify

#responsive-menu

Text or HTML for trigger button

<button type="button" class="navbar-toggle main-navbar">
    <span class="sr-only">Toggle navigation</span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
    <span class="icon-bar"></span>
  </button>

Screen width to respond to

1200

onOpen callback (function)

disable_scroll()

onClose callback (function)

enable_scroll()

Cerrar con JavaScript el menu

Agregar el siguiente código al archivo js que el theme carga para todas las páginas del sitio

$(window).resize(function() {
    if ($(this).width() > 940) {
      $.sidr('close', 'sidr-0');
    }
  });
  
  // left: 37, up: 38, right: 39, down: 40, spacebar: 32, pageup: 33, pagedown: 34, end: 35, home: 36
  var keys = [37, 38, 39, 40];

  function preventDefault(e) {
    e = e || window.event;
    if (e.preventDefault)
      e.preventDefault();
    e.returnValue = false;  
  }

  function keydown(e) {
    for (var i = keys.length; i--;) {
      if (e.keyCode === keys[i]) {
        preventDefault(e);
        return;
      }
    }
  }

  function wheel(e) {
    preventDefault(e);
  }
  
  window.disable_scroll = function () {
    if (window.addEventListener) {
        window.addEventListener('DOMMouseScroll', wheel, false);
    }
    window.onmousewheel = document.onmousewheel = wheel;
    document.onkeydown = keydown;
  }
  
  window.enable_scroll = function () {
    if (window.removeEventListener) {
        window.removeEventListener('DOMMouseScroll', wheel, false);
    }
    window.onmousewheel = document.onmousewheel = document.onkeydown = null;  
  }

Personalizar la hoja de estilos de Sird

Crear un archivo en themes/nombre_del_subtheme/less/sidr.less

// Fonts
@sidr-font-family: 'lucida grande',tahoma,verdana,arial,sans-serif;
@sidr-font-size: 12px;
// Colors
@sidr-background : #333;
@sidr-background-shadow-color : #222;
@sidr-text-color: #FFF;



.sidr {
  /* Default Settings */
  display: none;
  position: absolute;
  position: fixed;
  top: 0;
  height: 100%;
  z-index: 999999;
  width: 300px;
  overflow-x: none;
  overflow-y: auto;

  .sidr-inner {
    padding: 0 0 15px;

    > p {
      margin-left: 15px;
      margin-right: 15px;
    }
  }

  &.right {
    left: auto;
    right: -260px;
  }

  &.left {
    left: -260px;
    right: auto;
  }

  /* Theme Settings */
  font-family: @sidr-font-family;
  font-size: @sidr-font-size;
  background: @sidr-background;
  color: @sidr-text-color;
  box-shadow: 0 0 5px 5px @sidr-background-shadow-color;
  
/*

    The horizontal offset of the shadow, positive means the shadow will be on the right of the box, a negative offset will put the shadow on the left of the box.
    The vertical offset of the shadow, a negative one means the box-shadow will be above the box, a positive one means the shadow will be below the box.
    The blur radius (optional), if set to 0 the shadow will be sharp, the higher the number, the more blurred it will be.
    The spread radius (optional), positive values increase the size of the shadow, negative values decrease the size. Default is 0 (the shadow is same size as blur).
    Color
*/
  
  //@include single-box-shadow(@sidr-background-shadow-color, 0, 0, 5px, 5px, inset);

  h1, h2, h3, h4, h5, h6 {
    font-size: @sidr-font-size - 4;
    font-weight: normal;
    padding: 0 15px;
    margin: 0 0 5px;
    color: @sidr-text-color;
    line-height: 24px;
    //@include background-image(linear-gradient(lighten(@sidr-background, 10%), darken(@sidr-background, 10%)));
    //@include single-box-shadow(rgba(#000, .2), 0, 5px, 5px, 3px);
  }

  p {
    margin: 0 0 12px;
    a {

    }
  }

  > p {
    margin-left: 15px;
    margin-right: 15px;
  }

  ul {
    display: block;
    margin: 0 0 15px;
    padding: 0;

    li {
      display: block;
      margin: 0;
      padding: 0 6px;
      
      &:hover,
      &.active,
      &.sidr-class-active {

      }

      a, span {
        padding: 15px;
        display: block;
        text-decoration: none;
        text-transform: uppercase;
        color: #fff;
        border-bottom: 1px solid #666;
        
      }

      ul {
      }
    }
  }

  form {
    margin: 0 15px;
  }

  label {
    //font-size: @sidr-font-size - 2;
  }

  input[type="text"],
  input[type="password"],
  input[type="date"],
  input[type="datetime"],
  input[type="email"],
  input[type="number"],
  input[type="search"],
  input[type="tel"],
  input[type="time"],
  input[type="url"],
  textarea, select {
    width: 100%;
    //font-size: @sidr-font-size - 2;
    padding: 5px;
    //@include box-sizing(border-box);
    margin: 0 0 10px;
    //@include border-radius(2px);
    border: none;
    //background: rgba(#000, .1);
    //color: rgba(@sidr-text-color, .6);
    display: block;
    clear: both;
  }

  input[type=checkbox] {
    width: auto;
    display: inline;
    clear: none;
  }

  input[type=button],
  input[type=submit] {
    color: @sidr-background;
    background: @sidr-text-color;

    &:hover {
      //background: rgba(@sidr-text-color, .9);
    }
  }
  
  .sidr-class-element-invisible,
  .sidr-class-input-group-btn {
    display: none;
  }  
}

Ahora, es necesario incluir el nuevo archivo a los estilos del theme editando el archivo less/style.less

@import 'sidr.less';

Important! a los estilos de Bootstrap

Por último, es necesario "sobreescribir" algunos estilos del core de Bootstrap

.main-navbar {
  margin-right: 15px;
  .icon-bar {
    display: block !important;
  }
}