question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add useBreakpointValue hook

See original GitHub issue
  • I have searched the issues of this repository and believe that this is not a duplicate.

Summary 💡

Provide a JavaScript API as a counterpart to the CSS sx breakpoint’s API.

Examples 🌈

function MyApp() {
  const elevation = useBreakpointValue({ xs: 1, md: 2 });
  return <AppBar elevation={elevation} sx={{ mb: { xs: 1, md: 2 } }}>...</AppBar>;
}

Internally, useBreakpointValue would use useMediaQuery or useBreakpoint (probably better).

Motivation 🔦

We have started to surface the need for it in https://github.com/mui-org/material-ui/issues/15561#issuecomment-674454140. The hook can be used anytime a value needs to be determined based on the breakpoints. This comment https://github.com/mui-org/material-ui/issues/17000#issuecomment-740159615 triggered the idea around opening this issue. I think that this hook is best for props that are already dependent on JavaScript logic.

One could argue that many of the CSS properties could be made responsive, e.g. the color prop. However, it might be overkill, we could use this hook as an escape hatch.

We have been discussing the idea to remove the <HiddenCss> helper as the Box covers most of the usage thanks to the sx’s display feature. I think that this JS counterpart can solve the <HiddenJs> part of the migration issue: https://github.com/mui-org/material-ui/issues/19704#issuecomment-612034247.

Benchmarks

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:19
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dan-cookecommented, Dec 26, 2021

What are the use cases for this hook? I don’t consider the initial primer a valid use case. Why would you want to increase elevation depending on the breakpoint? That’s not what you should use elevation for.

What about changing Tabs , AppBar orientation from horizontal to vertical depending on the breakpoint.

This is something Chakra supports out of the box.

The general point here is that currently in MUI it is difficult to make some values responsive, https://github.com/mui-org/material-ui/issues/6140 was a step in the right direction, but it only applies to the Grid component.

Anyway heres a quick one I created for my own use - has not been tested in production to any great extent.


export const useBreakpointValue = <TValue>(
  values: {
    [key in Breakpoint]?: TValue;
  },
) => {
  const matches = {
    xs: useMediaQuery(theme.breakpoints.up(`xs`)),
    sm: useMediaQuery(theme.breakpoints.up(`sm`)),
    md: useMediaQuery(theme.breakpoints.up(`md`)),
    lg: useMediaQuery(theme.breakpoints.up(`lg`)),
    xl: useMediaQuery(theme.breakpoints.up(`xl`)),
  };

  const validBreakpoints = Object.entries(matches)
    .filter(
      ([breakpoint, isMatch]) =>
        Object.keys(values).includes(breakpoint) && isMatch,
    )
    .map(([key]) => key);

  const largestBreakpoint = validBreakpoints.pop();

  if (!largestBreakpoint) {
    return values[0];
  }

  return values[largestBreakpoint];
};

0reactions
oleecommented, Sep 20, 2022

Btw, it would also be great to have a basic hook which does not return a value from an object map, but just the breakpoint value as string and/or index:

export function useBreakpoint(): string; // xs | sm | md | ....
export function useBreakpointIndex(): number; // 0 | 1 | ....
Read more comments on GitHub >

github_iconTop Results From Across the Web

useBreakpointValue - Chakra UI
useBreakpointValue is a custom hook which returns the value for the current breakpoint from the provided responsive values object. This hook also responds ......
Read more >
Add useBreakpointValue hook · Issue #23885 · mui/material-ui
I think that this hook is best for props that are already dependent on JavaScript logic. One could argue that many of the...
Read more >
useBreakpointValue hook - Reverb
useBreakpointValue is a custom hook which returns the value for the current breakpoint from the provided responsive values object. This hook also responds ......
Read more >
React Hook 'useBreakpointValue' cannot be called inside a ...
React Hook "useBreakpointValue" cannot be called inside a callback. React Hooks must be called in a React function component or a custom React ......
Read more >
useBreakpointValue — Bumbag
The useBreakpointValue hook is similar to the useBreakpoint hook, however it accepts a range of ... A set of breakpoints (as keys) and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found