Components Data display

Avatar

Identify a person with a picture or a compact initials fallback.

Live previewGo + JavaScript

Usage

Supply Initials and Name. If an ImageURL is available, it overlays the initials. A failed image is removed by the optional enhancement. The group announces the full name once.

Register avatar with Head and pass its properties to Render. Go setup guide

Go usage
Go usage
err := mono.UI.Render(w, "avatar", mono.Props{
	"Initials": "AK",
	"Name":     "Ada Kim",
})
Rendered HTML
Rendered HTML
<span data-mono="avatar" id="demo-avatar" role="img" aria-label="Ada Kim">
  <span aria-hidden="true">AK</span>
  
</span>
Go template
Go template
<span data-mono="avatar" id="{{.ID}}" role="img" aria-label="{{.Name}}">
  <span aria-hidden="true">{{.Initials}}</span>
  {{with .ImageURL}}<img src="{{.}}" alt="" width="48" height="48">{{end}}
</span>
Component CSS
Component CSS
[data-mono="avatar"] { position:relative; display:inline-grid; place-items:center; width:3rem; height:3rem; flex:none; overflow:hidden; border:1px solid #c3c3c3; border-radius:50%; background:#eee; color:#252525; font-size:.95rem; font-weight:680; letter-spacing:-.04em; }
[data-mono="avatar"] img { position:absolute; inset:0; width:100%; height:100%; object-fit:cover; }
JavaScript module
JavaScript module
export function mount(root) {
  const image = root.querySelector('img');
  if (!image) return;
  if (image.complete && !image.naturalWidth) image.remove();
  else image.addEventListener('error', () => image.remove(), { once: true });
}
Use the HTML in a static site

Copy the rendered HTML above. Download these assets into the same folder as your page, then add the tags below to its head.

Shared CSS Component CSS Ready-to-use module

HTML head
<link rel="stylesheet" href="./base.css">
<link rel="stylesheet" href="./avatar.css">
<script type="module" src="./avatar.js"></script>

Copy into your project

Run this command from the Mono checkout. Set -out to the component directory in your application.

Terminal
go run ./cmd/mono-ui add -out ./components avatar
Example properties (JSON)
example.json
{
  "Initials": "AK",
  "Name": "Ada Kim"
}