npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-multilevel-accordion

v1.0.8

Published

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

Downloads

271

Readme

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>

| 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.