Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to add multiple shadows and shadow spread #277

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions __tests__/components/__snapshots__/RedBox.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ exports[`<RedBox /> renders simple errors 1`] = `
<view
name="RedBox"
resizingConstraint={undefined}
shadowGroup={undefined}
style={
Object {
"backgroundColor": "rgb(204, 0, 0)",
Expand Down Expand Up @@ -32,6 +33,7 @@ exports[`<RedBox /> renders simple errors 1`] = `
<view
name="Frames"
resizingConstraint={undefined}
shadowGroup={undefined}
style={
Object {
"color": "white",
Expand Down Expand Up @@ -176,6 +178,7 @@ exports[`<RedBox /> renders string errors 1`] = `
<view
name="RedBox"
resizingConstraint={undefined}
shadowGroup={undefined}
style={
Object {
"backgroundColor": "rgb(204, 0, 0)",
Expand Down
13 changes: 9 additions & 4 deletions __tests__/components/__snapshots__/View.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ exports[`<View /> name defaults to View 1`] = `
<view
name="View"
resizingConstraint={undefined}
style={undefined}
/>
shadowGroup={undefined}
style={undefined} />
`;

exports[`<View /> name passes its name 1`] = `
<view
name="foo"
resizingConstraint={undefined}
style={undefined}
shadowGroup={undefined}
style={undefined} />
/>
`;

exports[`<View /> passes its children 1`] = `
<view
name="View"
resizingConstraint={undefined}
style={undefined}
shadowGroup={undefined}
style={undefined}>
>
foo
</view>
Expand All @@ -30,6 +32,7 @@ exports[`<View /> style accepts a StyleSheet ordinal 1`] = `
<view
name="View"
resizingConstraint={undefined}
shadowGroup={undefined}
style={
Object {
"flex": 1,
Expand All @@ -42,6 +45,7 @@ exports[`<View /> style accepts a plain object 1`] = `
<view
name="View"
resizingConstraint={undefined}
shadowGroup={undefined}
style={
Object {
"flex": 1,
Expand All @@ -54,6 +58,7 @@ exports[`<View /> style accepts an array of plain objects and/or StyleSheet ordi
<view
name="View"
resizingConstraint={undefined}
shadowGroup={undefined}
style={
Object {
"flex": 1,
Expand Down
9 changes: 9 additions & 0 deletions src/components/ShadowGroupPropTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import PropTypes from 'prop-types';

module.exports = {
shadowColor: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add shadowInner to support multiple inner shadows as well

shadowOffset: { width: PropTypes.number, height: PropTypes.number },
shadowSpread: PropTypes.number,
shadowOpacity: PropTypes.number,
shadowRadius: PropTypes.number,
};
7 changes: 7 additions & 0 deletions src/components/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import StyleSheet from '../stylesheet';
import ViewStylePropTypes from './ViewStylePropTypes';
import ResizingConstraintPropTypes from './ResizingConstraintPropTypes';
import ShadowGroupPropTypes from './ShadowGroupPropTypes';

const propTypes = {
// TODO(lmr): do some nice warning stuff like RN does
Expand All @@ -18,6 +19,11 @@ const propTypes = {
resizingConstraint: PropTypes.shape({
...ResizingConstraintPropTypes,
}),
shadowGroup: PropTypes.arrayOf(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's just call it shadows?

PropTypes.shape({
...ShadowGroupPropTypes,
})
),
children: PropTypes.node,
};

Expand All @@ -33,6 +39,7 @@ class View extends React.Component {
name={this.props.name}
style={StyleSheet.flatten(this.props.style)}
resizingConstraint={this.props.resizingConstraint}
shadowGroup={this.props.shadowGroup}
>
{this.props.children}
</view>
Expand Down
2 changes: 1 addition & 1 deletion src/jsonUtils/shapeLayers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { SJFill, SJPath, SJRect, SJShapeGroupLayer } from '@skpm/sketchapp-json-flow-types';
import { makeResizeConstraint } from './hacksForJSONImpl';
import { generateID, makeRect } from './models';
import type { ResizeConstraints } from '../types';
import type { ResizeConstraints, SketchShadowGroup } from '../types';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

used anywhere?


type Radii = Array<number>;

Expand Down
19 changes: 16 additions & 3 deletions src/renderers/ViewRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import type { SJShapeGroupLayer } from '@skpm/sketchapp-json-flow-types';
import SketchRenderer from './SketchRenderer';
import { makeRect, makeColorFill, makeColorFromCSS } from '../jsonUtils/models';
import { makeRectShapeLayer, makeShapeGroup } from '../jsonUtils/shapeLayers';
import type { ViewStyle, LayoutInfo, TextStyle } from '../types';
import type {
ViewStyle,
LayoutInfo,
TextStyle,
SketchShadowGroup,
ResizeConstraints,
} from '../types';
import {
makeBorderOptions,
makeShadow,
Expand All @@ -24,6 +30,7 @@ const VISIBLE_STYLES = [
'shadowOffset',
'shadowOpacity',
'shadowRadius',
'shadowSpread',
'backgroundColor',
'borderColor',
'borderTopColor',
Expand Down Expand Up @@ -55,7 +62,7 @@ class ViewRenderer extends SketchRenderer {
style: ViewStyle,
textStyle: TextStyle,
// eslint-disable-next-line no-unused-vars
props: any,
props: any
): Array<SJShapeGroupLayer> {
const layers = [];
// NOTE(lmr): the group handles the position, so we just care about width/height here
Expand Down Expand Up @@ -106,7 +113,13 @@ class ViewRenderer extends SketchRenderer {
const fill = makeColorFill(backgroundColor);
const content = makeShapeGroup(frame, [shapeLayer], [fill]);

if (hasAnyDefined(style, SHADOW_STYLES)) {
if (props.shadowGroup) {
const shadows = [];
props.shadowGroup.map(shadowStyle =>
shadows.push(makeShadow(shadowStyle))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's push to innerShadows as well if shadowStyle.shadowInner is true

);
content.style.shadows = shadows;
} else if (hasAnyDefined(style, SHADOW_STYLES)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of an else if we should probably concatenate the shadow defined in style if there is one

const shadow = [makeShadow(style)];
if (style.shadowInner) {
content.style.innerShadows = shadow;
Expand Down
12 changes: 12 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export type ViewStyle = {
shadowOffset: { width: number, height: number },
shadowOpacity: number,
shadowRadius: number,
shadowSpread: number,
shadowInner: "innerShadow" | "shadow",
Copy link
Collaborator

@mathieudutour mathieudutour Jun 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a boolean.

And we could probably reuse the SketchShadow type as well (type ViewStyle = {...} & SketchShadow)

width: number,
height: number,
top: number,
Expand Down Expand Up @@ -180,3 +182,13 @@ export type ResizeConstraints = {
fixedHeight: boolean,
fixedWidth: boolean,
};

export type SketchShadow = {
shadowColor: Color,
shadowOffset: { width: number, height: number },
shadowSpread: number,
shadowOpacity: number,
shadowRadius: number
};

export type SketchShadowGroup = Array<SketchShadow>;