-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(combobox): fix positioning (#3459)
* chore(combobox): fix positioning * chore: initialize open * chore: final small fix * fix: chromatic skip * fix: random aria errors on sidebar fullcompositions
- Loading branch information
Showing
10 changed files
with
145 additions
and
25 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@twilio-paste/combobox': patch | ||
'@twilio-paste/core': patch | ||
--- | ||
|
||
[Combobox] Now opens the dropdown menu upwards when positioned towards the bottom of the viewport. |
65 changes: 65 additions & 0 deletions
65
packages/paste-core/components/combobox/src/ListboxPositioner.tsx
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,65 @@ | ||
import * as React from 'react'; | ||
import {Box, type BoxStyleProps} from '@twilio-paste/box'; | ||
import {useRect} from '@radix-ui/react-use-rect'; | ||
import {useWindowSize} from '@twilio-paste/utils'; | ||
|
||
interface ListBoxPositionerProps { | ||
children: React.ReactNode; | ||
inputBoxRef: React.RefObject<HTMLElement>; | ||
dropdownBoxRef: React.RefObject<HTMLElement>; | ||
} | ||
|
||
export const ListBoxPositioner: React.FC<ListBoxPositionerProps> = ({inputBoxRef, dropdownBoxRef, ...props}) => { | ||
const {height: windowHeight} = useWindowSize(); | ||
const inputBoxDimensions = useRect(inputBoxRef.current); | ||
const dropdownBoxDimensions = useRect(dropdownBoxRef.current); | ||
const dropdownBoxHeight = dropdownBoxDimensions?.height; | ||
|
||
const styles = React.useMemo((): BoxStyleProps => { | ||
if (dropdownBoxHeight == null || inputBoxDimensions == null || dropdownBoxHeight === 0) { | ||
return {}; | ||
} | ||
|
||
/* | ||
* Scenarios: | ||
* 1- Dropdown height is bigger than window height | ||
* - Then show at the top of the viewport | ||
* 2- Dropdown height + inputbox bottom is bigger than viewport height | ||
* - Show upwards | ||
* 3- Dropdown height + inputbox bottom is smaller than viewport height | ||
* - Show downwards | ||
*/ | ||
if (windowHeight) { | ||
if (dropdownBoxHeight >= windowHeight) { | ||
return { | ||
position: 'fixed', | ||
top: 0, | ||
left: inputBoxDimensions?.left, | ||
right: inputBoxDimensions?.right, | ||
width: inputBoxDimensions?.width, | ||
}; | ||
} | ||
if (dropdownBoxHeight + inputBoxDimensions?.bottom >= windowHeight) { | ||
return { | ||
position: 'fixed', | ||
// 6px to account for border things, should be fine on all themes | ||
top: inputBoxDimensions?.top - dropdownBoxHeight - 6, | ||
left: inputBoxDimensions?.left, | ||
right: inputBoxDimensions?.right, | ||
width: inputBoxDimensions?.width, | ||
}; | ||
} | ||
} | ||
|
||
return { | ||
position: 'fixed', | ||
top: inputBoxDimensions?.bottom, | ||
left: inputBoxDimensions?.left, | ||
right: inputBoxDimensions?.right, | ||
width: inputBoxDimensions?.width, | ||
}; | ||
}, [inputBoxDimensions, dropdownBoxHeight, windowHeight]); | ||
|
||
return <Box {...props} {...styles} zIndex="zIndex90" />; | ||
}; | ||
ListBoxPositioner.displayName = 'ListBoxPositioner'; |
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
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