import { Icon as WCIcon, BaseControl, TextControl, Button, Modal, __experimentalHStack as HStack, __experimentalVStack as VStack, } from '@wordpress/components'; import { useInstanceId } from '@wordpress/compose'; import type { TemplatePartArea } from '@wordpress/core-data'; import { store as coreStore } from '@wordpress/core-data'; import { useDispatch, useSelect } from '@wordpress/data'; import { useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { check, footer as footerIcon, header as headerIcon, sidebar as sidebarIcon, navigationOverlay as navigationOverlayIcon, symbolFilled as symbolFilledIcon, } from '@wordpress/icons'; import { store as noticesStore } from '@wordpress/notices'; import { serialize } from '@wordpress/blocks'; import { getCleanTemplatePartSlug, getUniqueTemplatePartTitle, useExistingTemplateParts, } from './utils'; function getAreaRadioId( value: string, instanceId: number ) { return `patterns-create-template-part-modal__area-option-${ value }-${ instanceId }`; } function getAreaRadioDescriptionId( value: string, instanceId: number ) { return `patterns-create-template-part-modal__area-option-description-${ value }-${ instanceId }`; } type CreateTemplatePartModalContentsProps = { defaultArea?: string; blocks: any[]; confirmLabel?: string; closeModal: () => void; onCreate: ( templatePart: any ) => void; onError?: () => void; defaultTitle?: string; }; /** * A React component that renders a modal for creating a template part. The modal displays a title and the contents for creating the template part. * @param props The component props. * @param props.modalTitle */ export default function CreateTemplatePartModal( { modalTitle, ...restProps }: { modalTitle?: string; } & CreateTemplatePartModalContentsProps ) { const defaultModalTitle = useSelect( ( select ) => select( coreStore ).getPostType( 'wp_template_part' )?.labels ?.add_new_item, [] ); return ( ); } /** * Helper function to retrieve the corresponding icon by area name. * * @param {string} areaOrIconName The area name (e.g., 'header', 'navigation-overlay'). * * @return {Object} The corresponding icon. */ const getTemplatePartIcon = ( areaOrIconName: string ) => { if ( 'header' === areaOrIconName ) { return headerIcon; } else if ( 'footer' === areaOrIconName ) { return footerIcon; } else if ( 'sidebar' === areaOrIconName ) { return sidebarIcon; } else if ( 'navigation-overlay' === areaOrIconName ) { return navigationOverlayIcon; } return symbolFilledIcon; }; /** * A React component that renders the content of a model for creating a template part. * * @param {Object} props - The component props. * @param {string} [props.defaultArea=uncategorized] - The default area for the template part. * @param {Array} [props.blocks=[]] - The blocks to be included in the template part. * @param {string} [props.confirmLabel='Add'] - The label for the confirm button. * @param {Function} props.closeModal - Function to close the modal. * @param {Function} props.onCreate - Function to call when the template part is successfully created. * @param {Function} [props.onError] - Function to call when there is an error creating the template part. * @param {string} [props.defaultTitle=''] - The default title for the template part. */ export function CreateTemplatePartModalContents( { defaultArea = 'uncategorized', blocks = [], confirmLabel = __( 'Add' ), closeModal, onCreate, onError, defaultTitle = '', }: CreateTemplatePartModalContentsProps ) { const { createErrorNotice } = useDispatch( noticesStore ); const { saveEntityRecord } = useDispatch( coreStore ); const existingTemplateParts = useExistingTemplateParts(); const [ title, setTitle ] = useState( defaultTitle ); const [ area, setArea ] = useState( defaultArea ); const [ isSubmitting, setIsSubmitting ] = useState( false ); const instanceId = useInstanceId( CreateTemplatePartModal ); const defaultTemplatePartAreas = useSelect( ( select ) => select( coreStore ).getCurrentTheme()?.default_template_part_areas, [] ); async function createTemplatePart() { if ( ! title || isSubmitting ) { return; } try { setIsSubmitting( true ); const uniqueTitle = getUniqueTemplatePartTitle( title, existingTemplateParts ); const cleanSlug = getCleanTemplatePartSlug( uniqueTitle ); const templatePart = await saveEntityRecord( 'postType', 'wp_template_part', { slug: cleanSlug, title: uniqueTitle, content: serialize( blocks ), area, }, { throwOnError: true } ); await onCreate( templatePart ); // TODO: Add a success notice? } catch ( error ) { const errorMessage = error instanceof Error && 'code' in error && error.message && error.code !== 'unknown_error' ? error.message : __( 'An error occurred while creating the template part.' ); createErrorNotice( errorMessage, { type: 'snackbar' } ); onError?.(); } finally { setIsSubmitting( false ); } } return (
{ event.preventDefault(); await createTemplatePart(); } } >
{ __( 'Area' ) }
{ ( defaultTemplatePartAreas ?? [] ).map( ( item: TemplatePartArea ) => { const icon = getTemplatePartIcon( item.icon ); return (
{ setArea( item.area ); } } aria-describedby={ getAreaRadioDescriptionId( item.area, instanceId ) } />

{ item.description }

); } ) }
); }