|
11 | 11 | - [`<Svg>`](#svg) |
12 | 12 | - [`<Text>`](#text) |
13 | 13 | - [`<View>`](#view) |
| 14 | +- [`Hooks`](#hooks) |
| 15 | + - [`useWindowDimensions`](#usewindowdimensions) |
14 | 16 | - [`Platform`](#platform) |
15 | 17 | - [`OS`](#os) |
16 | 18 | - [`Version`](#version) |
|
25 | 27 | - [`create`](#createstyleoptionsstyles) |
26 | 28 | - [`resolve`](#resolvestyle) |
27 | 29 | - [`Symbols`](#symbols) |
28 | | - - [`makeSymbol`](#makesymbolnode-name) |
| 30 | + - [`makeSymbol`](#makesymbolnode-props-document) |
29 | 31 |
|
30 | 32 | ### `render(element, container)` |
31 | 33 |
|
@@ -147,9 +149,11 @@ Wrapper for Sketch's Artboards. Requires a [`<Page>`](#page) component as a pare |
147 | 149 | | `name` | `String` | | The name to be displayed in the Sketch Layer List | |
148 | 150 | | `children` | `Node` | | | |
149 | 151 | | `style` | [`Style`](/docs/styling.md) | | | |
150 | | -| `viewport` | `Viewport` | | Object: { name: string, width: number, height: number} | |
| 152 | +| `viewport` | `Viewport` | | Object: { name: string, width: number, height: number, scale?: number, fontScale?: number } | |
151 | 153 | | `isHome` | `Boolean` | | Is prototype home screen if true | |
152 | 154 |
|
| 155 | +The `scale` and `fontScale` attributes in the `viewport` prop are not used by Sketch, but can be used together with the [`useWindowDimensions`](#usewindowdimensions) hook for conditional styling/rendering. |
| 156 | + |
153 | 157 | #### Examples |
154 | 158 |
|
155 | 159 | Hello world with width of 480px. |
@@ -382,6 +386,61 @@ View primitives |
382 | 386 | </Document> |
383 | 387 | ``` |
384 | 388 |
|
| 389 | +## Hooks |
| 390 | + |
| 391 | +### `useWindowDimensions()` |
| 392 | + |
| 393 | +Returns the window dimensions of the parent `<Artboard>`. Returns `{ width: number, height: number, fontScale: number, scale: number }`. |
| 394 | + |
| 395 | +#### Example |
| 396 | + |
| 397 | +```js |
| 398 | +import { Page, Artboard, View, Text, useWindowDimensions } from 'react-sketchapp'; |
| 399 | + |
| 400 | +const HomePage = () => { |
| 401 | + const { height, width } = useWindowDimensions(); |
| 402 | + |
| 403 | + return ( |
| 404 | + <View style={{ flex: 1 }}> |
| 405 | + <View style={{ height, width }}> |
| 406 | + <Text>Hello World</Text> |
| 407 | + </View> |
| 408 | + {(width >= 768) && ( |
| 409 | + <View style={{ height, width, backgroundColor: 'blue' }}> |
| 410 | + <Text style={{ color: 'white' }}> |
| 411 | + You can only see this text on tablet/desktop |
| 412 | + </Text> |
| 413 | + </View> |
| 414 | + )} |
| 415 | + </View> |
| 416 | + ); |
| 417 | +}; |
| 418 | + |
| 419 | +const devices = [{ |
| 420 | + name: 'Mobile', |
| 421 | + width: 360, |
| 422 | + height: 640, |
| 423 | +}, { |
| 424 | + name: 'Tablet', |
| 425 | + width: 768 |
| 426 | + height: 1024, |
| 427 | +}, { |
| 428 | + name: 'Desktop', |
| 429 | + width: 1024 |
| 430 | + height: 1280, |
| 431 | +}]; |
| 432 | + |
| 433 | +render( |
| 434 | + <Page style={{ flexDirection: 'row' }}> |
| 435 | + {devices.map(viewport => ( |
| 436 | + <Artboard viewport={viewport} style={{ marginRight: 80 }}> |
| 437 | + <HomePage /> |
| 438 | + </Artboard> |
| 439 | + ))} |
| 440 | + </Page>, |
| 441 | +); |
| 442 | +``` |
| 443 | + |
385 | 444 | ## Platform |
386 | 445 |
|
387 | 446 | ### `OS` |
@@ -734,7 +793,7 @@ export default () => { |
734 | 793 | }; |
735 | 794 | ``` |
736 | 795 |
|
737 | | -####Nested symbol + override example |
| 796 | +#### Nested symbol + override example |
738 | 797 |
|
739 | 798 | ```js |
740 | 799 | import sketch from 'sketch'; |
|
0 commit comments