-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modal | JS | Add page scrolling option #1304
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2584e00
feat: update custom events to bubble and include details about body s…
90071df
docs: add demo page showing how to set padding on element when modal …
5a731b0
feat: move scrollbar helpers to core, scrollbar calc functions to Cla…
29b622f
fix: pattern lab override, position 'relative' causing modal choppine…
353af95
docs: hide modal custom-events page for now
5904236
fix: remove unused classname
5b6ab74
fix: rename 'preventBodyScroll' to 'noBodyScroll'
702200a
fix: remove 'noBodyScroll' prop in favor of private variable
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
132 changes: 132 additions & 0 deletions
132
...s/pattern-lab/_patterns/02-components/modal/custom-events/_modal-usage-custom-events.twig
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,132 @@ | ||
{% set javascript %} | ||
<script> | ||
// If the browser body has scrollbar, set padding on the element the width of the scrollbar | ||
const setScrollbarPadding = function(element, hasScrollbar, scrollbarWidth) { | ||
if (!element) { | ||
return; | ||
} | ||
|
||
if (hasScrollbar) { | ||
const originalPadding = element.style.paddingRight; | ||
const calculatedPadding = window.getComputedStyle(element)[ | ||
'padding-right' | ||
]; | ||
|
||
// Save original padding value for later | ||
element.setAttribute('data-padding-right', originalPadding); | ||
element.style.paddingRight = parseFloat(calculatedPadding) + scrollbarWidth + 'px'; | ||
} | ||
} | ||
|
||
// Reset the padding on that element after modal is hidden | ||
const resetScrollbarPadding = function(element) { | ||
if (!element) { | ||
return; | ||
} | ||
|
||
const padding = element.getAttribute('data-padding-right'); | ||
|
||
element.style.paddingRight = ''; | ||
|
||
if (typeof padding === 'undefined') { | ||
element.style.paddingRight = ''; | ||
} else { | ||
element.removeAttribute('data-padding-right'); | ||
// Restore original padding value | ||
element.style.paddingRight = padding; | ||
} | ||
} | ||
|
||
// Arbitrary fixed element, could be .c-page-header | ||
const fixedElement = document.querySelector('.placeholder-fixed-element'); | ||
|
||
// Listen for 'modal:show' on the body, event will bubble up from the modal | ||
document.body.addEventListener('modal:show', function(e) { | ||
setScrollbarPadding(fixedElement, e.detail.hasScrollbar, e.detail.scrollbarWidth); | ||
}); | ||
|
||
// Listen for 'modal:hidden', fires after the modal has animated out | ||
document.body.addEventListener('modal:hidden', function(e) { | ||
resetScrollbarPadding(fixedElement); | ||
}); | ||
</script> | ||
{% endset %} | ||
|
||
<bolt-text headline font-size="xlarge" style="margin-top: 120px;">Modal Custom Events</bolt-text> | ||
<bolt-text>Bolt Modal emits the following custom events: <code>modal:show</code>, <code>modal:shown</code>, <code>modal:hide</code>, <code>modal:hidden</code>.</bolt-text> | ||
|
||
<div class="placeholder-fixed-element" style="background: yellow; padding: 2rem; text-align: center; position: fixed; top: 0; height: 100px; width: 100%;"> | ||
Placeholder "fixed" element, should not shift when modal shows/hides. | ||
</div> | ||
|
||
<bolt-text headline font-size="large">Demo</bolt-text> | ||
<div class="t-bolt-light u-bolt-padding-medium u-bolt-margin-bottom-small"> | ||
{% set modal_content %} | ||
{% include "@bolt-components-video/video.twig" with { | ||
videoId: "3861325118001", | ||
accountId: "1900410236", | ||
playerId: "r1CAdLzTW", | ||
showMeta: true, | ||
showMetaTitle: true, | ||
attributes: { | ||
class: "js-modal-video-123" | ||
} | ||
} only %} | ||
{% endset %} | ||
|
||
{% set trigger %} | ||
{% include "@bolt-components-button/button.twig" with { | ||
text: "Play the video", | ||
size: "small", | ||
width: "full", | ||
attributes: { | ||
"on-click": "show", | ||
"on-click-target": "js-modal-123" | ||
} | ||
} only %} | ||
{% include "@bolt-components-modal/modal.twig" with { | ||
attributes: { | ||
class: "js-modal-123" | ||
}, | ||
content: modal_content, | ||
width: "optimal", | ||
spacing: "none", | ||
theme: "none", | ||
scroll: "overall", | ||
} only %} | ||
{{ javascript }} | ||
{% endset %} | ||
{% set description %} | ||
<bolt-text headline font-size="large">Set padding on a "fixed" element when modal shows/hides</bolt-text> | ||
<bolt-text>Use the <code>modal:show</code> and <code>modal:hidden</code> events to set padding on a fixed element to prevent it from shifting.</bolt-text> | ||
<bolt-text>Note: for this example, you must use <code>modal:hidden</code> not <code>modal:hide</code> event, as <code>modal:hidden</code> fires after the modal animation, and that is key to getting the correct <code>hasScrollbar</code> state.</bolt-text> | ||
{% endset %} | ||
{% include "@bolt-components-grid/grid.twig" with { | ||
items: [ | ||
{ | ||
column_start: "1 1@small", | ||
column_span: "12 8@small 9@medium", | ||
row_start: "2 1@small", | ||
row_span: "1", | ||
valign: "center", | ||
content: description, | ||
}, | ||
{ | ||
column_start: "1 10@small 11@medium", | ||
column_span: "6 3@small 2@medium", | ||
row_start: "1 1@small", | ||
row_span: "1", | ||
valign: "center", | ||
content: trigger, | ||
}, | ||
] | ||
} only %} | ||
</div> | ||
|
||
<bolt-text headline font-size="large">Custom Javascript</bolt-text> | ||
<bolt-code-snippet syntax="dark" lang="html">{% spaceless %} | ||
{{ javascript | replace({ | ||
'<': '<', | ||
'>': '>', | ||
}) | trim | raw }} | ||
{% endspaceless %}</bolt-code-snippet> |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
+1 for commenting on this