Skip to content

Instantly share code, notes, and snippets.

View johnnyBira's full-sized avatar

John Persson johnnyBira

  • Polestar
  • Göteborg
View GitHub Profile
@johnnyBira
johnnyBira / reduce.js
Created April 3, 2019 09:20
Reduce algo
function reduce(arr, fn, initial) {
let result;
function recursive(i = 0, acc = initial) {
if (i < arr.length) {
result = fn(acc, arr[i], i)
recursive(i + 1, result)
}
return result
}
recursive();
@johnnyBira
johnnyBira / Medium #1 - Grid Example 5.jsx
Last active March 30, 2019 17:48
Medium #1 - Grid Example 5
import React from 'react'
// Import ThemeProvider from Styled Components
import { ThemeProvider } from 'styled-components'
// The components we created in the previous step
import { Row, Column } from './Grid'
// Create a theme
const theme = {
// Responsove Props config object
@johnnyBira
johnnyBira / Medium #1 - Grid Example 4.jsx
Created March 30, 2019 17:09
Medium #1 - Grid Example 4
// Notice how the prop names matches the names of the mixins/functions passed to Responsive Props
<Row>
<Column span={{ s: 6, l: 3, xl: 2 }} order={{ l: 2 }} />
<Column span={{ s: 6, l: 3, xl: 2 }} order={{ l: 1 }} />
<Column span={{ s: 6 }} hidden={{ l: true }} />
<Column span={{ s: 6 }} hidden={{ l: true }} />
</Row>
// 🎉 Works just as well with single values 🎉
<Row>
...
const span = (cols) => `
// calculate the width
width: ${cols / 12 * 100 }%;
`
const offset = (cols) => `
// calculate the offset
margin: ${cols / 12 * 100 }%;
@johnnyBira
johnnyBira / Medium #1 - Grid Example 2.jsx
Last active March 30, 2019 13:48
Medium #1 - Grid Example 2
import withResponsiveProps from 'responsive-props'
import styled from 'styled-components'
// A column component created with Styled Components
const Column = styled.div`
// Base styles
box-sizing: border-box;
width: 100%;
flex: 0 0 auto;
padding: 1rem,
@johnnyBira
johnnyBira / Medium #1 - Grid Example 1.jsx
Last active March 30, 2019 12:43
Medium #1 - Grid Example 1
<Row>
<Column span={{ m: 8 }} />
<Column span={{ m: 4 }} />
</Row>
@johnnyBira
johnnyBira / Medium #1 - Responsive Props API.jsx
Last active March 30, 2019 12:53
Medium #1 - Responsive Props API
// Pass multiple values that will apply different styles at differetn media queries
<MyStyledComponent someProp={{xs: 'val1', l: 'val2', xl: 'val3' }} />
// Optionally pass one value what will apply styles without media queries, i.e all window sizes
<MyStyledComponent someProp="val1" />
<Column
columns={{ m: 6, l: 3, xl: 2 }}
offset={{ m: 2, l: 3, xl: 4 }}
direction={{ m: 'reverse' }}
verticalAlignSelf={{ l: 'center' }}
/>
@johnnyBira
johnnyBira / column.js
Created December 8, 2017 15:35
Example of a column in Styled Components Flex Grid
<Column columns={{ m: 6, l: 3, xl: 2 }} />