Last active
May 5, 2019 20:30
-
-
Save patrickhpan/a47ee004b5c77854be4cbfd29c09be17 to your computer and use it in GitHub Desktop.
Multiple artboards in react-sketchapp
This file contains 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 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