Skip to content
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

[templates/nextjs-sxa] SXA components has been reworked with supporting metadata #1788

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Our versioning strategy is as follows:
* `[sitecore-jss-react]` `[sitecore-jss]` Introduces `PlaceholderMetadata` component which supports the hydration of chromes on Pages by rendering the components and placeholders with required metadata. ([#1776](https://github.com/Sitecore/jss/pull/1776))
* Chromes are hydrated based on the basis of new `editMode` property derived from LayoutData, which is defined as an enum consisting of `metadata` and `chromes`.
* `[sitecore-jss-react]` `[sitecore-jss-nextjs]` Introduce FieldMetadata component and functionality to render it when metadata field property is provided in the field's layout data. In such case the field component is wrapped with metadata markup to enable chromes hydration when editing in pages. Ability to render metadata has been added to the field rendering components for react and nextjs. ([#1773](https://github.com/Sitecore/jss/pull/1773))
* `[templates/nextjs-sxa]` A new approach `metadata` has been added in SXA components. ([#1788](https://github.com/Sitecore/jss/pull/1788))

### 🛠 Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
LinkField,
Text,
useSitecoreContext,
EditMode,
} from '@sitecore-jss/sitecore-jss-nextjs';

interface Fields {
Image: ImageField;
Image: ImageField & { metadata?: { [key: string]: unknown } };
ImageCaption: Field<string>;
TargetUrl: LinkField;
}
Expand All @@ -29,22 +30,25 @@ const ImageDefault = (props: ImageProps): JSX.Element => (
);

export const Banner = (props: ImageProps): JSX.Element => {
const id = props.params.RenderingIdentifier;
const { sitecoreContext } = useSitecoreContext();
const isPageEditing = sitecoreContext.pageEditing;
const isMetadataMode = sitecoreContext?.editMode === EditMode.Metadata;
const classHeroBannerEmpty =
isPageEditing && props.fields?.Image?.value?.class === 'scEmptyImage'
? 'hero-banner-empty'
: '';
const backgroundStyle = (props?.fields?.Image?.value?.src && {
backgroundImage: `url('${props.fields.Image.value.src}')`,
}) as CSSProperties;
const modifyImageProps = {
...props.fields.Image,
editable: props?.fields?.Image?.editable
?.replace(`width="${props?.fields?.Image?.value?.width}"`, 'width="100%"')
.replace(`height="${props?.fields?.Image?.value?.height}"`, 'height="100%"'),
};
const id = props.params.RenderingIdentifier;
const modifyImageProps = !isMetadataMode
? {
...props.fields.Image,
editable: props?.fields?.Image?.editable
?.replace(`width="${props?.fields?.Image?.value?.width}"`, 'width="100%"')
.replace(`height="${props?.fields?.Image?.value?.height}"`, 'height="100%"'),
}
: { ...props.fields.Image };

return (
<div
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react';
import {
Link,
Text,
useSitecoreContext,
LinkField,
Text,
TextField,
useSitecoreContext,
} from '@sitecore-jss/sitecore-jss-nextjs';
import React from 'react';

interface Fields {
data: {
Expand All @@ -17,7 +17,8 @@ interface Fields {
field: {
jsonValue: {
value: string;
editable: string;
editable?: string;
metadata?: { [key: string]: unknown };
};
};
};
Expand All @@ -29,7 +30,8 @@ interface Fields {
field: {
jsonValue: {
value: string;
editable: string;
editable?: string;
metadata?: { [key: string]: unknown };
};
};
};
Expand Down Expand Up @@ -61,16 +63,11 @@ const ComponentContent = (props: ComponentContentProps) => {
export const Default = (props: TitleProps): JSX.Element => {
const datasource = props.fields?.data?.datasource || props.fields?.data?.contextItem;
const { sitecoreContext } = useSitecoreContext();

const text: TextField = {
value: datasource?.field?.jsonValue?.value,
editable: datasource?.field?.jsonValue?.editable,
};
const text: TextField = datasource?.field?.jsonValue;
const link: LinkField = {
value: {
href: datasource?.url?.path,
title: datasource?.field?.jsonValue?.value,
editable: true,
},
};
if (sitecoreContext.pageState !== 'normal') {
Expand All @@ -84,7 +81,7 @@ export const Default = (props: TitleProps): JSX.Element => {
return (
<ComponentContent styles={props.params.styles} id={props.params.RenderingIdentifier}>
<>
{sitecoreContext.pageState === 'edit' ? (
{sitecoreContext.pageEditing ? (
<Text field={text} />
) : (
<Link field={link}>
Expand Down