Skip to content

Commit

Permalink
Merge branch 'main' into svelte-bump-version
Browse files Browse the repository at this point in the history
  • Loading branch information
jinglescode authored Dec 2, 2024
2 parents 742af95 + 828df50 commit f4c53dd
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function Right() {
example += ` <>\n`;
example += ` <CardanoWallet\n`;
example += ` label={"Connect a Wallet"}\n`;
example += ` isDark={isDark}\n`;
example += ` isDark={${isDark}}\n`;
example += ` onConnected={afterConnectedWallet}\n`;
example += ` metamask={{ network: "preprod" }}\n`;
example += ` />\n`;
Expand Down
3 changes: 3 additions & 0 deletions apps/playground/src/pages/svelte/getting-started/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import Metatags from "~/components/site/metatags";
import { metaSvelteGettingstarted } from "~/data/links-svelte";
import SvelteConnectWallet from "../ui-components/connect-wallet";
import SvelteInstall from "./install";
import SvelteState from "./state";

const SveltePage: NextPage = () => {
const sidebarItems = [
{ label: "Install", to: "SvelteInstall" },
{ label: "Connect Wallet", to: "connectWallet" },
{ label: "Get Wallet State", to: "svelteState" },
];

return (
Expand All @@ -34,6 +36,7 @@ const SveltePage: NextPage = () => {

<SvelteInstall />
<SvelteConnectWallet />
<SvelteState />
</SidebarFullwidth>
</>
);
Expand Down
90 changes: 90 additions & 0 deletions apps/playground/src/pages/svelte/getting-started/state.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import Link from "~/components/link";
import LiveCodeDemo from "~/components/sections/live-code-demo";
import TwoColumnsScroll from "~/components/sections/two-columns-scroll";
import Codeblock from "~/components/text/codeblock";

export default function SvelteState() {
return (
<TwoColumnsScroll
sidebarTo="svelteState"
title="Get Wallet State"
leftSection={Left()}
rightSection={Right()}
/>
);
}

function Left() {
let code = ``;
code += `<script lang="ts">\n`;
code += ` import { BrowserWalletState } from "@meshsdk/svelte";\n`;
code += `\n`;
code += ` const { wallet, connected, name, connecting, connect, disconnect } = BrowserWalletState;\n`;
code += `</script>`;

return (
<>
<p>
Provide information on the current wallet's state, and functions for
connecting and disconnecting user wallet.
</p>
<Codeblock data={code} />
<p>
<code>wallet</code> is a{" "}
<Link href="/apis/wallets/browserwallet">Browser Wallet</Link> instance,
which expose all CIP wallets functions from getting assets to signing
tranasction.
</p>
<p>
<code>connected</code>, a boolean, <code>true</code> if user's wallet is
connected.
</p>
<p>
<code>name</code>, a string, the name of the connect wallet.
</p>
<p>
<code>connecting</code>, a boolean, <code>true</code> if the wallet is
connecting and initializing.
</p>
<p>
<code>connect(walletName: string)</code>, a function, provide the wallet
name to connect wallet. Retrive a list of available wallets with{" "}
<code>useWalletList()</code>.
</p>
<p>
<code>disconnect()</code>, a function, to disconnect the connected
wallet.
</p>
</>
);
}

function Right() {
let example = ``;
example += `<script lang="ts">\n`;
example += ` import { CardanoWallet, BrowserWalletState } from "@meshsdk/svelte";\n`;
example += `\n`;
example += ` export async function getAddress() {\n`;
example += ` const { wallet } = BrowserWalletState;\n`;
example += `\n`;
example += ` if (wallet) {\n`;
example += ` const address = await wallet.getChangeAddress();\n`;
example += ` console.log(address);\n`;
example += ` }\n`;
example += ` }\n`;
example += `</script>\n`;
example += `\n`;
example += `<main>\n`;
example += ` <CardanoWallet />\n`;
example += ` <button on:click={getAddress}>getAddress</button>\n`;
example += `</div>\n`;

return (
<LiveCodeDemo
title="Wallet State"
subtitle="Get the current wallet's state"
code={example}
childrenAfterCodeFunctions={true}
></LiveCodeDemo>
);
}
13 changes: 2 additions & 11 deletions apps/playground/src/pages/svelte/ui-components/connect-wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export default function SvelteConnectWallet() {
}

function Left() {
const isDark = useDarkmode((state) => state.isDark);

let codeSignature = ``;
codeSignature += `{\n`;
codeSignature += ` label?: string;\n`;
Expand Down Expand Up @@ -65,7 +63,7 @@ function Left() {

<h3>Customization</h3>
<p>For dark mode style, add isDark.</p>
<Codeblock data={`<CardanoWallet isDark={${isDark}} />`} />
<Codeblock data={`<CardanoWallet isDark={true} />`} />

<p>For a custom label, add the label prop.</p>
<Codeblock data={`<CardanoWallet label={"Connect a Wallet"} />`} />
Expand Down Expand Up @@ -120,18 +118,11 @@ function Left() {
function Right() {
let example = ``;
example += `<script lang="ts">\n`;
example += ` import { CardanoWallet, BrowserWalletState} from "@meshsdk/svelte";\n`;
example += `\n`;
example += ` export async function getAddress() {\n`;
example += ` const { browserWallet } = BrowserWalletState;\n`;
example += ` const address = await browserWallet.getChangeAddress();\n`;
example += ` console.log(address);\n`;
example += ` }\n`;
example += ` import { CardanoWallet } from "@meshsdk/svelte";\n`;
example += `</script>\n`;
example += `\n`;
example += `<main>\n`;
example += ` <CardanoWallet />\n`;
example += ` <button on:click={getAddress}>getAddress</button>\n`;
example += `</main>\n`;

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
connect,
disconnect,
} from "../state/browser-wallet-state.svelte.js";
import { type ConnectWalletButtonProps } from "./";
import { type CardanoWalletButtonProps } from "./";
const {
label = "Connect Wallet",
onConnected,
isDark = true,
metamask = undefined,
extensions = [],
}: ConnectWalletButtonProps = $props();
}: CardanoWalletButtonProps = $props();
let availableWallets: Wallet[] = $state([]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Root from "./connect-wallet-button.svelte";
import { type ConnectWalletButtonProps } from "./types";
import { type CardanoWalletButtonProps } from "./types";

export { Root as ConnectWallet, type ConnectWalletButtonProps };
export { Root as CardanoWallet, type CardanoWalletButtonProps };
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type ConnectWalletButtonProps = {
export type CardanoWalletButtonProps = {
label?: string;
onConnected?: Function;
isDark?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export const BrowserWalletState = {
get connecting() {
return connecting;
},
get connect() {
return connect;
},
get disconnect() {
return disconnect;
},
};

export async function connect(w: Wallet) {
Expand All @@ -47,9 +53,3 @@ export function disconnect() {
icon = undefined;
connected = false;
}

// todo: export the following:
// import {
// BrowserWalletState,
// } from "@meshsdk/svelte";
// const { wallet, connected, name, connecting, connect, disconnect, error } = BrowserWalletState;

0 comments on commit f4c53dd

Please sign in to comment.