-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement max width for modal via data attribute
This commit adds a max-width aka. different width per modal. Just add the modal-maxwidth plugin to your code and `data-cssmodal-maxwidth` with the value of a pixel-based max-width. Reference #68.
- Loading branch information
Showing
7 changed files
with
162 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* CSS Modal Plugin for setting a max-width for modals | ||
* | ||
* @author Hans Christian Reinl | ||
* @date 2014-05-27 | ||
* | ||
*/ | ||
(function (global) { | ||
'use strict'; | ||
|
||
/** | ||
* Main modal object | ||
*/ | ||
var CSSModal; | ||
|
||
/** | ||
* Include styles into the DOM | ||
* @param {string} rule Styles to inject into the DOM | ||
* @param {string} id Unique ID for styles | ||
*/ | ||
var _injectStyles = function (rule, id) { | ||
var element = document.createElement('div'); | ||
|
||
id = 'modal__rule' + (id || ''); | ||
|
||
// Remove all current rules | ||
if (document.getElementById(id)) { | ||
document.getElementById(id).parentNode.removeChild(document.getElementById(id)); | ||
} | ||
|
||
element.id = id; | ||
element.innerHTML = '<style>' + rule + '</style>'; | ||
|
||
// Append a new rule | ||
document.body.appendChild(element); | ||
}; | ||
|
||
/** | ||
* Scale the modal according to its custom width | ||
*/ | ||
var _scale = function () { | ||
var element = CSSModal.activeElement; | ||
var maxWidth = element.getAttribute('data-cssmodal-maxwidth'); | ||
|
||
if (maxWidth) { | ||
_injectStyles('[data-cssmodal-maxwidth] .modal-inner {' + | ||
'max-width: ' + parseInt(maxWidth, 10) + 'px;' + | ||
'margin-left: -' + (parseInt(maxWidth, 10) / 2) + 'px;' + | ||
'}' + | ||
|
||
'[data-cssmodal-maxwidth] .modal-close:after {' + | ||
'margin-right: -' + (parseInt(maxWidth, 10) / 2) + 'px !important;' + | ||
'}', element.id); | ||
} | ||
}; | ||
|
||
/** | ||
* Plugin API | ||
*/ | ||
var _api = { | ||
scaleMaxSize: _scale | ||
}; | ||
|
||
/** | ||
* Initial call | ||
*/ | ||
var init = function (modal) { | ||
CSSModal = modal; | ||
|
||
/* | ||
* Assign basic event handlers | ||
*/ | ||
CSSModal.on('cssmodal:show', document, _scale); | ||
|
||
// Public API | ||
return _api; | ||
}; | ||
|
||
/* | ||
* AMD, module loader, global registration | ||
*/ | ||
|
||
// Expose modal for loaders that implement the Node module pattern. | ||
if (typeof module === 'object' && module && typeof module.exports === 'object') { | ||
module.exports = _api; | ||
|
||
// Register as an AMD module | ||
} else if (typeof define === 'function' && define.amd) { | ||
|
||
define(['CSSModal'], init); | ||
|
||
// Export CSSModal into global space | ||
} else if (typeof global === 'object' && typeof global.document === 'object') { | ||
init(global.CSSModal); | ||
} | ||
|
||
}(window)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Custom maximum width for a modal | ||
* | ||
* Usage: Set `data-cssmodal-maxwidth` with a pixel-based max-width as a value. | ||
*/ | ||
[data-cssmodal-maxwidth] { | ||
.modal-inner { | ||
width: 100%; | ||
} | ||
|
||
@media screen and (max-width: $modal-max-width + 40) { | ||
.modal-inner { | ||
left: 50%; | ||
} | ||
|
||
.modal-close:after { | ||
right: 50% !important; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70ace64
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice one! gtm