-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from toddkao/add-web-component
Add web component version of widget
- Loading branch information
Showing
9 changed files
with
197 additions
and
20 deletions.
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': minor | ||
--- | ||
|
||
Add web-component |
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,14 +1,8 @@ | ||
import '../styles/globals.css'; | ||
import type { AppProps } from 'next/app'; | ||
|
||
import { SwapWidgetProvider } from '@skip-go/widget'; | ||
|
||
function MyApp({ Component, pageProps }: AppProps) { | ||
return ( | ||
<SwapWidgetProvider> | ||
<Component {...pageProps} /> | ||
</SwapWidgetProvider> | ||
); | ||
return <Component {...pageProps} />; | ||
} | ||
|
||
export default MyApp; |
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
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,5 +1,6 @@ | ||
import './styles/global.css'; | ||
export { SwapWidgetProvider } from './provider'; | ||
export { SwapWidget, SwapWidgetProps } from './ui'; | ||
export { initializeSwapWidget } from './ui/WebComponent'; | ||
export { useAssets } from './provider/assets'; | ||
export { useChains, useChainByID } from './hooks/use-chains'; |
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,77 @@ | ||
import { SwapWidget, SwapWidgetProps } from './index'; | ||
import { SwapWidgetProvider, SwapWidgetProviderProps } from '../provider'; | ||
|
||
type WebComponentProps = SwapWidgetProps & SwapWidgetProviderProps; | ||
|
||
function isJsonString(str: string) { | ||
try { | ||
JSON.parse(str); | ||
} catch (e) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
const camelize = (inputString: string) => | ||
inputString.replace(/-./g, (x) => x[1].toUpperCase()); | ||
|
||
const WidgetWithProvider = (props: WebComponentProps) => { | ||
// @ts-ignore | ||
const parsedProps = Array.from(props.container.attributes).map( | ||
({ name, value }: any) => { | ||
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; | ||
}, {}); | ||
|
||
const { toasterProps, endpointOptions, apiURL, ...swapWidgetProps } = | ||
realProps as WebComponentProps; | ||
return ( | ||
<SwapWidgetProvider | ||
toasterProps={toasterProps} | ||
endpointOptions={endpointOptions} | ||
apiURL={apiURL} | ||
> | ||
<SwapWidget {...swapWidgetProps} /> | ||
</SwapWidgetProvider> | ||
); | ||
}; | ||
|
||
const WEB_COMPONENT_NAME = 'skip-widget'; | ||
|
||
let initialized = false; | ||
|
||
export const initializeSwapWidget = () => { | ||
if (!initialized && typeof window !== 'undefined') { | ||
import('@r2wc/react-to-web-component').then( | ||
({ default: ReactToWebComponent }) => { | ||
const WebComponent = ReactToWebComponent(WidgetWithProvider); | ||
|
||
if (!customElements.get(WEB_COMPONENT_NAME)) { | ||
customElements.define(WEB_COMPONENT_NAME, WebComponent); | ||
} | ||
initialized = true; | ||
} | ||
); | ||
} | ||
}; | ||
|
||
type Stringify<T> = { | ||
[K in keyof T]?: string; | ||
}; | ||
|
||
declare global { | ||
namespace JSX { | ||
interface IntrinsicElements { | ||
[WEB_COMPONENT_NAME]: Stringify<WebComponentProps>; | ||
} | ||
} | ||
} |
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