Skip to content
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
34 changes: 32 additions & 2 deletions packages/x-data-grid/src/material/variables.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as React from 'react';
import { alpha, darken, lighten, type Theme } from '@mui/material/styles';
import { useTheme } from '@mui/material/styles';
import { hash, stringify } from '@mui/x-internals/hash';
import { hash } from '@mui/x-internals/hash';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The change makes stringify() too specific to theme objects to be alongside hash(), and the function wasn't used anywhere else, so it's been moved here.

import { vars, type GridCSSVariablesInterface } from '../constants/cssVariables';

export function useMaterialCSSVariables() {
const theme = useTheme();
return React.useMemo(() => {
const id = hash(stringify(theme));
const id = hash(stringifyTheme(theme));
const variables = transformTheme(theme);
return { id, variables };
}, [theme]);
Expand Down Expand Up @@ -125,3 +125,33 @@ function formatFont(font: React.CSSProperties | undefined) {
}
return `${font.fontWeight} ${font.fontSize} / ${font.lineHeight} ${font.fontFamily}`;
}

/**
* A version of JSON.stringify for theme objects.
* Fixes: https://github.com/mui/mui-x/issues/17521
* Source: https://www.30secondsofcode.org/js/s/stringify-circular-json/
*/
function stringifyTheme(input: object | string | number | null) {
const seen = new WeakSet();
return JSON.stringify(input, (_, v) => {
// https://github.com/mui/mui-x/issues/17855
if (
(typeof window !== 'undefined' && v === window) ||
(typeof document !== 'undefined' && v === document)
) {
return v.toString();
}
if (v !== null && typeof v === 'object') {
// Do not attempt to serialize React elements due to performance concerns.
// Fixes https://github.com/mui/mui-x/issues/19414
if (React.isValidElement(v)) {
return null;
}
if (seen.has(v)) {
return null;
}
seen.add(v);
}
return v;
});
}
1 change: 0 additions & 1 deletion packages/x-internals/src/hash/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './hash';
export * from './stringify';
24 changes: 0 additions & 24 deletions packages/x-internals/src/hash/stringify.ts

This file was deleted.

Loading