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

@media queries #26

Open
junaid33 opened this issue Nov 30, 2018 · 6 comments
Open

@media queries #26

junaid33 opened this issue Nov 30, 2018 · 6 comments

Comments

@junaid33
Copy link

Hello,

Just wanted to say that ui-box is so convenient for CSS-in-JS. My question is can we do media queries using props? We are currently using other CSS-in-JS libraries and would like to just use ui-box if possible. Thank you!

@Rowno
Copy link
Contributor

Rowno commented Dec 7, 2018

I've been trying to come up with a good API that's nice to use while still being performant but I haven't thought of one yet.

But since they're just regular React props, you can easily use something like react-media as a workaround.

import React from "react"
import Media from "react-media"
import Box from "ui-box"

const mobileStyles = {
  background: 'green'
}

const desktopStyles = {
  background: 'yellow'
}

class App extends React.Component {
  render() {
    return (
      <div>
        <Media query="(max-width: 599px)">
          {isMobile => (
            <Box
              color={isMobile ? 'red' : 'blue'}
              {...(isMobile ? mobileStyles : desktopStyles)}
            >
              Test
            </Box>
          )}
        </Media>
      </div>
    )
  }
}

I'm open to suggestions though.

@jeroenransijn
Copy link
Contributor

There are two types of responsive design imo, on a property level, and component level.

  • Property level, you want a single property such as padding to change based on the viewport/parent
  • Component level, you want a component or structure to be different based on the viewport/parent

As for the Component level, I think in most cases you will need something like <MediaQuery> or even. As for property level I have some random thoughts below.

Array of values for predefined breakpoints

<MediaQueryProvider queries={['(max-width: 359px)', '(max-width: 599px)', '(max-width: 959px)']}>
   <Box padding={[8, 16, 32]} />
</MediaQueryProvider>

Array of values for predefined breakpoints

<MediaQueryProvider queries={['(max-width: 359px)', '(max-width: 599px)', '(max-width: 959px)']}>
   <Box padding={[8, 16, 32]} />
</MediaQueryProvider>

Properties could be functions taking a width

<Box color={width => width > 600 ? 'red' : 'blue} />

Return responsive props

<Box
  responsiveProps={width => {
    if (width < 360) return { padding: 8, margin: 8 }
    return { padding: 16, margin: 16 }
  }}
/>

Generate computed CSS custom properties (variables)

I am on the fence if this would actually work.

function createResponsiveValue(small, medium, large) {
  injectCSS(`
     @media (max-width: 359px) { --generated-var: ${small}; }
     @media (max-width: 599px) { --generated-var: ${medium}; }
     @media  (max-width: 959px) { --generated-var: ${large}; }
  `)
  return `var(--generated-var)`;
}

const R = createResponsiveValue;
<Box
  margin={R(8, 16, 32)}  
/>

@hennessyevan
Copy link

@jeroenransijn The array props looks the best to me. I like the way facepaint handles this. They also handle pseudo-selectors in the same way which could be a bonus.

I want to like the function props but I'm not sure how that would be converted to a media query efficiently, which I believe is the goal here no? Perhaps this is also an opportunity to switch to emotion or styled-component or something? IDK If that would make these enhancements easier.

@jeroenptrs
Copy link

@jeroenransijn I would suggest also looking at what's been described here: https://usehooks.com/useMedia, which complements Array of values for predefined breakpoints

@fs-c
Copy link

fs-c commented Oct 14, 2019

Any news on this?

@vikramrojo
Copy link

is there an interim recommendation or practice for media queries that plays well with evergreen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants