Skip to content

Commit

Permalink
add defaultTheme prop to specify default theme if no previous value i…
Browse files Browse the repository at this point in the history
…n storageContext
  • Loading branch information
Yahkob authored and = committed May 30, 2024
1 parent fa0e7f7 commit 0148cd5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/graphiql-react/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Theme>(() => {
Expand All @@ -26,6 +26,9 @@ export function useTheme() {
// Remove the invalid stored value
storageContext.set(STORAGE_KEY, '');
}
if (defaultTheme) {
return defaultTheme
}
return null;
}
});
Expand Down
4 changes: 3 additions & 1 deletion packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
Spinner,
Tab,
Tabs,
Theme,
ToolbarButton,
Tooltip,
UnStyledButton,
Expand Down Expand Up @@ -215,6 +216,7 @@ export type GraphiQLInterfaceProps = WriteableEditorProps &
* settings modal.
*/
showPersistHeadersSettings?: boolean;
defaultTheme?: Theme
disableTabs?: boolean;
};

Expand All @@ -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;

Expand Down

0 comments on commit 0148cd5

Please sign in to comment.