diff --git a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/components/Container.tsx b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/components/Container.tsx index 5f29777df0..d822306472 100644 --- a/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/components/Container.tsx +++ b/packages/create-sitecore-jss/src/templates/nextjs-sxa/src/components/Container.tsx @@ -2,6 +2,7 @@ import { ComponentParams, ComponentRendering, Placeholder, + useSitecoreContext, } from '@sitecore-jss/sitecore-jss-nextjs'; import React from 'react'; @@ -10,44 +11,67 @@ interface ComponentProps { params: ComponentParams; } -const DefaultContainer = (props: ComponentProps): JSX.Element => { - const containerStyles = props.params && props.params.Styles ? props.params.Styles : ''; - const styles = `${props.params.GridParameters} ${containerStyles}`.trimEnd(); - const phKey = `container-${props.params.DynamicPlaceholderId}`; - const id = props.params.RenderingIdentifier; - const mediaUrlPattern = new RegExp(/mediaurl=\"([^"]*)\"/, 'i'); - let backgroundImage = props.params.BackgroundImage as string; - let backgroundStyle: { [key: string]: string } = {}; - - if (backgroundImage && backgroundImage.match(mediaUrlPattern)) { - const mediaUrl = backgroundImage.match(mediaUrlPattern)?.[1] || ''; - - backgroundStyle = { - backgroundImage: `url('${mediaUrl}')`, - }; +const BACKGROUND_REG_EXP = /[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/gi; +const MEDIA_URL_PATTERN = /mediaurl="([^"]*)"/i; +const SITECORE_MEDIA_PREFIX = '/sitecore/shell/-/media/'; + +const getBackgroundStyle = ( + backgroundImage: string, + isEditMode: boolean +): { [key: string]: string } => { + if (!backgroundImage) return {}; + + if (isEditMode) { + const processedImage = backgroundImage + .match(BACKGROUND_REG_EXP) + ?.pop() + ?.replace(/-/gi, ''); + return processedImage + ? { backgroundImage: `url('${SITECORE_MEDIA_PREFIX}${processedImage}')` } + : {}; } + const mediaUrl = backgroundImage.match(MEDIA_URL_PATTERN)?.[1] || ''; + return mediaUrl ? { backgroundImage: `url('${mediaUrl}')` } : {}; +}; + +const DefaultContainer: React.FC = ({ params, rendering }): JSX.Element => { + const { sitecoreContext } = useSitecoreContext(); + const { + Styles: containerStyles = '', + GridParameters = '', + DynamicPlaceholderId, + RenderingIdentifier, + BackgroundImage, + } = params; + + const styles = `${GridParameters} ${containerStyles}`.trimEnd(); + const placeholderKey = `container-${DynamicPlaceholderId}`; + const backgroundStyle = getBackgroundStyle( + BackgroundImage as string, + sitecoreContext.pageState === 'edit' + ); + return ( -
+
- +
); }; -export const Default = (props: ComponentProps): JSX.Element => { - const splitStyles = props.params?.Styles?.split(' '); - - if (splitStyles && splitStyles.includes('container')) { - return ( -
- -
- ); - } +export const Default: React.FC = (props): JSX.Element => { + const { Styles = '' } = props.params; + const hasContainerStyle = Styles.split(' ').includes('container'); - return ; + return hasContainerStyle ? ( +
+ +
+ ) : ( + + ); };