-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add widgets customize inspector #29755
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e735723
Fix block toolbar not showing
kevin940726 4786062
Add inspector and only render one sidebar at a time
kevin940726 88dfadd
Position inspector
kevin940726 5e830ad
Implement hacky way to transition block settings
kevin940726 b7afb6d
Apply suggestions from code review
kevin940726 f135370
Apply changes from code reviews
kevin940726 20970b7
Add inspector title and fix styling
kevin940726 18ab26e
Use reducer to manage the open state
kevin940726 29e1357
Fix box-sizing
kevin940726 66c9964
Just use createPortal
kevin940726 ffdffbe
Add comment
kevin940726 ad070d0
Move focus back when closing the inspector
kevin940726 63ec016
Add style to block-inspector
kevin940726 d618fe7
Fix conflict
kevin940726 37f8077
Trigger CI
kevin940726 c48c246
Remove unneeded class name
kevin940726 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
packages/block-editor/src/components/block-inspector/style.scss
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
15 changes: 15 additions & 0 deletions
15
packages/customize-widgets/src/components/inspector/block-inspector-button.js
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { MenuItem } from '@wordpress/components'; | ||
|
||
function BlockInspectorButton( { isInspectorOpened = false, ...props } ) { | ||
const label = isInspectorOpened | ||
? __( 'Hide more settings' ) | ||
: __( 'Show more settings' ); | ||
|
||
return <MenuItem { ...props }>{ label }</MenuItem>; | ||
} | ||
|
||
export default BlockInspectorButton; |
123 changes: 123 additions & 0 deletions
123
packages/customize-widgets/src/components/inspector/index.js
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 |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useRef } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { | ||
BlockInspector, | ||
store as blockEditorStore, | ||
} from '@wordpress/block-editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useInstanceId } from '@wordpress/compose'; | ||
|
||
export { default as BlockInspectorButton } from './block-inspector-button'; | ||
noisysocks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function InspectorSectionMeta( { closeInspector, inspectorTitleId } ) { | ||
return ( | ||
<div className="customize-section-description-container section-meta"> | ||
<div className="customize-section-title"> | ||
{ /* Disable reason: We want to use the exiting style of the back button. */ } | ||
<button // eslint-disable-line react/forbid-elements | ||
kevin940726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
type="button" | ||
className="customize-section-back" | ||
tabIndex="0" | ||
onClick={ closeInspector } | ||
> | ||
<span className="screen-reader-text">Back</span> | ||
</button> | ||
<h3 id={ inspectorTitleId }> | ||
<span className="customize-action"> | ||
{ __( 'Customizing ▸ Widgets' ) } | ||
</span> | ||
{ __( 'Block Settings' ) } | ||
</h3> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default function Inspector( { | ||
isOpened, | ||
isAnimating, | ||
setInspectorOpenState, | ||
} ) { | ||
const selectedBlockClientId = useSelect( ( select ) => | ||
select( blockEditorStore ).getSelectedBlockClientId() | ||
); | ||
const selectedBlockRef = useRef(); | ||
|
||
useEffect( () => { | ||
selectedBlockRef.current = document.querySelector( | ||
`[data-block="${ selectedBlockClientId }"]` | ||
); | ||
}, [ selectedBlockClientId ] ); | ||
|
||
const inspectorTitleId = useInstanceId( | ||
Inspector, | ||
'customize-widgets-block-settings' | ||
); | ||
|
||
useEffect( () => { | ||
const openedSidebarSection = document.querySelector( | ||
'.control-section-sidebar.open' | ||
); | ||
|
||
if ( isOpened ) { | ||
openedSidebarSection.classList.add( 'is-inspector-open' ); | ||
} else { | ||
openedSidebarSection.classList.remove( 'is-inspector-open' ); | ||
} | ||
|
||
// In case the "transitionend" event for some reasons doesn't fire. | ||
// (Like when it's set to "display: none", or when the transition property is removed.) | ||
// See https://github.com/w3c/csswg-drafts/issues/3043 | ||
const timer = setTimeout( () => { | ||
kevin940726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setInspectorOpenState( 'TRANSITION_END' ); | ||
}, 180 ); | ||
|
||
return () => { | ||
openedSidebarSection.classList.remove( 'is-inspector-open' ); | ||
clearTimeout( timer ); | ||
}; | ||
}, [ isOpened, setInspectorOpenState ] ); | ||
|
||
return ( | ||
<div | ||
role="region" | ||
aria-labelledby={ inspectorTitleId } | ||
className={ classnames( | ||
'customize-pane-child', | ||
'accordion-section-content', | ||
'accordion-section', | ||
'customize-widgets-layout__inspector', | ||
{ | ||
open: isOpened, | ||
// Needed to keep the inspector visible while closing. | ||
busy: isAnimating, | ||
} | ||
) } | ||
onTransitionEnd={ () => { | ||
setInspectorOpenState( 'TRANSITION_END' ); | ||
} } | ||
> | ||
<InspectorSectionMeta | ||
closeInspector={ () => { | ||
setInspectorOpenState( 'CLOSE' ); | ||
|
||
// Wait a tick so that the block editor can transition back from being hidden. | ||
window.requestAnimationFrame( () => { | ||
selectedBlockRef.current?.focus(); | ||
} ); | ||
} } | ||
inspectorTitleId={ inspectorTitleId } | ||
/> | ||
|
||
<BlockInspector /> | ||
</div> | ||
); | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/customize-widgets/src/components/inspector/style.scss
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// To override the style in block-editor/block-inspector. | ||
.block-editor-block-inspector h3 { | ||
margin-bottom: 0; | ||
} | ||
|
||
#customize-theme-controls .customize-pane-child.accordion-section-content.customize-widgets-layout__inspector { | ||
background: $white; | ||
box-sizing: border-box; | ||
|
||
* { | ||
box-sizing: inherit; | ||
} | ||
|
||
.block-editor-block-inspector { | ||
margin: -$grid-unit-15; | ||
} | ||
} | ||
|
||
#customize-theme-controls .customize-pane-child.accordion-section-content.control-section-sidebar.open { | ||
transition: 0.18s transform cubic-bezier(0.645, 0.045, 0.355, 1); /* easeInOutCubic */ | ||
|
||
&.is-inspector-open { | ||
transform: translateX(-100%); | ||
} | ||
} |
109 changes: 89 additions & 20 deletions
109
packages/customize-widgets/src/components/sidebar-block-editor/index.js
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 |
---|---|---|
@@ -1,46 +1,115 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useReducer, createPortal } from '@wordpress/element'; | ||
import { | ||
BlockEditorProvider, | ||
BlockList, | ||
BlockSelectionClearer, | ||
ObserveTyping, | ||
WritingFlow, | ||
BlockEditorKeyboardShortcuts, | ||
__experimentalBlockSettingsMenuFirstItem, | ||
} from '@wordpress/block-editor'; | ||
import { | ||
DropZoneProvider, | ||
FocusReturnProvider, | ||
SlotFillProvider, | ||
Popover, | ||
} from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Inspector, { BlockInspectorButton } from '../inspector'; | ||
import useSidebarBlockEditor from './use-sidebar-block-editor'; | ||
|
||
const inspectorOpenStateReducer = ( state, action ) => { | ||
switch ( action ) { | ||
case 'OPEN': | ||
return { | ||
open: true, | ||
busy: true, | ||
}; | ||
case 'TRANSITION_END': | ||
return { | ||
...state, | ||
busy: false, | ||
}; | ||
case 'CLOSE': | ||
return { | ||
open: false, | ||
busy: true, | ||
}; | ||
default: | ||
throw new Error( 'Unexpected action' ); | ||
} | ||
}; | ||
|
||
export default function SidebarBlockEditor( { sidebar } ) { | ||
const [ blocks, onInput, onChange ] = useSidebarBlockEditor( sidebar ); | ||
const [ | ||
{ open: isInspectorOpened, busy: isInspectorAnimating }, | ||
setInspectorOpenState, | ||
] = useReducer( inspectorOpenStateReducer, { open: false, busy: false } ); | ||
const parentContainer = document.getElementById( | ||
'customize-theme-controls' | ||
); | ||
|
||
return ( | ||
<SlotFillProvider> | ||
<DropZoneProvider> | ||
<FocusReturnProvider> | ||
<BlockEditorProvider | ||
value={ blocks } | ||
onInput={ onInput } | ||
onChange={ onChange } | ||
> | ||
<BlockSelectionClearer> | ||
<WritingFlow> | ||
<ObserveTyping> | ||
<BlockList /> | ||
</ObserveTyping> | ||
</WritingFlow> | ||
</BlockSelectionClearer> | ||
</BlockEditorProvider> | ||
</FocusReturnProvider> | ||
</DropZoneProvider> | ||
</SlotFillProvider> | ||
<> | ||
<BlockEditorKeyboardShortcuts.Register /> | ||
<SlotFillProvider> | ||
<DropZoneProvider> | ||
<div hidden={ isInspectorOpened && ! isInspectorAnimating }> | ||
<BlockEditorProvider | ||
value={ blocks } | ||
onInput={ onInput } | ||
onChange={ onChange } | ||
useSubRegistry={ false } | ||
> | ||
<BlockEditorKeyboardShortcuts /> | ||
|
||
<BlockSelectionClearer> | ||
<WritingFlow> | ||
<ObserveTyping> | ||
<BlockList /> | ||
</ObserveTyping> | ||
</WritingFlow> | ||
</BlockSelectionClearer> | ||
</BlockEditorProvider> | ||
|
||
<Popover.Slot name="block-toolbar" /> | ||
</div> | ||
|
||
{ createPortal( | ||
<Inspector | ||
isOpened={ isInspectorOpened } | ||
isAnimating={ isInspectorAnimating } | ||
setInspectorOpenState={ setInspectorOpenState } | ||
/>, | ||
parentContainer | ||
) } | ||
|
||
<__experimentalBlockSettingsMenuFirstItem> | ||
noisysocks marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ ( { onClose } ) => ( | ||
<BlockInspectorButton | ||
onClick={ () => { | ||
// Open the inspector, | ||
setInspectorOpenState( 'OPEN' ); | ||
// Then close the dropdown menu. | ||
onClose(); | ||
} } | ||
kevin940726 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/> | ||
) } | ||
</__experimentalBlockSettingsMenuFirstItem> | ||
|
||
{ | ||
// We have to portal this to the parent of both the editor and the inspector, | ||
// so that the popovers will appear above both of them. | ||
createPortal( <Popover.Slot />, parentContainer ) | ||
} | ||
</DropZoneProvider> | ||
</SlotFillProvider> | ||
</> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
It feels like adding so general styles to the
BlockInspector
might have unpredicted consequences on the UI outside of the Customizer.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 remember seeing these styles before elsewhere in the codebase. They're styles that the Interface package would normally add:
https://github.com/WordPress/gutenberg/blob/trunk/packages/interface/src/components/complementary-area/style.scss#L34-L43
They probably shouldn't be there either.
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.
Yeah, these are the styles already applied to the inspector from
<ComplementaryArea>
. I was just trying to match the style here so that the inspector can have a consistent look when placing outside the complementary area.