-
Notifications
You must be signed in to change notification settings - Fork 24
[FRE-1668] Update web-component to allow passing props in the same format as the react component (camelCase) #1055
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fbb8aa3
WIP
toddkao fb21d8d
WIP
toddkao ec602e6
remove manually handling props, instead rely on library to handle it …
toddkao b184242
Update web-component to allow passing props via javascript properties
toddkao c8a5819
remove unnecessary mounted event
toddkao 08b59cc
remove unused imports
toddkao ebdf6c9
Update docs
toddkao 1c3f0ba
Merge branch 'staging' into wip-fixing-web-component
toddkao f816ff9
update to map all props as any
toddkao c492659
revert unintentional changes
toddkao c975697
delete unused page
toddkao acf8420
simplify interface type to no longer extend HTMLElement
toddkao 13e4c5b
coerce element as Node
toddkao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@skip-go/widget": patch | ||
--- | ||
|
||
Update web-component to allow passing props via javascript properties |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
'use client'; | ||
|
||
import React, { useState } from "react"; | ||
import { Widget } from "@skip-go/widget"; | ||
import { PhantomWalletAdapter } from "@solana/wallet-adapter-phantom"; | ||
import { createWalletClient, custom, Account } from "viem"; | ||
import { mainnet, optimism, polygon, base, arbitrum, avalanche } from 'viem/chains'; | ||
|
||
type ChainId = string; | ||
type Address = string; | ||
|
||
export default function Home() { | ||
// This state holds a mapping from chain IDs to connected addresses. | ||
const [connectedAddresses, setConnectedAddresses] = useState<Record<ChainId, Address>>(); | ||
|
||
/** | ||
* Helper to update the connectedAddresses with a given chainId and address. | ||
*/ | ||
const updateAccount = (chainId: ChainId, address: Address) => { | ||
setConnectedAddresses((prev) => ({ | ||
...prev, | ||
[chainId]: address, | ||
})); | ||
}; | ||
|
||
/** | ||
* Connect to an EVM-compatible wallet (e.g., MetaMask). | ||
*/ | ||
const connectEthereum = async () => { | ||
const ethereum = window.ethereum; | ||
if (!ethereum) { | ||
throw new Error("MetaMask not installed"); | ||
} | ||
|
||
// Request accounts | ||
const accounts = (await ethereum.request({ | ||
method: "eth_requestAccounts", | ||
})) as string[]; | ||
|
||
const evmAddress = accounts[0]; | ||
if (!evmAddress) throw new Error("No EVM accounts found"); | ||
|
||
// Get currently selected chain ID from MetaMask | ||
const chainIdHex = (await ethereum.request({ method: 'eth_chainId' })) as string; | ||
const chainId = parseInt(chainIdHex, 16).toString(); | ||
|
||
updateAccount(chainId, evmAddress); | ||
}; | ||
|
||
/** | ||
* Connect to a Solana wallet using Phantom Wallet Adapter. | ||
*/ | ||
const connectSolana = async () => { | ||
const phantom = new PhantomWalletAdapter(); | ||
await phantom.connect(); | ||
const publicKey = phantom.publicKey?.toBase58(); | ||
if (!publicKey) throw new Error("No public key found"); | ||
updateAccount("solana", publicKey); | ||
}; | ||
|
||
/** | ||
* Connect to Cosmos-based chains using Keplr. | ||
*/ | ||
const connectCosmos = async () => { | ||
const chainIds = ["cosmoshub-4", "osmosis-1"]; | ||
|
||
// Request access to the specified Cosmos chains from Keplr | ||
await window.keplr?.enable(chainIds); | ||
|
||
// Fetch and store addresses for each chain | ||
await Promise.all( | ||
chainIds.map(async (chainId) => { | ||
const keyInfo = await window.keplr?.getKey(chainId); | ||
if (keyInfo && keyInfo.bech32Address) { | ||
updateAccount(chainId, keyInfo.bech32Address); | ||
} | ||
}) | ||
); | ||
}; | ||
|
||
/** | ||
* Get an offline signer for a given Cosmos chain using Keplr. | ||
*/ | ||
const getCosmosSigner = async (chainId: string) => { | ||
if (window.keplr?.getOfflineSigner === undefined) { | ||
throw new Error("Keplr extension not installed"); | ||
} | ||
const offlineSigner = await window.keplr?.getOfflineSigner(chainId); | ||
return offlineSigner; | ||
} | ||
|
||
/** | ||
* Get an EVM-compatible signer by creating a viem wallet client. | ||
*/ | ||
const chainConfigMap: Record<string, any> = { | ||
"1": mainnet, | ||
"10": optimism, | ||
"137": polygon, | ||
"8453": base, | ||
"42161": arbitrum, | ||
"43114": avalanche, | ||
}; | ||
|
||
const getEVMSigner = async () => { | ||
const ethereum = window.ethereum; | ||
if (!ethereum) { | ||
throw new Error("MetaMask not installed"); | ||
} | ||
|
||
// Request accounts | ||
const accounts = (await ethereum.request({ | ||
method: "eth_requestAccounts", | ||
})) as Account[]; | ||
|
||
const evmAddress = accounts?.[0]; | ||
if (!evmAddress) { | ||
throw new Error("No EVM accounts found"); | ||
} | ||
|
||
// Get the currently selected chain ID | ||
const chainIdHex = (await ethereum.request({ method: 'eth_chainId' })) as string; | ||
const chainId = parseInt(chainIdHex, 16).toString(); | ||
|
||
const selectedChain = chainConfigMap[chainId] ?? mainnet; | ||
|
||
const client = createWalletClient({ | ||
account: evmAddress, | ||
chain: selectedChain, | ||
transport: custom(ethereum), | ||
}); | ||
|
||
return client; | ||
}; | ||
|
||
/** | ||
* Get an SVM-compatible signer using Phantom. | ||
*/ | ||
const getSVMSigner = async () => { | ||
const phantom = new PhantomWalletAdapter(); | ||
await phantom.connect(); | ||
return phantom; | ||
}; | ||
|
||
return ( | ||
<div | ||
style={{ | ||
width: "100%", | ||
maxWidth: 500, | ||
padding: "0 10px", | ||
boxSizing: "border-box", | ||
}} | ||
> | ||
<p>Connected addresses:</p> | ||
<ul> | ||
{Object.entries(connectedAddresses ?? {}).map(([chainId, address]) => ( | ||
<li key={chainId}> | ||
{chainId}: {address} | ||
</li> | ||
))} | ||
</ul> | ||
<div | ||
style={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
gap: "10px", | ||
}} | ||
> | ||
<button onClick={connectCosmos}>Connect Cosmos</button> | ||
<button onClick={connectEthereum}>Connect Ethereum</button> | ||
<button onClick={connectSolana}>Connect Solana</button> | ||
</div> | ||
<Widget | ||
// Provide the connected addresses and signer retrieval functions to the Widget | ||
connectedAddresses={connectedAddresses} | ||
getCosmosSigner={getCosmosSigner} | ||
getEVMSigner={getEVMSigner} | ||
getSVMSigner={getSVMSigner} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
<template> | ||
<div> | ||
<div style="width:100%; max-width:500px; padding: 0 10px;"> | ||
<skip-widget | ||
theme='{ | ||
"backgroundColor": "#191A1C", | ||
"textColor": "#E6EAE9", | ||
"borderColor": "#363B3F", | ||
"brandColor": "#FF4FFF", | ||
"highlightColor": "#1F2022" | ||
}' | ||
default-route='{ | ||
"srcChainID": "osmosis-1", | ||
"srcAssetDenom": "ibc/1480b8fd20ad5fcae81ea87584d269547dd4d436843c1d20f15e00eb64743ef4" | ||
}'> | ||
</skip-widget> | ||
<skip-widget></skip-widget> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
const skipWidget = document.querySelector("skip-widget"); | ||
|
||
if (skipWidget) { | ||
skipWidget.onRouteUpdated = (route) => { | ||
console.log("route updated", route); | ||
}; | ||
} | ||
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@codingki docs are here and this is where it shows up https://docs.skip.build/go/widget/web-component