-
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
Create toolbar item compatible for RN. #22232
Changes from all commits
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,44 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { View as BaseToolbarItem } from 'react-native'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { forwardRef, useContext } from '@wordpress/element'; | ||
import warning from '@wordpress/warning'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import ToolbarContext from '../toolbar-context'; | ||
|
||
function ToolbarItem( { children, ...props }, ref ) { | ||
const accessibleToolbarState = useContext( ToolbarContext ); | ||
|
||
if ( typeof children !== 'function' ) { | ||
warning( | ||
'`ToolbarItem` is a generic headless component that accepts only function children props' | ||
); | ||
return null; | ||
} | ||
|
||
const allProps = { ...props, ref, 'data-experimental-toolbar-item': true }; | ||
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.
|
||
|
||
if ( ! accessibleToolbarState ) { | ||
return children( allProps ); | ||
} | ||
|
||
return ( | ||
<BaseToolbarItem { ...accessibleToolbarState } { ...allProps }> | ||
{ ( htmlProps ) => | ||
// Overwriting BaseToolbarItem's onMouseDown since it disables drag | ||
// and drop | ||
children( { ...htmlProps, onMouseDown: allProps.onMouseDown } ) | ||
} | ||
</BaseToolbarItem> | ||
); | ||
Comment on lines
+33
to
+41
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. Does this work on RN? <View prop="value">{ ( props ) => children( props ) }</View> I would say that this is not needed at all. And the code would never reach to that point any way because 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. It works, but feel free to push to the PR, with your changes, and I can test it. |
||
} | ||
|
||
export default forwardRef( ToolbarItem ); |
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.
This
ToolbarContext
is only for the web. It handles the state of the toolbar when using arrow keys to move between toolbar items.