JSFiddle - React, Tailwind, and code Playground

by lollero

HTML

<div class="box"></div>

<div class="box" data-width="300"></div>

<div class="box"></div>

CSS

body { margin: 20px; }

.box {
	width: 20px;
    height: 20px;
    float: left;
    clear: both;
    margin: 20px;
    border: 1px solid #999;
}

.active {
    border: 1px solid green;
}

JavaScript

$(document).ready(function() {

    $('.box').togglebox({
    
        clickEvent: function() {
            var obj = $(this),
                objD = obj.data(),
                arClass = objD.active ? 'addClass' : 'removeClass';
            
            $(this)[ arClass ]('active'); 
            
        }
    
    });

});

(function($) {
  $.fn.extend({
    togglebox: function( options ) {
      
      var defaults = {
          width: 100,
          clickEvent: function() {}
      };
        
      return this.each(function() {

        var obj = $(this),
            objD = obj.data(),
            o = $.extend( true, {}, defaults, options, objD ),
            objW = obj.width();

        obj.data({ "active": false });    

        obj.on("click", function() {

            obj.data({ "active": ( !objD.active ? true : false ) });            
            
            var aniW = objD.active ? o.width : objW;

            obj.stop().animate({ width: aniW }, 400 );

            o.clickEvent.call( obj );
            
        });

                    
      });  

    }
  });
})(jQuery);