Skip to content

Instantly share code, notes, and snippets.

@jumbo
Forked from patrickhpan/multiple-artboards.js
Created January 22, 2018 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jumbo/6c6e79b05008e260412d016bc57e7321 to your computer and use it in GitHub Desktop.
Save jumbo/6c6e79b05008e260412d016bc57e7321 to your computer and use it in GitHub Desktop.
Multiple artboards in react-sketchapp
import React from 'react';
import { render, Artboard, Text, View, StyleSheet } from 'react-sketchapp';
const layout = {
width: 375,
height: 667,
cols: 3,
margin: 100
}
const getArtboardStyle = i => {
let { width, height, cols, margin } = layout;
let x = i % cols;
let y = (i - x) / cols;
// Top and left are switched for some reason.
let top = x * (width + margin) + margin;
let left = y * (height + margin) + margin;
let style = {
left,
top,
width,
height,
position: 'fixed',
backgroundColor: '#fff'
}
console.log(style)
return style
}
const getParentStyle = count => {
let { width, height, cols, margin } = layout;
let rows = (count - (count % cols)) / cols + 1;
console.log(`${rows} rows, ${cols} cols`)
let totalHeight = rows * (height + margin) + margin;
let totalWidth = cols * (width + margin) + margin;
let style = {
top: 0,
left: 0,
width: totalWidth,
height: totalHeight,
backgroundColor: '#eee'
}
return style
}
export default (context) => {
let content = [
<View style={{width: 100, height: 100, backgroundColor: 'green'}}>foo</View>,
<View style={{width: 100, height: 100, backgroundColor: 'green'}}>bar</View>,
<View style={{width: 100, height: 100, backgroundColor: 'green'}}>baz</View>,
<View style={{width: 100, height: 100, backgroundColor: 'green'}}>qux</View>
]
let x = <Artboard name="root" style={getParentStyle(content.length)}>
{content.map((item, i) => <Artboard style={getArtboardStyle(i)} children={item} />)}
</Artboard>
render(x, context.document.currentPage())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment