Skip to content

Commit

Permalink
GraphiQL component: showThemeSettings
Browse files Browse the repository at this point in the history
changeset

change prop to enforceTheme

clean

change prop name

cypress theme tests

delete onEditForceTheme

fix some

new changeset

fix

fix

Update packages/graphiql/cypress/e2e/theme.cy.ts

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>

Update packages/graphiql/cypress/e2e/theme.cy.ts

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>

Update packages/graphiql/cypress/e2e/theme.cy.ts

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>

Update packages/graphiql/resources/renderExample.js

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>

Update packages/graphiql/src/components/GraphiQL.tsx

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>

Update packages/graphiql/src/components/GraphiQL.tsx

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>

Update packages/graphiql/cypress/e2e/theme.cy.ts

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>

Update .changeset/famous-shirts-mate.md

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>

Update packages/graphiql/cypress/e2e/theme.cy.ts

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>

Update packages/graphiql/cypress/e2e/theme.cy.ts

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>

Update packages/graphiql/cypress/e2e/theme.cy.ts

Co-authored-by: Dimitri POSTOLOV <en3m@ya.ru>
  • Loading branch information
TuvalSimha and dimaMachina committed Aug 27, 2023
1 parent 3d4b9b7 commit 8f75b62
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 31 deletions.
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.
* 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.
</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

0 comments on commit 8f75b62

Please sign in to comment.