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

[core] Add debug values to various hooks #19515

Merged
merged 6 commits into from
Mar 22, 2020
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
12 changes: 10 additions & 2 deletions packages/material-ui-styles/src/makeStyles/makeStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export default function makeStyles(stylesOrCreator, options = {}) {
classNamePrefix,
};

return (props = {}) => {
const useStyles = (props = {}) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

Screenshot from 2020-02-02 00-44-29
vs.
Screenshot from 2020-02-02 00-44-10

We could potentially incorporate the name option into the display name of the hook but that might be confusing if the hook name changes between components. Usually you only have a single useStyles per component and even if you have more than 1 then the name will be reflected in the debug value.

const theme = useTheme() || defaultTheme;
const stylesOptions = {
...React.useContext(StylesContext),
Expand Down Expand Up @@ -236,6 +236,14 @@ export default function makeStyles(stylesOrCreator, options = {}) {
shouldUpdate.current = true;
});

return getClasses(instance.current, props.classes, Component);
const classes = getClasses(instance.current, props.classes, Component);
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
React.useDebugValue(classes);
}

return classes;
};

return useStyles;
}
9 changes: 8 additions & 1 deletion packages/material-ui-styles/src/useTheme/useTheme.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@ import React from 'react';
import ThemeContext from './ThemeContext';

export default function useTheme() {
return React.useContext(ThemeContext);
const theme = React.useContext(ThemeContext);

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
React.useDebugValue(theme);
}

return theme;
}
16 changes: 15 additions & 1 deletion packages/material-ui/scripts/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,21 @@ const commonjsOptions = {
};

function onwarn(warning) {
throw Error(warning.message);
if (
warning.code === 'UNUSED_EXTERNAL_IMPORT' &&
warning.source === 'react' &&
warning.names.filter((identifier) => identifier !== 'useDebugValue').length === 0
) {
// only warn for
// import * as React from 'react'
// if (__DEV__) React.useDebugValue()
// React.useDebug not fully dce'd from prod bundle
// in the sense that it's still imported but unused. Downgrading
// it to a warning as a reminder to fix at some point
console.warn(warning.message);
} else {
throw Error(warning.message);
}
}

export default [
Expand Down
10 changes: 9 additions & 1 deletion packages/material-ui/src/styles/useTheme.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { useTheme as useThemeWithoutDefault } from '@material-ui/styles';
import React from 'react';
import defaultTheme from './defaultTheme';

export default function useTheme() {
return useThemeWithoutDefault() || defaultTheme;
const theme = useThemeWithoutDefault() || defaultTheme;

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
React.useDebugValue(theme);
}

return theme;
}
5 changes: 5 additions & 0 deletions packages/material-ui/src/useMediaQuery/useMediaQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,10 @@ export default function useMediaQuery(queryInput, options = {}) {
};
}, [query, matchMedia, supportMatchMedia]);

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
React.useDebugValue({ query, match });
}

return match;
}
5 changes: 5 additions & 0 deletions packages/material-ui/src/utils/focusVisible.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,5 +139,10 @@ export function useIsFocusVisible() {
}
}, []);

if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line react-hooks/rules-of-hooks
React.useDebugValue(isFocusVisible);
}

return { isFocusVisible, onBlurVisible: handleBlurVisible, ref };
}