Skip to content

Instantly share code, notes, and snippets.

@rdswd
Created March 25, 2020 17:56
Show Gist options
  • Select an option

  • Save rdswd/c6748fa71257c2f49131ba3b7c20f781 to your computer and use it in GitHub Desktop.

Select an option

Save rdswd/c6748fa71257c2f49131ba3b7c20f781 to your computer and use it in GitHub Desktop.
Social media menu javascript
( function (){
/* Uses the menu id of your social media menu */
int_wl_social_menu("social-menu");
/** Uses loaded-in-header svg sprite to create svg icons for menus */
function int_wl_social_menu(id, altIcons ) {
var menu, menuLis;
/* Social menu ID */
menu = document.getElementById(id);
if ( ! menu ){
return;
}
/* menu children - eg li */
menuLis = menu.children;
/* Loop through children of menu */
for (var i = 0; i < menuLis.length; i++) {
var link = '',
appendSvgCode = document.createElement('span');
link = menuLis[i].childNodes[0];
if (link.href !== undefined && link.href.includes("mailto")) {
appendSvgCode.innerHTML = '<svg class="email-icon"><use xlink:href="#Email"></use></svg>';
}
if (link.href !== undefined && link.href.includes("book-form")) {
appendSvgCode.innerHTML = '<svg class="calendar-icon"><use xlink:href="#Calendar"></use></svg>';
}
if (link.href !== undefined && link.href.includes("tel")) {
appendSvgCode.innerHTML = '<svg class="tel-icon"><use xlink:href="#Phone"></use></svg>';
}
/* Alt */
if (altIcons !== undefined && link.href !== undefined && link.href.includes("tel")) {
appendSvgCode.innerHTML = '<svg class="alt-tel-icon"><use xlink:href="#Phone2"></use></svg>';
}
if (link.href !== undefined && link.href.includes("fax")) {
appendSvgCode.innerHTML = '<svg class="fax-icon"><use xlink:href="#Fax"></use></svg>';
}
if (link.href !== undefined && link.href.includes("google")) {
appendSvgCode.innerHTML = '<svg class="map-icon"><use xlink:href="#Map"></use></svg>';
}
if (link.href !== undefined && link.href.includes("twitter")) {
appendSvgCode.innerHTML = '<svg class="twitter-icon"><use xlink:href="#Twitter"></use></svg>';
}
if (link.href !== undefined && link.href.includes("facebook")) {
appendSvgCode.innerHTML = '<svg class="facebook-icon"><use xlink:href="#Facebook"></use></svg>';
}
if (link.href !== undefined && link.href.includes("instagram")) {
appendSvgCode.innerHTML = '<svg class="instagram-icon"><use xlink:href="#Instagram"></use></svg>';
}
if (link.href !== undefined && link.href.includes("pinterest")) {
appendSvgCode.innerHTML = '<svg class="pinterest-icon"><use xlink:href="#Pinterest"></use></svg>';
}
/* checks data attribute not link: data-type = ""; */
if (link.dataset.type !== undefined && link.dataset.type.includes("site")) {
appendSvgCode.innerHTML = '<svg class="world-icon"><use xlink:href="#World"></use></svg>';
}
/* Insert svg before link */
if ( link.href !== undefined ){
link.insertBefore(appendSvgCode, link.childNodes[0]);
}
if ( menuLis[i].classList.contains( 'menu-item-has-children' ) ){
appendSvgCode.innerHTML = '<svg class="arrow-icon"><use xlink:href="#Arrow"></use></svg>';
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment