Skip to content

Commit bb8465a

Browse files
Improve symbol api (#446)
Improve symbol api
2 parents ae15886 + 1f5c6bb commit bb8465a

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
This project adheres to [Semantic Versioning](http://semver.org/). Every release, along with the migration instructions, is documented on the Github [Releases](https://github.com/airbnb/react-sketchapp/releases) page.
44

5+
## Version 3.0.1
6+
7+
- Allow passing a style object when making a symbol
8+
- Expose `getSymbolMasterByName`
9+
510
## Version 3.0.0
611

712
- Export Svg components in the Svg/index.js file (Thanks @saschazar21!)

docs/API.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ Reset the registered styles.
625625

626626
An interface to Sketch's symbols. Create symbols and optionally inject them into the symbols page.
627627

628-
### `makeSymbol(node, name, document)`
628+
### `makeSymbol(node, props, document)`
629629

630630
Creates a new symbol and injects it into the `Symbols` page. The name of the symbol can be optionally provided and will default to the display name of the component.
631631

@@ -636,12 +636,18 @@ Returns a react component which is an can be used to render instances of the sym
636636
| Parameter | Type | Default | Note |
637637
| --- | --- | --- | --- |
638638
| `node` | `Node` | | The node object that will be rendered as a symbol |
639-
| `name` | `String` | The node name | Optional name for the symbol, string can include backslashes to organize these symbols with Sketch. For example `squares/blue` |
639+
| `props` | `Object` | The node name | Optional name for the symbol, string can include backslashes to organize these symbols with Sketch. For example `squares/blue` |
640+
| `props.name` | `String` | The node name | Optional name for the symbol, string can include backslashes to organize these symbols with Sketch. For example `squares/blue` |
641+
| `props.style` | [`Style`](/docs/styling.md) | | |
640642
| `document` | `Object` | The current document | The Sketch document to make the symbol in |
641643

642644
### `getSymbolComponentByName(name)`
643645

644-
Returns a react component which can be used to render the symbol that is associated with that name.
646+
Returns a react component which can be used to render the symbol instance that is associated with that name.
647+
648+
### `getSymbolMasterByName(name)`
649+
650+
Returns the JSON representation of the symbol master that is associated with that name.
645651

646652
### Symbol example
647653

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-sketchapp",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "A React renderer for Sketch.app",
55
"main": "lib/index.js",
66
"types": "lib/index.d.ts",

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import _TextStyles from './sharedStyles/TextStyles';
1313
import {
1414
makeSymbol as _makeSymbol,
1515
getSymbolComponentByName as _getSymbolComponentByName,
16+
getSymbolMasterByName as _getSymbolMasterByName,
1617
injectSymbols as _injectSymbols,
1718
} from './symbol';
1819

@@ -31,6 +32,7 @@ export const View = _View;
3132
export const Platform = _Platform;
3233
export const makeSymbol = _makeSymbol;
3334
export const getSymbolComponentByName = _getSymbolComponentByName;
35+
export const getSymbolMasterByName = _getSymbolMasterByName;
3436
export const injectSymbols = _injectSymbols;
3537

3638
export default {
@@ -49,5 +51,6 @@ export default {
4951
Platform,
5052
makeSymbol,
5153
getSymbolComponentByName,
54+
getSymbolMasterByName,
5255
injectSymbols,
5356
};

src/renderers/SymbolMasterRenderer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { makeSymbolMaster, makeRect } from '../jsonUtils/models';
22
import SketchRenderer from './SketchRenderer';
33
import { TreeNode } from '../types';
4+
import { SymbolMasterProps } from '../symbol';
45

56
export default class SymbolMasterRenderer extends SketchRenderer {
6-
renderGroupLayer({ layout, props }: TreeNode<{ symbolID: string; name: string }>) {
7+
renderGroupLayer({ layout, props }: TreeNode<SymbolMasterProps & { symbolID: string }>) {
78
return makeSymbolMaster(
89
makeRect(layout.left, layout.top, layout.width, layout.height),
910
props.symbolID,

src/symbol.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,24 +143,37 @@ export const createSymbolInstanceClass = (symbolMaster: FileFormat.SymbolMaster)
143143
};
144144
};
145145

146+
const SymbolMasterPropTypes = {
147+
style: PropTypes.shape(ViewStylePropTypes),
148+
name: PropTypes.string,
149+
};
150+
151+
export type SymbolMasterProps = PropTypes.InferProps<typeof SymbolMasterPropTypes>;
152+
146153
export const makeSymbol = (
147154
Component: React.ComponentType<any>,
148-
name: string,
155+
symbolProps: string | SymbolMasterProps,
149156
document?: SketchDocumentData | SketchDocument | WrappedSketchDocument,
150157
) => {
151158
if (!hasInitialized && getSketchVersion() !== 'NodeJS') {
152159
getExistingSymbols(getDocumentData(document));
153160
}
154161

155-
const masterName = name || displayName(Component);
162+
const masterName =
163+
(typeof symbolProps === 'string' ? symbolProps : (symbolProps || {}).name) ||
164+
displayName(Component);
156165
const existingSymbol = existingSymbols.find(symbolMaster => symbolMaster.name === masterName);
157166
const symbolID = existingSymbol
158167
? existingSymbol.symbolID
159168
: generateID(`symbolID:${masterName}`, !!name);
160169

161170
const symbolMaster = flexToSketchJSON(
162171
buildTree(
163-
<sketch_symbolmaster symbolID={symbolID} name={masterName}>
172+
<sketch_symbolmaster
173+
{...(typeof symbolProps !== 'string' ? symbolProps || {} : {})}
174+
symbolID={symbolID}
175+
name={masterName}
176+
>
164177
<Component />
165178
</sketch_symbolmaster>,
166179
),

0 commit comments

Comments
 (0)