Skip to content

Instantly share code, notes, and snippets.

@kimihito
Last active October 4, 2019 00:53
Show Gist options
  • Save kimihito/5e003daad409e4688ab04798acd94373 to your computer and use it in GitHub Desktop.
Save kimihito/5e003daad409e4688ab04798acd94373 to your computer and use it in GitHub Desktop.
= 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