From 5ddd6c986a0da4c7d386c9b95593df14509d69ad Mon Sep 17 00:00:00 2001 From: thednp Date: Thu, 18 Jun 2020 17:20:22 +0000 Subject: [PATCH] Changes: * removed some dependency on `shorter-js` for events and class manipulation * code updates --- demo/assets/css/style.css | 5 ++ demo/src/js/navbar.min.js | 4 +- dist/js/navbar.esm.js | 98 +++++++++++++-------------------------- dist/js/navbar.esm.min.js | 4 +- dist/js/navbar.js | 98 +++++++++++++-------------------------- dist/js/navbar.min.js | 4 +- package.json | 10 ++-- src/js/init.js | 17 +++---- src/js/navbar.js | 49 +++++++++----------- 9 files changed, 109 insertions(+), 180 deletions(-) diff --git a/demo/assets/css/style.css b/demo/assets/css/style.css index 64d4b32..d4a5967 100644 --- a/demo/assets/css/style.css +++ b/demo/assets/css/style.css @@ -203,6 +203,11 @@ ul { list-style: inherit; list-style-position: inside; } .hidden-xs {display: none;} } +/* smooth scroll */ +html { + scroll-behavior: smooth; +} + /* HERO */ .hero { padding: 2rem 3rem; diff --git a/demo/src/js/navbar.min.js b/demo/src/js/navbar.min.js index a588ccf..70b944d 100644 --- a/demo/src/js/navbar.min.js +++ b/demo/src/js/navbar.min.js @@ -1,2 +1,2 @@ -// Navbar.js v2.0.5 | thednp © 2020 | MIT-License -!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).Navbar=t()}(this,(function(){"use strict";function e(e,t,n,o){o=o||!1,e.addEventListener(t,n,o)}function t(e,t,n,o){o=o||!1,e.removeEventListener(t,n,o)}function n(e,t){e.classList.add(t)}function o(e,t){e.classList.remove(t)}function i(e,t){return e.classList.contains(t)}var a,r,s,u,m="onmouseleave"in document?["mouseenter","mouseleave"]:["mouseover","mouseout"],l="webkitTransition"in document.body.style||"transition"in document.body.style,c="webkitTransition"in document.body.style?"webkitTransitionDuration":"transitionDuration";function f(e){var t=l?window.getComputedStyle(e)[c]:0;return t="number"!=typeof(t=parseFloat(t))||isNaN(t)?0:1e3*t}function d(a,r){r=r||{};var s,u,l,c,d,p,b,g,y,v,N,E,h,T,L,w,A,B,C=function(e,t){i(e,"open")&&(o(e,"open"),t?setTimeout((function(){o(e,"open-position"),e.isOpen=0}),p):(o(e,"open-position"),e.isOpen=0)),i(e,"open-mobile")&&o(e,"open-mobile")},k=function(){return c&&"none"!==getComputedStyle(c).display||window.innerWidth{ - let Navbars = Array.from(document.querySelectorAll('[data-function="navbar"]')); - Navbars.map(x=>new Navbar(x)) - },'Navbar') +function initComponent(lookup) { + lookup = lookup ? lookup : document; + let Navbars = Array.from(lookup.querySelectorAll('[data-function="navbar"]')); + Navbars.map(x=>new Navbar(x)) } // initialize when loaded -document.body ? initComponent() : one(document, 'DOMContentLoaded', initComponent); +document.body ? initComponent() : document.addEventListener( 'DOMContentLoaded', function iniWrapper(){ + initComponent(); + document.removeEventListener( 'DOMContentLoaded', iniWrapper ) +}); + diff --git a/src/js/navbar.js b/src/js/navbar.js index fef5c53..e97df23 100644 --- a/src/js/navbar.js +++ b/src/js/navbar.js @@ -1,12 +1,6 @@ -import {on} from 'shorter-js/src/event/on.js' -import {off} from 'shorter-js/src/event/off.js' -import {addClass} from 'shorter-js/src/class/addClass.js' -import {removeClass} from 'shorter-js/src/class/removeClass.js' -import {hasClass} from 'shorter-js/src/class/hasClass.js' -import {mouseHoverEvents} from 'shorter-js/src/strings/mouseHoverEvents.js' -import {getElementTransitionDuration} from 'shorter-js/src/misc/getElementTransitionDuration.js' -import {queryElement} from 'shorter-js/src/misc/queryElement.js' -import {tryWrapper} from 'shorter-js/src/misc/tryWrapper.js' +import mouseHoverEvents from 'shorter-js/src/strings/mouseHoverEvents.js' +import getElementTransitionDuration from 'shorter-js/src/misc/getElementTransitionDuration.js' +import queryElement from 'shorter-js/src/misc/queryElement.js' // Navbar export default function Navbar(target, options) { @@ -44,20 +38,20 @@ export default function Navbar(target, options) { // private methods close = function (element, leave) { - if (hasClass(element,openClass)) { - removeClass(element, openClass) + if (element.classList.contains(openClass)) { + element.classList.remove(openClass) if (leave) { setTimeout(function () { - removeClass(element, openPosition) + element.classList.remove(openPosition) element.isOpen = 0 }, transitionDuration) } else { - removeClass(element, openPosition) + element.classList.remove(openPosition) element.isOpen = 0 } } - if (hasClass(element,openMobile)) { - removeClass(element,openMobile) + if (element.classList.contains(openMobile)) { + element.classList.remove(openMobile) } }, @@ -66,15 +60,16 @@ export default function Navbar(target, options) { return firstToggle && getComputedStyle(firstToggle).display !== 'none' || window.innerWidth < breakpoint; }, toggleEvents = function(action) { + action = action ? 'addEventListener' : 'removeEventListener'; Array.from(items).map(listItem => { - if (hasClass(listItem.lastElementChild,'subnav') ) { - action(listItem, mouseHoverEvents[0], enterHandler); - action(listItem, mouseHoverEvents[1], leaveHandler); + if (listItem.lastElementChild.classList.contains('subnav') ) { + listItem[action](mouseHoverEvents[0], enterHandler); + listItem[action](mouseHoverEvents[1], leaveHandler); } let toggleElement = listItem.getElementsByClassName(parentToggle)[0] - toggleElement && action(toggleElement, 'click', clickHandler); + toggleElement && toggleElement[action]( 'click', clickHandler); }) - navbarToggle && action(navbarToggle, 'click', clickHandler); + navbarToggle && navbarToggle[action]('click', clickHandler); }, // handlers @@ -83,12 +78,12 @@ export default function Navbar(target, options) { let that = this, lookup, element; if ( (e.target === that || that.contains(e.target)) ) { element = that.closest('li') || that.closest('.navbar'); - if ( !hasClass(element,openMobile) ) { - addClass(element,openMobile); + if ( !element.classList.contains(openMobile) ) { + element.classList.add(openMobile); lookup = toggleSiblings ? element.parentNode.getElementsByTagName('LI') : element.getElementsByTagName('LI'); Array.from(lookup).map(x=>x!==element && close(x)); } else { - removeClass(element,openMobile); + element.classList.remove(openMobile); } } }, @@ -97,8 +92,8 @@ export default function Navbar(target, options) { clearTimeout(that.timer) if (!that.isOpen && !checkView() ) { that.timer = setTimeout(function(){ - addClass(that,openPosition) - addClass(that,openClass) + that.classList.add(openPosition) + that.classList.add(openClass) that.isOpen = 1 Array.from(that.parentNode.getElementsByTagName('LI')) .map(x => x !== that && close(x)) @@ -115,7 +110,7 @@ export default function Navbar(target, options) { // public method this.dispose = function() { - toggleEvents(off) + toggleEvents() delete menu.Navbar } @@ -148,7 +143,7 @@ export default function Navbar(target, options) { delayDuration = !isNaN(delayOption) ? delayOption : dataDelay && !isNaN(dataDelay) ? parseInt(dataDelay) : 500; // attach events - toggleEvents(on) + toggleEvents(1) menu.Navbar = self } \ No newline at end of file