Skip to content

Commit cb273b1

Browse files
authored
Merge pull request #174 from episerver/feature/CMS-46529-preview-attributes-element
Pass down composition to components
2 parents 9b279fe + 86973d0 commit cb273b1

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

packages/optimizely-cms-sdk/src/infer.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,11 @@ export type InferFromProperty<T extends AnyProperty> =
105105
export type InferredBase = {
106106
_id: string;
107107
_metadata: InferredContentMetadata;
108+
109+
// Properties that don't come from Graph are prefixed with double-underscores
108110
__typename: string;
109111
__context?: { edit: boolean; preview_token: string };
112+
__composition?: ExperienceComponentNode;
110113
};
111114

112115
/** Only include keys where indexingType is not 'disabled' */

packages/optimizely-cms-sdk/src/react/server.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ type OptimizelyComponentProps = {
8484
/** Preview context */
8585
__context?: { edit: boolean; preview_token: string };
8686

87-
composition?: ExperienceCompositionNode;
87+
__composition?: ExperienceCompositionNode;
8888
};
8989

9090
displaySettings?: Record<string, string>;
@@ -98,7 +98,8 @@ export async function OptimizelyComponent({
9898
if (!componentRegistry) {
9999
throw new Error('You should call `initReactComponentRegistry` first');
100100
}
101-
const dtKey = opti.composition?.displayTemplateKey ?? opti.displayTemplateKey;
101+
const dtKey =
102+
opti.__composition?.displayTemplateKey ?? opti.displayTemplateKey;
102103
const Component = await componentRegistry.getComponent(opti.__typename, {
103104
tag: opti.__tag ?? getDisplayTemplateTag(dtKey),
104105
});
@@ -260,7 +261,9 @@ export function OptimizelyGridSection({
260261
return (
261262
<OptimizelyComponent
262263
opti={{
264+
// `node.component` contains user-defined properties
263265
...node.component,
266+
__composition: node,
264267
__tag: tag,
265268
}}
266269
key={node.key}
@@ -307,17 +310,19 @@ export function OptimizelyGridSection({
307310
export function getPreviewUtils(opti: OptimizelyComponentProps['opti']) {
308311
return {
309312
/** Get the HTML data attributes required for a property */
310-
pa(property: string | { key: string }) {
313+
pa(property?: string | { key: string }) {
311314
if (opti.__context?.edit) {
312315
if (typeof property === 'string') {
313316
return {
314317
'data-epi-property-name': property,
315318
};
316-
} else {
319+
} else if (property) {
317320
return {
318321
'data-epi-block-id': property.key,
319322
};
320323
}
324+
325+
return {};
321326
} else {
322327
return {};
323328
}

0 commit comments

Comments
 (0)