[Feature Request]: Responsive values outside of `sx` prop
See original GitHub issueI 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:
- Created 4 years ago
- Reactions:4
- Comments:13 (13 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Thanks! I could certainly see the value in adding an optional hook package for using
window.matchMedia
with the theme objectUsing
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