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.

[Feature Request]: Responsive values outside of `sx` prop

See original GitHub issue

I have a component that takes a size prop that cannot be set using the sx prop, which I want to pass a responsive value based on the current breakpoint.

Currently I’ve made a makeshift solution that reads theme.breakpoints and uses window.matchMedia to match the current breakpoint. I think that theme-ui could benefit by having something similar.

import { useThemeUI } from "theme-ui";

function matchBreakpoint(breakpoint) {
  const test = window.matchMedia(`(max-width: ${breakpoint})`);

  return test.matches ? true : false;
}

function getCurrentBreakpointIndex(breakpoints) {
  let matchedIndex = breakpoints.findIndex(matchBreakpoint);

  if (matchedIndex === -1) {
    matchedIndex = breakpoints.length - 1;
  }

  return matchedIndex;
}

const useResponsiveValue = responsiveValues => {
  const { theme: { breakpoints } } = useThemeUI();
  const responsiveIndex = getCurrentBreakpointIndex(breakpoints);

  // @TODO: update on window resize event

  return responsiveValues[responsiveIndex];
};

// ...

const SomeComponent = () => {
  const size = useResponsiveValue([10, 20, 30]);

  return <OtherComponent size={size} />;
}

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:13 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
jxnblkcommented, Aug 9, 2019

Thanks! I could certainly see the value in adding an optional hook package for using window.matchMedia with the theme object

1reaction
jxnblkcommented, Sep 17, 2019

Using npm link with webpack (and Gatsby) tends to be kind of buggy, which is why we use Yarn workspaces in this repo – not sure if that’s something that might be related to the issues you’re seeing. Let me know if you’re still interested in working on this

Read more comments on GitHub >

github_iconTop Results From Across the Web

The `sx` Prop - Theme UI
The sx prop lets you add any valid CSS to an element, while using values from your theme to keep styles consistent. You...
Read more >
3 Ways To Implement Responsive Design In Your React App
I've opted to create an object that uses conditional logic to determine if any changes to width require showing/hiding the top navigation. This...
Read more >
A Guide on Material UI AutoComplete in React - Refine Dev
This article will dive deep into the Material UI Autocomplete component, highlight its accompanied features and explore a potential use case in ...
Read more >
React Admin September 2022 Update - Marmelab
React-admin v4 introduced the meta option in dataProvider queries, letting you add extra parameters to any API call. For instance:.
Read more >
Component Style - Chakra UI
Styling this component as a whole might require styling each component part. ... It has the same API as the sx prop, but...
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