Components
Aspect ratio
A box that keeps its shape as it grows: a 1:1 post, a 4:5 ad, a 16:9 slide. It draws nothing itself; the brand corner and a #262626 ground are classes on it.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add aspect-ratio<script type="module" src="/spyglass-ds/elements/aspect-ratio.js"></script>Usage
Creatives in a grid, previews of posts and ads, a video or a slide that must not reflow while it loads.
Never
- Stretching or cropping a logo to fit the box
- Grading or filtering the creative inside: show it as posted
Props
The web component takes the same as attributes or properties (<spyglass-aspect-ratio>); title is heading there.
<AspectRatio>
| Prop | Type | Default |
|---|---|---|
ratio | number Width over height (1 for a post, 4 / 5 for an ad, 16 / 9 for a slide). | 1 |
Examples
The same example in React and as web components; both render identically.
import { AspectRatio } from '@/react/components/ui/AspectRatio'
import { BrandLight } from '@/react/components/visuals/BrandLight'
// The brand's three canvases holding their shape at any width: a 1:1 post, a 4:5 ad and a 16:9 slide, each with the
// light behind its label.
const formats = [
{ label: 'Post 1:1', ratio: 1, variant: 'corner' as const },
{ label: 'Ad 4:5', ratio: 4 / 5, variant: 'strip-left' as const },
{ label: 'Slide 16:9', ratio: 16 / 9, variant: 'field' as const },
]
export default function Formats() {
return (
<div className="grid w-200 max-w-full grid-cols-3 items-end gap-4 max-sm:grid-cols-1">
{formats.map((f) => (
<AspectRatio key={f.label} ratio={f.ratio} className="rounded-container bg-surface-raised">
<BrandLight variant={f.variant} seed={1} cover className="absolute inset-0" />
<span className="absolute bottom-3 left-3 type-label text-fg">{f.label}</span>
</AspectRatio>
))}
</div>
)
}<!-- The brand's three canvases holding their shape at any width: a 1:1 post, a 4:5 ad and a 16:9 slide, each with the
light behind its label. -->
<div class="grid w-200 max-w-full grid-cols-3 items-end gap-4 max-sm:grid-cols-1">
<spyglass-aspect-ratio ratio="1" class="rounded-container bg-surface-raised">
<spyglass-brand-light variant="corner" seed="1" cover class="absolute inset-0"></spyglass-brand-light>
<span class="absolute bottom-3 left-3 type-label text-fg">Post 1:1</span>
</spyglass-aspect-ratio>
<spyglass-aspect-ratio ratio="0.8" class="rounded-container bg-surface-raised">
<spyglass-brand-light variant="strip-left" seed="1" cover class="absolute inset-0"></spyglass-brand-light>
<span class="absolute bottom-3 left-3 type-label text-fg">Ad 4:5</span>
</spyglass-aspect-ratio>
<spyglass-aspect-ratio ratio="1.7777777777777777" class="rounded-container bg-surface-raised">
<spyglass-brand-light variant="field" seed="1" cover class="absolute inset-0"></spyglass-brand-light>
<span class="absolute bottom-3 left-3 type-label text-fg">Slide 16:9</span>
</spyglass-aspect-ratio>
</div>Source
import type { HTMLAttributes } from 'react'
import { cn } from '@/react/lib/cn'
// shadcn's aspect ratio: a box that keeps its shape as it grows (CSS aspect-ratio), for a creative or a frame that
// must not reflow: a 1:1 post, a 4:5 ad, a 16:9 deck slide. `ratio` is width over height; the content fills it. It
// draws nothing itself: add rounded-container for the brand corner, and bg-surface-raised for a ground while a picture
// loads.
export interface AspectRatioProps extends HTMLAttributes<HTMLDivElement> {
/** Width over height (1 for a post, 4 / 5 for an ad, 16 / 9 for a slide). */
ratio?: number
}
export function AspectRatio({ ratio = 1, className, style, children, ...rest }: AspectRatioProps) {
return (
<div data-ds="aspect-ratio" className={cn('relative w-full overflow-hidden', className)} style={{ ...style, aspectRatio: String(ratio) }} {...rest}>
{children}
</div>
)
}
export default AspectRatio// 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, type PropertyValues } from 'lit'
import { DsElement, define } from '@/elements/lib/element'
import styles from 'virtual:tw'
// AspectRatio.tsx as <spyglass-aspect-ratio>: a box that keeps its shape as it grows (CSS aspect-ratio), for a creative
// or a frame that must not reflow: a 1:1 post, a 4:5 ad, a 16:9 deck slide. `ratio` is width over height; the content
// fills it. The host is the box (aspect-ratio.css), so a class on it (the corner, a ground, a width) lands as a class
// on <AspectRatio> does.
/**
* @summary A box that keeps its shape as it grows.
* @slot - What fills it (position it absolutely to fill the box).
*/
export class AspectRatio extends DsElement {
static styles = [DsElement.styles, styles]
static properties = { ratio: { type: Number } }
/** Width over height. */
ratio = 1
protected updated(changed: PropertyValues) {
super.updated(changed)
// As React's style on the root: the host is the root.
if (changed.has('ratio')) this.style.aspectRatio = String(this.ratio)
}
render() {
return html`<slot></slot>`
}
}
define('aspect-ratio', AspectRatio)/* The host is the box (AspectRatio.tsx's root div), so a class on <spyglass-aspect-ratio> lands on it as a class on
<AspectRatio> does. */
:host { @apply relative block w-full overflow-hidden; }Reference: Derived from the brand assets' canvases (posts 1080x1080, ads 1080x1350, decks 1920x1080). shadcn's aspect ratio for anatomy..