Skip to content
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

Added animation when modal backdrop is static #29516

Merged
merged 19 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
9434aaa
Added animation when modal backdrop is static
Oct 12, 2019
a99b27e
Merge branch 'master' of https://github.com/twbs/bootstrap into modal…
Oct 12, 2019
9a28600
Update modal.spec.js
XhmikosR Oct 12, 2019
dbe7577
Merge branch 'master' into modal-backdrop-static
XhmikosR Oct 14, 2019
85e1c04
Refactored modal js code and changed classname
Oct 14, 2019
bc97ef0
Added static backdrop info on docs
Oct 14, 2019
c3ff74a
Merge branch 'modal-backdrop-static' of https://github.com/anjoshigor…
Oct 14, 2019
a19bcc3
Merge branch 'master' of https://github.com/twbs/bootstrap into modal…
Oct 14, 2019
efcc341
Removed comments on JS code
Oct 16, 2019
6f4aee4
Merge branch 'master' of https://github.com/twbs/bootstrap into modal…
Oct 16, 2019
69a000b
Merge branch 'master' of https://github.com/twbs/bootstrap into modal…
Oct 16, 2019
363e838
Merge branch 'master' into modal-backdrop-static
XhmikosR Oct 17, 2019
10e443b
Update modal.md
XhmikosR Oct 17, 2019
53b6d08
Merge branch 'master' into modal-backdrop-static
XhmikosR Oct 17, 2019
4bd9be5
Merge branch 'master' into modal-backdrop-static
XhmikosR Oct 23, 2019
2be6919
Merge branch 'master' into modal-backdrop-static
XhmikosR Oct 24, 2019
71eda1d
Merge branch 'master' of https://github.com/twbs/bootstrap into modal…
Oct 25, 2019
20108a1
Made scale transform a variable
Oct 25, 2019
3441faa
Merge branch 'modal-backdrop-static' of https://github.com/anjoshigor…
Oct 25, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions js/src/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const DefaultType = {

const Event = {
HIDE: `hide${EVENT_KEY}`,
HIDE_PREVENTED: `hidePrevented${EVENT_KEY}`,
HIDDEN: `hidden${EVENT_KEY}`,
SHOW: `show${EVENT_KEY}`,
SHOWN: `shown${EVENT_KEY}`,
Expand All @@ -68,7 +69,8 @@ const ClassName = {
BACKDROP: 'modal-backdrop',
OPEN: 'modal-open',
FADE: 'fade',
SHOW: 'show'
SHOW: 'show',
STATIC: 'modal-static'
}

const Selector = {
Expand Down Expand Up @@ -307,8 +309,8 @@ class Modal {
if (this._isShown && this._config.keyboard) {
EventHandler.on(this._element, Event.KEYDOWN_DISMISS, event => {
if (event.which === ESCAPE_KEYCODE) {
event.preventDefault()
this.hide()
// This will trigger a scale transition if the backdrop is static
this._triggerBackdropTransition()
}
})
} else {
Expand Down Expand Up @@ -367,11 +369,8 @@ class Modal {
return
}

if (this._config.backdrop === 'static') {
this._element.focus()
} else {
this.hide()
}
// This will trigger a scale transition if the backdrop is static
this._triggerBackdropTransition()
})

if (animate) {
Expand Down Expand Up @@ -409,6 +408,27 @@ class Modal {
}
}

// This function will trigger a HIDE_PREVENTED event and do a scale animation if
// the backdrop is static
_triggerBackdropTransition() {
if (this._config.backdrop === 'static') {
const hideEvent = EventHandler.trigger(this._element, Event.HIDE_PREVENTED)
if (hideEvent.defaultPrevented) {
return
}

this._element.classList.add(ClassName.STATIC)
const modalTransitionDuration = getTransitionDurationFromElement(this._element)
EventHandler.one(this._element, TRANSITION_END, () => {
this._element.classList.remove(ClassName.STATIC)
})
emulateTransitionEnd(this._element, modalTransitionDuration)
this._element.focus()
} else {
this.hide()
}
}

// ----------------------------------------------------------------------
// the following methods are used to handle overflowing modals
// ----------------------------------------------------------------------
Expand Down
27 changes: 27 additions & 0 deletions js/tests/unit/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,33 @@ describe('Modal', () => {
modal.show()
})

it('should not close modal when clicking outside of modal-content if backdrop = static', done => {
fixtureEl.innerHTML = '<div class="modal" data-backdrop="static" ><div class="modal-dialog" /></div>'

const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: 'static'
})

const shownCallback = () => {
setTimeout(() => {
expect(modal._isShown).toEqual(true)
done()
}, 10)
}

modalEl.addEventListener('shown.bs.modal', () => {
modalEl.click()
shownCallback()
})

modalEl.addEventListener('hidden.bs.modal', () => {
throw new Error('Should not hide a modal')
})

modal.show()
})

it('should not adjust the inline body padding when it does not overflow', done => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog" /></div>'

Expand Down
5 changes: 5 additions & 0 deletions scss/_modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
.modal.show & {
transform: $modal-show-transform;
}

// When trying to close, animate focus to scale
.modal.modal-static & {
MartijnCuppens marked this conversation as resolved.
Show resolved Hide resolved
transform: scale(1.02);
MartijnCuppens marked this conversation as resolved.
Show resolved Hide resolved
}
}

.modal-dialog-scrollable {
Expand Down
65 changes: 64 additions & 1 deletion site/content/docs/4.3/components/modal.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,65 @@ Toggle a working modal demo by clicking the button below. It will slide down and
</div>
{{< /highlight >}}

### Static backdrop

When backdrop is set to static, the modal will not close when clicking outside it. Click the button below to try it.

<div id="staticBackdropLive" class="modal fade" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLiveLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLiveLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<p>I'll not close if you click outside me. Don't even try to press escape key.</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>

<div class="bd-example">
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#staticBackdropLive">
Launch static backdrop modal
</button>
</div>

{{< highlight html >}}
<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#staticBackdrop">
Launch static backdrop modal
</button>

<!-- Modal -->
<div class="modal fade" id="staticBackdrop" data-backdrop="static" tabindex="-1" role="dialog" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</div>
</div>
</div>
{{< /highlight >}}


### Scrolling long content

When modals become too long for the user's viewport or device, they scroll independent of the page itself. Try the demo below to see what we mean.
Expand Down Expand Up @@ -753,7 +812,7 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
<td>backdrop</td>
<td>boolean or the string <code>'static'</code></td>
<td>true</td>
<td>Includes a modal-backdrop element. Alternatively, specify <code>static</code> for a backdrop which doesn't close the modal on click.</td>
<td>Includes a modal-backdrop element. Alternatively, specify <code>static</code> for a backdrop which doesn't close the modal on click or on escape key press.</td>
</tr>
<tr>
<td>keyboard</td>
Expand Down Expand Up @@ -859,6 +918,10 @@ Bootstrap's modal class exposes a few events for hooking into modal functionalit
<td>hidden.bs.modal</td>
<td>This event is fired when the modal has finished being hidden from the user (will wait for CSS transitions to complete).</td>
</tr>
<tr>
<td>hidePrevented.bs.modal</td>
<td>This event is fired when the modal is shown, its backdrop is <code>static</code> and a click outside the modal or a scape key press is performed.</td>
</tr>
XhmikosR marked this conversation as resolved.
Show resolved Hide resolved
</tbody>
</table>

Expand Down