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

butter-slide-toggle

v0.1.2

Published

ButterSlideToggle is the Better Slide Toggle you wish you had!

Downloads

2

Readme

Butter Slide Toggle

The slide toggle is one of the simplest and most common web problems but ends up being one of the more difficult solutions to implement well with respect to performance and behavior. This is an effort to make a tool as simple to use as jQuery's slideToggle method, but way more performant. Butter Slide Toggle leverages CSS transitions, Javascript, and some trickery to bypass the complications of trying to transition height or max height manually to get the slide toggle effect you're looking for.

Installation

You can get Butter Slide Toggle with either npm or bower. You can also manually include the main dist/js/butter-slide-toggle.js file as a script tag your project if you like.

NPM

npm install butter-slide-toggle --save

Bower

bower install butter-slide-toggle --save

Manual

<script src="path/to/js/dir/butter-slide-toggle.js"></script>
<link href="path/to/css/dir/butter-slide-toggle.css" rel="stylesheet" type="text/css">

Prerequisites

Butter Slide Toggle is plain Javascript and has no dependencies.

Getting Started

Once you've included Butter Slide Toggle in your project it is very simple to use. Behavior is a little different from jQuery's slide toggle in that you'll first need to initialize your toggle before you can use it. The simplest implementation in plain javascript looks something like the following.

const contentSelection = document.querySelector('.toggle-content'),
  contentToggle = new ButterSlideToggle(contentSelection);

document.querySelector('.toggle-button').addEventListener('click', e => {
  e.preventDefault();
  contentToggle.toggle();
});

Once a Butter Slide Toggle is initialized, you can use the toggle() method to trigger the toggle. Simple!

Butter Slide Toggle Responsibilities

Butter Slide Toggle aims to provide you with a tool that implements a toggle behavior with CSS transitions, and all the extras around that you might want to have access to. It does not aim to handle theming (outside of the base transition definitions), presentation, or implementation of the tool. That's up to you!

Butter Slide Toggle functionality has some conditional requirements, just like any slide toggle implementation would have.

  1. Consider Margins - Placing margins on any element that collapses in one way or another will usually end up with jumping behavior when the element opens or closes. Butter Slide Toggle will not handle this, or any other theming considerations, for you.
  2. Implementation - Butter Slide Toggle doesn't provide a default implementation, so you'll need to determine and implement that on your own. Though something like the above example is all you really need for a basic trigger implementation!
  3. Wrapper - Butter Slide Toggle creates and extra <div> wrapper, which is necessary for the max-height transition. When theming, be aware that this is there, as you might need to occasionally take it into account.

Parameters

Element

Type: object (required)

The first parameter is the DOM element to initialize as a toggleable element. If you are using jQuery to select items, then you'll just need to get the native DOM element from the jQuery selection. For example:

var $toggle = $('.toggle'),
    toggle = new ButterSlideToggle($toggle[0]);

toggle.toggle()

Options

The second parameter is a settings object and is optional. The following settings can be defined.

transitionClass

Type: string

Default: butter-slide-toggle-transition

Set the transition class to use for this item's toggle transition. The default class has a default CSS definition. This is how you can implement many different types and lengths of transitions. Since the transition is being defined by CSS, that's where you'll go to change the length of the toggle transition.

Methods

toggle

Initiates the toggle transition on an initialized element. This will always change the state of the item from it's current. Open to closed, or closed to open.

isCollapsed

A helper method that returns a boolean to determine if an item is collapsed or not.

Events

All events are triggered from the element that is passed on the initialization of a toggle.

butterToggle.opened

Triggered after the open transition is complete.

butterToggle.closed

Triggered after the close transition is complete.

butterToggle.openStart

Triggered immediately before the open transition begins.

butterToggle.closeStart

Triggered immediately before the close transition begins.

Development Build

Butter Slide Toggle uses Webpack to compile a development build and compiles script and css assets to the dist folder. The project is set up with Browser Sync to view the demo locally. You can view the demo locally by cloning the repo and running the following.

npm install
npm run dev

Authors

License

This project is free and open source under the ISC License.

Acknowledgments

The original inspiration came from a Stack Overflow post from years ago which unfortunately couldn't be found again.