Coder Social home page Coder Social logo

gabrielsenadev / dynamic-drop-down-menu Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 5 KB

Create dropdown menus and listen to static (HTML) and dynamic dropdown menus

License: MIT License

CSS 25.66% JavaScript 74.34%
dropdown javascrip javascript-dropdown html-dropdown static-dropdown dynamic-dropdown

dynamic-drop-down-menu's Introduction

Dynamic Dropdown menu

Create dropdown menus and listen to static (HTML) and dynamic dropdown menus

Table of Contents

Install

npm i dynamic-drop-down-menu

Usage

  1. Import components that you should use.
import {
  Dropdown,
  ClickoutDropdown,
  setupHTMLDropdowns
} from 'dynamic-drop-down-menu'
  1. (Optional) Import css file: css/dropdown.css (If you wanna test, you can import the css file in node_modules)

  2. Try!

// Creating dropdown
const dropdownRemove = Dropdown('Remove file', document.body, ['strings.txt', 'documents.txt', 'players.txt', 'users.json'])

// Adding event to dropdown items
dropdownRemove.addEventToItems('click', dropdownRemove.getItems(), (event) => {
  const confirmation = confirm(`You wanna remove ${event.target.textContent} file?`)
  if (confirmation) {
    event.target.remove()
    alert('File deleted!')
  }
})

// Auto close visible dropdown when clicks out.
ClickoutDropdown.listen()

// Loads all HTML dropdowns
setupHTMLDropdowns()

Components

Name Description Example
Dropdown Creates new dropdown element Dropdown("Button name", parentNode, itemsArray)
ClickoutDropdown Module Pattern that control what happens when click out of visible dropdown ClickoutDropdown.listen()
setupHTMLDropdowns Loads all HTML dropdowns setupHTMLDropdowns()
getDropdowns Return a list of all dropdowns getDropdowns()
getVisibleDropdowns Return a list of all visible dropdowns getVisibleDropdowns()

Dropdown components

Name Description Example
getDropdownDOM Returns dropdown element dropdown.getDropdownDOM()
getContentDOM Return dropdown content dropdown.getContentDOM()
getButtonDOM Return dropdown button dropdown.getButtonDOM()
getItems Return dropdown items dropdown.getItems()
addItem Add dropdown item by text dropdown.addItem("Dropdown item 3")
removeItemByText Remove item by text dropdown.removeItemByText("Dropdown item 3")
removeItemByIndex Remove item by index dropdown.removeItemByIndex(2)
addEventToItems Add event listener to selected dropdown items dropdown.addEventToItems("eventName", dropdown.getItems(), (event) => console.log(You clicked ${event.target.textContent}))

HTML Dropdown

You can create dropdown on HTML. See how:

  1. Create dropdown on HTML
<div class="drop-down">
  <button class="drop-down-button">Dropdown button</button>
  <ul class="drop-down-content">
    <li class="drop-down-item">Dropdown item 1></li>
    <li class="drop-down-item">Dropdown item 2</li>
    <li class="drop-down-item">Dropdown item 3</li>
  </ul>
</div>
  1. Enable dropdown listener using setupHTMLDropdowns
import {setupHTMLDropdowns} from 'dynamic-drop-down-menu'
setupHTMLDropdowns()

The Odin Project

This is a project to how publish package on npm.

dynamic-drop-down-menu's People

Contributors

gabrielsenadev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.