From 971eba1355142c26a2ad6317217ac9a4c10e484e Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Thu, 9 Apr 2020 18:37:36 +0200 Subject: [PATCH] Move burger menu file to its own file --- app/javascript/burger_menu.js | 17 ++++++++++++++++ app/javascript/packs/application.js | 30 ++--------------------------- 2 files changed, 19 insertions(+), 28 deletions(-) create mode 100644 app/javascript/burger_menu.js diff --git a/app/javascript/burger_menu.js b/app/javascript/burger_menu.js new file mode 100644 index 0000000..a37539d --- /dev/null +++ b/app/javascript/burger_menu.js @@ -0,0 +1,17 @@ +document.addEventListener('DOMContentLoaded', () => { + const $navbarBurgers = document.querySelectorAll('.navbar-burger'); + // Check if there are any navbar burgers + if ($navbarBurgers.length > 0) { + // Add a click event on each of them + $navbarBurgers.forEach(el => { + el.addEventListener('click', () => { + // Get the target from the "data-target" attribute + const target = el.dataset.target; + const $target = document.getElementById(target); + // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu" + el.classList.toggle('is-active'); + $target.classList.toggle('is-active'); + }); + }); + } +}); diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index 472e587..80561d9 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -8,37 +8,11 @@ require("turbolinks").start() require("@rails/activestorage").start() require("channels") +require('burger_menu'); // Uncomment to copy all static images under ../images to the output folder and reference // them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>) // or the `imagePath` JavaScript helper below. // // const images = require.context('../images', true) -// const imagePath = (name) => images(name, true) - - -document.addEventListener('DOMContentLoaded', () => { - - // Get all "navbar-burger" elements - const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0); - - // Check if there are any navbar burgers - if ($navbarBurgers.length > 0) { - - // Add a click event on each of them - $navbarBurgers.forEach(el => { - el.addEventListener('click', () => { - - // Get the target from the "data-target" attribute - const target = el.dataset.target; - const $target = document.getElementById(target); - - // Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu" - el.classList.toggle('is-active'); - $target.classList.toggle('is-active'); - - }); - }); - } - - }); \ No newline at end of file +// const imagePath = (name) => images(name, true) \ No newline at end of file