Components

Avatar

A picture of someone, or their initials when it does not load.

Previews are drawn in the theme you made.

Edit it
size
shape
color
ring
scheme
direction

Pictures #

import { Avatar, AvatarSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { portrait } from '../../snippets/portrait.js'
import { row } from '../../snippets/layout.js'

// `src` is a URL. The picture covers the initials, which show until it loads
// and where it fails.
export default example({
  title: 'Pictures',
  slots: AvatarSlots,
  recipe: Recipes.Avatar,
  view: ({ style, h }) =>
    row(h, [
      Avatar.view({ name: 'Ada Lovelace', src: portrait(210), style }, h),
      Avatar.view({ name: 'Grace Hopper', src: portrait(20), style }, h),
      Avatar.view({ name: 'Alan Turing', src: portrait(140), style }, h),
    ]),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

A picture from foldkit-media #

import { Schema } from 'effect'
import { ImageAsset, Provider } from 'foldkit-media'
import { Avatar, AvatarSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { portrait } from '../../snippets/portrait.js'

const asset = Schema.decodeUnknownSync(ImageAsset)({
  key: 'ada.jpg',
  width: 512,
  height: 512,
  alpha: false,
  hash: null,
  placeholder: null,
})

// Where the variants live: here, one file at each width the avatar may ask for.
const provider = Provider.fromFiles(
  [40, 80, 160].map(width => ({ width, format: 'webp' as const, url: portrait(width) })),
  { sources: [], fallback: 'webp' },
)

// `picture` is a value: `Avatar.picture` brings `foldkit-media` with it, so a
// page that passes `src` as text never ships it. It asks the provider for the
// widths that cover a 2.5rem avatar at 1x and 2x.
export default example({
  title: 'A picture from foldkit-media',
  slots: AvatarSlots,
  recipe: Recipes.Avatar,
  view: ({ style, h }) =>
    Avatar.view(
      {
        name: 'Ada Lovelace',
        picture: Avatar.picture({ asset, provider, width: Avatar.widthOf('md') }),
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Initials #

import { Avatar, AvatarSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row } from '../../snippets/layout.js'

// No picture: initials, in a colour from the name, the same everywhere for one person.
export default example({
  title: 'Initials',
  slots: AvatarSlots,
  recipe: Recipes.Avatar,
  pins: { color: 'name' },
  view: ({ style, h }) =>
    row(
      h,
      ['Ada Lovelace', 'Grace Hopper', 'Alan Turing', 'Edsger Dijkstra'].map(name =>
        Avatar.view({ name, style }, h),
      ),
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Status #

import { Avatar, AvatarSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { portrait } from '../../snippets/portrait.js'
import { row } from '../../snippets/layout.js'

// A dot on the lower end corner, said in the avatar's name: "Ada Lovelace, Online".
// The statuses are `words`, so a translation is one object.
export default example({
  title: 'Status',
  slots: AvatarSlots,
  recipe: Recipes.Avatar,
  view: ({ style, h }) =>
    row(
      h,
      (['online', 'away', 'busy', 'offline'] as const).map((status, index) =>
        Avatar.view(
          {
            name: 'Ada Lovelace',
            src: portrait(40 + index * 70),
            status,
            words: { busy: 'Do not disturb' },
            style,
          },
          h,
        ),
      ),
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

A group #

import { Avatar, AvatarSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { portrait } from '../../snippets/portrait.js'

const Stack = Recipes.AvatarGroup.style({})

// Overlapping, each ringed in the page, at most four and a count of the rest.
// `wrap` draws around each avatar: here a link to the person's page.
export default example({
  title: 'A group',
  slots: AvatarSlots,
  recipe: Recipes.Avatar,
  view: ({ style, h }) =>
    Avatar.group(
      {
        label: 'Editors',
        people: [
          'Ada Lovelace',
          'Grace Hopper',
          'Alan Turing',
          'Edsger Dijkstra',
          'Barbara Liskov',
          'Donald Knuth',
        ].map((name, index) => ({ name, src: portrait(index * 60) })),
        max: 4,
        wrap: (avatar, person) =>
          h.a([h.Href('/components/avatar'), h.AriaLabel(`${person.name}’s page`)], [avatar]),
        style: Stack,
        avatarStyle: style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

A lifted group #

import { Avatar, AvatarSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'
import { portrait } from '../../snippets/portrait.js'

const Stack = Recipes.AvatarGroup.style({ lift: 'hover' })

// `lift: 'hover'` raises the avatar under the pointer; `rest` draws an icon
// in the count's disc in place of `+2`.
export default example({
  title: 'A lifted group',
  slots: AvatarSlots,
  recipe: Recipes.Avatar,
  view: ({ style, h }) =>
    Avatar.group(
      {
        label: 'Reviewers',
        people: ['Ada Lovelace', 'Grace Hopper', 'Alan Turing', 'Edsger Dijkstra'].map(
          (name, index) => ({ name, src: portrait(index * 70) }),
        ),
        max: 2,
        rest: () => outline(h, 'M3 8h10M8 3v10'),
        style: Stack,
        avatarStyle: style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Badges #

import { Avatar, AvatarSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { outline } from '../../snippets/icon.js'
import { portrait } from '../../snippets/portrait.js'

// A mark on a corner: a count, an icon. The avatar is one image, so a badge is
// hidden and its `label` is read with the name ("Ada Lovelace, 3 unread").
export default example({
  title: 'Badges',
  slots: AvatarSlots,
  recipe: Recipes.Avatar,
  view: ({ style, h }) =>
    Avatar.view(
      {
        name: 'Ada Lovelace',
        src: portrait(210),
        badges: [
          { content: h.span([], ['3']), place: 'top-end', label: '3 unread' },
          { content: outline(h, 'M3 8.5l3 3 7-7'), place: 'bottom-end', label: 'Verified' },
        ],
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Shapes #

import { Avatar, AvatarSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { portrait } from '../../snippets/portrait.js'
import { row } from '../../snippets/layout.js'

// A disc, a rounded square and a square, ringed in the accent.
const Shapes = (['circle', 'rounded', 'square'] as const).map(shape =>
  Recipes.Avatar.style({ shape, size: 'lg', ring: 'accent' }),
)

export default example({
  title: 'Shapes',
  slots: AvatarSlots,
  recipe: Recipes.Avatar,
  view: ({ h }) =>
    row(
      h,
      Shapes.map(style => Avatar.view({ name: 'Grace Hopper', src: portrait(20), style }, h)),
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

With a name #

Alan Turing
alan@example.com
import { Avatar, AvatarSlots, Item, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { portrait } from '../../snippets/portrait.js'

const Row = Recipes.Item.style({ density: 'compact' })

// An avatar with a name and a caption is an Item with the avatar as its media.
export default example({
  title: 'With a name',
  slots: AvatarSlots,
  recipe: Recipes.Avatar,
  view: ({ style, h }) =>
    Item.view(
      {
        media: Avatar.view({ name: 'Alan Turing', src: portrait(140), style }, h),
        title: 'Alan Turing',
        description: 'alan@example.com',
        style: Row,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Configure once #

import { Avatar, AvatarSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { portrait } from '../../snippets/portrait.js'
import { row } from '../../snippets/layout.js'

// Stated once, at module scope: the look and words every avatar of the
// application starts from, and the look of its groups. A preset's `style` is
// worn by a group's avatars too.
const Face = Recipes.Avatar.style({ ring: 'page', color: 'name' })
const Big = Recipes.Avatar.style({ size: 'lg', color: 'name' })
const Stack = Recipes.AvatarGroup.style({})

const AppAvatar = Avatar.configure({
  base: { style: Face, groupStyle: Stack, words: { online: 'Here now' } },
  presets: { profile: { style: Big } },
})

export default example({
  title: 'Configure once',
  slots: AvatarSlots,
  recipe: Recipes.Avatar,
  view: ({ h }) =>
    row(h, [
      AppAvatar.view({ name: 'Ada Lovelace', status: 'online', src: portrait(210) }, h),
      AppAvatar.view({ name: 'Grace Hopper', preset: 'profile' }, h),
      AppAvatar.group({ people: [{ name: 'Alan Turing' }, { name: 'Edsger Dijkstra' }] }, h),
    ]),
})

In your application: recipe goes to forSlots, and the body of view is the call. example is this site’s harness.

Usage

Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.

The recipe ↗This code ↗