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

collapsible-block

v1.1.10

Published

Lightweight Collapse/Expand component library.

Downloads

17

Readme

Collapsible Block

npm version DragsterJS gzip size

Lightweight Collapse/Expand component library.

Installation

Setting up is pretty straight-forward. Download the js and css files from dist folder and include them in your HTML:

<link rel='stylesheet' id='collapsible-block'  href='dist/collapsible-block.min.css' type='text/css' media='all' />
<script type="text/javascript" src="path/to/dist/collapsible-block.min.js"></script>

NPM

Collapsible Block is also available on NPM:

$ npm install collapsible-block

Initialization

Once the Collapsible Block script is loaded all functions will be available through the global variable window.CollapsibleBlock, however to enable the components you need to call the function init:

Call the function CollapsibleBlock.init( options ); passing the options parameter as an object.

Options Available

The options parameter accept any of the available options from the default settings by passing the new values as an object. You can simply ommit the options you don't want to change the default values of.

These are the currently accepted options with their default values, if in doubt check the source code:

	var _defaults = {
		bodyClass: 'has-collapsible-block',

		elementSelector: '[data-collapsible]',
		contentElementSelector: '[data-collapsible-content]',
		contentInnerSelector: '.collapsible-content__inner',
		handlerSelector: '[data-collapsible-handler]',
		handlerMultiTargetSelector: '[data-collapsible-targets]',

		autoFocusSelector: '[data-autofocus]',
		focusableElementsSelector: 'a[href], button:not([disabled]), input:not([disabled]):not([type="hidden"]), textarea:not([disabled]), select:not([disabled]), details, summary, iframe, object, embed, [contenteditable] [tabindex]:not([tabindex="-1"])',
		selectContentsOnFocus: true,

		isCollapsedClass: 'is-collapsed',
		isExpandedClass: 'is-expanded',
		isActivatedClass: 'is-activated',
		cssTransition: 'height .15s linear',

		targetAttribute: 'aria-controls',
		multiTargetAttribute: 'data-collapsible-targets',
		maxHeightAttribute: 'data-collapsible-max-height',
		createHandlerAttribute: 'data-collapsible-create-handler',
		changeStateOnResizeAttribute: 'data-collapsible-change-state-resize',

		initialState: _publicMethods.states.FIRST_EXPANDED,
		initialStateAttribute: 'data-collapsible-initial-state',

		idPrefix: 'collapsible',
		createHandler: false,
		maxHeight: 0,
		handlerTemplate: '<a href="#collapsible" role="button" data-collapsible-handler>Read more</a>',
		contentInnerTemplate: '<div class="collapsible-content__inner"></div>',
	};

For example, if your application already has the markup defined in many places and you want to change the selector used for the collapsible sections, initialize the component with the options below:

var options = {
	elementSelector: '[data-collapsible], .schema-faq-section',
	contentElementSelector: '[data-collapsible-content], .schema-faq-answer',
	handlerSelector: '[data-collapsible-handler], .schema-faq-question',
}
CollapsibleBlock.init( options );

Another use of the options is to translate the label text displayed as the collapsible block handler in some cases:

var options = {
	handlerTemplate: '<a href="#collapsible" role="button" data-collapsible-handler>Leia mais</a>',
}
CollapsibleBlock.init( options );

Everything else will use the default values.

CollapsibleBlock.states values accepted

When using referencing to the state of a collapsible block you can use the values exposed as CollapsibleBlock.states in JS, or the state's correpondent string values.

  • CollapsibleBlock.states.COLLAPSED, string value collapsed: Represents the state of a collapsible block with it's content hidden.
  • CollapsibleBlock.states.EXPANDED, string value expanded: Represents the state of a collapsible block with it's content visible.
  • CollapsibleBlock.states.FIRST_EXPANDED, string value first_expanded: Represents the initial state of a group of collapsible blocks where the first collapsible block in the group will start in the "EXPANDED" state and all the other blocks in the same group will start as "COLLAPSED".

To set the initial state of the collapsible block individually using data attributes you will have use the correspondent string values because the JS variable will not be parsed or evaluated on html data attributes.

So use this (right way):

<div class="collapsible-content" data-collapsible-content data-collapsible-initial-state="collapsed"></div>

Instead of this (this won't work):

<div class="collapsible-content" data-collapsible-content data-collapsible-initial-state="CollapsibleBlock.states.COLLAPSED"></div>

See the section "Basic Usage > Using data attributes to change the initial state" below.

Basic Usage

1. Recommended HTML elements structure

The collapsible block component requires the following HTML elements structure:

<div data-collapsible>
	<a href="#" data-collapsible-handler>
		Toggle State
	</a>
	<div class="collapsible-content" data-collapsible-content>
		<div class="collapsible-content__inner">
			Content block
		</div>
	</div>
</div>

The element classes are not actually needed as this library does not add any css styles.

2. Using data attributes to change the initial state

Another way to change the behavior of a collapsible block component is to add data attributes to specific elements, this is useful if you want to change the properties of one instance of the collapsible block component but keep the other instances with the default options.

You can define one collapsible block element to start as collapsed or expanded using the data attribute data-collapsible-initial-state.

Starting as collapsed;

<div data-collapsible>
	<a href="#" data-collapsible-handler>
		Toggle State
	</a>
	<div class="collapsible-content" data-collapsible-content data-collapsible-initial-state="collapsed">
		<div class="collapsible-content__inner">
			This content will start collapsed
		</div>
	</div>
</div>

Starting as expanded;

<div data-collapsible>
	<a href="#" data-collapsible-handler>
		Toggle State
	</a>
	<div class="collapsible-content" data-collapsible-content data-collapsible-initial-state="expanded">
		<div class="collapsible-content__inner">
			This content will start expanded
		</div>
	</div>
</div>

3. Collapsible block groups

<div class="collapsible-block__group">

	<div data-collapsible>
		<a href="#" data-collapsible-handler>
			Toggle State 1
		</a>
		<div class="collapsible-content" data-collapsible-content>
			<div class="collapsible-content__inner">
				Content block 1
			</div>
		</div>
	</div>

	<div data-collapsible>
		<a href="#" data-collapsible-handler>
			Toggle State 1
		</a>
		<div class="collapsible-content" data-collapsible-content>
			<div class="collapsible-content__inner">
				Content block 1
			</div>
		</div>
	</div>

	<div data-collapsible>
		<a href="#" data-collapsible-handler>
			Toggle State 1
		</a>
		<div class="collapsible-content" data-collapsible-content>
			<div class="collapsible-content__inner">
				Content block 1
			</div>
		</div>
	</div>

</div>

4. Targetting a collapsbile block outside of the structure

It is possible to control the state of a collapsible block even when it does not follow the recommented HTML structure.

For that you'll need to know the ID of the collapsible content element and set the attribute aria-controls="<KNOWN_COLLAPSIBLE_CONTENT_ID>" replacing <KNOWN_COLLAPSIBLE_CONTENT_ID> with the actual ID of the content element. The handler element will have a markup similar to this:

<a href="#" data-collapsible-handler aria-controls="collapsible-content-disconnected"></a>

In this case, you'll also have to set the attribute data-collapsible to the collapsible block content element. The content element will have a markup similar to this:

<div id="collapsible-content-disconnected" data-collapsible data-collapsible-content></div>

Here is a full example:

<div class="disconnected-handler">
	<a href="#" data-collapsible-handler aria-controls="collapsible-content-disconnected">
		Toggle State
	</a>
</div>

<div id="collapsible-content-disconnected" data-collapsible data-collapsible-content>
	<div class="collapsible-content__inner">
		#collapsible-content-disconnected
	</div>
</div>

Contributing to Development

This isn't a large project by any means, but you are definitely welcome to contribute.

Development environment

Clone the repo and run npm install:

$ cd path/to/collapsible-block
$ npm install

Run the build command:

$ gulp build

Build on file save:

$ gulp
$ gulp watch

License

Licensed under MIT.