[ui] add media query template literals, convert everything to the superior.

This commit is contained in:
41666 2019-04-03 13:59:13 -05:00
parent c40c04e55e
commit f2bb6a7b18
No known key found for this signature in database
GPG key ID: BC51D07640DC10AF
10 changed files with 90 additions and 45 deletions

5
.gitignore vendored
View file

@ -1,16 +1,11 @@
.env .env
*.env *.env
dist dist
/docker-compose.test.yml /docker-compose.test.yml
node_modules node_modules
.vscode .vscode
.data .data
yarn-error\.log yarn-error\.log
*.log *.log
\.next/ \.next/
coverage/ coverage/

View file

@ -15,8 +15,7 @@
"lint:css": "stylelint 'packages/roleypoly-ui/**/*.js'", "lint:css": "stylelint 'packages/roleypoly-ui/**/*.js'",
"lint:js": "standard", "lint:js": "standard",
"rpcrepl": "babel-node packages/roleypoly-server/util/rpcrepl.js", "rpcrepl": "babel-node packages/roleypoly-server/util/rpcrepl.js",
"dev:up": "docker-compose up -d", "dev:up": "docker-compose up -d"
"precommit": "yarn lint && yarn test"
}, },
"private": true, "private": true,
"workspaces": [ "workspaces": [
@ -24,8 +23,7 @@
], ],
"husky": { "husky": {
"hooks": { "hooks": {
"pre-commit": "yarn lint", "pre-commit": "yarn lint"
"pre-push": "yarn lint && yarn test"
} }
}, },
"devDependencies": { "devDependencies": {

View file

@ -1,8 +1,9 @@
// @flow
import * as React from 'react' import * as React from 'react'
import moment from 'moment' import moment from 'moment'
import Typist from 'react-typist' import Typist from 'react-typist'
import styled from 'styled-components' import styled from 'styled-components'
import MediaQuery from '../../kit/media' import { sm } from '../../kit/media'
import demoRoles from '../../config/demo' import demoRoles from '../../config/demo'
const Outer = styled.div` const Outer = styled.div`
@ -15,7 +16,7 @@ const Outer = styled.div`
const Chat = styled.div` const Chat = styled.div`
padding: 10px 0; padding: 10px 0;
font-size: 0.8em; font-size: 0.8em;
${() => MediaQuery({ sm: 'font-size: 1em;' })} ${sm`font-size: 1em;`}
& span { & span {
display: inline-block; display: inline-block;
@ -28,7 +29,7 @@ const TextArea = styled.div`
border-radius: 5px; border-radius: 5px;
padding: 10px; padding: 10px;
font-size: 0.8em; font-size: 0.8em;
${() => MediaQuery({ sm: 'font-size: 1em;' })} ${sm`font-size: 1em;`}
& .Typist .Cursor { & .Typist .Cursor {
display: inline-block; display: inline-block;

View file

@ -1,5 +1,5 @@
import styled from 'styled-components' import styled from 'styled-components'
import MediaQuery from '../../kit/media' import { md } from '../../kit/media'
export default styled.div` export default styled.div`
border: solid 1px var(--role-color-outline); border: solid 1px var(--role-color-outline);
@ -92,20 +92,22 @@ export default styled.div`
transform: none; transform: none;
} }
${(props: any) => MediaQuery({ ${md`
md: `
font-size: 1em; font-size: 1em;
text-shadow: none; text-shadow: none;
padding-left: 32px; padding-left: 32px;
${(props.active) ? `box-shadow: none;` : ''}
&::after { &::after {
${(props.active) ? `background-color: var(--role-color-base);` : ''}
display: block; display: block;
} }
`}
${(props: any) => props.active
? md`
box-shadow: none;
&:hover::after { &:hover::after {
${(props.active) ? `background-color: var(--role-color-active);` : ''} background-color: var(--role-color-active);
} }
` &::after {
})} background-color: var(--role-color-base);
}`
: ''}
` `

View file

@ -1,5 +1,5 @@
import styled from 'styled-components' import styled from 'styled-components'
import MediaQuery from '../kit/media' import { md } from '../kit/media'
export default styled.div` export default styled.div`
position: absolute; position: absolute;
@ -15,7 +15,7 @@ export default styled.div`
overflow: auto; overflow: auto;
pointer-events: none; pointer-events: none;
white-space: normal; white-space: normal;
${() => MediaQuery({ md: ` ${md`
white-space: nowrap; ` white-space: nowrap;
})} `}
` `

View file

@ -17,3 +17,21 @@ font-size: 2em;
font-size: 2.5em; font-size: 2.5em;
};" };"
`; `;
exports[`mediaTemplateLiteral renders how we expect 1`] = `
"@media screen and (min-width: 0px) {
font-size: 0.5em;
};
@media screen and (min-width: 544px) {
font-size: 1em;
};
@media screen and (min-width: 768px) {
font-size: 1.5em;
};
@media screen and (min-width: 1012px) {
font-size: 2em;
};
@media screen and (min-width: 1280px) {
font-size: 2.5em;
};"
`;

View file

@ -1,5 +1,5 @@
/* eslint-env jest */ /* eslint-env jest */
import MediaQuery from '../media' import MediaQuery, { xs, sm, md, lg, xl } from '../media'
describe('MediaQuery', () => { describe('MediaQuery', () => {
it('outputs media queries', () => { it('outputs media queries', () => {
@ -14,3 +14,26 @@ describe('MediaQuery', () => {
expect(mq).toMatchSnapshot() expect(mq).toMatchSnapshot()
}) })
}) })
describe('mediaTemplateLiteral', () => {
it('renders how we expect', () => {
// this is a weird fixture because of how we render MediaQuery for testing
const mq = `${xs`font-size: 0.5em;`}
${sm`font-size: 1em;`}
${md`font-size: 1.5em;`}
${lg`font-size: 2em;`}
${xl`font-size: 2.5em;`}`
expect(mq).toMatchSnapshot()
const mq2 = MediaQuery({//
xs: 'font-size: 0.5em;',
sm: 'font-size: 1em;',
md: 'font-size: 1.5em;',
lg: 'font-size: 2em;',
xl: 'font-size: 2.5em;'
})
expect(mq).toEqual(mq2)
})
})

View file

@ -17,6 +17,15 @@ export const breakpoints = {
xl: 1280 xl: 1280
} }
const mediaTemplateLiteral = (size: number, ...stuff: any): string =>
`@media screen and (min-width: ${size}px) {\n${stuff.join()}\n};`
export const xs = mediaTemplateLiteral.bind(null, breakpoints.xs)
export const sm = mediaTemplateLiteral.bind(null, breakpoints.sm)
export const md = mediaTemplateLiteral.bind(null, breakpoints.md)
export const lg = mediaTemplateLiteral.bind(null, breakpoints.lg)
export const xl = mediaTemplateLiteral.bind(null, breakpoints.xl)
const MediaQuery = (mq: MediaQueryConfig) => { const MediaQuery = (mq: MediaQueryConfig) => {
const out = [] const out = []

View file

@ -1,6 +1,6 @@
import * as React from 'react' import * as React from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import MediaQuery from '../kit/media' import { md } from '../kit/media'
export const Overlay = styled.div` export const Overlay = styled.div`
opacity: 0.6; opacity: 0.6;
@ -23,9 +23,12 @@ const ResponsiveSplitter = styled.div`
line-height: 1.6; line-height: 1.6;
font-size: 1.3em; font-size: 1.3em;
flex-direction: column; flex-direction: column;
${() => MediaQuery({ ${md`
md: `flex-direction: row; min-height: 100vh; position: relative; top: -50px;` flex-direction: row;
})} min-height: 100vh;
position: relative;
top: -50px;
`}
& > div { & > div {
margin: 1rem; margin: 1rem;
@ -33,7 +36,7 @@ const ResponsiveSplitter = styled.div`
& section { & section {
text-align: center; text-align: center;
${() => MediaQuery({ md: `text-align: left;` })} ${md`text-align: left;`}
} }
` `
@ -46,9 +49,7 @@ const Code = styled.h1`
margin: 0; margin: 0;
padding: 0; padding: 0;
font-size: 4em; font-size: 4em;
${() => MediaQuery({ ${md`font-size: 2em;`}
md: `font-size: 2em;`
})}
` `
export default class CustomErrorPage extends React.Component { export default class CustomErrorPage extends React.Component {

View file

@ -6,7 +6,7 @@ import redirect from '../lib/redirect'
import TypingDemo from '../components/demos/typing' import TypingDemo from '../components/demos/typing'
import TapDemo from '../components/demos/tap' import TapDemo from '../components/demos/tap'
import styled from 'styled-components' import styled from 'styled-components'
import MediaQuery from '../kit/media' import { md } from '../kit/media'
const HeroBig = styled.h1` const HeroBig = styled.h1`
color: var(--c-7); color: var(--c-7);
@ -52,7 +52,7 @@ const FooterLink = styled.a`
const DemoArea = styled.div` const DemoArea = styled.div`
display: flex; display: flex;
flex-direction: column; flex-direction: column;
${() => MediaQuery({ md: `flex-direction: row;` })} ${md`flex-direction: row;`}
& > div { & > div {
flex: 1; flex: 1;
@ -66,15 +66,13 @@ const DemoArea = styled.div`
const Wrapper = styled.div` const Wrapper = styled.div`
flex-wrap: wrap; flex-wrap: wrap;
${() => MediaQuery({ ${md`
md: ` display: flex;
display: flex; justify-content: center;
justify-content: center; align-items: center;
align-items: center; height: 80vh;
height: 80vh; min-height: 500px;
min-height: 500px; `}
`
})}
` `
export default class Home extends React.Component { export default class Home extends React.Component {