From 0148cd593d7b4f80d936170f0a036dae75136f60 Mon Sep 17 00:00:00 2001 From: yahkob Date: Wed, 29 May 2024 15:44:43 -0700 Subject: [PATCH] add defaultTheme prop to specify default theme if no previous value in storageContext --- packages/graphiql-react/src/theme.ts | 5 ++++- packages/graphiql/src/components/GraphiQL.tsx | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/graphiql-react/src/theme.ts b/packages/graphiql-react/src/theme.ts index 5dc3b8ea669..e8eed0e3209 100644 --- a/packages/graphiql-react/src/theme.ts +++ b/packages/graphiql-react/src/theme.ts @@ -7,7 +7,7 @@ import { useStorageContext } from './storage'; */ export type Theme = 'light' | 'dark' | null; -export function useTheme() { +export function useTheme(defaultTheme?: Theme) { const storageContext = useStorageContext(); const [theme, setThemeInternal] = useState(() => { @@ -26,6 +26,9 @@ export function useTheme() { // Remove the invalid stored value storageContext.set(STORAGE_KEY, ''); } + if (defaultTheme) { + return defaultTheme + } return null; } }); diff --git a/packages/graphiql/src/components/GraphiQL.tsx b/packages/graphiql/src/components/GraphiQL.tsx index fb2a44522ff..f4d54b2c74d 100644 --- a/packages/graphiql/src/components/GraphiQL.tsx +++ b/packages/graphiql/src/components/GraphiQL.tsx @@ -38,6 +38,7 @@ import { Spinner, Tab, Tabs, + Theme, ToolbarButton, Tooltip, UnStyledButton, @@ -215,6 +216,7 @@ export type GraphiQLInterfaceProps = WriteableEditorProps & * settings modal. */ showPersistHeadersSettings?: boolean; + defaultTheme?: Theme disableTabs?: boolean; }; @@ -230,7 +232,7 @@ export function GraphiQLInterface(props: GraphiQLInterfaceProps) { const merge = useMergeQuery(); const prettify = usePrettifyEditors(); - const { theme, setTheme } = useTheme(); + const { theme, setTheme } = useTheme(props.defaultTheme); const PluginContent = pluginContext?.visiblePlugin?.content;