Skip to content

Commit

Permalink
Integrate @cartridge/ui-next to keychain
Browse files Browse the repository at this point in the history
  • Loading branch information
JunichiSugiura committed Nov 27, 2024
1 parent 8a8840c commit 968a457
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 25 deletions.
4 changes: 4 additions & 0 deletions packages/keychain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@cartridge/controller": "workspace:^",
"@cartridge/penpal": "^6.2.3",
"@cartridge/ui": "workspace:^",
"@cartridge/ui-next": "workspace:^",
"@cartridge/utils": "workspace:^",
"@emotion/react": "^11.11.1",
"@emotion/styled": "^11.11.0",
Expand Down Expand Up @@ -66,6 +67,7 @@
"@types/js-cookie": "^3.0.2",
"@types/node": "^20.6.0",
"@types/react-dom": "^18.2.7",
"autoprefixer": "^10.4.18",
"concurrently": "^9.0.1",
"eslint": "^8.23.0",
"eslint-config-next": "^12.2.5",
Expand All @@ -74,8 +76,10 @@
"jest": "^29.3.1",
"jest-environment-jsdom": "^29.3.1",
"playwright": "^1.47.1",
"postcss": "^8.4.35",
"prettier": "^2.7.1",
"storybook": "^8.2.9",
"tailwindcss": "^3.4.3",
"typescript": "^5.4.5",
"wait-on": "^8.0.1"
},
Expand Down
7 changes: 7 additions & 0 deletions packages/keychain/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
plugins: {
"postcss-import": {},
tailwindcss: {},
autoprefixer: {},
},
};
2 changes: 1 addition & 1 deletion packages/keychain/src/components/Provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function Provider({ children }: PropsWithChildren) {
<ChakraProvider theme={chakraTheme}>
<CartridgeAPIProvider url={ENDPOINT}>
<QueryClientProvider client={queryClient}>
<ControllerThemeProvider value={controllerTheme}>
<ControllerThemeProvider value={controllerTheme} theme={preset}>
<ConnectionProvider value={connection}>
<PostHogProvider client={posthog}>{children}</PostHogProvider>
</ConnectionProvider>
Expand Down
4 changes: 2 additions & 2 deletions packages/keychain/src/components/connect/Upgrade.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export const Upgrade = () => {
executionError={upgrade.error}
>
<Content>
<Text color="text.secondary" fontSize="sm" pb="10px">
<div className="text-sm text-muted-foreground pb-2">
Install the latest to continue
</Text>
</div>
<VStack
w="full"
align="left"
Expand Down
6 changes: 5 additions & 1 deletion packages/keychain/src/hooks/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
defaultPresets,
} from "@cartridge/controller";
import { CartridgeTheme } from "@cartridge/ui";
import { useThemeEffect } from "@cartridge/ui-next";
import { useColorMode } from "@chakra-ui/react";
import { useRouter } from "next/router";
import {
Expand All @@ -18,15 +19,18 @@ import {
const ControllerThemeContext = createContext<ControllerTheme>(undefined);

export function ControllerThemeProvider({
theme,
value,
children,
}: ProviderProps<ControllerTheme>) {
}: ProviderProps<ControllerTheme> & { theme: ControllerThemePreset }) {
const { setColorMode } = useColorMode();

useEffect(() => {
setColorMode(value.colorMode);
}, [setColorMode, value.colorMode]);

useThemeEffect({ theme, assetUrl: "/" });

return (
<ControllerThemeContext.Provider value={value}>
{children}
Expand Down
7 changes: 7 additions & 0 deletions packages/keychain/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@import "@cartridge/ui-next/dist/themes/default.css";
@import "@cartridge/ui-next/dist/themes/dark.css";
@import "@cartridge/ui-next/dist/themes/fonts.css";

@tailwind base;
@tailwind components;
@tailwind utilities;
2 changes: 2 additions & 0 deletions packages/keychain/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { useEffect } from "react";
import { Provider } from "components/Provider";
import { ErrorBoundary } from "components/ErrorBoundary";

import "../index.css";

const inter = Inter({ subsets: ["latin"] });
const ibmPlexMono = IBM_Plex_Mono({
weight: "600",
Expand Down
22 changes: 22 additions & 0 deletions packages/keychain/tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { cartridgeTWPreset } from "@cartridge/ui-next/preset";
import { Config } from "tailwindcss";

const config: Config = {
content: [
"./src/**/*.{html,ts,tsx}",
"./node_modules/@cartridge/ui-next/dist/**/*.{js,jsx}",
],
presets: [cartridgeTWPreset],
theme: {
extend: {
width: {
desktop: "432px",
},
height: {
desktop: "600px",
},
},
},
};

export default config;
24 changes: 9 additions & 15 deletions packages/profile/src/components/context/theme.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { ControllerThemePreset, defaultPresets } from "@cartridge/controller";
import { useThemeEffect } from "@cartridge/ui-next";
import { hexToHsl } from "@cartridge/utils";
import {
createContext,
useCallback,
useEffect,
useMemo,
useState,
} from "react";
import { createContext, useCallback, useEffect, useState } from "react";
import { useSearchParams } from "react-router-dom";

type ColorScheme = "dark" | "light" | "system";
Expand Down Expand Up @@ -64,13 +59,6 @@ export function ThemeProvider({
},
[storageKey],
);

const appliedColorMode = useMemo(() => {
return document.documentElement.className.includes("dark")
? "dark"
: "light";
}, []);

const [theme, setTheme] = useState<ControllerThemePreset>(initialState.theme);
const themeParam = searchParams.get("theme");

Expand All @@ -80,7 +68,13 @@ export function ThemeProvider({
setTheme(JSON.parse(decodeURIComponent(themeParam)));
}, [themeParam]);

useThemeEffect({ theme, assetUrl: import.meta.env.VITE_KEYCHAIN_URL });

useEffect(() => {
const appliedColorMode = document.documentElement.className.includes("dark")
? "dark"
: "light";

document.documentElement.style.setProperty(
"--theme-icon-url",
`url("${import.meta.env.VITE_KEYCHAIN_URL}${theme.icon}")`,
Expand Down Expand Up @@ -113,7 +107,7 @@ export function ThemeProvider({
hexToHsl(val),
);
}
}, [theme, appliedColorMode]);
}, [theme]);

const value = {
colorScheme,
Expand Down
71 changes: 71 additions & 0 deletions packages/ui-next/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useEffect } from "react";
import { hexToHsl } from "@cartridge/utils";

export function useThemeEffect({
theme,
assetUrl,
}: {
theme: ControllerThemePreset;
assetUrl: string;
}) {
useEffect(() => {
const appliedColorMode = document.documentElement.className.includes("dark")
? "dark"
: "light";

document.documentElement.style.setProperty(
"--theme-icon-url",
`url("${assetUrl}${theme.icon}")`,
);
const coverUrl =
typeof theme.cover === "string"
? `url("${assetUrl}${theme.cover}")`
: `url("${assetUrl}${theme.cover[appliedColorMode]}")`;
document.documentElement.style.setProperty("--theme-cover-url", coverUrl);

if (!theme.colors) return;

if (theme.colors?.primary) {
const val =
typeof theme.colors.primary === "string"
? theme.colors?.primary
: theme.colors?.primary[appliedColorMode];
document.documentElement.style.setProperty("--primary", hexToHsl(val));
}

if (theme.colors?.primaryForeground) {
const val =
typeof theme.colors.primaryForeground === "string"
? theme.colors?.primaryForeground
: theme.colors?.primaryForeground[appliedColorMode];
document.documentElement.style.setProperty(
"--primary-foreground",
hexToHsl(val),
);
}
}, [theme, assetUrl]);
}

// dup of @cartridge/controller/types
type ColorMode = "light" | "dark";
type ControllerTheme = {
id: string;
name: string;
icon: string;
cover: ThemeValue<string>;
colorMode: ColorMode;
};
type ControllerThemePreset = Omit<ControllerTheme, "colorMode"> & {
colors?: ControllerColors;
};
type ControllerColors = {
primary?: ControllerColor;
primaryForeground?: ControllerColor;
};
type ControllerColor = ThemeValue<string>;
type ThemeValue<T> =
| T
| {
dark: T;
light: T;
};
1 change: 1 addition & 0 deletions packages/ui-next/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./components";
export * from "./hooks";
export * from "./utils";
1 change: 1 addition & 0 deletions packages/ui-next/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"include": [
"src/components/**/*",
"src/hooks.ts",
"src/index.ts",
"src/preset.ts",
"src/utils.ts",
Expand Down
24 changes: 18 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 968a457

Please sign in to comment.