-
Notifications
You must be signed in to change notification settings - Fork 598
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
Adds open, onOpenChange, and anchorRef props to DropdownMenu #1372
Changes from all commits
e264181
e1cfaf9
cd9431d
113a7a2
8f67fe8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@primer/components': patch | ||
--- | ||
|
||
Extends DropdownMenu to allow anchorRef, open, and onOpenChange props. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import React from 'react' | |
import {theme, ThemeProvider} from '..' | ||
import {ItemInput} from '../ActionList/List' | ||
import BaseStyles from '../BaseStyles' | ||
import Box from '../Box' | ||
import {DropdownMenu, DropdownButton} from '../DropdownMenu' | ||
import TextInput from '../TextInput' | ||
|
||
|
@@ -52,3 +53,32 @@ export function FavoriteColorStory(): JSX.Element { | |
) | ||
} | ||
FavoriteColorStory.storyName = 'Favorite Color' | ||
|
||
export function ExternalAnchorStory(): JSX.Element { | ||
const items = React.useMemo(() => [{text: '🔵 Cyan'}, {text: '🔴 Magenta'}, {text: '🟡 Yellow'}], []) | ||
const [selectedItem, setSelectedItem] = React.useState<ItemInput | undefined>() | ||
const anchorRef = React.useRef<HTMLDivElement>(null) | ||
const [open, setOpen] = React.useState(false) | ||
|
||
return ( | ||
<Box display="flex" flexDirection="column" alignItems="flex-start"> | ||
<Box display="flex" flexDirection="row"> | ||
<DropdownButton onClick={() => setOpen(true)}>Click me to open the dropdown</DropdownButton> | ||
<Box ref={anchorRef} bg="papayawhip" p={4} ml={40} borderRadius={2} display="inline-block"> | ||
Anchored on me! | ||
</Box> | ||
</Box> | ||
<DropdownMenu | ||
renderAnchor={null} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is explicitly setting There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. I tried to mirror
It's a little wonky, but my inclination is to try to clean all of these up in a future PR. I'd like to hear your thoughts though! |
||
anchorRef={anchorRef} | ||
open={open} | ||
onOpenChange={setOpen} | ||
placeholder="🎨" | ||
items={items} | ||
selectedItem={selectedItem} | ||
onChange={setSelectedItem} | ||
/> | ||
</Box> | ||
) | ||
} | ||
ExternalAnchorStory.storyName = 'DropdownMenu with External Anchor' |
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.
Just curious, what happens if both
renderAnchor
andanchorRef
are passed toDropdownMenu
?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 they're both passed, the
renderAnchor
function is called withanchorRef
as a prop (which would mean putting the same ref in two DOM nodes, which seems wrong but I don't know how it'd break. It's almost certainly not an intended usage, but I'm aiming to mirror what I saw elsewhere.I spoke with @dgreif about this yesterday, and it sounds like the prop types in
AnchoredOverlay
could have been narrowed (specifically inAnchoredOverlayPropsWithAnchor
such that anchorRef did not exist), except it made destructuring props messier.