1.0.8 • Published 4 years ago

vue-multilevel-accordion v1.0.8

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

Multilevel Accordion Component for Vue 2.5+

An extremely customizable multi level accordion. It's receives a high level tree structure in json format.

Example 1

Table of Contents

  1. Motivation
  2. Installation
  3. Options
  4. Demo
  5. Contribution
  6. License

Motivation

  • Although there are some accordion plugins, there aren't so many that offers a nested structure of any number of levels.
  • Existing accordions are usually hard to customize because they come with their own styling configuration.

The above reasons led me to build an extremely customizable recursive accordion.

Installation

  1. First install package by npm.

npm i vue-multilevel-accordion

  1. Import the component and register it where you want to use it.
<script>
  import MultilevelAccordion from "vue-multilevel-accordion";
  import tree from "./tree.js";

  export default {
    components: {
      MultilevelAccordion
    },
    data() {
      return {
        tree
      };
    }
  };
</script>
  1. Create the tree structure and save it on a file (in this case it's called tree.js). The first level (root level) it's not going to be rendered but is required by the component.
const tree = {
  text: "Root",
  children: [
    {
      text: "Musicians",
      children: [
        {
          text: "IDM",
          children: [
            {
              text: "Ae"
            },
            {
              text: "BOC"
            }
          ]
        },
        {
          text: "Post Rock",
          children: [
            {
              text: "Tortoise"
            }
          ]
        }
      ]
    }
  ]
};

export { tree };
  1. Define the recursive template that is going to be in every tab. You've to use slot-scope and destructurate { tree } property. For more options see the Options section.

For example, this is going to show text field defined previously in every tab. Also, when the user clicks on the tab, the method select(value) will be called.

<template slot-scope="{ tree }">
  <div @click="select(tree.text)" style="padding: 10px;">
    <p>{{ tree.text }}</p>
  </div>
</template>
  1. Place the component with the slot wherever you want. You've to pass the component the tree structure and optionally a margin on the left that it's going to growth with every level of depth. For more options see the Options section.
<multilevel-accordion :tree="tree" :marginLeft="2">
  <template slot-scope="{ tree }">
    <div @click="select(tree.text)">
      <p>{{ tree.text }}</p>
    </div>
  </template>
</multilevel-accordion>
  1. You can see the result!

Musicians Example

Options

  1. Props:

The component receives 2 posible props:

<multilevel-accordion :tree="tree" :marginLeft="2"> </multilevel-accordion>
PropTypeDefaultDescription
treeObjectnullContains the tree in json format that's required to render the component.
marginLeftFloat0It's the accumulative margin on the left that growths with each level. Is in rem units.
  1. Destructuring:

For each element in the tree some properties can be individually accesed via slot-scope directive.

<multilevel-accordion :tree="tree" :marginLeft="2">
  <template slot-scope="{ tree, interleaved, level, expanded, leaf }">
    <div>
      <p>{{ tree.text }}</p>
    </div>
  </template>
</multilevel-accordion>
FieldTypeDescription
treeObjectContains the fields defined in the tree structure.
interleavedBooleanAlternates between true and false depending on the row. It can be use to select a background color, for example.
levelNumberInteger that indicates the depth level of the row.
expandedBooleanIs true if the row is expanded, else otherwise.
leafBooleanIs true if the tab is a leaf (doesnt have any children element), false otherwise. You can override this default behaviour by adding leaf: true or leaf: false in the tree structure for each element.

Demo

Important:

In this repository there is a functional example that you can run locally in your pc. Please go to the example folder and follow the instructions.

Adding some styling you can quickly scalate the complexity of the component and the design.

For example, you can:

  1. Alternate the color for each row to bring a better UX.

    This is done with the { interleaved } destructuring.

  2. Specific rules for leaf and non leaf tabs.

    This can be done with the { leaf } destructuring: leaf is going to be true if the element is a leaf, and false otherwise. Combine this with v-if or conditionals.

  3. Specific rules or css styles when the element is expanded.

    This can be done with the { expanded } destructuring, which is going to be true is the tab is expanded and false otherwise.

  4. Specifics rules or styles depending of the depth level

    This can be done with the { level } destructuring which is an Integer that represents the current level of depth.

For example, using this component and the library Tailwind Css you could reach pretty different styles:

You can run locally in your computer the following examples. To do this, just go to the github repository and clone the folder example. To install, follow the instructions listed on the readme file of that folder.

Example 1:

Example 1

Example 2:

Example 2

Example 3:

Example 3

Contribution

This is a pretty basic project that I did in a weekend. If you want to contribute improving this, adding more functionality of fixing issues I'll be glad to receive PR.

License

MIT License

Copyright (c) 2019 Santiago Díaz de Valdés Williamson

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.1.0

4 years ago