Skip to content

Commit a56ac6c

Browse files
authored
Adds option to use image URL for icon (#821)
1 parent 09f630c commit a56ac6c

File tree

4 files changed

+29
-4
lines changed

4 files changed

+29
-4
lines changed

packages/core/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ An object that defines your app:
5454
type AppMetadata = {
5555
// app name
5656
name: string
57-
// SVG icon string, with height or width (whichever is larger) set to 100%
57+
// SVG icon string, with height or width (whichever is larger) set to 100% or a valid image URL
5858
icon: string
5959
// description of app
6060
description?: string

packages/core/src/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,16 @@ export function validEnsChain(chainId: ChainId): boolean {
4343
return false
4444
}
4545
}
46+
47+
export function isUrl(str: string): boolean {
48+
let url
49+
50+
try {
51+
url = new URL(str)
52+
} catch (_) {
53+
console.log(false)
54+
return false
55+
}
56+
console.log(url.protocol === 'http:' || url.protocol === 'https:')
57+
return url.protocol === 'http:' || url.protocol === 'https:'
58+
}

packages/core/src/views/connect/Sidebar.svelte

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import { internalState$ } from '../../streams'
55
import en from '../../i18n/en.json'
66
import type { i18n } from '../../types'
7+
import { isUrl } from '../../utils'
78
89
export let step: keyof i18n['connect']
910
@@ -118,7 +119,11 @@
118119
<div class="inner-container">
119120
<div class="icon-container">
120121
{#if icon}
121-
{@html icon}
122+
{#if isUrl(icon)}
123+
<img height="100%" src={icon} alt="logo" />
124+
{:else}
125+
{@html icon}
126+
{/if}
122127
{:else}
123128
{@html blocknative}
124129
{/if}

packages/core/src/views/shared/WalletAppBadge.svelte

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script lang="ts">
22
import { fade } from 'svelte/transition'
3+
import { isUrl } from '../../utils'
34
import Spinner from './Spinner.svelte'
45
export let size: number // px
5-
export let icon: Promise<string> | string // svg string
6+
export let icon: Promise<string> | string // svg string or url string
67
export let loading = false
78
89
export let border:
@@ -148,7 +149,13 @@
148149
<div class="placeholder-icon" />
149150
{:then iconLoaded}
150151
<div in:fade class="icon">
151-
{@html iconLoaded}
152+
{#if isUrl(iconLoaded)}
153+
<!-- load img url -->
154+
<img height="100%" src={iconLoaded} alt="logo" />
155+
{:else}
156+
<!-- render svg string -->
157+
{@html iconLoaded}
158+
{/if}
152159
</div>
153160
{/await}
154161
{/if}

0 commit comments

Comments
 (0)