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

GraphiQL component: forcedTheme #3407

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/famous-shirts-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'graphiql': minor
---

Add a new prop to GraphiQL component: `forcedTheme` to force the theme and hide the theme switcher.
17 changes: 17 additions & 0 deletions packages/graphiql/cypress/e2e/theme.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
describe('Theme', () => {
it('Switches to light theme when forcedTheme is light', () => {
cy.visit('/?query={test}&forcedTheme=light');
cy.get('body').should('have.class', 'graphiql-light');
});

it('Switches to dark theme when forcedTheme is dark', () => {
cy.visit('/?query={test}&forcedTheme=dark');
cy.get('body').should('have.class', 'graphiql-dark');
});

it('Defaults to light theme when forcedTheme value is invalid', () => {
cy.visit('/?query={test}&forcedTheme=invalid');
cy.get('body').should('have.class', 'graphiql-light');
cy.get('.graphiql-dialog-section-title').should('have.text', 'Theme'); // Check for the presence of the theme dialog
});
});
1 change: 1 addition & 0 deletions packages/graphiql/resources/renderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,6 @@ root.render(
shouldPersistHeaders: true,
inputValueDeprecation: GraphQLVersion.includes('15.5') ? undefined : true,
onTabChange,
forcedTheme: parameters.forcedTheme,
}),
);
86 changes: 55 additions & 31 deletions packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import React, {
ReactElement,
useCallback,
useState,
useEffect,
} from 'react';

import {
Expand Down Expand Up @@ -162,6 +163,7 @@ export function GraphiQL({
>
<GraphiQLInterface
showPersistHeadersSettings={shouldPersistHeaders !== false}
forcedTheme={props.forcedTheme}
{...props}
/>
</GraphiQLProvider>
Expand Down Expand Up @@ -209,6 +211,12 @@ export type GraphiQLInterfaceProps = WriteableEditorProps &
* settings modal.
*/
showPersistHeadersSettings?: boolean;
/**
* forcedTheme allows to enforce a specific theme for GraphiQL.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* forcedTheme allows to enforce a specific theme for GraphiQL.
* forcedTheme allows enforcement of a specific theme for GraphiQL.

* This is useful when you want to make sure that GraphiQL is always
* rendered with a specific theme
*/
forcedTheme?: 'light' | 'dark' | 'system';
};

export function GraphiQLInterface(props: GraphiQLInterfaceProps) {
Expand All @@ -225,6 +233,20 @@ export function GraphiQLInterface(props: GraphiQLInterfaceProps) {

const { theme, setTheme } = useTheme();

useEffect(() => {
switch (props.forcedTheme) {
case 'light':
setTheme('light');
break;
case 'dark':
setTheme('dark');
break;
case 'system':
setTheme(null);
break;
}
}, [props.forcedTheme, setTheme]);

const PluginContent = pluginContext?.visiblePlugin?.content;

const pluginResize = useDragResize({
Expand Down Expand Up @@ -760,39 +782,41 @@ export function GraphiQLInterface(props: GraphiQLInterfaceProps) {
</ButtonGroup>
</div>
) : null}
<div className="graphiql-dialog-section">
<div>
<div className="graphiql-dialog-section-title">Theme</div>
<div className="graphiql-dialog-section-caption">
Adjust how the interface looks like.
{!props.forcedTheme && (
<div className="graphiql-dialog-section">
<div>
<div className="graphiql-dialog-section-title">Theme</div>
<div className="graphiql-dialog-section-caption">
Adjust how the interface looks like.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Adjust how the interface looks like.
Adjust how the interface appears.

</div>
</div>
<ButtonGroup>
<Button
type="button"
className={theme === null ? 'active' : ''}
onClick={handleChangeTheme}
>
System
</Button>
<Button
type="button"
className={theme === 'light' ? 'active' : ''}
data-theme="light"
onClick={handleChangeTheme}
>
Light
</Button>
<Button
type="button"
className={theme === 'dark' ? 'active' : ''}
data-theme="dark"
onClick={handleChangeTheme}
>
Dark
</Button>
</ButtonGroup>
</div>
<ButtonGroup>
<Button
type="button"
className={theme === null ? 'active' : ''}
onClick={handleChangeTheme}
>
System
</Button>
<Button
type="button"
className={theme === 'light' ? 'active' : ''}
data-theme="light"
onClick={handleChangeTheme}
>
Light
</Button>
<Button
type="button"
className={theme === 'dark' ? 'active' : ''}
data-theme="dark"
onClick={handleChangeTheme}
>
Dark
</Button>
</ButtonGroup>
</div>
)}
{storageContext ? (
<div className="graphiql-dialog-section">
<div>
Expand Down