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

Block Previews: dynamic preview size #19987

Closed
Closed
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ export class InserterMenu extends Component {
! isEmpty( itemsPerCollection );
const hoveredItemBlockType = hoveredItem ? getBlockType( hoveredItem.name ) : null;
const hasHelpPanel = hasItems && showInserterHelpPanel;
const viewportWidth =
Copy link

Choose a reason for hiding this comment

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

@scruffian Neat! Prop pass through with value fallback :)

I lack a bit of project context for this update. In isoloation, the change makes sense to me.

For implementation, I feel like we can clean up the && checks by using lodash.get (since the package already has lodash as a dependency)

That way, you can do something like this:

import { get } from 'lodash'

const viewportWidth = get(hoveredItemBlockType, 'example.viewportWidth', 500)

Would love your feedback 😊

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great idea thanks! Done in ec596e0.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ItsJonQ Curious, does gutenberg support the optional chaining operator (meaning, is it transpiled in the build tools for browser compatibility)?

Copy link

Choose a reason for hiding this comment

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

@creativecoder Good question! I remember this was mentioned recently. It looks like something was merged #19831

I haven't personally tried it yet. @gziolo Is this project optional chaining ready? :D

Copy link
Member

Choose a reason for hiding this comment

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

It should support, however in practice there was a blocker discovered by @aduth and filed in #19952. docgen tool that we use for documentation doesn't support ES2019 and ES2020 😞

hoveredItemBlockType &&
hoveredItemBlockType.example &&
hoveredItemBlockType.example.viewportWidth
? hoveredItemBlockType.example.viewportWidth
: 500;

// Disable reason (no-autofocus): The inserter menu is a modal display, not one which
// is always visible, and one which already incurs this behavior of autoFocus via
Expand Down Expand Up @@ -416,7 +422,7 @@ export class InserterMenu extends Component {
<div className="block-editor-inserter__preview-content">
<BlockPreview
padding={ 10 }
viewportWidth={ 500 }
viewportWidth={ viewportWidth }
blocks={
hoveredItemBlockType.example
? getBlockFromExample( hoveredItem.name, {
Expand Down