Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Svelte component example #10

Open
Joshimello opened this issue Apr 22, 2024 · 5 comments
Open

Svelte component example #10

Joshimello opened this issue Apr 22, 2024 · 5 comments

Comments

@Joshimello
Copy link

Awesome library!
Just wanted to share a component method for anyone using svelte :D
Only the username prop is required.

Component:

<script lang="ts">
  import { minidenticon } from "minidenticons"

  export let username: string
  export let saturation: number = 95
  export let lightness: number = 45

  $: svgURI = 
    'data:image/svg+xml;utf8,' + 
    encodeURIComponent(minidenticon(username, saturation, lightness))
    
</script>

<img
  src={svgURI} 
  alt={username} 
  {...$$restProps}  
/>

Example usage with class props:

<Minidenticon username={USERNAME} class="h-12 w-12" />
@laurentpayot
Copy link
Owner

Thanks @Joshimello!
I’d love to add your example to the readme, but if I’m right Svelte 5 will be released soon with the new "runes" syntax that makes $: svgURI = ... and other stuff obsolete.
Last time I used Svelte it was still in version 3 so I’m not a Svelte expert 😉 Do you think you could rewrite your example with the new runes syntax?

@Joshimello
Copy link
Author

Sure! No problem!

Heres a typed version

<script lang="ts">
  import { minidenticon } from "minidenticons"

  let { 
    username,
    saturation = 95,
    lightness = 45,
    ...others
  } : {
    username: string,
    saturation?: number | string,
    lightness?: number | string,
    [key: string]: any
  } = $props()

  let svgURI: string = $derived(
    'data:image/svg+xml;utf8,' +
    encodeURIComponent(minidenticon(username, saturation, lightness))
  )

</script>

<img
  src={svgURI} 
  alt={username} 
  {...others}
/>

And an untyped version

<script>
  import { minidenticon } from "minidenticons"

  let { 
    username,
    saturation = 95,
    lightness = 45,
    ...others
  } = $props()

  let svgURI = $derived(
    'data:image/svg+xml;utf8,' +
    encodeURIComponent(minidenticon(username, saturation, lightness))
  )

</script>

<img
  src={svgURI} 
  alt={username} 
  {...others}
/>

@laurentpayot
Copy link
Owner

Thanks again @Joshimello 👍
I will probably add this in the docs in not too distant future. Hopefully before Svelte 6 is released 😉

@laurentpayot
Copy link
Owner

Update: I’m actually waiting for React 19 to be officially released (not the beta) to revamp the docs.

@laurentpayot
Copy link
Owner

@Joshimello Svelte 5 was just released, I’ll try to include your example soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants