2.3.0 ā€¢ Published 8 months ago

@viivue/easy-tab-accordion v2.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Easy Tab & Accordion (ETA)

release minified jsdelivr license Netlify Status

Javascript library to create tabs or accordion.

Getting started

NPM Package

Install NPM package

npm i @viivue/easy-tab-accordion

Import

import "@viivue/easy-tab-accordion";

Download

šŸ‘‰ Self hosted - Download the latest release

<script src="./easy-tab-accordion.min.js"></script>

šŸ‘‰ CDN Hosted - jsDelivr

<!-- JS (10KB) -->
<script src="https://cdn.jsdelivr.net/gh/viivue/easy-tab-accordion@2.3.0/dist/easy-tab-accordion.min.js"></script>

Initialize

To initialize an ETA script, always remember three things:

  1. Wrapper
  2. Trigger(s)
  3. Receiver(s)

With HTML

Using these HTML attributes to initialize without JavaScript.

<!-- No Js init -->
<div data-eta>
    <!-- section 1 -->
    <div>
        <button data-eta-trigger="section-1">Section 1</button>
        <div data-eta-receiver="section-1">Content</div>
    </div>

    <!-- section 2 -->
    <div>
        <button data-eta-trigger="section-2">Section 2</button>
        <div data-eta-receiver="section-2">Content</div>
    </div>
</div>

Or using data-attributes (with JSON format) to initialize.

<div data-eta='{ "id":"my-id", "animation":"slide", "hash":"false", "duration":"100"}'>
    <!-- section 1 -->
    <div>
        <button data-eta-trigger="section-1">Section 1</button>
        <div data-eta-receiver="section-1">Content</div>
    </div>

    <!-- section 2 -->
    <div>
        <button data-eta-trigger="section-2">Section 2</button>
        <div data-eta-receiver="section-2">Content</div>
    </div>
</div>

āš ļø Notice that value of data-eta-trigger and data-eta-receiver must be the same.

With JavaScript

Assume that we have the HTML like below

<!-- Custom HTML -->
<div class="my-accordion">
    <!-- section 1 -->
    <div>
        <button data-trigger="section-1">Section 1</button>
        <div class="content" id="section-1">Content</div>
    </div>

    <!-- section 2 -->
    <div>
        <button data-trigger="section-2">Section 2</button>
        <div class="content" id="section-2">Content</div>
    </div>
</div>
// Init
ETA.init({
    el: document.querySelector('.my-accordion'), // DOM element
    trigger: '[data-trigger]', // CSS selector
    triggerAttr: 'data-trigger', // attribute name
    receiver: '.content', // CSS selector
    receiverAttr: 'id', // attribute name
});

Options

Selectors

NameTypeDefaultRequiredDescription
elDOM element[data-eta]āœ…Wrapper element
triggerstring[data-eta-trigger]āœ…CSS selector for trigger elements
triggerAttrstringdata-eta-triggerāœ…Attribute name of trigger elements
receiverstring[data-eta-receiver]āœ…CSS selector for receiver elements
receiverAttrstringdata-eta-receiverāœ…Attribute name of receiver elements
activeClassstring"active"āŒClass name for active trigger and receiver

āš ļø Notice that value of triggerAttr and receiverAttr must be the same.

Animation

NameTypeDefaultDescription
animationstring"slide""slide" for accordion style (slide up and slide down), "fade" for tabbed style (fade in and fade out)
durationnumber450Duration of animation in millisecond
scrollIntoViewbooleanfalseScroll panel into view when open

Hash

NameTypeDefaultDescription
hashbooleanfalseUpdate hash URL
hashScrollbooleanfalseScroll into view when page loaded with a valid hash

Responsive

NameTypeDefaultDescription
liveBreakpointarray[]An array of two numbers

liveBreakpoint defines a range to enable ETA, when the browser's width is outside this range ETA will be destroyed ( detecting via window resizing event).

For instance:

  • liveBreakpoint:[99999,768]: destroy ETA on screen that smaller than 768px
  • liveBreakpoint:[1024,-1]: destroy ETA on screen that bigger than 1024px

Open and close

NameTypeDefaultDescription
activeSectionnumber/array0Index(s) of section to be active on init, array input only available for animation:"slide"

For animation:"slide" only

NameTypeDefaultDescription
allowCollapseAllbooleanfalseAllow to collapse all sections at the same time
allowExpandAllbooleanfalseAllow to expand all sections at the same time

Prevent default option

NameTypeDefaultDescription
isPreventDefaultbooleantrueAllow preventing the default behavior when clicking on the trigger

HTML attributes

Add these attributes on the wrapper element.

AttributeAs for option
data-eta-animation="fade"animation: "fade"
data-eta-hashhash: true
data-eta-hash-scrollhashScroll: true

Events

NameDescription
onBeforeInit: (data) => {}
onAfterInit: (data) => {}
onBeforeOpen: (data) => {}
onBeforeClose: (data) => {}
onAfterOpen: (data) => {}
onAfterClose: (data) => {}
onDestroy: (data) => {}
onUpdate: (data) => {}

Methods

NameUsageDescription
toggleeta.toggle(panelId)Toggle a panel
openPaneleta.openPanel(panelId, isStrict=false)Open a panel. Turn isStrict on to only open is currently closing.
closePaneleta.closePanel(panelId, isStrict=false)Close a panel. Turn isStrict on to only close is currently opening.
toggleByIndexeta.toggleByIndex(index)Toggle a section by index
destroyeta.destroy()Remove all style and events
initeta.init()Could be use after destroy
updateeta.update()Update styling
oneta.on()Assign events

Get the instance with JS init

ETA.init({
    id: 'my-eta'
});

const eta = ETA.get('my-eta');

// use methods
eta.update();
eta.on("open", () => {
    // do something
});

Deployment

# Start dev server
npm run dev

# Distribute production files (set new version in `package.json` first)
npm run prod

# Build dev site (for Netlify only)
npm run build

# Research replace to set new version
npm publish

License

MIT License

Copyright (c) 2023 ViiVue

2.3.0

8 months ago

2.2.0

8 months ago

2.1.4

1 year ago

2.1.6

1 year ago

2.1.5

1 year ago

2.1.1

1 year ago

2.1.0

2 years ago