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

Add overlay props to Autocomplete.Overlay #1967

Merged
merged 3 commits into from
Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .changeset/ten-apes-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@primer/react": patch
---

Add overlay props to Autocomplete.Overlay
3 changes: 2 additions & 1 deletion docs/content/Autocomplete.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ The `Autocomplete.Input` should not be rendered without a `<label>` who's `htmlF

### Autocomplete.Overlay

The `Autocomplete.Overlay` wraps the `Autocomplete.Menu` to display it in an [Overlay]() component.
The `Autocomplete.Overlay` wraps the `Autocomplete.Menu` to display it in an [Overlay](./Overlay) component.
This component takes the same props as the `Overlay` component.
Most `Autocomplete` implementations will use the `Autocomplete.Overlay` component, but there could be special cases where the `Autocomplete.Menu` should be rendered directly after the `Autocomplete.Input` (for example: an `Autocomplete` that is already being rendered in an `Overlay`).

### Autocomplete.Menu
Expand Down
11 changes: 9 additions & 2 deletions src/Autocomplete/AutocompleteOverlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,20 @@ type AutocompleteOverlayInternalProps = {
*/
overlayProps?: Partial<OverlayProps>
children?: React.ReactNode
} & Pick<React.AriaAttributes, 'aria-labelledby'> // TODO: consider making 'aria-labelledby' required
} & Partial<OverlayProps> &
Pick<React.AriaAttributes, 'aria-labelledby'> // TODO: consider making 'aria-labelledby' required

function AutocompleteOverlay({menuAnchorRef, overlayProps, children}: AutocompleteOverlayInternalProps) {
function AutocompleteOverlay({
menuAnchorRef,
overlayProps: oldOverlayProps,
children,
...newOverlayProps
}: AutocompleteOverlayInternalProps) {
const autocompleteContext = useContext(AutocompleteContext)
if (autocompleteContext === null) {
throw new Error('AutocompleteContext returned null values')
}
const overlayProps = {...oldOverlayProps, ...newOverlayProps}
Copy link
Member

@siddharthkp siddharthkp Mar 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backwards compatibility! love to see it!

const {inputRef, scrollContainerRef, selectedItemLength, setShowMenu, showMenu = false} = autocompleteContext
const {floatingElementRef, position} = useAnchoredPosition(
{
Expand Down
7 changes: 1 addition & 6 deletions src/stories/Autocomplete.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -558,12 +558,7 @@ export const WithCustomOverlayProps = () => {
</Text>
<Autocomplete>
<Autocomplete.Input id="autocompleteInput" />
<Autocomplete.Overlay
overlayProps={{
width: 'large',
height: 'xsmall'
}}
>
<Autocomplete.Overlay width="large" height="xsmall">
<Autocomplete.Menu items={items} selectedItemIds={[]} aria-labelledby="autocompleteLabel" />
</Autocomplete.Overlay>
</Autocomplete>
Expand Down