Last active
February 26, 2020 17:34
-
-
Save Kikobeats/0dbda3ec8154fa942dc47d36504681c6 to your computer and use it in GitHub Desktop.
Hide + rebass
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import styled from 'styled-components' | |
import { theme } from 'rebass' | |
const { breakpoints } = theme | |
const lastIndex = breakpoints.length - 1 | |
const getMediaBreakpoint = (breakpoints, breakpoint, index) => { | |
if (index === 0) return `@media screen and (max-width: ${breakpoint})` | |
const prevBreakpoint = breakpoints[index - 1] | |
if (index === lastIndex) { | |
return `@media screen and (min-width: ${prevBreakpoint})` | |
} | |
return `@media screen and (min-width: ${prevBreakpoint}) and (max-width: ${breakpoint})` | |
} | |
const mediaBreakpoints = breakpoints.reduce((acc, breakpoint, index) => { | |
acc[index] = getMediaBreakpoint(breakpoints, breakpoint, index) | |
return acc | |
}, {}) | |
const hidden = key => props => { | |
const breakpoints = [].concat(props.breakpoints) | |
return breakpoints.includes(key) | |
? { | |
[mediaBreakpoints[key]]: { | |
display: 'none' | |
} | |
} | |
: null | |
} | |
const Hide = styled.div( | |
[], | |
...Object.keys(mediaBreakpoints).map(i => hidden(Number(i))) | |
) | |
export default Hide |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is awesome, exactly what I was trying to achieve.