-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add: Modal to choose a start pattern on new templates.
- Loading branch information
1 parent
c75db83
commit 48a1569
Showing
7 changed files
with
279 additions
and
20 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
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
189 changes: 189 additions & 0 deletions
189
packages/edit-site/src/components/start-template-options/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,189 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Modal } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { useState, useEffect, useMemo } from '@wordpress/element'; | ||
import { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useAsyncList, useResizeObserver } from '@wordpress/compose'; | ||
import { store as preferencesStore } from '@wordpress/preferences'; | ||
import { parse } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editSiteStore } from '../../store'; | ||
import { store as coreStore, useEntityBlockEditor } from '@wordpress/core-data'; | ||
import apiFetch from '@wordpress/api-fetch'; | ||
import { addQueryArgs } from '@wordpress/url'; | ||
|
||
function useFallbackTemplateContent( slug, isCustom = false ) { | ||
const [ templateContent, setTemplateContent ] = useState( '' ); | ||
|
||
useEffect( () => { | ||
apiFetch( { | ||
path: addQueryArgs( '/wp/v2/templates/lookup', { | ||
slug, | ||
is_custom: isCustom, | ||
ignore_empty: true, | ||
} ), | ||
} ).then( ( { content } ) => setTemplateContent( content.raw ) ); | ||
}, [ slug ] ); | ||
return templateContent; | ||
} | ||
|
||
const START_BLANK_TITLE = __( 'Start blank' ); | ||
|
||
function PatternSelection( { fallbackContent, onChoosePattern, postType } ) { | ||
const [ resizeObserver, sizes ] = useResizeObserver(); | ||
const [ gridHeight, setGridHeight ] = useState( '320px' ); | ||
const [ , , onChange ] = useEntityBlockEditor( 'postType', postType ); | ||
const blockPatterns = useMemo( | ||
() => [ | ||
{ | ||
name: 'fallback', | ||
blocks: parse( fallbackContent ), | ||
title: __( 'Fallback content' ), | ||
}, | ||
{ | ||
name: 'start-blank', | ||
blocks: parse( | ||
'<!-- wp:paragraph --><p></p><!-- /wp:paragraph -->' | ||
), | ||
title: START_BLANK_TITLE, | ||
}, | ||
], | ||
[ fallbackContent ] | ||
); | ||
const shownBlockPatterns = useAsyncList( blockPatterns ); | ||
useEffect( () => { | ||
const elementOffSetWidth = window?.document?.querySelector( | ||
'.edit-site-start-template-options__pattern-container .block-editor-block-patterns-list__list-item' | ||
)?.offsetWidth; | ||
if ( elementOffSetWidth ) { | ||
setGridHeight( `${ ( elementOffSetWidth * 4 ) / 3 }px` ); | ||
} | ||
}, [ blockPatterns, sizes.width ] ); | ||
return ( | ||
<div | ||
className="edit-site-start-template-options__pattern-container" | ||
style={ { | ||
'--wp-edit-site-start-template-options-start-blank': `"${ START_BLANK_TITLE }"`, | ||
'--wp-edit-site-start-template-options-grid-height': gridHeight, | ||
} } | ||
> | ||
{ resizeObserver } | ||
<BlockPatternsList | ||
blockPatterns={ blockPatterns } | ||
shownPatterns={ shownBlockPatterns } | ||
showTitlesAsTooltip | ||
onClickPattern={ ( pattern, blocks ) => { | ||
onChange( 'start-blank' === pattern.name ? [] : blocks, { | ||
selection: undefined, | ||
} ); | ||
onChoosePattern(); | ||
} } | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
function StartModal( { slug, isCustom, onClose, postType } ) { | ||
const fallbackContent = useFallbackTemplateContent( slug, isCustom ); | ||
if ( ! fallbackContent ) { | ||
return null; | ||
} | ||
return ( | ||
<Modal | ||
className="edit-site-start-template-options__modal" | ||
title={ __( 'Choose a pattern' ) } | ||
closeLabel={ __( 'Cancel' ) } | ||
focusOnMount="firstElement" | ||
onRequestClose={ onClose } | ||
> | ||
<div className="edit-site-start-template-options__modal-content"> | ||
<PatternSelection | ||
fallbackContent={ fallbackContent } | ||
slug={ slug } | ||
isCustom={ isCustom } | ||
postType={ postType } | ||
onChoosePattern={ () => { | ||
onClose(); | ||
} } | ||
/> | ||
</div> | ||
</Modal> | ||
); | ||
} | ||
|
||
const START_TEMPLATE_MODAL_STATES = { | ||
INITIAL: 'INITIAL', | ||
PATTERN: 'PATTERN', | ||
CLOSED: 'CLOSED', | ||
}; | ||
|
||
export default function StartTemplateOptions() { | ||
const [ modalState, setModalState ] = useState( | ||
START_TEMPLATE_MODAL_STATES.INITIAL | ||
); | ||
const { shouldOpenModel, slug, isCustom, postType } = useSelect( | ||
( select ) => { | ||
const { getEditedPostType, getEditedPostId } = | ||
select( editSiteStore ); | ||
const _postType = getEditedPostType(); | ||
const postId = getEditedPostId(); | ||
const { | ||
__experimentalGetDirtyEntityRecords, | ||
getEditedEntityRecord, | ||
} = select( coreStore ); | ||
const templateRecord = getEditedEntityRecord( | ||
'postType', | ||
_postType, | ||
postId | ||
); | ||
|
||
const hasDirtyEntityRecords = | ||
__experimentalGetDirtyEntityRecords().length > 0; | ||
|
||
return { | ||
shouldOpenModel: | ||
modalState === START_TEMPLATE_MODAL_STATES.INITIAL && | ||
! hasDirtyEntityRecords && | ||
'' === templateRecord.content && | ||
'wp_template' === _postType && | ||
! select( preferencesStore ).get( | ||
'core/edit-site', | ||
'welcomeGuide' | ||
), | ||
slug: templateRecord.slug, | ||
isCustom: templateRecord.is_custom, | ||
postType: _postType, | ||
}; | ||
}, | ||
[ modalState ] | ||
); | ||
|
||
useEffect( () => { | ||
if ( shouldOpenModel ) { | ||
setModalState( START_TEMPLATE_MODAL_STATES.PATTERN ); | ||
} | ||
}, [ shouldOpenModel ] ); | ||
|
||
if ( | ||
modalState === START_TEMPLATE_MODAL_STATES.INITIAL || | ||
modalState === START_TEMPLATE_MODAL_STATES.CLOSED | ||
) { | ||
return null; | ||
} | ||
return ( | ||
<StartModal | ||
slug={ slug } | ||
isCustom={ isCustom } | ||
postType={ postType } | ||
onClose={ () => | ||
setModalState( START_TEMPLATE_MODAL_STATES.CLOSED ) | ||
} | ||
/> | ||
); | ||
} |
56 changes: 56 additions & 0 deletions
56
packages/edit-site/src/components/start-template-options/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,56 @@ | ||
.edit-site-start-template-options__modal.components-modal__frame { | ||
// To keep modal dimensions consistent as subsections are navigated, width | ||
// and height are used instead of max-(width/height). | ||
@include break-small() { | ||
width: calc(100% - #{ $grid-unit-20 * 2 }); | ||
height: calc(100% - #{ $header-height * 2 }); | ||
} | ||
@include break-medium() { | ||
width: 70%; | ||
} | ||
@include break-large() { | ||
height: fit-content; | ||
} | ||
} | ||
|
||
.edit-site-start-template-options__modal-content .block-editor-block-patterns-list { | ||
display: grid; | ||
width: 100%; | ||
margin-top: $grid-unit-05; | ||
gap: $grid-unit-10; | ||
grid-template-columns: repeat(auto-fit, minmax(min(100%/2, max(240px, 100%/10)), 1fr)); | ||
grid-auto-rows: var(--wp-edit-site-start-template-options-grid-height); | ||
.block-editor-block-patterns-list__list-item { | ||
break-inside: avoid-column; | ||
margin-bottom: $grid-unit-30; | ||
width: 100%; | ||
|
||
.block-editor-block-preview__container { | ||
height: 100%; | ||
} | ||
|
||
.block-editor-block-preview__content { | ||
width: 100%; | ||
position: absolute; | ||
} | ||
} | ||
|
||
// The start blank pattern is the last and we are selecting it. | ||
.block-editor-block-patterns-list__list-item:nth-last-child(2) { | ||
.block-editor-block-preview__container { | ||
position: absolute; | ||
padding: 0; | ||
background: #f0f0f0; | ||
&::after { | ||
width: 100%; | ||
top: 50%; | ||
margin-top: -1em; | ||
content: var(--wp-edit-site-start-template-options-start-blank); | ||
text-align: center; | ||
} | ||
} | ||
iframe { | ||
display: none; | ||
} | ||
} | ||
} |
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