Site
Creative card
A creative as the brand shows it: an ad, a product shot or a UGC frame in the brand corner, its color exactly as posted. Without an image it draws a neutral placeholder, so no layout needs a third-party photo.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add creative-card<script type="module" src="/spyglass-ds/elements/creative-card.js"></script>Usage
Every creative on a page or in a view: search results, boards, the hero's row, a ring or a corridor. Pass the image as src (the user's own creative); the placeholder stands in until there is one.
Never
- A filter, a tint, a grade or neon over the image: color as posted
- A border, a shadow or a glow around it
- Another brand's photo in a template or an example: use the placeholder or the user's image
- Moving or zooming on hover
src is used exactly as given: object-fit cover, no filter. Without src the card is a neutral gradient from two neutral steps (tone 1 to 4) with the advertiser in capitals (from 112px wide) and a headline (from 192px wide), named for assistive tech by alt or by those two. The arrangements (creative grid, row, ring and corridor) take the same fields as a list. fit and position place the image in the card (object-fit, cover by default, and object-position). The placeholder carries data-ds-placeholder.
Props
The web component takes the same as attributes or properties (<spyglass-creative-card>); title is heading there.
<CreativeCard>
| Prop | Type | Default |
|---|---|---|
src | string The image, used exactly as posted. | |
alt | string What the creative shows (the image's alt text, or the placeholder's name). | '' |
fit | ImageFit How the image fills the card (object-fit); cover by default. | |
position | string Where the image is anchored (object-position), for example "50% 20%". | |
ratio | CreativeRatio The shape: 4/5, 1/1, 3/4, 7/5, 3/5, 9/16, 16/9 (the deck's frame) or fill. | '4/5' |
brand | string The placeholder's advertiser, in capitals at the foot (shown from 112px wide). | |
caption | string The placeholder's headline (shown from 192px wide). | |
tone | CreativeTone The placeholder's neutral gradient, 1 (darkest) to 4. | 2 |
href | string Makes the creative a link. | |
className | string | |
style | CSSProperties |
Examples
The same example in React and as web components; both render identically.
import { CreativeCard } from '@/react/components/site/CreativeCard'
// Placeholders in the four neutral tones, with the advertiser and a headline, until the real creatives arrive.
export default function Placeholder() {
return (
<div className="grid grid-cols-4 gap-4 max-sm:grid-cols-2">
<CreativeCard tone={1} brand="Acme Skin" caption="Morning routine." />
<CreativeCard tone={2} brand="Acme Drinks" caption="Two new flavors." />
<CreativeCard tone={3} brand="Acme Studio" caption="Made to move." />
<CreativeCard tone={4} brand="Acme Fit" caption="Ten minute plan." />
</div>
)
}<!-- Placeholders in the four neutral tones, with the advertiser and a headline, until the real creatives arrive. -->
<div class="grid grid-cols-4 gap-4 max-sm:grid-cols-2">
<spyglass-creative-card tone="1" brand="Acme Skin" caption="Morning routine."></spyglass-creative-card>
<spyglass-creative-card tone="2" brand="Acme Drinks" caption="Two new flavors."></spyglass-creative-card>
<spyglass-creative-card tone="3" brand="Acme Studio" caption="Made to move."></spyglass-creative-card>
<spyglass-creative-card tone="4" brand="Acme Fit" caption="Ten minute plan."></spyglass-creative-card>
</div>import { CreativeCard } from '@/react/components/site/CreativeCard'
// The shapes the assets use: 4/5 ads, 1/1 tiles, 7/5 landscape, 3/5 tall stories.
export default function Shapes() {
return (
<div className="flex items-end gap-4 max-sm:gap-2">
<CreativeCard ratio="4/5" tone={2} brand="Acme" className="w-40 max-sm:w-20" />
<CreativeCard ratio="1/1" tone={3} brand="Acme" className="w-40 max-sm:w-20" />
<CreativeCard ratio="7/5" tone={1} brand="Acme" className="w-48 max-sm:w-24" />
<CreativeCard ratio="3/5" tone={4} brand="Acme" className="w-32 max-sm:w-16" />
</div>
)
}<!-- The shapes the assets use: 4/5 ads, 1/1 tiles, 7/5 landscape, 3/5 tall stories. -->
<div class="flex items-end gap-4 max-sm:gap-2">
<spyglass-creative-card ratio="4/5" tone="2" brand="Acme" class="w-40 max-sm:w-20"></spyglass-creative-card>
<spyglass-creative-card ratio="1/1" tone="3" brand="Acme" class="w-40 max-sm:w-20"></spyglass-creative-card>
<spyglass-creative-card ratio="7/5" tone="1" brand="Acme" class="w-48 max-sm:w-24"></spyglass-creative-card>
<spyglass-creative-card ratio="3/5" tone="4" brand="Acme" class="w-32 max-sm:w-16"></spyglass-creative-card>
</div>Source
import type { CSSProperties } from 'react'
import { cn } from '@/react/lib/cn'
import { imageFit, type ImageFit } from '@/lib/image'
// A creative as the brand assets show it: an ad, a product shot or a UGC frame in a rounded rectangle with the brand
// corner, the image's color exactly as posted (no grading, filter, overlay or neon). `src` is the image; without one it
// draws a neutral placeholder (a soft gradient from the neutral steps, with the advertiser and a headline when there is
// room), so layouts never need a third-party photo. `ratio` keeps the shapes the assets use (4/5 ads, 1/1 tiles, 3/4
// the ad's three up, 7/5 the ring's landscape tiles, 3/5 the tall ones) or `fill` takes the box its classes give it.
// `href` makes it a link. `fit` and `position` place the image in its box (object-fit, cover by default, and
// object-position). The placeholder carries data-ds-placeholder. It does not move on hover.
export type CreativeRatio = '4/5' | '1/1' | '3/4' | '7/5' | '3/5' | '9/16' | '16/9' | 'fill'
export type CreativeTone = 1 | 2 | 3 | 4
export interface CreativeCardProps {
/** The image, used exactly as posted. */
src?: string
/** What the creative shows (the image's alt text, or the placeholder's name). */
alt?: string
/** How the image fills the card (object-fit); cover by default. */
fit?: ImageFit
/** Where the image is anchored (object-position), for example "50% 20%". */
position?: string
/** The shape: 4/5, 1/1, 3/4, 7/5, 3/5, 9/16, 16/9 (the deck's frame) or fill. */
ratio?: CreativeRatio
/** The placeholder's advertiser, in capitals at the foot (shown from 112px wide). */
brand?: string
/** The placeholder's headline (shown from 192px wide). */
caption?: string
/** The placeholder's neutral gradient, 1 (darkest) to 4. */
tone?: CreativeTone
/** Makes the creative a link. */
href?: string
className?: string
style?: CSSProperties
}
// Placeholder grounds: two neutral steps apart, lighter at the top, as a photo lit from above reads on Black.
const TONES: Record<CreativeTone, string> = {
1: 'bg-linear-to-b from-ds-neutral-4 to-ds-neutral-2',
2: 'bg-linear-to-b from-ds-neutral-5 to-ds-neutral-3',
3: 'bg-linear-to-b from-ds-neutral-6 to-ds-neutral-4',
4: 'bg-linear-to-b from-ds-neutral-7 to-ds-neutral-5',
}
export function CreativeCard({ src, alt = '', fit, position, ratio = '4/5', brand, caption, tone = 2, href, className, style }: CreativeCardProps) {
const box: CSSProperties = ratio === 'fill' ? { ...style } : { aspectRatio: ratio.replace('/', ' / '), ...style }
const classes = cn('@container relative block overflow-hidden rounded-container', src ? 'bg-surface-subtle' : TONES[tone] ?? TONES[2], ratio === 'fill' && 'size-full', className)
const body = src ? (
<img src={src} alt={alt} loading="lazy" decoding="async" className="block size-full object-cover" style={imageFit({ fit, position })} />
) : (
<>
{caption && <span aria-hidden="true" className="absolute inset-x-4 top-4 hidden type-heading-4 text-fg @min-[12rem]:block">{caption}</span>}
{brand && <span aria-hidden="true" className="absolute bottom-3 left-3 hidden type-label text-fg @min-[7rem]:block">{brand}</span>}
</>
)
const label = src ? undefined : alt || [brand, caption].filter(Boolean).join(', ') || undefined
const blank = src ? undefined : ''
if (href) return <a data-ds="creative-card" data-ds-placeholder={blank} href={href} aria-label={label} style={box} className={classes}>{body}</a>
return <div data-ds="creative-card" data-ds-placeholder={blank} role={label ? 'img' : undefined} aria-label={label} style={box} className={classes}>{body}</div>
}
export default CreativeCard
/** One creative in an arrangement (grid, row, ring, corridor): a creative's props, or `light` for a light tile. */
export interface CreativeItem extends Omit<CreativeCardProps, 'ratio' | 'className' | 'style'> {
/** A tile of the brand light instead of a creative (the grid and the ring interleave them). */
light?: boolean
/** The light tile's seed. */
seed?: number
}// Where the brand assets put their creatives, in Figma's numbers (page "8. Brand assets"), for the arrangements both
// flavors draw: the creative ring (Post, Creative ring, 1080x1080), the corridor (Ad, The world's largest creative
// search engine: twelve walls receding to the light at 540, 700) and the deck cover's row of creatives (sizes rising
// to the middle). Positions become percentages of the frame, so an arrangement scales with its box.
export interface Rect { x: number; y: number; w: number; h: number }
/** Percent of a length, rounded to 4 places. */
export const pct = (v: number, of: number) => `${Math.round((v / of) * 1e6) / 1e4}%`
/** A rect as left, top, width and height in percent of its frame. */
export const placeAt = (r: Rect, width: number, height: number) => ({ left: pct(r.x, width), top: pct(r.y, height), width: pct(r.w, width), height: pct(r.h, height) })
/** The creative ring: 1080x1080, twelve tiles clockwise from the top, four of them light tiles, the lockup inside. */
export const RING_FRAME = 1080
export const RING_SLOTS: (Rect & { light?: boolean })[] = [
{ x: 475, y: 116, w: 128, h: 128 },
{ x: 674, y: 154, w: 112, h: 146 },
{ x: 789, y: 303, w: 160, h: 114 },
{ x: 856, y: 475, w: 128, h: 128, light: true },
{ x: 813, y: 647, w: 112, h: 146 },
{ x: 650, y: 795, w: 160, h: 114 },
{ x: 483, y: 827, w: 112, h: 145, light: true },
{ x: 286, y: 788, w: 128, h: 128 },
{ x: 131, y: 663, w: 160, h: 114 },
{ x: 96, y: 475, w: 128, h: 128, light: true },
{ x: 155, y: 287, w: 112, h: 146 },
{ x: 294, y: 155, w: 112, h: 145, light: true },
]
/** How many creatives the ring shows (the other four slots are light tiles). */
export const RING_CREATIVES = RING_SLOTS.filter((s) => !s.light).length
/** The corridor, cut to its walls: 1080 wide, 744 tall (the ad's y 328 to 1072), vanishing at 540, 372. */
export const CORRIDOR_FRAME = { width: 1080, height: 744 }
const VP = 540
const WALLS: Rect[] = [
{ x: -60, y: 0, w: 200, h: 744 },
{ x: 170, y: 142, w: 87, h: 459 },
{ x: 272, y: 206, w: 48, h: 332 },
{ x: 330, y: 242, w: 31, h: 260 },
{ x: 368, y: 265, w: 21, h: 213 },
{ x: 394, y: 281, w: 15, h: 181 },
]
/** The light at the end of the corridor (140x104, the close-up's proportions). */
export const CORRIDOR_LIGHT: Rect = { x: 470, y: 320, w: 140, h: 104 }
export interface Wall extends Rect {
side: 'left' | 'right'
/** The clip that draws the wall in perspective: its far edge shorter, toward the vanishing point. */
clip: string
}
/** The twelve walls, nearest first, left then right: L0, R0, L1, R1 ... L5, R5. */
export function corridorWalls(): Wall[] {
const out: Wall[] = []
for (const l of WALLS) {
const near = VP - l.x
const far = VP - (l.x + l.w)
const inset = Math.round(((1 - far / near) / 2) * 1e4) / 100
out.push({ ...l, side: 'left', clip: `polygon(0 0, 100% ${inset}%, 100% ${100 - inset}%, 0 100%)` })
out.push({ ...l, x: 2 * VP - l.x - l.w, side: 'right', clip: `polygon(0 ${inset}%, 100% 0, 100% 100%, 0 ${100 - inset}%)` })
}
return out
}
/** The deck cover's row: widths rising to the middle (200, 240, 304 on the 1920 canvas), all 4/5. */
export function peakWeights(n: number): number[] {
const mid = (n - 1) / 2
return Array.from({ length: n }, (_, i) => {
const d = Math.abs(i - mid)
return d < 0.75 ? 304 : d < 1.75 ? 240 : 200
})
}// An image slot: what every picture in the library takes in place of its placeholder, in both flavors. `src` is the
// image (the user's own, used as posted), `alt` what it shows ('' when it is decoration), `fit` how it fills its box
// (object-fit: cover by default) and `position` where it is anchored (object-position, for example "50% 20%" or
// "top"). Without `src` an item keeps its placeholder, and every placeholder carries `data-ds-placeholder`, so a page
// can be checked for pictures still to fill.
export type ImageFit = 'cover' | 'contain' | 'fill' | 'none' | 'scale-down'
export interface ImageSlot {
/** The image. */
src?: string
/** What it shows; empty when it is decoration. */
alt?: string
/** How it fills its box (object-fit); cover by default. */
fit?: ImageFit
/** Where it is anchored in its box (object-position), for example "50% 20%" or "top". */
position?: string
}
/** The attribute every placeholder carries (a picture still to fill). */
export const PLACEHOLDER_ATTR = 'data-ds-placeholder'
/** The object-fit and object-position an image asks for, as a style object both flavors take; undefined when it asks for neither. */
export function imageFit(slot?: { fit?: ImageFit; position?: string } | null): { objectFit?: ImageFit; objectPosition?: string } | undefined {
if (!slot || (!slot.fit && !slot.position)) return undefined
return { ...(slot.fit ? { objectFit: slot.fit } : {}), ...(slot.position ? { objectPosition: slot.position } : {}) }
}
/** The same as a style attribute's text, for markup written as a string; '' when it asks for neither. */
export function imageFitText(slot?: { fit?: ImageFit; position?: string } | null): string {
if (!slot) return ''
return [slot.fit ? `object-fit: ${slot.fit}` : '', slot.position ? `object-position: ${slot.position}` : ''].filter(Boolean).join('; ')
}
/** A string or a slot as a slot (lists of pictures take either). */
export const toSlot = (v?: string | ImageSlot | null): ImageSlot => (typeof v === 'string' ? { src: v } : v ?? {})
/**
* The page's sections in the order given: the keys it knows, each once, in the order listed; the page's own order when
* none is given. Leave a key out to omit that section.
*/
export function pageSections<K extends string>(all: readonly K[], given?: readonly string[] | null): K[] {
if (!Array.isArray(given)) return [...all]
const out: K[] = []
for (const k of given) if ((all as readonly string[]).includes(k) && !out.includes(k as K)) out.push(k as K)
return out
}
/** Whether a src is a placeholder: none, or a generated blank that carries the placeholder attribute in its markup. */
export const isPlaceholderSrc = (src?: string) => !src || src.includes(PLACEHOLDER_ATTR)
/** A creative in a page's data: an image slot, or (without `src`) the creative card's placeholder: its advertiser, a
* headline and a neutral tone (1 to 4). */
export interface CreativeImage extends ImageSlot {
/** The placeholder's advertiser. */
brand?: string
/** The placeholder's headline. */
caption?: string
/** The placeholder's neutral gradient, 1 (darkest) to 4. */
tone?: 1 | 2 | 3 | 4
}
/**
* The sections in order with those that live inside one element (an article's head, cover and body) gathered into a
* list where the first of them falls, so a page can keep that element whole whatever the order.
*/
export function groupSections<K extends string>(order: readonly K[], inside: readonly K[]): (K | K[])[] {
const group = order.filter((k) => inside.includes(k))
const out: (K | K[])[] = []
for (const k of order) if (!inside.includes(k)) out.push(k)
else if (k === group[0]) out.push(group)
return out
}// Joins class names, skipping falsy ones: cn('a', on && 'b', ['c', undefined]). It does not merge conflicting
// utilities (no tailwind-merge), as Vue's class binding does not: the library's classes are written not to compete,
// and a className from the caller is appended after them, as Vue appends a fallthrough class.
export type ClassValue = string | false | null | undefined | 0 | ClassValue[]
export function cn(...values: ClassValue[]): string {
const out: string[] = []
for (const v of values) {
if (!v) continue
if (Array.isArray(v)) {
const s = cn(...v)
if (s) out.push(s)
}
else out.push(v)
}
return out.join(' ')
}import { html, nothing } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { styleMap } from 'lit/directives/style-map.js'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import { imageFit, type ImageFit } from '@/lib/image'
import styles from 'virtual:tw'
// CreativeCard.tsx as <spyglass-creative-card>: a creative in the brand corner, the image's color as posted. `src` is
// the image; without one it draws the neutral placeholder (the advertiser from 112px wide, a headline from 192px).
// `ratio` is 4/5, 1/1, 3/4, 7/5, 3/5, 9/16 or fill; `href` makes it a link; `fit` and `position` place the image
// (object-fit and object-position). The placeholder carries data-ds-placeholder. It does not move on hover.
export type CreativeRatio = '4/5' | '1/1' | '3/4' | '7/5' | '3/5' | '9/16' | '16/9' | 'fill'
export type CreativeTone = 1 | 2 | 3 | 4
const TONES: Record<CreativeTone, string> = {
1: 'bg-linear-to-b from-ds-neutral-4 to-ds-neutral-2',
2: 'bg-linear-to-b from-ds-neutral-5 to-ds-neutral-3',
3: 'bg-linear-to-b from-ds-neutral-6 to-ds-neutral-4',
4: 'bg-linear-to-b from-ds-neutral-7 to-ds-neutral-5',
}
/**
* @summary A creative: an image as posted in the brand corner, or a neutral placeholder.
*/
export class CreativeCard extends DsElement {
static styles = [DsElement.styles, styles]
protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="creative-card"]'))
static properties = { src: {}, alt: {}, fit: {}, position: {}, ratio: {}, brand: {}, caption: {}, tone: { type: Number }, href: {} }
/** The image, used exactly as posted. */
src?: string
/** What the creative shows. */
alt = ''
/** How the image fills the card (object-fit); cover by default. */
fit?: ImageFit
/** Where the image is anchored (object-position). */
position?: string
/** 4/5, 1/1, 3/4, 7/5, 3/5, 9/16, 16/9 or fill. */
ratio: CreativeRatio = '4/5'
/** The placeholder's advertiser. */
brand?: string
/** The placeholder's headline. */
caption?: string
/** The placeholder's neutral gradient, 1 to 4. */
tone: CreativeTone = 2
/** Makes the creative a link. */
href?: string
render() {
const ratio = this.ratio || '4/5'
const box = ratio === 'fill' ? {} : { aspectRatio: ratio.replace('/', ' / ') }
const classes = ['@container relative block overflow-hidden rounded-container', this.src ? 'bg-surface-subtle' : TONES[this.tone] ?? TONES[2], ratio === 'fill' ? 'size-full' : ''].filter(Boolean).join(' ')
const body = this.src
? html`<img src=${this.src} alt=${this.alt} loading="lazy" decoding="async" class="block size-full object-cover" style=${styleMap(imageFit(this) ?? {})}>`
: html`${this.caption ? html`<span aria-hidden="true" class="absolute inset-x-4 top-4 hidden type-heading-4 text-fg @min-[12rem]:block">${this.caption}</span>` : nothing}${this.brand ? html`<span aria-hidden="true" class="absolute bottom-3 left-3 hidden type-label text-fg @min-[7rem]:block">${this.brand}</span>` : nothing}`
const label = this.src ? undefined : this.alt || [this.brand, this.caption].filter(Boolean).join(', ') || undefined
return this.href
? html`<a data-ds="creative-card" data-ds-placeholder=${ifDefined(this.src ? undefined : '')} href=${this.href} aria-label=${ifDefined(label)} style=${styleMap(box)} class=${classes}>${body}</a>`
: html`<div data-ds="creative-card" data-ds-placeholder=${ifDefined(this.src ? undefined : '')} role=${ifDefined(label ? 'img' : undefined)} aria-label=${ifDefined(label)} style=${styleMap(box)} class=${classes}>${body}</div>`
}
}
define('creative-card', CreativeCard)Reference: Brand guidelines page 36 (Brand imagery: real creative, color as posted, on Black); Figma page 8: every Creative rounded rectangle (ads 4/5, the post's 200 squares, the ad's 296x400 three up, the ring's 160x114 and 112x146).