-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
TuvalSimha
wants to merge
1
commit into
graphql:main
Choose a base branch
from
TuvalSimha:tuval/theme
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -14,6 +14,7 @@ import React, { | |||||
ReactElement, | ||||||
useCallback, | ||||||
useState, | ||||||
useEffect, | ||||||
} from 'react'; | ||||||
|
||||||
import { | ||||||
|
@@ -162,6 +163,7 @@ export function GraphiQL({ | |||||
> | ||||||
<GraphiQLInterface | ||||||
showPersistHeadersSettings={shouldPersistHeaders !== false} | ||||||
forcedTheme={props.forcedTheme} | ||||||
{...props} | ||||||
/> | ||||||
</GraphiQLProvider> | ||||||
|
@@ -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) { | ||||||
|
@@ -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({ | ||||||
|
@@ -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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
</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> | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.