Skip to content

Commit 9aef83a

Browse files
YahkobdimaMachina
andauthored
add defaultTheme prop to specify default theme if no previous value stored (#3619)
* add defaultTheme prop to specify default theme if no previous value in storageContext * yoyo --------- Co-authored-by: Dimitri POSTOLOV <[email protected]>
1 parent 959ed21 commit 9aef83a

File tree

5 files changed

+38
-14
lines changed

5 files changed

+38
-14
lines changed

.changeset/orange-cars-glow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphiql/react': minor
3+
'graphiql': minor
4+
---
5+
6+
add new prop `defaultTheme` to set the default color preference theme

packages/graphiql-react/src/theme.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useStorageContext } from './storage';
77
*/
88
export type Theme = 'light' | 'dark' | null;
99

10-
export function useTheme() {
10+
export function useTheme(defaultTheme: Theme = null) {
1111
const storageContext = useStorageContext();
1212

1313
const [theme, setThemeInternal] = useState<Theme>(() => {
@@ -26,7 +26,7 @@ export function useTheme() {
2626
// Remove the invalid stored value
2727
storageContext.set(STORAGE_KEY, '');
2828
}
29-
return null;
29+
return defaultTheme;
3030
}
3131
});
3232

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
describe('Theme', () => {
2-
it('Switches to light theme when `forcedTheme` is light', () => {
3-
cy.visit('/?query={test}&forcedTheme=light');
4-
cy.get('body').should('have.class', 'graphiql-light');
5-
});
2+
describe('`forcedTheme`', () => {
3+
it('Switches to light theme when `forcedTheme` is light', () => {
4+
cy.visit('/?forcedTheme=light');
5+
cy.get('body').should('have.class', 'graphiql-light');
6+
});
7+
8+
it('Switches to dark theme when `forcedTheme` is dark', () => {
9+
cy.visit('/?forcedTheme=dark');
10+
cy.get('body').should('have.class', 'graphiql-dark');
11+
});
612

7-
it('Switches to dark theme when `forcedTheme` is dark', () => {
8-
cy.visit('/?query={test}&forcedTheme=dark');
9-
cy.get('body').should('have.class', 'graphiql-dark');
13+
it('Defaults to light theme when `forcedTheme` value is invalid', () => {
14+
cy.visit('/?forcedTheme=invalid');
15+
cy.get('[data-value=settings]').click();
16+
cy.get('.graphiql-dialog-section-title')
17+
.eq(1)
18+
.should('have.text', 'Theme'); // Check for the presence of the theme dialog
19+
});
1020
});
1121

12-
it('Defaults to light theme when `forcedTheme` value is invalid', () => {
13-
cy.visit('/?query={test}&forcedTheme=invalid');
14-
cy.get('[data-value=settings]').click();
15-
cy.get('.graphiql-dialog-section-title').eq(1).should('have.text', 'Theme'); // Check for the presence of the theme dialog
22+
describe('`defaultTheme`', () => {
23+
it('should have light theme', () => {
24+
cy.visit('/?defaultTheme=light');
25+
cy.get('body').should('have.class', 'graphiql-light');
26+
});
27+
it('should have dark theme', () => {
28+
cy.visit('/?defaultTheme=dark');
29+
cy.get('body').should('have.class', 'graphiql-dark');
30+
});
1631
});
1732
});

packages/graphiql/resources/renderExample.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,5 +95,6 @@ root.render(
9595
onTabChange,
9696
forcedTheme: parameters.forcedTheme,
9797
defaultQuery: parameters.defaultQuery,
98+
defaultTheme: parameters.defaultTheme,
9899
}),
99100
);

packages/graphiql/src/components/GraphiQL.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040
Spinner,
4141
Tab,
4242
Tabs,
43+
Theme,
4344
ToolbarButton,
4445
Tooltip,
4546
UnStyledButton,
@@ -219,6 +220,7 @@ export type GraphiQLInterfaceProps = WriteableEditorProps &
219220
* settings modal.
220221
*/
221222
showPersistHeadersSettings?: boolean;
223+
defaultTheme?: Theme;
222224
disableTabs?: boolean;
223225
/**
224226
* `forcedTheme` allows enforcement of a specific theme for GraphiQL.
@@ -263,7 +265,7 @@ export function GraphiQLInterface(props: GraphiQLInterfaceProps) {
263265
const merge = useMergeQuery();
264266
const prettify = usePrettifyEditors();
265267

266-
const { theme, setTheme } = useTheme();
268+
const { theme, setTheme } = useTheme(props.defaultTheme);
267269

268270
useEffect(() => {
269271
if (forcedTheme === 'system') {

0 commit comments

Comments
 (0)