-
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.
- Loading branch information
Showing
27 changed files
with
263 additions
and
184 deletions.
There are no files selected for viewing
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
84 changes: 84 additions & 0 deletions
84
packages/block-editor/src/components/embedded-admin-context/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,84 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMergeRefs, useRefEffect } from '@wordpress/compose'; | ||
import { useState, createPortal, forwardRef } from '@wordpress/element'; | ||
import { ENTER, SPACE, ESCAPE } from '@wordpress/keycodes'; | ||
import { focus } from '@wordpress/dom'; | ||
import { __experimentalStyleProvider as StyleProvider } from '@wordpress/components'; | ||
|
||
function EmbeddedAdminContext( props, forwardedRef ) { | ||
const [ shadow, setShadow ] = useState(); | ||
const [ hasFocus, setHasFocus ] = useState(); | ||
const ref = useRefEffect( ( element ) => { | ||
const root = element.attachShadow( { mode: 'open' } ); | ||
Array.from( document.styleSheets ).forEach( ( styleSheet ) => { | ||
if ( styleSheet.ownerNode.getAttribute( 'data-emotion' ) ) { | ||
return; | ||
} | ||
|
||
let child = document.createElement( 'style' ); | ||
|
||
// Try to avoid requests for stylesheets of which we already | ||
// know the CSS rules. | ||
try { | ||
let cssText = ''; | ||
|
||
for ( const cssRule of styleSheet.cssRules ) { | ||
cssText += cssRule.cssText; | ||
} | ||
|
||
child.textContent = cssText; | ||
} catch ( e ) { | ||
child = styleSheet.ownerNode.cloneNode( true ); | ||
} | ||
|
||
root.appendChild( child ); | ||
} ); | ||
setShadow( root ); | ||
|
||
function onFocusIn() { | ||
setHasFocus( true ); | ||
} | ||
|
||
function onFocusOut() { | ||
setHasFocus( false ); | ||
} | ||
|
||
root.addEventListener( 'focusin', onFocusIn ); | ||
root.addEventListener( 'focusout', onFocusOut ); | ||
return () => { | ||
root.addEventListener( 'focusin', onFocusIn ); | ||
root.addEventListener( 'focusout', onFocusOut ); | ||
}; | ||
}, [] ); | ||
return ( | ||
<div | ||
{ ...props } | ||
ref={ useMergeRefs( [ ref, forwardedRef ] ) } | ||
tabIndex={ 0 } | ||
role="button" | ||
aria-pressed={ hasFocus } | ||
onKeyDown={ ( event ) => { | ||
if ( event.keyCode === ENTER || event.keyCode === SPACE ) { | ||
focus.focusable.find( shadow )[ 0 ].focus(); | ||
event.preventDefault(); | ||
} else if ( event.keyCode === ESCAPE ) { | ||
shadow.host.focus(); | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
} | ||
} } | ||
> | ||
{ shadow && | ||
createPortal( | ||
<StyleProvider document={ { head: shadow } }> | ||
{ props.children } | ||
</StyleProvider>, | ||
shadow | ||
) } | ||
</div> | ||
); | ||
} | ||
|
||
export default forwardRef( EmbeddedAdminContext ); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useConstrainedTabbing } from '@wordpress/compose'; | ||
import { Placeholder } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import EmbeddedAdminContext from '../embedded-admin-context'; | ||
|
||
/** | ||
* Placeholder for use in blocks. Creates an admin styling context and a tabbing | ||
* context in the block editor's writing flow. | ||
* | ||
* @param {Object} props | ||
* @param {Object} props.wrapperProps | ||
* | ||
* @return {WPComponent} The component | ||
*/ | ||
export default function IsolatedPlaceholder( { wrapperProps, ...props } ) { | ||
return ( | ||
<EmbeddedAdminContext { ...wrapperProps }> | ||
<Placeholder | ||
{ ...props } | ||
role="dialog" | ||
aria-label={ props.label } | ||
ref={ useConstrainedTabbing() } | ||
/> | ||
</EmbeddedAdminContext> | ||
); | ||
} |
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
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
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
Oops, something went wrong.