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

add defaultTheme prop to specify default theme if no previous value stored #3619

Merged
merged 3 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/orange-cars-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@graphiql/react': minor
'graphiql': minor
---

add new prop `defaultTheme` to set the default color preference theme
4 changes: 2 additions & 2 deletions 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 = null) {
const storageContext = useStorageContext();

const [theme, setThemeInternal] = useState<Theme>(() => {
Expand All @@ -26,7 +26,7 @@ export function useTheme() {
// Remove the invalid stored value
storageContext.set(STORAGE_KEY, '');
}
return null;
return defaultTheme;
}
});

Expand Down
37 changes: 26 additions & 11 deletions packages/graphiql/cypress/e2e/theme.cy.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
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');
});
describe('`forcedTheme`', () => {
it('Switches to light theme when `forcedTheme` is light', () => {
cy.visit('/?forcedTheme=light');
cy.get('body').should('have.class', 'graphiql-light');
});

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

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('/?forcedTheme=invalid');
cy.get('[data-value=settings]').click();
cy.get('.graphiql-dialog-section-title')
.eq(1)
.should('have.text', 'Theme'); // Check for the presence of the theme dialog
});
});

it('Defaults to light theme when `forcedTheme` value is invalid', () => {
cy.visit('/?query={test}&forcedTheme=invalid');
cy.get('[data-value=settings]').click();
cy.get('.graphiql-dialog-section-title').eq(1).should('have.text', 'Theme'); // Check for the presence of the theme dialog
describe('`defaultTheme`', () => {
it('should have light theme', () => {
cy.visit('/?defaultTheme=light');
cy.get('body').should('have.class', 'graphiql-light');
});
it('should have dark theme', () => {
cy.visit('/?defaultTheme=dark');
cy.get('body').should('have.class', 'graphiql-dark');
});
});
});
1 change: 1 addition & 0 deletions packages/graphiql/resources/renderExample.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,6 @@ root.render(
onTabChange,
forcedTheme: parameters.forcedTheme,
defaultQuery: parameters.defaultQuery,
defaultTheme: parameters.defaultTheme,
}),
);
4 changes: 3 additions & 1 deletion packages/graphiql/src/components/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
Spinner,
Tab,
Tabs,
Theme,
ToolbarButton,
Tooltip,
UnStyledButton,
Expand Down Expand Up @@ -219,6 +220,7 @@ export type GraphiQLInterfaceProps = WriteableEditorProps &
* settings modal.
*/
showPersistHeadersSettings?: boolean;
defaultTheme?: Theme;
disableTabs?: boolean;
/**
* `forcedTheme` allows enforcement of a specific theme for GraphiQL.
Expand Down Expand Up @@ -263,7 +265,7 @@ export function GraphiQLInterface(props: GraphiQLInterfaceProps) {
const merge = useMergeQuery();
const prettify = usePrettifyEditors();

const { theme, setTheme } = useTheme();
const { theme, setTheme } = useTheme(props.defaultTheme);

useEffect(() => {
if (forcedTheme === 'system') {
Expand Down