Skip to content

Swap the icons and add avatars

Your own icon set, the extra glyphs core ships, an avatar beside each message, and an element of your own kept in sync when the icons or the language change.

On this page
ts
import { aparteGlobalConfig } from '@aparte/core';

aparteGlobalConfig.setIconProvider({
  copy: () => '<svg class="aparte-icon" viewBox="0 0 24 24" aria-hidden="true">…</svg>',
  send: () => '<i class="fa fa-paper-plane" aria-hidden="true"></i>',
});

Give the icons you replace, by name; the others keep core’s. Each call replaces the whole set, so give every icon you replace in one call, or add to the set with registerIcons. A value is markup you trust: an SVG, an icon font’s element, an emoji, an <img>. An SVG or an <img> with class="aparte-icon" takes its size from the place it sits, through --aparte-icon-size, as core’s glyphs do; an icon font or an emoji is text, and follows the font-size around it. The icon names are listed with every glyph; the inline message editor’s save and cancel use check and close.

ts
import { aparteGlobalConfig, type AparteIconProvider } from '@aparte/core';
import { historyIcon, sparklesIcon } from '@aparte/core/icons';

declare const myIcons: AparteIconProvider;   // the set you already gave

aparteGlobalConfig.setIconProvider({ ...myIcons, retry: () => historyIcon });
document.querySelector('#new-chat')?.insertAdjacentHTML('afterbegin', sparklesIcon);

@aparte/core/icons holds glyphs core does not draw itself, each imported by name, so a bundle carries the ones you use. Give one to a name core knows, or put it in your own markup.

ts
import { aparteGlobalConfig } from '@aparte/core';
import { shareIcon } from '@aparte/core/icons';

declare module '@aparte/core' {
  interface AparteIconProvider {
    share?: () => string;
  }
}

aparteGlobalConfig.registerIcons({ share: () => shareIcon });
aparteGlobalConfig.registerAction({ id: 'share', icon: 'share', label: 'Share', zones: ['bubble'] });

registerIcons adds to the set and keeps the rest, so a plugin and the app can each register theirs; a later setIconProvider replaces them all. The new name is then an icon like core’s: an action’s icon, <aparte-composer-action icon="share"> and <aparte-icon name="share"> find it. The declare module block makes share a name the types know: setIconProvider takes it beside core’s, and AparteIconName lists it. getIcon('share') still returns string | undefined, unlike one of core’s names: a configuration nobody registered it on has no drawing for it.

Open
An avatar beside each message, drawn by the avatar provider.
ts
import { aparteGlobalConfig } from '@aparte/core';

aparteGlobalConfig.setAvatarProvider({
  render(role, host) {
    host.setAttribute('aria-hidden', 'true');   // the message already names its author
    host.textContent = role === 'assistant' ? 'AI' : 'Me';
  },
});

A message has no avatar until a provider draws one or the stylesheet names a picture (below): the slot shows once render fills it, and setAvatarProvider(null) takes them off every message. Core empties the slot and calls render again whenever the configuration changes — a new language, a new icon set — or the message’s role does. If render returns a function, core calls it before each new render and when the message goes away, so you can unmount a component you put there.

A picture needs no provider:

css
:root { --aparte-avatar-image-assistant: url('/logo.svg'); }

--aparte-avatar-image-user and --aparte-avatar-image-assistant paint a picture in the slot, and an empty slot whose role names one shows it, with or without a provider. Core reads them when a message is drawn and on every configuration change, on the message element: declare them on :root, on the chat or on aparte-chat-bubble, not on the slot itself. --aparte-avatar-bg-user and --aparte-avatar-bg-assistant colour the ground behind the picture or what your provider writes.

Keep your own element in sync with the icons and language

Section titled “Keep your own element in sync with the icons and language”
ts
import { subscribeConfigChange, resolveConfig } from '@aparte/core';

class MySendButton extends HTMLElement {
  private off: (() => void) | null = null;

  connectedCallback(): void {
    this.innerHTML = '<button type="button"></button>';
    this.paint();
    this.off = subscribeConfigChange(this, () => this.paint());
  }

  disconnectedCallback(): void {
    this.off?.();
    this.off = null;
  }

  private paint(): void {
    const button = this.querySelector('button');
    if (!button) return;
    const config = resolveConfig(this);
    button.innerHTML = config.getIcon('send');
    button.setAttribute('aria-label', config.t('sendButton'));
  }
}

customElements.define('my-send-button', MySendButton);

subscribeConfigChange calls you back when the configuration that governs your element changes — a new icon set, another language — and returns the function that stops it. resolveConfig(this) reads that configuration: the chat’s own when the chat has one, the page-wide one otherwise, so place <my-send-button> inside the chat it should follow. Repaint the parts that change, rather than rebuilding the element, so its focus and its listeners stay.