Browse Source

Specify modal selector in data attribute with backward compatibility.

juriejan-modal-selector-attribute
Jurie-Jan Botha 10 years ago committed by XhmikosR
parent
commit
e2cf27cf59
  1. 8
      docs/components.html
  2. 15
      js/modals.js

8
docs/components.html

@ -1107,7 +1107,7 @@ document
<h3 class="component-title">Modals</h3> <h3 class="component-title">Modals</h3>
<div class="component-example"> <div class="component-example">
<a href="#myModalexample" class="btn">Open modal</a> <a href="#" class="btn" data-modal="myModalExample">Open modal</a>
<div id="myModalexample" class="modal"> <div id="myModalexample" class="modal">
<header class="bar bar-nav"> <header class="bar bar-nav">
<a class="icon icon-close pull-right" href="#myModalexample"></a> <a class="icon icon-close pull-right" href="#myModalexample"></a>
@ -1118,8 +1118,10 @@ document
</div> </div>
</div> </div>
<p class="component-description">Modals are designed to only fire from links. Set the value of the toggle link's data-modal attribute to a selector that will produce the desired modal.</p>
{% highlight html %} {% highlight html %}
<a href="#myModalexample" class="btn">Open modal</a> <a href="#" class="btn" data-modal="#myModalExample">Open modal</a>
<div id="myModalexample" class="modal"> <div id="myModalexample" class="modal">
<header class="bar bar-nav"> <header class="bar bar-nav">
<a class="icon icon-close pull-right" href="#myModalexample"></a> <a class="icon icon-close pull-right" href="#myModalexample"></a>
@ -1132,7 +1134,7 @@ document
</div> </div>
{% endhighlight %} {% endhighlight %}
<p class="component-description">Modals are designed to only fire from links. Set the value of the toggle links href to the id of a modal.</p> <p class="component-description">DEPRECATED: Alternatively you can specify the selector of the desired modal in the href attribute of the link. This will fail without a warning if a modal is not found.</p>
</article> </article>

15
js/modals.js

@ -32,9 +32,22 @@
var getModal = function (event) { var getModal = function (event) {
var modalToggle = findModals(event.target); var modalToggle = findModals(event.target);
if (modalToggle && modalToggle.hash) { if (modalToggle) {
var modalSelector = modalToggle.getAttribute('data-modal');
if (modalSelector) {
return document.querySelector(modalSelector);
}
else if (modalToggle.hash) {
try {
return document.querySelector(modalToggle.hash); return document.querySelector(modalToggle.hash);
} }
catch (error) {
if (error.name !== 'SyntaxError') {
throw error;
}
}
}
}
}; };
window.addEventListener('touchend', function (event) { window.addEventListener('touchend', function (event) {

Loading…
Cancel
Save