-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Prevent dropdown misplacement in iOS (#3202)
- Loading branch information
Showing
4 changed files
with
159 additions
and
30 deletions.
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 are 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