-
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
Style book: make the style book slot generic #49973
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5029e87
Initial commit:
ramonjd b366e0e
- adding state for editor canvas view container
ramonjd 7feeed1
- attempting to juggle refs so that we retain keyboard navigation and…
ramonjd 2fae441
Remove comment that was the motivation behind this PR in the first pl…
ramonjd 848d03c
Adding the label to the interactive element
ramonjd b2a2a70
Make the slot private.
ramonjd 4fbb2d1
Resetting default state of `editorCanvasContainerView` to `undefined`
ramonjd 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
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
115 changes: 115 additions & 0 deletions
115
packages/edit-site/src/components/editor-canvas-container/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,115 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Children, cloneElement, useState, useMemo } from '@wordpress/element'; | ||
import { | ||
Button, | ||
privateApis as componentsPrivateApis, | ||
} from '@wordpress/components'; | ||
import { ESCAPE } from '@wordpress/keycodes'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { closeSmall } from '@wordpress/icons'; | ||
import { useFocusOnMount, useFocusReturn } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { unlock } from '../../private-apis'; | ||
import { store as editSiteStore } from '../../store'; | ||
|
||
/** | ||
* Returns a translated string for the title of the editor canvas container. | ||
* | ||
* @param {string} view Editor canvas container view. | ||
* | ||
* @return {string} Translated string corresponding to value of view. Default is ''. | ||
*/ | ||
export function getEditorCanvasContainerTitle( view ) { | ||
switch ( view ) { | ||
case 'style-book': | ||
return __( 'Style Book' ); | ||
default: | ||
return ''; | ||
} | ||
} | ||
|
||
// Creates a private slot fill. | ||
const { createPrivateSlotFill } = unlock( componentsPrivateApis ); | ||
const SLOT_FILL_NAME = 'EditSiteEditorCanvasContainerSlot'; | ||
const { Slot: EditorCanvasContainerSlot, Fill: EditorCanvasContainerFill } = | ||
createPrivateSlotFill( SLOT_FILL_NAME ); | ||
|
||
function EditorCanvasContainer( { | ||
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. Nice, I like this abstraction — should make it easier building subsequent screens (which I'm sure is why you made it like this 😄) |
||
children, | ||
closeButtonLabel, | ||
onClose = () => {}, | ||
} ) { | ||
const editorCanvasContainerView = useSelect( | ||
( select ) => | ||
unlock( select( editSiteStore ) ).getEditorCanvasContainerView(), | ||
[] | ||
); | ||
const [ isClosed, setIsClosed ] = useState( false ); | ||
const { setEditorCanvasContainerView } = unlock( | ||
useDispatch( editSiteStore ) | ||
); | ||
const focusOnMountRef = useFocusOnMount( 'firstElement' ); | ||
const sectionFocusReturnRef = useFocusReturn(); | ||
const title = useMemo( | ||
() => getEditorCanvasContainerTitle( editorCanvasContainerView ), | ||
[ editorCanvasContainerView ] | ||
); | ||
function onCloseContainer() { | ||
onClose(); | ||
setEditorCanvasContainerView( undefined ); | ||
setIsClosed( true ); | ||
} | ||
|
||
function closeOnEscape( event ) { | ||
if ( event.keyCode === ESCAPE && ! event.defaultPrevented ) { | ||
event.preventDefault(); | ||
onCloseContainer(); | ||
} | ||
} | ||
|
||
const childrenWithProps = Array.isArray( children ) | ||
? Children.map( children, ( child, index ) => | ||
index === 0 | ||
? cloneElement( child, { | ||
ref: sectionFocusReturnRef, | ||
} ) | ||
: child | ||
) | ||
: cloneElement( children, { | ||
ref: sectionFocusReturnRef, | ||
} ); | ||
|
||
if ( isClosed ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<EditorCanvasContainerFill> | ||
{ /* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */ } | ||
<section | ||
className="edit-site-editor-canvas-container" | ||
ref={ focusOnMountRef } | ||
onKeyDown={ closeOnEscape } | ||
aria-label={ title } | ||
> | ||
<Button | ||
className="edit-site-editor-canvas-container__close-button" | ||
icon={ closeSmall } | ||
label={ closeButtonLabel || __( 'Close' ) } | ||
onClick={ onCloseContainer } | ||
showTooltip={ false } | ||
/> | ||
{ childrenWithProps } | ||
</section> | ||
</EditorCanvasContainerFill> | ||
); | ||
} | ||
|
||
EditorCanvasContainer.Slot = EditorCanvasContainerSlot; | ||
export default EditorCanvasContainer; |
19 changes: 19 additions & 0 deletions
19
packages/edit-site/src/components/editor-canvas-container/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,19 @@ | ||
.edit-site-editor-canvas-container { | ||
background: $white; // Fallback color, overridden by JavaScript. | ||
border-radius: $radius-block-ui; | ||
bottom: 0; | ||
left: 0; | ||
overflow: hidden; | ||
position: absolute; | ||
right: 0; | ||
top: 0; | ||
transition: all 0.3s; // Match .block-editor-iframe__body transition. | ||
} | ||
|
||
.edit-site-editor-canvas-container__close-button { | ||
position: absolute; | ||
right: $grid-unit-10; | ||
top: math.div($grid-unit-60 - $button-size, 2); // ( tab height - button size ) / 2 | ||
z-index: 1; | ||
background: $white; | ||
} |
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.
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.
Not sure where this should go. Oh the agony.
I want a central place where we can match the current value of
store.editorCanvasContainerView
to a translated string.Something like this is needed to get the string across the app, e.g.,
header-edit-mode/index.js
?
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.
Good question — personally I'd probably leave it in this module for now, since it's close to where it's being used? It could always be moved around later.