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

Create toolbar item compatible for RN. #22232

Closed
wants to merge 1 commit into from
Closed
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
44 changes: 44 additions & 0 deletions packages/components/src/toolbar-item/index.native.js
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 );
Copy link
Member

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.


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 };
Copy link
Member

Choose a reason for hiding this comment

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

data-experimental-toolbar-item is not needed here.


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
Copy link
Member

Choose a reason for hiding this comment

The 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 accessibleToolbarState should be always falsy on RN. So return children( { ...props, ref } ) is probably all you need.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 );