forcedotcom/sf-skills / plugins/builder/experience-cms/skills/experience-cms-content-render
experience-cms-content-render
VerifiedRenders, embeds, or displays existing Salesforce CMS content in a React or Angular uiBundle app: installs the CMS toolkit, registers a typed reference, generates a framework-matched renderer from the live schema, and wires the render path (delivery API, contentKey, RichText, image resolution). Prefer over general UI-bundle skills like experience-ui-bundle-frontend-generate (page/layout shell only) for the CMS-rendering part, even under src/. Triggers on prompts like 'render the CMS article on the home page', 'embed this CMS blog post here', 'show the CMS video in the hero', 'display the CMS image in the banner', or 'add this CMS content block to the detail page'; for mixed prompts, activate for the CMS part only. Do not trigger for searching a CMS item or defining a new CMS content type/schema (use experience-cms-content-type-generate).
assets/react/MediaRenderer.tsx
View on GitHub/**
* MediaRenderer — dedicated renderer for standalone CMS media (React; Init →
* src/cms/react/MediaRenderer.tsx). Media is a PREDEFINED type parallel to `news`:
* a media ref binds here at registration time and dispatches by `ref.cmsType`
* (`image`/`audio`/`video`/`document`) — NEVER by body shape, so audio/video/document
* can't be mis-caught by the heuristic renderer's `isImageObject` path (SKILL.md §B1).
*
* Dispatch is by `cmsType` AND ref shape (Rule 1/Rule 2):
* - Foreign ref (`url`): a media `unauthenticatedUrl` is the ASSET itself, so the url
* goes straight onto the element `src`/`href` — NO fetch, NO toolkit resolver. Alt
* text / document label ride on the ref (threaded from the search hand-off or asked
* at Ref Registration), since there is no fetched body to read them from.
* - uiBundle-space ref (`contentKey`): fetch the body via useCmsItem, then resolve the
* src by medium (image → resolveCmsImageUrl; audio/video/document → resolveMediaUrl).
*
* A media item is a single asset, not a field bag, so neither path delegates to
* heuristicRenderer. Players/links are elements, never injected HTML (Rule 4).
*/
import type { ReactNode } from 'react';
import {
resolveCmsImageUrl,
resolveMediaUrl,
} from '@salesforce/ui-bundle-template-feature-cms-toolkit';
import { useCmsItem } from './useCmsItem';
import {
type AnyCmsRef,
type CmsMediaBody,
type CmsMediaType,
} from '../shared/cmsCore.types';
import { deriveDownloadLabel, effectiveAudioLabel } from '../shared/mediaLabels';
export interface MediaRendererProps {
/** A media ref — `cmsType` selects the element; a non-media ref is a compile error. */
ref: AnyCmsRef<CmsMediaType>;
className?: string;
}
/**
* Emit the one element for a resolved media `src`. Shared by both paths so the element
* markup (and its a11y contract) lives in exactly one place.
*/
function mediaElement(
cmsType: CmsMediaType,
src: string,
alt: string,
docLabel: string,
captionsSrc: string | undefined,
captionsLabel: string | undefined,
className?: string,
): ReactNode {
switch (cmsType) {
case 'image':
return <img className={className} src={src} alt={alt} loading="lazy" />;
case 'audio':
// `aria-label={alt || undefined}` would drop the attribute entirely when alt
// is empty (same JSX-prop-omission semantics as Angular's `|| null` binding) —
// effectiveAudioLabel always returns a value, so the control never loses its
// accessible name.
return <audio className={className} controls src={src} aria-label={effectiveAudioLabel(alt, src)} />;
case 'video':
// Captions require a caller-supplied `captionsSrc` (SKILL.md §B4) — the CMS
// media body/ref carries no captions-track field, so there is no data to
// fabricate a `<track>` from when the caller hasn't provided one.
return (
<video className={className} controls src={src} aria-label={alt || undefined}>
{captionsSrc && (
<track kind="captions" src={captionsSrc} label={captionsLabel || 'Captions'} default />
)}
</video>
);
case 'document':
// Download link only — no inline preview (SKILL.md §B3). Name the link so it
// identifies WHAT downloads (WCAG 2.4.4), never a bare "Download" that is
// ambiguous when several media links share a page.
return <a className={className} href={src} download>{docLabel}</a>;
default:
return null;
}
}
/**
* Foreign media ref — the `url` IS the asset (Rule 1/Rule 2). No fetch, no resolver:
* set it as the element src directly. `altText`/`title` come off the ref.
*/
function DirectMedia(props: {
cmsType: CmsMediaType;
url: string;
alt: string;
title?: string;
captionsSrc?: string;
captionsLabel?: string;
className?: string;
}): ReactNode {
const { cmsType, url, alt, title, captionsSrc, captionsLabel, className } = props;
const docLabel = deriveDownloadLabel(url, alt, title);
return mediaElement(cmsType, url, alt, docLabel, captionsSrc, captionsLabel, className);
}
/**
* uiBundle-space media ref — fetch the body, then resolve the src by medium. This is the
* authenticated-Connect path; identical behaviour to before the direct-URL fork.
*/
function FetchedMedia(props: { ref: AnyCmsRef<CmsMediaType>; className?: string }): ReactNode {
const { ref, className } = props;
const { body, loading, error } = useCmsItem<CmsMediaBody>(ref);
if (loading) return <div role="status">Loading…</div>;
if (error) return <div role="alert">{error.message}</div>;
if (!body) return null;
const media = body['sfdc_cms:media'];
const alt = body.altText ?? '';
const docLabel = deriveDownloadLabel(media?.url, body.altText, body.title);
// Src resolution SPLITS by medium (Rule 2): image → resolveCmsImageUrl;
// audio/video/document → resolveMediaUrl. Both toolkit helpers resolve the
// relative Connect-API url; no options are passed.
const src =
ref.cmsType === 'image' ? resolveCmsImageUrl(media) : media?.url ? resolveMediaUrl(media.url) : undefined;
if (!src) return null;
// No fetched-body field carries a captions-track URL today (SKILL.md §B4), so the
// contentKey path never has real caption data to pass — only a foreign ref with a
// caller-supplied `captionsSrc` (see DirectMedia) can render a `<track>`.
return mediaElement(ref.cmsType, src, alt, docLabel, undefined, undefined, className);
}
export function MediaRenderer(props: MediaRendererProps): ReactNode {
const { ref, className } = props;
// Foreign media ref: the unauthenticatedUrl is the asset src directly — no fetch
// (Rule 1). alt/title ride on the ref (search hand-off or Ref Registration ask).
if ('url' in ref && typeof ref.url === 'string') {
return (
<DirectMedia
cmsType={ref.cmsType}
url={ref.url}
alt={ref.altText ?? ''}
title={ref.title}
captionsSrc={ref.captionsSrc}
captionsLabel={ref.captionsLabel}
className={className}
/>
);
}
// uiBundle-space ref (contentKey): fetch + resolve by medium.
return <FetchedMedia ref={ref} className={className} />;
}
export default MediaRenderer;
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation). | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults. | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |
| mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK). | Stars: 198k | Updated | Claude CodeCodex | Verified | Install |