Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ladda

hakimel179.5kMIT2.0.3TypeScript support: included

Buttons with built-in loading indicators

readme

Ladda

Buttons with built-in loading indicators, effectively bridging the gap between action and feedback.

Check out the demo page.

Installation

npm install ladda

Module bundling

Ladda 2.x is distributed as a standard ES6 module. Since not all browsers/environments support native ES6 modules, it is recommended to use a bundler such as Rollup, Parcel, or Webpack to create a production-ready code bundle.

Usage

CSS

You will need to include ONE of the two style sheets in the /dist directory. If you want the button styles used on the demo page, use the ladda.min.css file. If you want to have the functional buttons without the visual style (colors, padding, etc.), use the ladda-themeless.min.css file.

HTML

Below is an example of a button using the expand-right animation style.

<button class="ladda-button" data-style="expand-right">Submit</button>

When the JS code runs to bind Ladda to the button, the ladda-button class will be automatically added if it doesn't already exist. Additionally, a span with class ladda-label will automatically wrap the button text, resulting in the following DOM structure:

<button class="ladda-button" data-style="expand-right">
    <span class="ladda-label">Submit</span>
</button>

Buttons accept the following attributes:

  • data-style: one of the button styles [required]
    • expand-left, expand-right, expand-up, expand-down
    • contract, contract-overlay
    • zoom-in, zoom-out
    • slide-left, slide-right, slide-up, slide-down
  • data-color: green/red/blue/purple/mint
  • data-size: xs/s/l/xl, defaults to medium
  • data-spinner-size: pixel dimensions of spinner, defaults to dynamic size based on the button height
  • data-spinner-color: a hex code or any named CSS color, defaults to #fff
  • data-spinner-lines: the number of lines for the spinner, defaults to 12

JavaScript

Start by importing the Ladda module:

import * as Ladda from 'ladda';

The following approach is recommended for JavaScript control over your buttons:

// Create a new instance of ladda for the specified button
var l = Ladda.create(document.querySelector('.my-button'));

// Start loading
l.start();

// Will display a progress bar for 50% of the button width
l.setProgress(0.5);

// Stop loading
l.stop();

// Toggle between loading/not loading states
l.toggle();

// Check the current state
l.isLoading();

// Delete the button's ladda instance
l.remove();

To show the loading animation for a form that is submitted to the server (always resulting in a page reload) the bind() method can be used:

// Automatically trigger the loading animation on click
Ladda.bind('button[type=submit]');

// Same as the above but automatically stops after two seconds
Ladda.bind('button[type=submit]', {timeout: 2000});

Note: when using the bind() method on buttons that are inside a form, loading indicators will not be shown until the form is valid.

All loading animations on the page can be stopped by using:

Ladda.stopAll();

Browser support

Ladda has been tested in Firefox, Microsoft Edge, Chrome, and Internet Explorer 11. It also Should Work™ in Safari and Internet Explorer 10.

Changelog

https://github.com/hakimel/Ladda/blob/master/CHANGELOG.md

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

2.0.3 - 2021-12-02

Fixed

  • Deprecated slash as division when using Dart Sass (issue #109)

2.0.2 - 2021-09-01

Changed

  • Set package type to module to better support native ES module imports
  • Code cleanup and documentation improvements

2.0.1 - 2018-05-14

Added

  • Support for reassigning $spinnerSize Sass variable (PR #81)
  • Re-included Sass files in the npm package

2.0.0 - 2018-05-13

Added

  • Built-in TypeScript definitions
  • Support for the style-src 'self'; Content Security Policy

Changed

  • Rewritten as a native ES6 module using spin.js v4

Removed

  • Deprecated jQuery API
  • Deprecated enable and disable methods
  • Bower support
  • Support for Internet Explorer 9 (not supported by spin.js v4)

1.0.6 - 2018-02-04

Added

  • Support for forms with novalidate attribute (issue #80)

Fixed

  • Protruding outline in Chrome on buttons with a progress bar

1.0.5 - 2017-09-24

Added

  • Validation for URL input fields in IE 9

Fixed

  • Detection of required HTML5 inputs in IE 9
  • Bug where the stop() method would remove a button's disabled state even though Ladda was already stopped (issue #68)

1.0.4 - 2017-08-27

Fixed

  • Incorrect return value for jQuery isLoading argument (PR #62, issue #65)

Note that the Ladda jQuery API is deprecated - it is recommended to use the plain JavaScript API instead.

1.0.3 - 2017-08-27

Changed

  • Code cleanup and performance improvements

Deprecated

  • Undocumented enable and disable instance methods

1.0.2 - 2017-08-27

Added

  • Support for right-to-left text direction (PR #66)

Deprecated

  • jQuery API

1.0.1 - 2017-07-11

Fixed

  • Bug where focus outline protruded outside button in Chrome

Removed

  • Unnecessary toArray() function

1.0.0 - 2016-03-08

Added

  • data-spinner-lines attribute for controlling the number of lines in the spinner (PR #50)

Changed

  • The ladda-button class and data-style attribute are now automatically set if missing (PR #52)
  • ladda-label now wraps existing elements instead of reinserting them using innerHTML (PR #55)
  • checkValidity is now used to validate forms where supported (PR #58)

0.9.8 - 2015-04-05

Added

  • main field to package.json for easier CommonJS loading (PR #47)
  • Validity check for email inputs (PR #48)

Changed

  • Updated spin.js to v2.0.2 (PR #49)

0.9.7 - 2015-01-17

Fixed

  • The Ladda theme is now baked into the output ladda.min.css file again. Was broken after upgrading grunt Sass in v0.9.5.

0.9.6 - 2015-01-16

Removed

  • Unintended references to non-existing sourcemap files that were introduced in v0.9.5 as part of the grunt Sass upgrade.

0.9.5 - 2015-01-16

Fixed

  • Validation of required select, radio, and checkbox inputs (PR #43)
  • The ladda-spinner element is no longer created if it already exists (PR #44)

0.9.4 - 2014-06-21

Added

  • remove() method to Ladda instances to stop memory leaks (PR #36)

Changed

  • Updated from spin.js v1.3 to v2.0 (PR #37)

0.9.3 - 2014-04-16

Added

  • jQuery wrapper (PR #33)

Fixed

  • Overlay effect in Safari
  • Missing CommonJS requirement

0.9.2 - 2013-12-03

Fixed

  • Spinner height is now calculated when spinning starts as opposed to when Ladda is initialized. This fixes spinner sizing issues with buttons that are initially hidden.

0.9.1 - 2013-11-27

Fixed

  • Issue where loading animations did not start for buttons outside of forms. This only affected use through the Ladda.bind() method.

0.9.0 - 2013-10-23

Changed

  • Ladda now confirms that all required fields in its parent form are filled out before starting the spinning animation. Note that this only applies to buttons bound using Ladda.bind().

Removed

  • jQuery dependencies

0.8.0 - 2013-09-05

Added

  • data-spinner-color attribute for configuring spinner color

Changed

  • Disabled pointer events on spinner element

Fixed

  • Array conversion bug which prevented binding Ladda to a selector
  • Default z-index value, was initial but should be auto

0.7.0 - 2013-07-19

Added

  • startAfter(delay) method

Changed

  • Limit progress value to number between 0 and 1

Fixed

  • Issue that prevented forms from submitting
  • Error that caused IE 8 to blow up on page load (still not supported, though)

0.6.0 - 2013-07-07

Added

  • data-spinner-size attribute for setting explicit pixel size of spinner

Changed

  • ladda-label wrapper is now automatically created if it doesn't exist

0.5.2 - 2013-06-27

Added

  • bower.json

Fixed

  • Error when passing element to Ladda.create()

0.5.1 - 2013-06-19

Added

  • Bootstrap integration example

0.5.0 - 2013-06-19

Changed

  • Split visual and functional styles into separate stylesheets for framework compatibility (issue #20)
  • Spinner size is now automatically calculated using JavaScript

0.4.2 - 2013-06-14

Added

  • Size options (issue #15)
  • Mint color preset

Changed

  • All settings are now applied via data-* attributes

0.4.1 - 2013-06-14

Added

  • enable and disable helper methods

0.4.0 - 2013-06-14

Added

  • Common.js / AMD module support
  • Grunt build file and minified files in /dist folder

Changed

  • Migrated from plain CSS to Sass

0.3.0 - 2013-06-08

Added

  • Built-in progress bars which can be controlled using setProgress method

Changed

  • Replaced spinner GIF with spin.js
  • spinner and label classes are now prefixed with ladda-

Removed

  • Hardcoded timeout

0.2.0 - 2013-06-05

Added

  • Simple JavaScript API
  • Instructions in readme
  • MIT license

0.1.0 - 2013-06-05

  • Initial release