1.1.0 • Published 4 years ago

spine-dropdown v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 years ago

SpineDropdown

A barebone class to create dropdown functionality within projects.

Dependencies

Disclosure - Little to no testing has been performed and there are likely more dependencies

Tested environments (Successful)

Quickstart

Installation

npm install spine-dropdown --save-dev

Usage

app.js

const { SpineDropdown } = require('spine-dropdown')

function main() {

    // Lists
    const list1 = ['one', 'two', 'three', 'four', 'five']
    const list2 = ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
    const list3 = ['not', 'added', 'to', 'doc', 'yet']

    // Create instance of SpineDropdown with target stylesheet 'app.css'
    const sdd = new SpineDropdown('app')

    // Attach dropdown as the last child of target html element
    sdd.make(list1, {
        target: 'dropdown',
        class: 'black',
        classBody: 'white'
    })

    // Replace the target html element with dropdown
    sdd.make(list2, {
        target: 'another-dropdown',
        replace: true
    })

    // Store document fragment locally without adding to document
    const dropdown = sdd.make(list3)

    // Initialise all 'made' dropdowns
    sdd.init()

}
main()

index.html

<!DOCTYPE html>
<html>

<head>
    <title id="title">Title</title>
    <meta charset="UTF-8" />
    <meta http-equiv="Content-Security-Policy" content="script-src 'self';">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="app.css" defer/>
</head>

<body>

    <div id="dropdown"></div>
    <div id="another-dropdown"></div>

    <script src="app.js"></script>

</body>

</html>

app.css

.black {
    color: #fff;
    background-color: #000;
}

.white {
    color: #000;
    background-color: #fff;
}

Documentation

Constructor

Parameters

  • stylesheet (String) (required)

    Linked stylesheet in html (typically app.css or style.css)

Example initialisation (index.html)

<head>
    <link href="app.css" rel="stylesheet">
</head>

Example initialisation (app.js)

const sdd = new SpineDropdown('app')

Methods

make

Create dropdown html with provided list

  • Parameters

    • list (Array) (required)
    • options (Object)

      propertytypeActionDefault
      targetStringHTML element indexnull
      selectedStringInitial selected dropdown text (similate HMTL selected attribute)First list item
      replaceBooleanInsert dropdown in-place of given element idappendChild()
      classStringClass/es string to add to dropdown parent divnull
      classBodyStringClass/es string to add to dropdown list (body) divnull
  • Return value

    • Default
      • document fragment
    • If target property is passed
      • none

init

Attach functionality to all 'made' dropdowns with event listeners

  • Parameters
    • none
  • Return value
    • Promise
      • Success: true
      • Error or Warning: null