-
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.
Site Editor: Add New - Custom/General template
- Loading branch information
1 parent
c489f75
commit 3faa4df
Showing
3 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
packages/edit-site/src/components/add-new-template/add-custom-generic-template-modal.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,100 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { kebabCase } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { | ||
Button, | ||
Flex, | ||
FlexItem, | ||
Modal, | ||
TextControl, | ||
} from '@wordpress/components'; | ||
|
||
function AddCustomGenericTemplateModal( { onClose, createTemplate } ) { | ||
const [ title, setTitle ] = useState( '' ); | ||
const defaultTitle = __( 'Custom Template' ); | ||
const [ isBusy, setIsBusy ] = useState( false ); | ||
async function onCreateTemplate( event ) { | ||
event.preventDefault(); | ||
|
||
if ( isBusy ) { | ||
return; | ||
} | ||
|
||
setIsBusy( true ); | ||
|
||
await createTemplate( | ||
{ | ||
slug: | ||
'wp-custom-template-' + kebabCase( title || defaultTitle ), | ||
title: title || defaultTitle, | ||
}, | ||
false | ||
); | ||
|
||
setIsBusy( false ); | ||
onClose( false ); | ||
} | ||
return ( | ||
<Modal | ||
title={ __( 'Create custom template' ) } | ||
closeLabel={ __( 'Close' ) } | ||
onRequestClose={ () => { | ||
onClose(); | ||
} } | ||
overlayClassName="edit-site-custom-generic-template__modal" | ||
> | ||
<form onSubmit={ onCreateTemplate }> | ||
<Flex align="flex-start" gap={ 8 }> | ||
<FlexItem> | ||
<TextControl | ||
label={ __( 'Name' ) } | ||
value={ title } | ||
onChange={ setTitle } | ||
placeholder={ defaultTitle } | ||
disabled={ isBusy } | ||
help={ __( | ||
'Describe the purpose of the template, e.g. "Full Width". Custom templates can be applied to any post or page.' | ||
) } | ||
/> | ||
</FlexItem> | ||
</Flex> | ||
|
||
<Flex | ||
className="edit-site-custom-generic-template__modal-actions" | ||
justify="flex-end" | ||
expanded={ false } | ||
> | ||
<FlexItem> | ||
<Button | ||
variant="tertiary" | ||
onClick={ () => { | ||
onClose(); | ||
} } | ||
> | ||
{ __( 'Cancel' ) } | ||
</Button> | ||
</FlexItem> | ||
<FlexItem> | ||
<Button | ||
variant="primary" | ||
type="submit" | ||
isBusy={ isBusy } | ||
aria-disabled={ isBusy } | ||
> | ||
{ __( 'Create' ) } | ||
</Button> | ||
</FlexItem> | ||
</Flex> | ||
</form> | ||
</Modal> | ||
); | ||
} | ||
|
||
export default AddCustomGenericTemplateModal; |
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