-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
fix(modal): adjust modal background to avoid shifting #2508
fix(modal): adjust modal background to avoid shifting #2508
Conversation
0823f0f
to
1cb52d8
Compare
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.
LGTM, a couple of minor comments.
It's a shame to do a couple of style recalcs and a layout, but it's what bootstrap does indeed. Can't find a better solution either.
src/util/scrollbar.ts
Outdated
@@ -0,0 +1,46 @@ | |||
import {Injectable, Inject} from '@angular/core'; | |||
import {DOCUMENT} from '@angular/platform-browser'; |
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.
@angular/common
, this one should be deprecated
src/util/scrollbar.ts
Outdated
import {Injectable, Inject} from '@angular/core'; | ||
import {DOCUMENT} from '@angular/platform-browser'; | ||
|
||
import {isDefined} from './util'; |
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.
unused
src/util/scrollbar.ts
Outdated
return this.adjustBody(width); | ||
} | ||
|
||
adjustBody(width: number) { |
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.
adjustBody
, isPresent
, getWidth
→ private _adjustBody
, private _isPresent
, private _getWidth
1cb52d8
to
e5f0da1
Compare
src/util/scrollbar.ts
Outdated
private _adjustBody(width: number) { | ||
const {body} = this._document; | ||
const userSetPadding = body.style.paddingRight; | ||
const paddingAmount = parseFloat(window.getComputedStyle(body)['padding-right']); |
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.
Hmm, I was just fixing SSR for the carousel and realised that window
might break it too...
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.
Regardless, I don't think the fix for this should be a part of this PR
@@ -45,6 +46,7 @@ export class NgbModalStack { | |||
let windowCmptRef: ComponentRef<NgbModalWindow> = this._attachWindowComponent(containerEl, contentRef); | |||
let ngbModalRef: NgbModalRef = new NgbModalRef(windowCmptRef, contentRef, backdropCmptRef, options.beforeDismiss); | |||
|
|||
ngbModalRef.result.then(revertPaddingForScrollBar, revertPaddingForScrollBar); |
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.
Maybe write as ngbModalRef.result.finally(revertPaddingForScrollBar)
?
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.
Forget it, it seems like browser support is still lacking... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally
src/util/scrollbar.ts
Outdated
export class ScrollBar { | ||
constructor(@Inject(DOCUMENT) private _document) {} | ||
|
||
compensate() { |
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.
Could you please add explicit return type so it is easier to read? We've got a return
statement in this function but it is not immediately clear what is returned.
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.
Yes, I'll add it along with documentation about it
src/util/scrollbar.ts
Outdated
|
||
|
||
@Injectable() | ||
export class ScrollBar { |
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.
Could you add a bit of JS doc to this class with a brief description on how it works, what it can be used for etc.?
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.
Yes, since I finally made it as a separate utility it makes sense
src/util/scrollbar.ts
Outdated
} | ||
|
||
private _adjustBody(width: number) { | ||
const {body} = this._document; |
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.
Wouldn't explicit const body = this._document.body;
be more readable here?
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.
Same on line 33, if you want to do something with it. But it's just a question of style to me...
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.
Well, destructuring is standard feature, used elsewhere in this project, and avoids repetition. Then I could say it makes more sense when destructuring pure data objects (like parameters). I don't know 😛
src/util/scrollbar.ts
Outdated
return rect.left + rect.right < window.innerWidth; | ||
} | ||
|
||
private _getWidth(): number { |
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.
This could be called only once, if I'm not mistaken? (I mean, we don't have to call it in each and every compensate()
call.
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.
I don't know... could there be any situation where the width of the scrollbar would change (with custom styling then) throughout the application lifetime? (and would we support that?)
If not, I can cache the result.
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.
Let's leave it as-is for now.
src/util/scrollbar.ts
Outdated
|
||
compensate() { | ||
if (!this._isPresent()) { | ||
return () => {}; |
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.
nit: this is always the same, you could extract it as const noop = () => {};
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.
Sure I will do
@ymeine thnx for this PR, it fixes a long-lasting bug! I've added few comments in the code - I would mostly like to see more docs (or at least types!). |
src/util/scrollbar.ts
Outdated
const document = this._document; | ||
const {body} = document; | ||
|
||
const measurer = document.createElement('div'); |
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.
By the way just realised that this is a lot more confusing to me. Unclear if it's a global document
(wrong) or a local one.
(nitpicking, feel free to ignore)
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.
Yeah... again that's a matter of style. And here the context is not far to check. I made like that to avoid repetition, but also to write in a more familiar style - which is exactly what is making you confused ^^
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.
If you ask me I would:
- drop the
document
const (and use this._document everywhere) - have
const documentBody = this._document.createElement('div');
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.
I see the word 'document' 4 times in the snippet above, so I won't agree with the repetition argument :)
vs
const body = this._document.body; // or const {body} = this._document;
const measurer = this._document.createElement('div')
I can read your version fine too though
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.
I was referring to repetition of property access mainly. But anyways this case is too small to have real benefit. I'll make the change.
Shifting potentially occurs when a scrollbar present on the container disappears once the modal opens. The solution is taken from Bootstrap itself. Remaining: - tests
e5f0da1
to
4cbea01
Compare
Shifting potentially occurs when a scrollbar present on the container disappears once the modal opens.
The solution is taken from Bootstrap itself.
Tests are missing but require an e2e testing application.
fixes #641