Pages

Contact page

The contact page template, built only from library items: the heading and a line beside a form on the #262626 fill (name, work email, message, one White button), the header and the footer row.

Install

React apps get the React files; every other stack gets the web component.

npx spyglass-ds add contact-page
<script type="module" src="/spyglass-ds/elements/contact-page.js"></script>
Contact page

Talk to us.

Questions about a plan, your team or the index. Send a note and a person reads it.

Where we write back.

Usage

A website's contact page. Edit lib/pages/contact.ts or pass content; the form posts to content.form.action, or onSubmit (the element's send event) takes the values.

Never

  • An address, phone number or reply time you cannot keep
  • More fields than you need, or a second White button
  • New colors, shadows or type sizes added to a section: use the items' props

sections lists the sections to show, in order (intro, form); a key left out is not shown. menus passes the header menus (the live card's picture, the use cases' shots).

Adds with it: button, creative-card, field, footer-row, input, site-header, textarea

Props

The web component takes the same as attributes or properties (<spyglass-contact-page>); title is heading there.

<ContactPage>

PropTypeDefault
contentContactContent
onSubmit(values: { name: string; email: string; message: string }) => void

Receives the form's values instead of posting it.

menusHeaderMenus

The header menus' content, pictures included (the live card's `picture`, the use cases' `shots`); Acme by default.

sectionsreadonly ContactSection[]

The sections to show, in order; all of them in the page order by default (leave one out to omit it).

classNamestring

Examples

The same example in React and as web components; both render identically.

react/examples/contact-page/Page.tsx
import { ContactPage } from '@/react/components/pages/ContactPage'
// The contact page template with its own copy.

export default function Page() {
  return <ContactPage />
}
elements/examples/contact-page/Page.html
<!-- The contact page template with its own copy. -->
<spyglass-contact-page></spyglass-contact-page>

Source

react/components/pages/ContactPage.tsx
'use client'
import { Button } from '@/react/components/ui/Button'
import { Field } from '@/react/components/ui/Field'
import { Input } from '@/react/components/ui/Input'
import { Textarea } from '@/react/components/ui/Textarea'
import { FooterRow } from '@/react/components/site/FooterRow'
import { SiteHeader } from '@/react/components/site/SiteHeader'
import { contactContent, type ContactContent, contactSections, type ContactSection } from '@/lib/pages/contact'
import { cn } from '@/react/lib/cn'
import { pageSections } from '@/lib/image'
import { Fragment, type ReactNode } from 'react'
import type { HeaderMenus } from '@/lib/site-header'

// The contact page from library items: the heading and a line in the first column, the form on the card fill in the
// other (name, work email, message and one White button), one column on phones. The form posts to `content.form.action`
// unless `onSubmit` takes it. `content` replaces the copy (lib/pages/contact.ts).
export interface ContactPageProps {
  content?: ContactContent
  /** Receives the form's values instead of posting it. */
  onSubmit?: (values: { name: string; email: string; message: string }) => void
  /** The header menus' content, pictures included (the live card's `picture`, the use cases' `shots`); Acme by default. */
  menus?: HeaderMenus
  /** The sections to show, in order; all of them in the page order by default (leave one out to omit it). */
  sections?: readonly ContactSection[]
  className?: string
}

const WRAP = 'mx-auto w-full max-w-[calc(var(--ds-layout-content)+2*var(--ds-layout-gutter))] px-(--ds-layout-gutter)'

export function ContactPage({ content: c = contactContent, onSubmit, menus, sections, className }: ContactPageProps) {
  const f = c.form
  const parts: Record<ContactSection, ReactNode> = {
    intro: (
      <div className="grid max-w-xl gap-6">
        <h1 className="type-section">{c.headline}</h1>
        <p className="type-body-lg text-fg-soft">{c.body}</p>
      </div>
    ),
    form: (
      <form
        aria-label={f.label}
        action={f.action}
        method="post"
        onSubmit={onSubmit ? (e) => {
          e.preventDefault()
          const d = new FormData(e.currentTarget)
          onSubmit({ name: String(d.get('name') ?? ''), email: String(d.get('email') ?? ''), message: String(d.get('message') ?? '') })
        } : undefined}
        className="grid gap-6 rounded-container bg-surface-raised p-8 max-sm:p-6"
      >
        <Field label={f.name.label}>
          {({ id }) => <Input id={id} name="name" autoComplete="name" placeholder={f.name.placeholder} required />}
        </Field>
        <Field label={f.email.label} description={f.email.description}>
          {({ id, describedBy }) => <Input id={id} name="email" type="email" autoComplete="email" placeholder={f.email.placeholder} aria-describedby={describedBy} required />}
        </Field>
        <Field label={f.message.label}>
          {({ id }) => <Textarea id={id} name="message" rows={6} placeholder={f.message.placeholder} required />}
        </Field>
        <Button type="submit" className="w-full">{f.submit}</Button>
      </form>
    ),
  }
  return (
    <div data-ds="contact-page" className={cn('flex min-h-dvh flex-col bg-surface text-fg', className)}>
      <SiteHeader menus={menus} links={c.header.nav} secondary={c.header.secondary} cta={c.header.cta} />
      <main className={cn(WRAP, 'grid flex-1 grid-cols-2 items-start gap-12 py-24 max-md:grid-cols-1 max-md:gap-8 max-sm:py-16')}>
        {pageSections(contactSections, sections).map((k) => <Fragment key={k}>{parts[k]}</Fragment>)}
      </main>
      <footer className="border-t border-edge">
        <div className={cn(WRAP, 'py-10')}>
          <FooterRow domain={c.footer.domain} homeHref="/" links={c.footer.links} />
        </div>
      </footer>
    </div>
  )
}

export default ContactPage
lib/pages/contact.ts
// The contact page template's copy: short declarative sentences and a form with three fields. No address, phone
// number or reply time is stated: the brand assets give none. Both flavors read this file.
const nav = [
	{ label: 'Product', href: '/product' },
	{ label: 'Pricing', href: '/pricing' },
	{ label: 'Changelog', href: '/changelog' },
	{ label: 'Blog', href: '/blog' },
]
const footer = {
	domain: 'spyglass.so',
	links: [
		{ label: 'Product', href: '/product' },
		{ label: 'Pricing', href: '/pricing' },
		{ label: 'About', href: '/about' },
		{ label: 'Changelog', href: '/changelog' },
		{ label: 'Blog', href: '/blog' },
		{ label: 'Terms', href: '/legal' },
	],
}
const header = { nav, secondary: { label: 'Sign in', href: '/sign-in' }, cta: { label: 'Get 50% off', href: '/pricing' } }

export const contactContent = {
	header,
	headline: 'Talk to us.',
	body: 'Questions about a plan, your team or the index. Send a note and a person reads it.',
	form: {
		label: 'Contact',
		action: '/contact',
		name: { label: 'Name', placeholder: 'Alex at Acme' },
		email: { label: 'Work email', placeholder: 'you@acme.com', description: 'Where we write back.' },
		message: { label: 'Message', placeholder: 'What would you like to know?' },
		submit: 'Send message',
	},
	footer,
}

export type ContactContent = typeof contactContent

/** The contact page's sections in order; pass `sections` to reorder them or leave some out. */
export const contactSections = ['intro', 'form'] as const
export type ContactSection = (typeof contactSections)[number]
react/lib/cn.ts
// 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(' ')
}
elements/components/pages/contact-page.ts
import { html } from 'lit'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import { contactContent, type ContactContent, contactSections, type ContactSection } from '@/lib/pages/contact'
import '@/elements/components/ui/button'
import '@/elements/components/ui/field'
import '@/elements/components/ui/input'
import '@/elements/components/ui/textarea'
import '@/elements/components/site/footer-row'
import '@/elements/components/site/site-header'
import styles from 'virtual:tw'
import { pageSections } from '@/lib/image'
import { headerMenus, type HeaderMenus } from '@/lib/site-header'

// ContactPage.tsx as <spyglass-contact-page>: the heading and a line beside the form on the card fill. The form posts
// to `content.form.action`; it first fires a cancelable `send` event with { name, email, message }, and a listener
// that calls preventDefault() takes the values instead (React's `onSubmit`).
const WRAP = 'mx-auto w-full max-w-[calc(var(--ds-layout-content)+2*var(--ds-layout-gutter))] px-(--ds-layout-gutter)'

/**
 * @summary The contact page template.
 * @fires send - On submit, with { name, email, message }; preventDefault() keeps the form from posting.
 */
export class ContactPage extends DsElement {
	static styles = [DsElement.styles, styles]
	protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('[data-ds="contact-page"]'))
	static properties = { content: { type: Object }, menus: { type: Object }, sections: { type: Array } }
	/** The page's copy (lib/pages/contact.ts by default). */
	content: ContactContent = contactContent
	/** The header menus' content, pictures included (the live card's `picture`, the use cases' `shots`); Acme by default. */
	menus?: HeaderMenus
	/** The sections to show, in order (a list of names); all of them in the page order by default. */
	sections?: ContactSection[]

	private onSubmit(e: SubmitEvent) {
		const form = e.currentTarget as HTMLFormElement
		const d = new FormData(form)
		const detail = { name: String(d.get('name') ?? ''), email: String(d.get('email') ?? ''), message: String(d.get('message') ?? '') }
		const ev = new CustomEvent('send', { detail, bubbles: true, composed: true, cancelable: true })
		if (!this.dispatchEvent(ev)) e.preventDefault()
	}

	render() {
		const c = this.content ?? contactContent
		const f = c.form
		const parts: Record<ContactSection, unknown> = {
			intro: html`<div class="grid max-w-xl gap-6"><h1 class="type-section">${c.headline}</h1><p class="type-body-lg text-fg-soft">${c.body}</p></div>`,
			form: html`<form aria-label=${f.label} action=${f.action} method="post" @submit=${this.onSubmit} class="grid gap-6 rounded-container bg-surface-raised p-8 max-sm:p-6">
<spyglass-field label=${f.name.label}><spyglass-input name="name" autocomplete="name" placeholder=${f.name.placeholder} required></spyglass-input></spyglass-field>
<spyglass-field label=${f.email.label} description=${f.email.description}><spyglass-input name="email" type="email" autocomplete="email" placeholder=${f.email.placeholder} required></spyglass-input></spyglass-field>
<spyglass-field label=${f.message.label}><spyglass-textarea name="message" rows="6" placeholder=${f.message.placeholder} required></spyglass-textarea></spyglass-field>
<spyglass-button type="submit" class="w-full">${f.submit}</spyglass-button>
</form>`,
		}
		return html`<div data-ds="contact-page" class="flex min-h-dvh flex-col bg-surface text-fg">
<spyglass-site-header .menus=${this.menus ?? headerMenus} .links=${c.header.nav} .secondary=${c.header.secondary} .cta=${c.header.cta}></spyglass-site-header>
<main class=${`${WRAP} grid flex-1 grid-cols-2 items-start gap-12 py-24 max-md:grid-cols-1 max-md:gap-8 max-sm:py-16`}>${pageSections(contactSections, this.sections).map((k) => parts[k])}</main>
<footer class="border-t border-edge"><div class=${`${WRAP} py-10`}><spyglass-footer-row domain=${c.footer.domain} home-href="/" .links=${c.footer.links}></spyglass-footer-row></div></footer>
</div>`
	}
}

define('contact-page', ContactPage)

Reference: Built from the library (site header, field, input, textarea, button, footer row); the brand assets have no contact layout, so it follows the page templates and the plan card's fill.