-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Support controlling open/closed state for Dropdown and DropdownMenu #54257
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2fc6530
Allow dropdown open state to be controlled
ciampo d8379d9
Update Dropdown stories to use controlled mode
ciampo bf72b26
Allow DropdownMenu open state to be controlled
ciampo 2d35f36
Update DropdownMenu stories to use controlled mode
ciampo c78a46f
Add temporary storybook example to show how to keep only dropdownmenu…
ciampo 17ad46e
Remove ad-hoc storybook example and restore close callback
ciampo a5a400c
Restore uncontrolled storybook examples
ciampo ab6e115
CHANGELOG
ciampo 1beb750
Add docs for the new dropdown props
ciampo 982f0fa
Add docs for the new DropdownMenu props
ciampo 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,33 +7,18 @@ import type { ForwardedRef } from 'react'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useRef, useState } from '@wordpress/element'; | ||
import { useRef, useState } from '@wordpress/element'; | ||
import { useMergeRefs } from '@wordpress/compose'; | ||
import deprecated from '@wordpress/deprecated'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { contextConnect, useContextSystem } from '../ui/context'; | ||
import { useControlledValue } from '../utils/hooks'; | ||
import Popover from '../popover'; | ||
import type { DropdownProps, DropdownInternalContext } from './types'; | ||
|
||
function useObservableState( | ||
initialState: boolean, | ||
onStateChange?: ( newState: boolean ) => void | ||
) { | ||
const [ state, setState ] = useState( initialState ); | ||
return [ | ||
state, | ||
( value: boolean ) => { | ||
setState( value ); | ||
if ( onStateChange ) { | ||
onStateChange( value ); | ||
} | ||
}, | ||
] as const; | ||
} | ||
|
||
const UnconnectedDropdown = ( | ||
props: DropdownProps, | ||
forwardedRef: ForwardedRef< any > | ||
|
@@ -51,6 +36,9 @@ const UnconnectedDropdown = ( | |
onToggle, | ||
style, | ||
|
||
open, | ||
defaultOpen, | ||
|
||
// Deprecated props | ||
position, | ||
|
||
|
@@ -74,20 +62,12 @@ const UnconnectedDropdown = ( | |
const [ fallbackPopoverAnchor, setFallbackPopoverAnchor ] = | ||
useState< HTMLDivElement | null >( null ); | ||
const containerRef = useRef< HTMLDivElement >(); | ||
const [ isOpen, setIsOpen ] = useObservableState( false, onToggle ); | ||
|
||
useEffect( | ||
() => () => { | ||
if ( onToggle && isOpen ) { | ||
onToggle( false ); | ||
} | ||
}, | ||
[ onToggle, isOpen ] | ||
); | ||
Comment on lines
-79
to
-86
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. Nice one, this is the removal that I was missing from my experiment 🎉 |
||
|
||
function toggle() { | ||
setIsOpen( ! isOpen ); | ||
} | ||
const [ isOpen, setIsOpen ] = useControlledValue( { | ||
defaultValue: defaultOpen, | ||
value: open, | ||
onChange: onToggle, | ||
} ); | ||
|
||
/** | ||
* Closes the popover when focus leaves it unless the toggle was pressed or | ||
|
@@ -112,13 +92,15 @@ const UnconnectedDropdown = ( | |
} | ||
|
||
function close() { | ||
if ( onClose ) { | ||
onClose(); | ||
} | ||
onClose?.(); | ||
setIsOpen( false ); | ||
} | ||
|
||
const args = { isOpen, onToggle: toggle, onClose: close }; | ||
const args = { | ||
isOpen: !! isOpen, | ||
onToggle: () => setIsOpen( ! isOpen ), | ||
onClose: close, | ||
}; | ||
const popoverPropsHaveAnchor = | ||
!! popoverProps?.anchor || | ||
// Note: `anchorRef`, `getAnchorRect` and `anchorRect` are deprecated and | ||
|
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
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.
Moved this entry to the previous release section, since I realised that the corresponding PR was merged before August 31st