-
Notifications
You must be signed in to change notification settings - Fork 162
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: Prevent dropdown misplacement in iOS #3202
Merged
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
316f946
fix: Prevent dropdown misplacement in iOS
jperals 0f0296f
Revert "fix: Prevent dropdown misplacement in iOS"
jperals 5711fa4
Use absolute position
jperals 5a37cd4
Use absolute positioning only when iOS virtual keyboard is present
jperals 69c948d
Refactor
jperals ef79ef3
Apply absolute positioning on mobile
jperals 614d7cf
Use scrollLeft also in RTL
jperals d0bd249
Add unit test coverage
jperals 973698b
Add comments
jperals aa7c16f
Refactor tests
jperals 69c6207
Typo
jperals 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
91 changes: 91 additions & 0 deletions
91
src/internal/components/dropdown/__tests__/dropdown-position.test.ts
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,91 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { applyDropdownPositionRelativeToViewport } from '../../../../../lib/components/internal/components/dropdown/dropdown-position'; | ||
|
||
describe('applyDropdownPositionRelativeToViewport', () => { | ||
const triggerRect = { | ||
blockSize: 50, | ||
inlineSize: 100, | ||
insetBlockStart: 100, | ||
insetInlineStart: 100, | ||
insetBlockEnd: 150, | ||
insetInlineEnd: 200, | ||
}; | ||
|
||
const baseDropdownPosition = { | ||
blockSize: '100px', | ||
inlineSize: '100px', | ||
insetInlineStart: '100px', | ||
dropBlockStart: false, | ||
dropInlineStart: false, | ||
}; | ||
|
||
test("sets block end when the dropdown is anchored to the trigger's block start (expands up)", () => { | ||
const dropdownElement = document.createElement('div'); | ||
applyDropdownPositionRelativeToViewport({ | ||
dropdownElement, | ||
triggerRect, | ||
position: { ...baseDropdownPosition, dropBlockStart: true }, | ||
isMobile: false, | ||
}); | ||
expect(dropdownElement.style.insetBlockEnd).toBeTruthy(); | ||
expect(dropdownElement.style.insetBlockStart).toBeFalsy(); | ||
}); | ||
|
||
test("aligns block start with the trigger's block end when the dropdown is anchored to the trigger's block end (expands down)", () => { | ||
const dropdownElement = document.createElement('div'); | ||
applyDropdownPositionRelativeToViewport({ | ||
dropdownElement, | ||
triggerRect, | ||
position: baseDropdownPosition, | ||
isMobile: false, | ||
}); | ||
expect(dropdownElement.style.insetBlockEnd).toBeFalsy(); | ||
expect(dropdownElement.style.insetBlockStart).toEqual(`${triggerRect.insetBlockEnd}px`); | ||
}); | ||
|
||
test("aligns inline start with the trigger's inline start when the dropdown is anchored to the trigger's inline start (anchored from the left in LTR)", () => { | ||
const dropdownElement = document.createElement('div'); | ||
applyDropdownPositionRelativeToViewport({ | ||
dropdownElement, | ||
triggerRect, | ||
position: baseDropdownPosition, | ||
isMobile: false, | ||
}); | ||
expect(dropdownElement.style.insetInlineStart).toEqual(`${triggerRect.insetInlineStart}px`); | ||
}); | ||
|
||
test("sets inline end when the dropdown is anchored to the trigger's inline start (anchored from the right in LTR)", () => { | ||
const dropdownElement = document.createElement('div'); | ||
applyDropdownPositionRelativeToViewport({ | ||
dropdownElement, | ||
triggerRect, | ||
position: { ...baseDropdownPosition, dropInlineStart: true }, | ||
isMobile: false, | ||
}); | ||
expect(dropdownElement.style.insetInlineStart).toBeTruthy(); | ||
}); | ||
|
||
test('uses fixed position on desktop', () => { | ||
const dropdownElement = document.createElement('div'); | ||
applyDropdownPositionRelativeToViewport({ | ||
dropdownElement, | ||
triggerRect, | ||
position: baseDropdownPosition, | ||
isMobile: false, | ||
}); | ||
expect(dropdownElement.style.position).toEqual('fixed'); | ||
}); | ||
|
||
test('uses absolute position on mobile', () => { | ||
const dropdownElement = document.createElement('div'); | ||
applyDropdownPositionRelativeToViewport({ | ||
dropdownElement, | ||
triggerRect, | ||
position: baseDropdownPosition, | ||
isMobile: true, | ||
}); | ||
expect(dropdownElement.style.position).toEqual('absolute'); | ||
}); | ||
}); |
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,49 @@ | ||
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { DropdownPosition } from './dropdown-fit-handler'; | ||
|
||
export interface LogicalDOMRect { | ||
blockSize: number; | ||
inlineSize: number; | ||
insetBlockStart: number; | ||
insetBlockEnd: number; | ||
insetInlineStart: number; | ||
insetInlineEnd: number; | ||
} | ||
|
||
// Applies its position to the dropdown element when expandToViewport is set to true. | ||
export function applyDropdownPositionRelativeToViewport({ | ||
position, | ||
dropdownElement, | ||
triggerRect, | ||
isMobile, | ||
}: { | ||
position: DropdownPosition; | ||
dropdownElement: HTMLElement; | ||
triggerRect: LogicalDOMRect; | ||
isMobile: boolean; | ||
}) { | ||
// Fixed positions is not respected in iOS when the virtual keyboard is being displayed. | ||
// For this reason we use absolute positioning in mobile. | ||
const useAbsolutePositioning = isMobile; | ||
|
||
// Since when using expandToViewport=true the dropdown is attached to the root of the body, | ||
// the same coordinates can be used for fixed or absolute position, | ||
// except when using absolute position we need to take into account the scroll position of the body itself. | ||
const verticalScrollOffset = useAbsolutePositioning ? document.documentElement.scrollTop : 0; | ||
const horizontalScrollOffset = useAbsolutePositioning ? document.documentElement.scrollLeft : 0; | ||
|
||
dropdownElement.style.position = useAbsolutePositioning ? 'absolute' : 'fixed'; | ||
|
||
if (position.dropBlockStart) { | ||
dropdownElement.style.insetBlockEnd = `calc(100% - ${verticalScrollOffset + triggerRect.insetBlockStart}px)`; | ||
} else { | ||
dropdownElement.style.insetBlockStart = `${verticalScrollOffset + triggerRect.insetBlockEnd}px`; | ||
} | ||
if (position.dropInlineStart) { | ||
dropdownElement.style.insetInlineStart = `calc(${horizontalScrollOffset + triggerRect.insetInlineEnd}px - ${position.inlineSize})`; | ||
} else { | ||
dropdownElement.style.insetInlineStart = `${horizontalScrollOffset + triggerRect.insetInlineStart}px`; | ||
} | ||
} |
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.
In a couple cases (here and 3 tests further down) I'm testing just that a value is set (truthy), instead of the actual value. This is because the value is a CSS expression using
calc()
, and I don't think it makes sense to be so specific about the implementation. We already have visual regression tests for the expected user-facing end result, which is what matters.