-
Notifications
You must be signed in to change notification settings - Fork 53
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
Discussion for v3 API for more flexibility and readability #11
Comments
@oyeanuj Thanks! I had something similar in mind for a while now. There's a couple of ways to do this. One possible implementation is to have 2 top level keys on the breakpoints object: const breakpoints = {
width: {
large: '1170px',
medium: '768px',
small: '450px',
},
height: {
small: '700px',
}
} Then probably we can change the API like this: const Box = styled.div`
background: black;
${media.width.lessThan('medium')`
background: red;
`}
` Although it's a rough idea. I'm afraid I don't have time currently (maybe after this week?) I'll probably want to talk to SC teammates to refine the API to be more idiomatic to styled components for version 3. |
@morajabi Thanks for the quick response. Your proposal looks good! I was thinking along the same lines for the API with additional support for: media.orientation.is('landscape')
media.orientation.not('portrait')
media.screen A bonus API feature could be if they could be chained like below: media.width.lessThan('medium').orientation('landscape') Thoughts? |
@oyeanuj That would add a lot of additional API complexity which I don't want this to be harder than the default CSS media query syntax. BTW for your problem a quick fix might be to export your breakpoint variables and just use them with interpolation in SC styles: import styled from 'styled-components'
import { breakpoints } from '../utils/media'
const Wrapper = styled.div`
@media landscape and (min-width: ${breakpoints.medium}) {
/* whatever */
}
` |
Chainable API is pretty elegant and imho does not increase the complexity. If you dont need to specify the orientation, it changes nothing and if you do, you only have to chain another constraint, you dont have to modify your current constraint to accommodate it. |
What if we stick to the syntax that we have but let the library write the most confusing part which is
and this one for height:
Now we are not reinventing the wheel. Thus we now have a very flexible API and syntax highlighting which I believe we don't have now because we're not using Basically, the The only problem is it's a bit lengthy, and I don't like lengthy media queries, I often wrap them in a function to use them like so:
Thoughts about the API and if you have suggestions to make it less lengthy? @oyeanuj @ApacheEx @tlvenn |
here is all media features: https://www.w3.org/TR/css3-mediaqueries/#media1 should we support all media features or
💯 agree p.s. I will try to make some suggestions a bit later 🤔 |
That's exactly why I'm saying going this route might not be the best way to do it, replicating everything from the original API doesn't seem good. @ApacheEx What do you think about that API I suggested? |
ok,
const Button = styled.button`
width: 120px;
@media (orientation: landscape) and ${media.width.between('small', 'big')} {
width: 300px;
}
`;
IdeaAdd I love current API, let's make something similar ❤️ const defaultFeatures = {
landscape: {
orientation: 'landscape',
},
portrait: {
orientation: 'portrait',
},
} If I want to add my own features: // ./src/styles/style-utils.js
// My own breakpoints.
const breakpoints = {
xs: '250px',
sm: '450px',
};
// My own features.
const features = {
iphoneX: {
orientation: 'landscape',
'device-aspect-ratio': '21/9',
...etc
},
..etc
};
export const media = generateMedia(breakpoints, features); Finally: // ./src/components/Button.js
import { media } from 'styles/style-utils';
const Button = styled.button`
width: 120px;
${media.width.between('xs', 'sm')('iphoneX')`
width: 300px;
`};
${media.width.greaterThan('sm')('landscape')`
width: 400px;
`};
${media.greaterThan('sm')`
width: 400px;
`};
`; will generate @media all and (min-width: 250px) and (max-width: 450px) and (orientation: landscape) and (device-aspect-ratio: 21/9) { … }
@media all and (min-width: 450px) and (orientation: landscape) { … }
@media all and (min-width: 450px) { … } Thoughts? |
I really love the API design of features. 💯 @ApacheEx I'd like to add one more thing named const aliases = media => {
mobile: media.width.lessThan('sm'),
tablet: media.width.between('sm', 'lg'),
}
export const media(breakpoints, features, aliases) Then you can use the old API as well as the new features API AND the shorthands/aliases you declared:
Sometimes I only want a really custom media query and I only use it once, it doesn't make sense to make a feature for everything, to solve this, we can get the media string by calling it without argument:
All of these are quite optional for user to provide. I'm was a fan of V1 API because it was so minimal like so: |
wow, looks awesome 💪 🔥 so, as conclusion:
agree? am I miss something? |
@ApacheEx Looks good, and the readability is helped with the aliases and chaining. Looking forward to this! |
Is version 3 under development? I hope so, because the API for version 3 looks excellent and it would be a shame if it doesn't get implemented. |
Hi @ALL, as this issue is open long enough and, as seen with #17, some things gotta change, I just proposed a backwards compatible way of going at the really excellent looking API for V3: Have a look at #20 and tell me what you think, should you be so kind. Best, Tim. |
Hi @morajabi, thank you for putting out this library. It has been super helpful so far. In my current project, I keep needing to write height based media queries, and in doing so, I have to opt traditional media query rather than use this library.
So, I was wondering if you are considering adding support for other aspects of media query in the library API?
The text was updated successfully, but these errors were encountered: