Skip to content

Commit

Permalink
Detect theme mode for embedded applications
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed Nov 14, 2023
1 parent 2d2187d commit bb6124d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
5 changes: 2 additions & 3 deletions dapp/src/DApp.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react"
import { ChakraProvider, Button, Box } from "@chakra-ui/react"
import { useEmbedFeatureFlag } from "./hooks"
import { useEmbedFeatureFlag, useDetectThemeMode } from "./hooks"
import { LedgerWalletAPIProvider } from "./providers"
import { getThemeConfig } from "./theme/utils/utils"
import theme from "./theme/index"
Expand Down Expand Up @@ -32,8 +32,7 @@ function DApp() {
}

function DAppProviders() {
const params = new URLSearchParams(window.location.search)
const themeMode = params.get("theme")
const themeMode = useDetectThemeMode()

return (
<ChakraProvider
Expand Down
2 changes: 1 addition & 1 deletion dapp/src/constants/local-storage.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// eslint-disable-next-line import/prefer-default-export
export const LOCAL_STORAGE_EMBED = "embed"
export const LOCAL_STORAGE_CHAKRA_COLOR_MODE = "chakra-ui-color-mode"
16 changes: 16 additions & 0 deletions dapp/src/hooks/helpers-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { useEffect } from "react"
import { LOCAL_STORAGE_CHAKRA_COLOR_MODE } from "../constants"

// eslint-disable-next-line import/prefer-default-export
export function useDetectThemeMode(): string | null {
const params = new URLSearchParams(window.location.search)
const themeMode = params.get("theme")

useEffect(() => {
// For the ledger live app, we should clear the localStorage and set the theme mode again.
// As a result, we will detect an update when a user's settings change in the live ledger.
if (themeMode) localStorage.removeItem(LOCAL_STORAGE_CHAKRA_COLOR_MODE)
}, [themeMode])

return themeMode
}
1 change: 1 addition & 0 deletions dapp/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./dom-hooks"
export * from "./feature-hooks"
export * from "./helpers-hooks"

0 comments on commit bb6124d

Please sign in to comment.