Giter Site home page Giter Site logo

timodal's Introduction

TiModal

TiModal is a

- lightweight

- pure javascript

library to create modal dialog.

It's simple, flexible and easy to customize. You can change source code if you want.

How to use TiModal

1. CSS.

Add following CSS rules to your css stylesheet before using TiModal

/* REQUIRED CSS */
.tioverlay {
  position: fixed;
  z-index:100;
  top: 0px;
  left: 0px;
  height:100%;
  width:100%;
  display: none;
  text-align: center;
  overflow-y: auto;
}

/* CUSTOM CSS*/
.popup-wrapper {
  margin: 20px auto;
  display: inline-block;
  background: #fff;
  border-radius: 3px;
  padding: 30px;
  text-align: left;
}

.popup-content-wrapper .close-btn {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 25px;
  height: 25px;
  text-align: center;
  line-height: 25px;
  z-index: 20;
  padding: 0;
}

2. Basic usage

Html code

<button id="show-default-modal" class="btn-default">Default modal</button>

<!-- template for modal -->
<script type="text" id="default-modal">
    <div class="popup-wrapper">
      Hello my friend!
      <br/>
      This is a default modal
      <br/>
      <b>Click on overlay to hide me.</b>
    </div>
  </script>

Bind button clicked event with a function to show modal

Pure Javascript

var button = document.querySelector('#show-default-modal');
button.addEventListener('click', function(){
  var html = document.getElementById("default-modal").innerHTML;
  var modal = tiModal.create(html)
  .show();
});

Using jquery

$('#show-default-modal').click(function(){
  var html = $('#default-modal').html();
  tiModal.create(html).show();
});

3. Binding event using code

Html code

<button id="show-confirm-modal" class="btn-default">Confirm modal</button>

<script type="text" id="confirm-modal">
    <div class="popup-wrapper">
      <div class="popup-header"> Warning </div>
      <div class="popup-content">
        This action cannot be reverted.
        Do you want to proceed?
      </div>
      <div class="popup-footer">
        <button class="btn-success cancel">Bring me back</button>
        <button class="btn-danger ok">Do it!</button>
      </div>
    </div>
  </script>

Javascript

$('#show-confirm-modal').click(function(){
  var html = $('#confirm-modal').html();
  tiModal.create(html, {
    events: {
      'click .cancel': function(e){
        this.close();
      },
      'click .ok': function(e){
        this.close();
        alert('User has been deleted!')
      }
    }
  }).show();
});

4. API

4.1 Public methods

  • Create modal

    TiModal.create(html, options)

    Params

    • html: html template for modal
    • options: see below

    Return Modal object

  • Hide current modal

    TiModal.closeCurrent()

  • Hide all modal

    TiModal.closeAll()

4.2 Modal object methods

  • .show() : show dialog
  • .hide() : hide dialog
  • .dispose() : destroy dialog and remove from the memory

5. All possible options

modal: false | true,
option type default description
modal boolean false false: close modal when click on the overlay true: only close modal when call close modal API

timodal's People

Contributors

bluzky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

timodal's Issues

Update document

Currently Document is out of date.
some features have been removed/added.
Update example code and options in readme.md

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.