

Bulma Dropdown with Go Toggle
source link: http://siongui.github.io/2018/02/01/bulma-dropdown-with-go-toggle/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Bulma Dropdown with Go Toggle
February 01, 2018
Go toggle code for Bulma dropdown component, compiled to JavaScript via GopherJS.
The full code example, including JavaScript counterpart, of this post is on my GitHub.
HTML code for Dropdown:
<div class="dropdown is-active"> <div class="dropdown-trigger"> <button class="button" aria-haspopup="true" aria-controls="dropdown-menu"> <span>Dropdown button</span> <span class="icon is-small"> <i class="fas fa-angle-down" aria-hidden="true"></i> </span> </button> </div> <div class="dropdown-menu" id="dropdown-menu" role="menu"> <div class="dropdown-content"> <a href="#" class="dropdown-item"> Dropdown item </a> <a class="dropdown-item"> Other dropdown item </a> <a href="#" class="dropdown-item is-active"> Active dropdown item </a> <a href="#" class="dropdown-item"> Other dropdown item </a> <hr class="dropdown-divider"> <a href="#" class="dropdown-item"> With a divider </a> </div> </div> </div>
Go toggle code:
import ( . "github.com/siongui/godom" ) func main() { Document.AddEventListener("DOMContentLoaded", func(e Event) { // Dropdowns dds := Document.QuerySelectorAll(".dropdown:not(.is-hoverable)") closeDropdowns := func() { for _, dd := range dds { dd.ClassList().Remove("is-active") } } if len(dds) > 0 { for _, dd := range dds { dd.AddEventListener("click", func(e Event) { e.StopPropagation() dd.ClassList().Toggle("is-active") }) } Document.AddEventListener("click", func(e Event) { closeDropdowns() }) } // Close dropdowns if ESC pressed Document.AddEventListener("keydown", func(e Event) { if e.KeyCode() == 27 { closeDropdowns() } }) }) }
The above code use godom package to make the code more readable.
The following JavaScript code is equivalent to above Go code:
'use strict'; document.addEventListener('DOMContentLoaded', function () { // Dropdowns var $dropdowns = getAll('.dropdown:not(.is-hoverable)'); if ($dropdowns.length > 0) { $dropdowns.forEach(function ($el) { $el.addEventListener('click', function (event) { event.stopPropagation(); $el.classList.toggle('is-active'); }); }); document.addEventListener('click', function (event) { closeDropdowns(); }); } function closeDropdowns() { $dropdowns.forEach(function ($el) { $el.classList.remove('is-active'); }); } // Close dropdowns if ESC pressed document.addEventListener('keydown', function (event) { var e = event || window.event; if (e.keyCode === 27) { closeDropdowns(); } }); // Functions function getAll(selector) { return Array.prototype.slice.call(document.querySelectorAll(selector), 0); } });
References:
[1]Dropdown | Bulma: a modern CSS framework based on Flexbox
Recommend
-
135
Hello all, I've been working on a web app starter kit using Vue's single file component system and styling it with Bulma. I'm looking for fellow...
-
37
I’ll come out and say it, I’m still a fan of Bootstrap. With that, I’m not oblivious to the many alternatives that are floating around out there. One of the more inspired CSS frameworks I have come across recently is...
-
12
Bulma Modal with Pure CSS Toggle January 27, 2018 CSS only toggle
-
8
Bulma Navbar with Go Toggle January 18, 2018 Go toggle code for Bulma responsive...
-
6
Pure CSS Bulma Dropdown Toggle October 02, 2018 CSS only toggle
-
9
JavaScript for Bulma Dropdown Updated: October 02, 2018 This post is for thos...
-
9
Toggle Element (Dropdown/Menu) Visibility with CSS February 07, 2015 Togg...
-
12
Pure CSS Toggle Bulma Responsive Navbar July 14, 2020
-
6
Bulma Modal with Go Toggle February 10, 2018 Go toggle code for Bulma...
-
11
Radio buttons, checkboxes, toggle switche...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK