Skip to content

use types for breakpoints #21

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

Merged
merged 4 commits into from
Feb 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 30 additions & 20 deletions src/components/header.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
// @flow
import { styled } from 'styletron-react';
import { hidden, clearFix } from '../styles/mixins';
import { from, until } from '../styles/functions';
import {
until,
mobileLandscape,
mobileMedium,
tablet,
desktop,
leftCol,
wide,
between,
} from '../styles/functions';
import TheGuardianLogoSVG from '../../static/inline-svgs/the-guardian-logo.svg';

// .new-header.pillar-scheme--News
const Head = styled('header', {
'margin-bottom': 0,
'background-color': '#e9eff1',
position: 'relative',
[from('tablet')]: {
[tablet]: {
display: 'block',
},
});

// .new-header__inner.gs-container
const Nav = styled('nav', {
...clearFix,
[from('tablet')]: {
[between.mobileMedium.and.desktop]: {},
[tablet]: {
'max-width': '740px',
},
[from('desktop')]: {
[desktop]: {
'max-width': '980px',
},
[from('leftCol')]: {
[leftCol]: {
'max-width': '1140px',
},
[from('wide')]: {
[wide]: {
'max-width': '1300px',
},
position: 'relative',
Expand All @@ -39,13 +49,13 @@ const HomeLink = styled('a', {
'margin-bottom': '15px',
'margin-right': '45px',
'margin-top': '5px',
[from('mobileMedium')]: {
[mobileMedium]: {
'margin-right': '5px',
},
[from('mobileLandscape')]: {
[mobileLandscape]: {
'margin-right': '17px',
},
[from('desktop')]: {
[desktop]: {
'margin-bottom': '-34px',
'margin-top': '5px',
position: 'relative',
Expand Down Expand Up @@ -93,15 +103,15 @@ const topBarItem = props => {
},
':hover': focusHoverStyles,
':focus': focusHoverStyles,
[from('tablet')]: {
[tablet]: {
'font-size': '14px',
},
};

if (props.isPayingMember || props.isRecentContributor) {
styles[':nth-child(2)'] = Object.assign({}, styles[':nth-child(2)'], {
'padding-left': 0,
'margin-left': '20px',
paddingLeft: 0,
marginLeft: '20px',
});
}

Expand All @@ -122,7 +132,7 @@ const BecomeAMemberLink = styled('a', props => {
'font-weight': 800,
padding: 0,
margin: 0,
[until('mobileLandscape')]: {
[until.mobileLandscape]: {
'margin-left': '-10px',
},
':focus': focusHoverStyles,
Expand Down Expand Up @@ -177,22 +187,22 @@ const PillarList = styled('ul', props => {
padding: '0 10px',
'list-style': 'none',
'list-style-image': 'none',
[from('mobileLandscape')]: {
[mobileLandscape]: {
'padding-left': '20px',
},
};

if (props.isHeaderOpen) {
styles = Object.assign({}, styles, {
[from('desktop')]: {
[desktop]: {
'z-index': 1070,
},
});
}

if (props.isHeaderSlim) {
styles = Object.assign({}, styles, {
[from('tablet')]: {
[tablet]: {
display: 'none',
},
});
Expand All @@ -205,7 +215,7 @@ const PillarList = styled('ul', props => {
const PillarListItem = styled('li', {
display: 'block',
float: 'left',
[from('desktop')]: {
[desktop]: {
width: '118px',
},
});
Expand Down Expand Up @@ -240,7 +250,7 @@ const PillarListItemLink = styled('a', props => {
padding: '0 4px',
position: 'relative',
overflow: 'hidden',
[from('tablet')]: {
[tablet]: {
'font-size': '22px',
height: '42px',
'padding-right': '20px',
Expand Down Expand Up @@ -334,8 +344,8 @@ export default () => {
</TopBar>
<PillarList>
{pillars.map((pillar, i) => (
<PillarListItem key={i}>
<PillarListItemLink index={i} key={i}>
<PillarListItem key={pillar.label}>
<PillarListItemLink index={i} key={pillar.label}>
{pillar.label}
</PillarListItemLink>
</PillarListItem>
Expand Down
135 changes: 110 additions & 25 deletions src/styles/functions.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,114 @@
// @flow
const breakpoints = {
mobile: 320,
mobileMedium: 360,
mobileLandscape: 480,
phablet: 660,
tablet: 740,
desktop: 980,
leftCol: 1140,
wide: 1300,
/* eslint-disable no-underscore-dangle */

// underscored names to avoid clashing with the `from` shorthand exports
const _mobile = 320;
const _mobileMedium = 360;
const _mobileLandscape = 480;
const _phablet = 660;
const _tablet = 740;
const _desktop = 980;
const _leftCol = 1140;
const _wide = 1300;

const minWidth = (from: number): string => `@media (min-width: ${`${from}px`})`;

const maxWidth = (until: number): string =>
`@media (max-width: ${`${until - 1}px`})`;

const minWidthMaxWidth = (from: number, until: number): string =>
`@media (min-width: ${`${from}px`}) and (max-width: ${`${until - 1}px`})`;

// e.g. from.*
export const from = {
mobile: minWidth(_mobile),
mobileMedium: minWidth(_mobileMedium),
mobileLandscape: minWidth(_mobileLandscape),
phablet: minWidth(_phablet),
tablet: minWidth(_tablet),
desktop: minWidth(_desktop),
leftCol: minWidth(_leftCol),
wide: minWidth(_wide),
};

// shorthands for from.*
export const {
mobile,
mobileMedium,
mobileLandscape,
phablet,
tablet,
desktop,
leftCol,
wide,
} = from;

// e.g. until.*
export const until = {
mobile: maxWidth(_mobile),
mobileMedium: maxWidth(_mobileMedium),
mobileLandscape: maxWidth(_mobileLandscape),
phablet: maxWidth(_phablet),
tablet: maxWidth(_tablet),
desktop: maxWidth(_desktop),
leftCol: maxWidth(_leftCol),
wide: maxWidth(_wide),
};

const getSizeFromBreakpoint = breakpoint => {
if (breakpoints[breakpoint]) {
return breakpoints[breakpoint];
}
return '0';
// e.g. between.*.and.*
export const between = {
mobile: {
and: {
mobileMedium: minWidthMaxWidth(_mobile, _mobileMedium),
mobileLandscape: minWidthMaxWidth(_mobile, _mobileLandscape),
phablet: minWidthMaxWidth(_mobile, _phablet),
tablet: minWidthMaxWidth(_mobile, _tablet),
desktop: minWidthMaxWidth(_mobile, _desktop),
leftCol: minWidthMaxWidth(_mobile, _leftCol),
wide: minWidthMaxWidth(_mobile, _wide),
},
},
mobileMedium: {
and: {
mobileLandscape: minWidthMaxWidth(_mobileMedium, _mobileLandscape),
phablet: minWidthMaxWidth(_mobileMedium, _phablet),
tablet: minWidthMaxWidth(_mobileMedium, _tablet),
desktop: minWidthMaxWidth(_mobileMedium, _desktop),
leftCol: minWidthMaxWidth(_mobileMedium, _leftCol),
wide: minWidthMaxWidth(_mobileMedium, _wide),
},
},
mobileLandscape: {
and: {
phablet: minWidthMaxWidth(_mobileLandscape, _phablet),
tablet: minWidthMaxWidth(_mobileLandscape, _tablet),
desktop: minWidthMaxWidth(_mobileLandscape, _desktop),
leftCol: minWidthMaxWidth(_mobileLandscape, _leftCol),
wide: minWidthMaxWidth(_mobileLandscape, _wide),
},
},
phablet: {
and: {
tablet: minWidthMaxWidth(_phablet, _tablet),
desktop: minWidthMaxWidth(_phablet, _desktop),
leftCol: minWidthMaxWidth(_phablet, _leftCol),
wide: minWidthMaxWidth(_phablet, _wide),
},
},
tablet: {
and: {
desktop: minWidthMaxWidth(_tablet, _desktop),
leftCol: minWidthMaxWidth(_tablet, _leftCol),
wide: minWidthMaxWidth(_tablet, _wide),
},
},
desktop: {
and: {
leftCol: minWidthMaxWidth(_desktop, _leftCol),
wide: minWidthMaxWidth(_desktop, _wide),
},
},
leftCol: {
and: { wide: minWidthMaxWidth(_leftCol, _wide) },
},
};
const getMinWidth = breakpoint =>
`(min-width: ${`${getSizeFromBreakpoint(breakpoint)}px`})`;
const getMaxWidth = breakpoint =>
`(max-width: ${`${getSizeFromBreakpoint(breakpoint) - 1}px`})`;

export const from = breakpoint => `@media ${getMinWidth(breakpoint)}`;
export const until = breakpoint => `@media ${getMaxWidth(breakpoint)}`;
export const between = (lowerBreakpoint, upperBreakpoint) =>
`@media ${getMinWidth(lowerBreakpoint)} and ${getMaxWidth(
upperBreakpoint,
)}`;