Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
= image_tag '', class: 'avatar width-full height-full', data: { controller: 'avatar', 'avatar-seed': user.name }
import * as Stimulus from 'stimulus'
import Avatars from '@dicebear/avatars'
import sprites from '@dicebear/avatars-identicon-sprites'
export default class extends Stimulus.Controller {
public connect() {
if (!(this.element instanceof HTMLImageElement)) return
if (this.element.getAttribute('src')) return
const seed: string = this.data.get('seed')
const avatars = new Avatars(sprites())
const svg = avatars.create(seed)
this.element.setAttribute('src', `data:image/svg+xml, ${encodeURIComponent(svg)}`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.