Blocks
Newsletter block
A newsletter sign-up on a #262626 card: the title, a line, an email field and the White button, small print under them.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add newsletter-block<script type="module" src="/spyglass-ds/elements/newsletter-block.js"></script>What changed, every Monday.
One email with the launches worth a look.
No spam. Leave any time.
Usage
A newsletter or waitlist sign-up at the foot of a page or a post.
Never
- A second White button
- A pre-checked consent box
Props
The web component takes the same as attributes or properties (<spyglass-newsletter-block>); title is heading there.
<NewsletterBlock>
| Prop | Type | Default |
|---|---|---|
title | ReactNode | |
body | ReactNode | |
placeholder | string | 'you@company.com' |
cta | string The button's label. | 'Subscribe' |
note | ReactNode Small print under the field. | |
done | string Said in the field's place once subscribed. | 'You are on the list.' |
onSubscribe | (email: string) => void | |
className | string |
Examples
The same example in React and as web components; both render identically.
import { NewsletterBlock } from '@/react/components/blocks/NewsletterBlock'
// A newsletter sign-up for Acme's weekly notes.
export default function Signup() {
return <NewsletterBlock title="What changed, every Monday." body="One email with the launches worth a look." note="No spam. Leave any time." />
}<!-- A newsletter sign-up for Acme's weekly notes. -->
<spyglass-newsletter-block heading="What changed, every Monday." body="One email with the launches worth a look." note="No spam. Leave any time."></spyglass-newsletter-block>Source
'use client'
import { useState, type FormEvent, type ReactNode } from 'react'
import { Button } from '@/react/components/ui/Button'
import { Input } from '@/react/components/ui/Input'
import { cn } from '@/react/lib/cn'
// A newsletter sign-up on a #262626 card with the 4px corner: the title, a #D4D4D4 line, an email field and the White
// button side by side from 640 (stacked on phones), and a line of small print under them. onSubscribe gets the address;
// after it the card says `done` in its place. Nothing moves.
export interface NewsletterBlockProps {
title: ReactNode
body?: ReactNode
placeholder?: string
/** The button's label. */
cta?: string
/** Small print under the field. */
note?: ReactNode
/** Said in the field's place once subscribed. */
done?: string
onSubscribe?: (email: string) => void
className?: string
}
export function NewsletterBlock({ title, body, placeholder = 'you@company.com', cta = 'Subscribe', note, done = 'You are on the list.', onSubscribe, className }: NewsletterBlockProps) {
const [sent, setSent] = useState(false)
const submit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault()
const email = String(new FormData(e.currentTarget).get('email') ?? '').trim()
if (!email) return
onSubscribe?.(email)
setSent(true)
}
return (
<section data-ds="newsletter-block" className={cn('grid gap-6 rounded-container bg-surface-raised p-8 text-fg max-sm:p-6 lg:grid-cols-2 lg:items-end lg:gap-12', className)}>
<div className="grid gap-3">
<h2 className="type-heading-1">{title}</h2>
{body && <p className="max-w-measure type-body text-fg-soft">{body}</p>}
</div>
<div className="grid gap-3">
{sent ? (
<p role="status" className="type-body text-fg">{done}</p>
) : (
<form onSubmit={submit} className="flex gap-3 max-sm:flex-col">
<Input name="email" type="email" autoComplete="email" required placeholder={placeholder} aria-label="Email" className="min-w-0 flex-1" />
<Button type="submit">{cta}</Button>
</form>
)}
{note && <p className="type-caption text-fg-muted">{note}</p>}
</div>
</section>
)
}
export default NewsletterBlock// 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 { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import '@/elements/components/ui/button'
import '@/elements/components/ui/input'
import styles from 'virtual:tw'
// NewsletterBlock.tsx as <spyglass-newsletter-block>: the `heading` (React's `title`), `body`, an email field and the
// `cta` button, the `note` under them; fires `subscribe` with { email } and then shows `done`.
/**
* @summary A newsletter sign-up on a card.
* @fires subscribe - With { email }.
*/
export class NewsletterBlock extends DsElement {
static styles = [DsElement.styles, styles]
protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="newsletter-block"]'))
static properties = { heading: {}, body: {}, placeholder: {}, cta: {}, note: {}, done: {}, sent: { state: true } }
/** The heading (React's `title`). */
heading = ''
body?: string
placeholder = 'you@company.com'
/** The button's label. */
cta = 'Subscribe'
/** Small print under the field. */
note?: string
/** Said in the field's place once subscribed. */
done = 'You are on the list.'
private sent = false
private submit(e: Event) {
e.preventDefault()
const input = this.renderRoot.querySelector('spyglass-input') as (HTMLElement & { value?: string }) | null
const email = String(input?.value ?? '').trim()
if (!email) return
this.emit('subscribe', { email })
this.sent = true
}
render() {
return html`<section data-ds="newsletter-block" class="grid gap-6 rounded-container bg-surface-raised p-8 text-fg max-sm:p-6 lg:grid-cols-2 lg:items-end lg:gap-12"><div class="grid gap-3"><h2 class="type-heading-1">${this.heading}</h2>${this.body ? html`<p class="max-w-measure type-body text-fg-soft">${this.body}</p>` : nothing}</div><div class="grid gap-3">${this.sent ? html`<p role="status" class="type-body text-fg">${this.done}</p>` : html`<form @submit=${(e: Event) => this.submit(e)} class="flex gap-3 max-sm:flex-col"><spyglass-input name="email" type="email" autocomplete="email" required placeholder=${this.placeholder} aria-label="Email" class="min-w-0 flex-1"></spyglass-input><spyglass-button type="submit" @click=${(e: Event) => this.submit(e)}>${this.cta}</spyglass-button></form>`}${this.note ? html`<p class="type-caption text-fg-muted">${this.note}</p>` : nothing}</div></section>`
}
}
define('newsletter-block', NewsletterBlock)Reference: Built from the library (input, button) on the site's card fill.