-
Notifications
You must be signed in to change notification settings - Fork 22
[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 all 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 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 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 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 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 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 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,73 +1,74 @@ | ||
/* eslint-disable @typescript-eslint/consistent-type-definitions */ | ||
/* eslint-disable @typescript-eslint/no-namespace */ | ||
|
||
import toWebComponent from "@r2wc/react-to-web-component"; | ||
import { Widget } from "./widget/Widget"; | ||
import { NewSkipClientOptions, Widget, WidgetProps } from "./widget/Widget"; | ||
|
||
type WebComponentProps = { | ||
container: { | ||
attributes: Record<string, string>[]; | ||
}; | ||
}; | ||
const WEB_COMPONENT_NAME = "skip-widget"; | ||
|
||
function isJsonString(str: string) { | ||
try { | ||
JSON.parse(str); | ||
} catch (_err) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
type WebComponentProps = WidgetProps & NewSkipClientOptions; | ||
|
||
const camelize = (inputString: string) => { | ||
inputString = inputString.toLowerCase(); | ||
return inputString.replace(/-./g, (x) => x[1].toUpperCase()); | ||
type PropDescriptors = { | ||
[K in keyof WebComponentProps]: "any"; | ||
}; | ||
|
||
const WidgetWithProvider = (props: WebComponentProps) => { | ||
const parsedProps = Array.from(props.container.attributes).map(({ name, value }) => { | ||
return { key: name, value }; | ||
}); | ||
|
||
const realProps = parsedProps.reduce( | ||
(accumulator, initialValue) => { | ||
const { key, value } = initialValue; | ||
|
||
accumulator[camelize(key)] = isJsonString(value) ? JSON.parse(value) : value; | ||
return accumulator; | ||
}, | ||
{} as Record<string, string>, | ||
); | ||
|
||
return <Widget {...realProps} />; | ||
const widgetPropTypes: Required<PropDescriptors> = { | ||
rootId: "any", | ||
theme: "any", | ||
brandColor: "any", | ||
onlyTestnet: "any", | ||
defaultRoute: "any", | ||
settings: "any", | ||
routeConfig: "any", | ||
filter: "any", | ||
filterOut: "any", | ||
filterOutUnlessUserHasBalance: "any", | ||
walletConnect: "any", | ||
enableSentrySessionReplays: "any", | ||
enableAmplitudeAnalytics: "any", | ||
connectedAddresses: "any", | ||
simulate: "any", | ||
disableShadowDom: "any", | ||
ibcEurekaHighlightedAssets: "any", | ||
assetSymbolsSortedToTop: "any", | ||
hideAssetsUnlessWalletTypeConnected: "any", | ||
apiUrl: "any", | ||
apiKey: "any", | ||
endpointOptions: "any", | ||
aminoTypes: "any", | ||
registryTypes: "any", | ||
chainIdsToAffiliates: "any", | ||
cacheDurationMs: "any", | ||
getCosmosSigner: "any", | ||
getEVMSigner: "any", | ||
getSVMSigner: "any", | ||
onWalletConnected: "any", | ||
onWalletDisconnected: "any", | ||
onTransactionBroadcasted: "any", | ||
onTransactionComplete: "any", | ||
onTransactionFailed: "any", | ||
onRouteUpdated: "any", | ||
}; | ||
|
||
const WEB_COMPONENT_NAME = "skip-widget"; | ||
|
||
const WebComponent = toWebComponent(WidgetWithProvider); | ||
const WebComponent = toWebComponent(Widget, { | ||
// @ts-expect-error any is not one of the valid types but it works | ||
props: widgetPropTypes, | ||
}); | ||
|
||
function initializeSkipWidget() { | ||
if (!customElements.get(WEB_COMPONENT_NAME)) { | ||
customElements.define(WEB_COMPONENT_NAME, WebComponent); | ||
} | ||
|
||
// Upgrade any existing skip-widget elements | ||
document.querySelectorAll(WEB_COMPONENT_NAME).forEach((el) => { | ||
customElements.upgrade(el); | ||
}); | ||
document.querySelectorAll(WEB_COMPONENT_NAME).forEach((el) => customElements.upgrade(el as Node)); | ||
} | ||
|
||
initializeSkipWidget(); | ||
|
||
export default WebComponent; | ||
|
||
type Stringify<T> = { | ||
[K in keyof T]?: string; | ||
}; | ||
|
||
declare global { | ||
namespace JSX { | ||
interface IntrinsicElements { | ||
[WEB_COMPONENT_NAME]: Stringify<WebComponentProps>; | ||
} | ||
interface HTMLElementTagNameMap { | ||
[WEB_COMPONENT_NAME]: WidgetProps; | ||
Comment on lines
+71
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
} |
This file contains 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
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