Git Product home page Git Product logo

vue-multilevel-accordion's Introduction

Multilevel Accordion Component for Vue 2.5+

An extremely customizable multi level accordion. It's receives a high level tree structure in json format.
极其可定制的多层手风琴。 它以json格式接收高级树结构。

Example 1:

Example 1

Example 2:

Example 2

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>
Prop Type Default Description
tree Object null Contains the tree in json format that's required to render the component.
marginLeft Float 0 It'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>
Field Type Description
tree Object Contains the fields defined in the tree structure.
interleaved Boolean Alternates between true and false depending on the row. It can be use to select a background color, for example.
level Number Integer that indicates the depth level of the row.
expanded Boolean Is true if the row is expanded, else otherwise.
leaf Boolean Is 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.

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.