Skip to content
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

Support colorMode via Controller constructor #309

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const connectors = [
{
url,
// theme: "rollyourown",
// colorMode: "light"
},
) as never as Connector,
];
15 changes: 12 additions & 3 deletions packages/controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
Modal,
ControllerOptions,
ControllerThemePresets,
ColorMode,
} from "./types";
import { createModal } from "./modal";
import { defaultPresets } from "./presets";
Expand Down Expand Up @@ -69,6 +70,9 @@ class Controller {
}

this.setTheme(options?.theme, options?.config?.presets);
if (options?.colorMode) {
this.setColorMode(options.colorMode);
}

if (typeof document === "undefined") {
return;
Expand Down Expand Up @@ -107,14 +111,19 @@ class Controller {
}

private setTheme(
theme: string = "cartridge",
id: string = "cartridge",
presets: ControllerThemePresets = defaultPresets,
) {
const t = presets[theme] ?? defaultPresets.cartridge;
const theme = presets[id] ?? defaultPresets.cartridge;

const url = new URL(this.url);
url.searchParams.set("theme", encodeURIComponent(JSON.stringify(t)));
url.searchParams.set("theme", encodeURIComponent(JSON.stringify(theme)));
this.url = url.toString();
}

private setColorMode(colorMode: ColorMode) {
const url = new URL(this.url);
url.searchParams.set("colorMode", colorMode);
this.url = url.toString();
}

Expand Down
11 changes: 7 additions & 4 deletions packages/controller/src/presets.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { ControllerThemePreset } from "./types";
import { ControllerThemePresets } from "./types";

export const defaultPresets: { [key: string]: ControllerThemePreset } = {
export const defaultPresets: ControllerThemePresets = {
cartridge: {
id: "cartridge",
name: "Cartridge",
icon: "/whitelabel/cartridge/icon.svg",
cover: "/whitelabel/cartridge/cover-dark.png",
cover: {
light: "/whitelabel/cartridge/cover-light.png",
dark: "/whitelabel/cartridge/cover-dark.png",
},
},
"force-prime": {
id: "force-prime",
Expand Down Expand Up @@ -34,4 +37,4 @@ export const defaultPresets: { [key: string]: ControllerThemePreset } = {
primary: "#11ED83",
},
},
} as const;
};
8 changes: 6 additions & 2 deletions packages/controller/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,21 +153,25 @@ export type ControllerOptions = {
starterPackId?: string;
chainId?: constants.StarknetChainId;
theme?: string;
colorMode?: ColorMode;
config?: {
presets?: ControllerThemePresets;
};
};

export type ColorMode = "light" | "dark";

export type ControllerTheme = {
id: string;
name: string;
icon: string;
cover: string;
cover: ThemeValue<string>;
colorMode: ColorMode;
};

export type ControllerThemePresets = Record<string, ControllerThemePreset>;

export type ControllerThemePreset = ControllerTheme & {
export type ControllerThemePreset = Omit<ControllerTheme, "colorMode"> & {
colors?: ControllerColors;
};

Expand Down
14 changes: 10 additions & 4 deletions packages/keychain/src/components/Container/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
VStack,
Image,
Center,
useColorMode,
} from "@chakra-ui/react";
import { constants } from "starknet";
import { ArrowLeftIcon, CartridgeColorIcon } from "@cartridge/ui";
Expand All @@ -34,20 +35,25 @@ export function Header({
const [controller] = useController();
const address = useMemo(() => controller?.address, [controller]);
const theme = useControllerTheme();
const { colorMode } = useColorMode();

const cover = useMemo(() =>
typeof theme.cover === "string" ? theme.cover : theme.cover[colorMode]
, [theme, colorMode])

if (!address || hideAccount) {
return (
<Container h={BANNER_HEIGHT} position="relative">
<VStack
h="full"
w="full"
bg={`linear-gradient(to top, black, transparent), url('${theme.cover}')`}
bg={`url('${cover}')`}
bgSize="cover"
bgPos="center"
position="relative"
>
<Center position="absolute" bottom={-ICON_OFFSET / 4} left={0} right={0}>
<Flex bg="solid.primary" borderRadius="lg" h={`${ICON_SIZE}px`} w={`${ICON_SIZE}px`} justify="center" alignItems="center">
<Flex bg="darkGray.800" borderRadius="lg" h={`${ICON_SIZE}px`} w={`${ICON_SIZE}px`} justify="center" alignItems="center" borderWidth={4} borderColor={colorMode === "dark" ? "solid.bg" : "blueGray.100"}>
<Image src={theme.icon} boxSize={ICON_IMAGE_SIZE / 4} alt="Controller Icon" />
</Flex>
</Center>
Expand Down Expand Up @@ -115,6 +121,6 @@ function Container({
}

export const BANNER_HEIGHT = 150
export const ICON_IMAGE_SIZE = 56
export const ICON_SIZE = 72
export const ICON_IMAGE_SIZE = 64
export const ICON_SIZE = 80
export const ICON_OFFSET = 16
7 changes: 5 additions & 2 deletions packages/keychain/src/components/Provider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import { ChakraProvider } from "@chakra-ui/react";
import { ChakraProvider, ColorMode } from "@chakra-ui/react";
import { PropsWithChildren, useMemo } from "react";
import { QueryClient, QueryClientProvider } from "react-query";
import { ControllerThemeProvider, useChakraTheme, useControllerThemePreset } from "hooks/theme";
import { useRouter } from "next/router";

export function Provider({ children }: PropsWithChildren) {
const preset = useControllerThemePreset();
const chakraTheme = useChakraTheme(preset);
const router = useRouter();

const controllerTheme = useMemo(
() => ({
id: preset.id,
name: preset.name,
icon: preset.icon,
cover: preset.cover,
colorMode: (router.query.colorMode as ColorMode) ?? "dark"
}),
[preset],
[preset, router.query],
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@ import {
defaultPresets,
} from "@cartridge/controller";
import { CartridgeTheme } from "@cartridge/ui";
import { useColorMode } from "@chakra-ui/react";
import { useRouter } from "next/router";
import { useContext, createContext, useMemo } from "react";
import { useContext, createContext, useMemo, PropsWithChildren, useEffect } from "react";

const ControllerThemeContext = createContext<ControllerThemeContext>(undefined);

type ControllerThemeContext = ControllerTheme;

export const ControllerThemeProvider = ControllerThemeContext.Provider;
export function ControllerThemeProvider({ value, children }: { value: ControllerThemeContext } & PropsWithChildren) {
const { setColorMode } = useColorMode();

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

return <ControllerThemeContext.Provider value={value}>{children}</ControllerThemeContext.Provider>;
}

export function useControllerTheme() {
const ctx = useContext<ControllerThemeContext>(ControllerThemeContext);
Expand Down Expand Up @@ -44,7 +53,8 @@ export function useControllerThemePreset() {

export function useChakraTheme(preset: ControllerThemePreset) {
return useMemo(
() => ({
() =>
({
...CartridgeTheme,
semanticTokens: {
...CartridgeTheme.semanticTokens,
Expand Down
15 changes: 15 additions & 0 deletions packages/keychain/src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ColorModeScript } from '@chakra-ui/react'
import { Html, Head, Main, NextScript } from 'next/document'

export default function Document() {
return (
<Html lang="en">
<Head />
<body>
<ColorModeScript />
<Main />
<NextScript />
</body>
</Html>
)
}
Loading