Add a modal window into SharePoint - Feedback Button


A project I was tasked with had me looking into how to put a modal JavaScript feedback button that opens a window on the left into SharePoint. Here is what I did to accomplish that.


I took some info from these sources and modified them to accomplish what I wanted to do

The way I tweaked them was to make it open on the left instead of the top, and to not refresh the page every time that it is used.



So I opened SharePoint Designer and opened my custom master page.

I put this in the header:


<style>
#myBtn
{
z-index: 10; 
position: fixed; 
left: 0px; 
bottom: 0px; 
}
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    left: 0;
    top: 0;
    width: 100%; /* Full width */
    height: 100%; /* Full height */
    overflow: auto; /* Enable scroll if needed */
    background-color: rgb(0,0,0); /* Fallback color */
    background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
    -webkit-animation-name: fadeIn; /* Fade in the background */
    -webkit-animation-duration: 0.4s;
    animation-name: fadeIn;
    animation-duration: 0.4s
}

/* Modal Content */
.modal-content {
    position: fixed;
    left: 0;
    background-color: #fefefe;
    height: 100%;
    -webkit-animation-name: slideIn;
    -webkit-animation-duration: 0.4s;
    animation-name: slideIn;
    animation-duration: 0.4s
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding: 2px 16px;
    background-color: #C80000;
    color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
    padding: 2px 16px;
    background-color: #C80000;
    color: white;
}

/* Add Animation */
@-webkit-keyframes slideIn {
    from {left: -300px; opacity: 0} 
    to {left: 0; opacity: 1}
}

@keyframes slideIn {
    from {left: -300px; opacity: 0}
    to {left: 0; opacity: 1}
}

@-webkit-keyframes fadeIn {
    from {opacity: 0} 
    to {opacity: 1}
}

@keyframes fadeIn {
    from {opacity: 0} 
    to {opacity: 1}
}
</style>   


Then in the body, at the end I put this in:


<button id="myBtn">Feedback</button>

<!-- The Modal -->
<div id="myModal" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">&times;</span>
      <h2>Suggest Feedback</h2>
    </div>
    <div class="modal-body">
          Stuff that goes in the body
    <!--<div class="modal-footer">
      <h3></h3>
    </div>-->
  </div>

</div>
<script>
// Get the modal
var modal = document.getElementById('myModal');

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function(event) {
    event.preventDefault();
    modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
    modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
        event.preventDefault();
    }
}
</script>

BONUS!
If you want your link on your modal menu to open your Add Item (NewForm.aspx) in a modal dialog box, add in this snippet of code (Source)

 <script>
function openDialog(pageUrl) {
var options = {
    url: pageUrl,
    title: 'Title of the Dialog',
    allowMaximize: false,
    showClose: true,
    width: 550,
    height: 500
};
SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);     
}

</script>
<a href="#" onclick="openDialog('/Lists/Suggestion%20Box/NewForm.aspx');">Open Feedback Form</a>



Share on Google Plus

About Tom DeMeulenaere

Highly accomplished information technology professional with extensive knowledge in System Center Configuration Manager, Windows Server, SharePoint, and Office 365.
    Blogger Comment

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.