-
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
Block Editor: Tips feature #22109
Block Editor: Tips feature #22109
Changes from all commits
4dfe9de
26dcd32
2861b11
66b9ea2
d7dd46e
21ebd69
46f248c
c399a4d
d7d9633
f47fb0f
cfdffc1
7bf4307
e1b03d3
caa99e8
14b711f
6345fd5
4d07311
55beff0
5fba8f5
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ import { | |
getBlockType, | ||
} from '@wordpress/blocks'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { select } from '@wordpress/data'; | ||
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. the usage of the global 'select' or 'dispatch' in Gutenberg should be limited as much as possible because they are singletons and not aware of the parent registries (nested contexts). They are also not reactive. So prefer |
||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -17,6 +18,8 @@ import BlockPreview from '../block-preview'; | |
|
||
function InserterPreviewPanel( { item } ) { | ||
const hoveredItemBlockType = getBlockType( item.name ); | ||
const { __experimentalGetBlockTipsByType } = select( 'core/block-editor' ); | ||
|
||
return ( | ||
<div className="block-editor-inserter__menu-preview-panel"> | ||
<div className="block-editor-inserter__preview"> | ||
|
@@ -50,7 +53,12 @@ function InserterPreviewPanel( { item } ) { | |
</div> | ||
) } | ||
</div> | ||
{ ! isReusableBlock( item ) && <BlockCard blockType={ item } /> } | ||
{ ! isReusableBlock( item ) && ( | ||
<BlockCard | ||
blockType={ item } | ||
tip={ __experimentalGetBlockTipsByType( item.name, true ) } | ||
/> | ||
) } | ||
</div> | ||
); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
|
||
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. useless empty line :P |
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { createInterpolateElement, useState } from '@wordpress/element'; | ||
import { Tip } from '@wordpress/components'; | ||
import { select } from '@wordpress/data'; | ||
|
||
const globalTips = [ | ||
createInterpolateElement( | ||
|
@@ -28,7 +30,13 @@ const globalTips = [ | |
__( "Change a block's type by pressing the block icon on the toolbar." ), | ||
]; | ||
|
||
function Tips() { | ||
function Tips( { filterValue } ) { | ||
// Return a contextual tip when it's appropriate. | ||
const contextualTip = select( 'core/block-editor' ).__experimentalGetBlockInserterTipsByContext( filterValue, true ); | ||
if ( contextualTip ) { | ||
return <Tip>{ contextualTip }</Tip>; | ||
} | ||
|
||
const [ randomIndex ] = useState( | ||
// Disable Reason: I'm not generating an HTML id. | ||
// eslint-disable-next-line no-restricted-syntax | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1010,3 +1010,35 @@ export function toggleBlockHighlight( clientId, isHighlighted ) { | |
isHighlighted, | ||
}; | ||
} | ||
|
||
export function __experimentalRegisterTip( | ||
scope, | ||
context, | ||
keywords, | ||
description | ||
) { | ||
return { | ||
type: 'REGISTER_TIP', | ||
scope, | ||
context, | ||
keywords, | ||
description, | ||
}; | ||
} | ||
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. The more I think about this, the more I lean towards a dedicated package to register and retrieve tips across screens (and not in block-editor directly). A package similar to the keyboard-shortcuts one. I'd like @aduth's opinion here too if possible. It would also be cool to clarify the meaning of each argument (maybe using typing).
Should we use an object in order to have a more extensible API? 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.
Sounds good to me too as long as we agree that the Redux approach could fit well for the Tips feature.
Probably, yes. Does it mean we could add arbitrary data into the state tree? Or maybe we could an
It's a good example of additional data required only for some tips. In this case, I used it as a way to define tip context in the block inserter menu. Its values could be 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. Could the |
||
|
||
export function __experimentalRegisterBlockInserterTip( | ||
context, | ||
keywords, | ||
description | ||
) { | ||
return __experimentalRegisterTip( | ||
'blockInserter', | ||
context, | ||
keywords, | ||
description | ||
); | ||
} | ||
|
||
export function __experimentalRegisterBlockTip( type, description ) { | ||
return __experimentalRegisterTip( type, null, null, description ); | ||
} |
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.
Adding this here means potentially, the tip could show up in the inspector too. Did you really mean to add it to the BlockCard or more to the InserterPreviewPanel?
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.
I added it as an example of showing the scope of this approach, where it isn't limited only to the block inserter menu. There a handful of issues about Tips. Here, we'd like to show a block tip if its defined. This PR tackles it as well, allowing us registering tips by block type: