-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Try] Customizable toolbar contents (#23613)
* Customizable toolbar contents * Export BlockToolbarContents as __experimentalBlockToolbarContents * Update package-lock.json * Update package-lock.json * Use BlockControlsSlot instead of a custom BlockToolbarContents slot * Update package-lock.json * rename resize to resizeToolbar * Renamed symbols for increased readability * Rename CSS class for increased readability
- Loading branch information
Showing
7 changed files
with
191 additions
and
12 deletions.
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.
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
130 changes: 130 additions & 0 deletions
130
packages/block-editor/src/components/block-toolbar/expanded-block-controls-container.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,130 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { TransitionGroup, CSSTransition } from 'react-transition-group'; | ||
import { throttle } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useRef, useState, useEffect, useCallback } from '@wordpress/element'; | ||
import warning from '@wordpress/warning'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockControls from '../block-controls'; | ||
|
||
export default function ExpandedBlockControlsContainer( { | ||
children, | ||
className, | ||
} ) { | ||
return ( | ||
<BlockControls.Slot __experimentalIsExpanded> | ||
{ ( fills ) => { | ||
return ( | ||
<ExpandedBlockControlsHandler | ||
className={ className } | ||
fills={ fills } | ||
> | ||
{ children } | ||
</ExpandedBlockControlsHandler> | ||
); | ||
} } | ||
</BlockControls.Slot> | ||
); | ||
} | ||
|
||
function ExpandedBlockControlsHandler( { fills, className = '', children } ) { | ||
const containerRef = useRef(); | ||
const fillsRef = useRef(); | ||
const toolbarRef = useRef(); | ||
const [ dimensions, setDimensions ] = useState( {} ); | ||
|
||
const fillsPropRef = useRef(); | ||
fillsPropRef.current = fills; | ||
const resizeToolbar = useCallback( | ||
throttle( () => { | ||
const toolbarContentElement = fillsPropRef.current.length | ||
? fillsRef.current | ||
: toolbarRef.current; | ||
if ( ! toolbarContentElement ) { | ||
return; | ||
} | ||
toolbarContentElement.style.position = 'absolute'; | ||
toolbarContentElement.style.width = 'auto'; | ||
const contentCSS = window.getComputedStyle( | ||
toolbarContentElement, | ||
null | ||
); | ||
setDimensions( { | ||
width: contentCSS.getPropertyValue( 'width' ), | ||
height: contentCSS.getPropertyValue( 'height' ), | ||
} ); | ||
toolbarContentElement.style.position = ''; | ||
toolbarContentElement.style.width = ''; | ||
}, 100 ), | ||
[] | ||
); | ||
|
||
useEffect( () => { | ||
const observer = new window.MutationObserver( function ( | ||
mutationsList | ||
) { | ||
const hasChildList = mutationsList.find( | ||
( { type } ) => type === 'childList' | ||
); | ||
if ( hasChildList ) { | ||
resizeToolbar(); | ||
} | ||
} ); | ||
|
||
observer.observe( containerRef.current, { | ||
childList: true, | ||
subtree: true, | ||
} ); | ||
|
||
return () => observer.disconnect(); | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
if ( fills.length > 1 ) { | ||
warning( | ||
`${ fills.length } <BlockControls isExpanded> slots were registered but only one may be displayed.` | ||
); | ||
} | ||
}, [ fills.length ] ); | ||
|
||
const displayFill = fills[ 0 ]; | ||
return ( | ||
<div | ||
className="block-editor-block-toolbar-animated-width-container" | ||
ref={ containerRef } | ||
style={ dimensions } | ||
> | ||
<TransitionGroup> | ||
{ displayFill ? ( | ||
<CSSTransition | ||
key="fills" | ||
timeout={ 300 } | ||
classNames="block-editor-block-toolbar-content" | ||
> | ||
<div className={ className } ref={ fillsRef }> | ||
{ displayFill } | ||
</div> | ||
</CSSTransition> | ||
) : ( | ||
<CSSTransition | ||
key="default" | ||
timeout={ 300 } | ||
classNames="block-editor-block-toolbar-content" | ||
> | ||
<div className={ className } ref={ toolbarRef }> | ||
{ children } | ||
</div> | ||
</CSSTransition> | ||
) } | ||
</TransitionGroup> | ||
</div> | ||
); | ||
} |
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
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