forked from thirdweb-dev/js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Storybook for SDK (thirdweb-dev#4230)
## Problem solved Short description of the bug fixed or feature added <!-- start pr-codex --> --- ## PR-Codex overview The focus of this PR is to add Storybook configuration, update dependencies, and improve story components. ### Detailed summary - Added Storybook configuration for `storybook` and `build-storybook` - Updated dependencies for Storybook addons and Vite - Improved story components for `ConnectButton` and `PayEmbed` - Defined absolute paths in Storybook config for addons - Added global CSS styling for Storybook preview - Created utility functions for story client and wallet adapter > The following files were skipped due to too many changes: `pnpm-lock.yaml` > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
- Loading branch information
Showing
13 changed files
with
1,565 additions
and
67 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
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,8 @@ | ||
# copy this file to .env in the same directory and fill in the values | ||
# Note: Adding new env also requires defining it in vite.config.ts file | ||
|
||
# required | ||
STORYBOOK_CLIENT_ID= | ||
|
||
# optional - for testing using a specific account | ||
STORYBOOK_ACCOUNT_PRIVATE_KEY= |
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,3 +1,4 @@ | ||
dist/ | ||
coverage/ | ||
.watchmanconfig | ||
.watchmanconfig | ||
*storybook.log |
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,12 @@ | ||
/** I | ||
IMPORTANT: Do not add tailwind or any global CSS here - so that we can test the components properly without any interference. | ||
*/ | ||
|
||
@import url("https://fonts.googleapis.com/css2?family=Inter:[email protected]&display=swap"); | ||
|
||
body { | ||
font-family: "Inter", sans-serif; | ||
font-optical-sizing: auto; | ||
font-variation-settings: "slnt" 0; | ||
} |
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,27 @@ | ||
import { dirname, join } from "node:path"; | ||
import type { StorybookConfig } from "@storybook/react-vite"; | ||
|
||
/** | ||
* This function is used to resolve the absolute path of a package. | ||
* It is needed in projects that use Yarn PnP or are set up within a monorepo. | ||
*/ | ||
|
||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
function getAbsolutePath(value: string): any { | ||
return dirname(require.resolve(join(value, "package.json"))); | ||
} | ||
const config: StorybookConfig = { | ||
stories: ["../src/**/*.mdx", "../src/**/*.stories.@(js|jsx|mjs|ts|tsx)"], | ||
addons: [ | ||
getAbsolutePath("@storybook/addon-onboarding"), | ||
getAbsolutePath("@storybook/addon-links"), | ||
getAbsolutePath("@storybook/addon-essentials"), | ||
getAbsolutePath("@chromatic-com/storybook"), | ||
getAbsolutePath("@storybook/addon-interactions"), | ||
], | ||
framework: { | ||
name: getAbsolutePath("@storybook/react-vite"), | ||
options: {}, | ||
}, | ||
}; | ||
export default config; |
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,26 @@ | ||
import type { Preview } from "@storybook/react"; | ||
import React from "react"; | ||
import { ThirdwebProvider } from "../src/exports/react"; | ||
import "./global.css"; | ||
|
||
const preview: Preview = { | ||
parameters: { | ||
controls: { | ||
matchers: { | ||
color: /(background|color)$/i, | ||
date: /Date$/i, | ||
}, | ||
}, | ||
}, | ||
decorators: [ | ||
(Story) => { | ||
return ( | ||
<ThirdwebProvider> | ||
<Story /> | ||
</ThirdwebProvider> | ||
); | ||
}, | ||
], | ||
}; | ||
|
||
export default preview; |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { ConnectButton } from "../react/web/ui/ConnectWallet/ConnectButton.js"; | ||
import { storyClient } from "./utils.js"; | ||
|
||
const meta = { | ||
title: "Connect/ConnectButton", | ||
component: ConnectButton, | ||
parameters: { | ||
layout: "centered", | ||
}, | ||
tags: ["autodocs"], | ||
args: { | ||
client: storyClient, | ||
}, | ||
} satisfies Meta<typeof ConnectButton>; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Dark: Story = { | ||
args: { | ||
theme: "dark", | ||
}, | ||
}; | ||
|
||
export const Light: Story = { | ||
args: { | ||
theme: "light", | ||
}, | ||
}; | ||
|
||
export default meta; |
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,42 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import { PayEmbed } from "../react/web/ui/PayEmbed.js"; | ||
import { storyClient } from "./utils.js"; | ||
|
||
const meta = { | ||
title: "Connect/PayEmbed", | ||
component: PayEmbed, | ||
parameters: { | ||
layout: "centered", | ||
}, | ||
tags: ["autodocs"], | ||
args: { | ||
client: storyClient, | ||
}, | ||
} satisfies Meta<typeof PayEmbed>; | ||
|
||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Dark: Story = { | ||
args: { | ||
theme: "dark", | ||
}, | ||
}; | ||
|
||
export const FiatTestMode: Story = { | ||
args: { | ||
theme: "dark", | ||
payOptions: { | ||
buyWithFiat: { | ||
testMode: true, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export const Light: Story = { | ||
args: { | ||
theme: "light", | ||
}, | ||
}; | ||
|
||
export default meta; |
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,29 @@ | ||
import { generatePrivateKey } from "viem/accounts"; | ||
import { createWalletAdapter } from "../adapters/wallet-adapter.js"; | ||
import { ethereum } from "../chains/chain-definitions/ethereum.js"; | ||
import { createThirdwebClient } from "../client/client.js"; | ||
import type { Account } from "../wallets/interfaces/wallet.js"; | ||
import { privateKeyToAccount } from "../wallets/private-key.js"; | ||
|
||
const clientId = process.env.STORYBOOK_CLIENT_ID; | ||
|
||
if (!clientId) { | ||
throw new Error("STORYBOOK_CLIENT_ID env is not configured"); | ||
} | ||
|
||
export const storyClient = createThirdwebClient({ | ||
clientId: clientId, | ||
}); | ||
|
||
export const storyAccount: Account = privateKeyToAccount({ | ||
client: storyClient, | ||
privateKey: process.env.STORYBOOK_ACCOUNT_PRIVATE_KEY || generatePrivateKey(), | ||
}); | ||
|
||
export const storyWallet = createWalletAdapter({ | ||
adaptedAccount: storyAccount, | ||
client: storyClient, | ||
chain: ethereum, | ||
onDisconnect: () => {}, | ||
switchChain: () => {}, | ||
}); |
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,23 @@ | ||
import react from "@vitejs/plugin-react"; | ||
import { defineConfig } from "vite"; | ||
|
||
// This config is required for storybook HMR | ||
|
||
const quotesIfString = (value: string | undefined) => { | ||
if (value === undefined) { | ||
return undefined; | ||
} | ||
return `"${value}"`; | ||
}; | ||
|
||
export default defineConfig({ | ||
plugins: [react()], | ||
define: { | ||
"process.env.STORYBOOK_CLIENT_ID": quotesIfString( | ||
process.env.STORYBOOK_CLIENT_ID, | ||
), | ||
"process.env.STORYBOOK_ACCOUNT_PRIVATE_KEY": quotesIfString( | ||
process.env.STORYBOOK_ACCOUNT_PRIVATE_KEY, | ||
), | ||
}, | ||
}); |
Oops, something went wrong.