diff --git a/__tests__/__snapshots__/css.test.tsx.snap b/__tests__/__snapshots__/css.test.tsx.snap index 5ee8859f3..3a5e19b9a 100644 --- a/__tests__/__snapshots__/css.test.tsx.snap +++ b/__tests__/__snapshots__/css.test.tsx.snap @@ -156,96 +156,17 @@ exports[`supports CSS in style element 1`] = ` xmlns="http://www.w3.org/2000/svg" > void; - ref?: (instance: Component | null) => void; - markerStart?: string; - markerMid?: string; - markerEnd?: string; - clipPath?: string; - clipRule?: number; - display?: string; - } = { - matrix, - ...transformProps, + const extracted: extractedProps = { propList: styleProperties, - opacity: extractOpacity(opacity), - ...extractResponder(props, ref), - ...extractFill(props, styleProperties), - ...extractStroke(props, styleProperties), - display: display === 'none' ? 'none' : undefined, }; + extractResponder(extracted, props, ref); + extractFill(extracted, props, styleProperties); + extractStroke(extracted, props, styleProperties); + + if (matrix !== null) { + extracted.matrix = matrix; + } + + if (opacity != null) { + extracted.opacity = extractOpacity(opacity); + } + + if (display != null) { + extracted.display = display === 'none' ? 'none' : undefined; + } if (onLayout) { extracted.onLayout = onLayout; diff --git a/src/lib/extract/extractResponder.ts b/src/lib/extract/extractResponder.ts index eddc76b1f..950ddf6e7 100644 --- a/src/lib/extract/extractResponder.ts +++ b/src/lib/extract/extractResponder.ts @@ -1,10 +1,15 @@ import { PanResponder } from 'react-native'; -import { ResponderInstanceProps, ResponderProps } from './types'; +import { + extractedProps, + ResponderInstanceProps, + ResponderProps, +} from './types'; const responderKeys = Object.keys(PanResponder.create({}).panHandlers); const numResponderKeys = responderKeys.length; export default function extractResponder( + o: extractedProps, // eslint-disable-next-line @typescript-eslint/no-explicit-any props: { [x: string]: any } & ResponderProps, ref: ResponderInstanceProps, @@ -20,9 +25,6 @@ export default function extractResponder( delayLongPress, pointerEvents, } = props; - const o: { - [touchableProperty: string]: unknown; - } = {}; let responsible = false; for (let i = 0; i < numResponderKeys; i++) { @@ -62,6 +64,4 @@ export default function extractResponder( if (responsible) { o.responsible = true; } - - return o; } diff --git a/src/lib/extract/extractStroke.ts b/src/lib/extract/extractStroke.ts index a89e96eb2..6bb865b51 100644 --- a/src/lib/extract/extractStroke.ts +++ b/src/lib/extract/extractStroke.ts @@ -1,7 +1,7 @@ import extractBrush from './extractBrush'; import extractOpacity from './extractOpacity'; import extractLengthList from './extractLengthList'; -import { StrokeProps } from './types'; +import { extractedProps, StrokeProps } from './types'; const caps = { butt: 0, @@ -25,6 +25,7 @@ const vectorEffects = { }; export default function extractStroke( + o: extractedProps, props: StrokeProps, styleProperties: string[], ) { @@ -42,50 +43,48 @@ export default function extractStroke( if (stroke != null) { styleProperties.push('stroke'); + o.stroke = extractBrush(stroke); } if (strokeWidth != null) { styleProperties.push('strokeWidth'); + o.strokeWidth = strokeWidth; } if (strokeOpacity != null) { styleProperties.push('strokeOpacity'); + o.strokeOpacity = extractOpacity(strokeOpacity); } if (strokeDasharray != null) { styleProperties.push('strokeDasharray'); + const strokeDash = + !strokeDasharray || strokeDasharray === 'none' + ? null + : extractLengthList(strokeDasharray); + o.strokeDasharray = + strokeDash && strokeDash.length % 2 === 1 + ? strokeDash.concat(strokeDash) + : strokeDash; } if (strokeDashoffset != null) { styleProperties.push('strokeDashoffset'); + o.strokeDashoffset = + strokeDasharray && strokeDashoffset ? +strokeDashoffset || 0 : null; } if (strokeLinecap != null) { styleProperties.push('strokeLinecap'); + o.strokeLinecap = (strokeLinecap && caps[strokeLinecap]) || 0; } if (strokeLinejoin != null) { styleProperties.push('strokeLinejoin'); + o.strokeLinejoin = (strokeLinejoin && joins[strokeLinejoin]) || 0; } if (strokeMiterlimit != null) { styleProperties.push('strokeMiterlimit'); - } - - const strokeDash = - !strokeDasharray || strokeDasharray === 'none' - ? null - : extractLengthList(strokeDasharray); - - return { - stroke: extractBrush(stroke), - strokeOpacity: extractOpacity(strokeOpacity), - strokeLinecap: (strokeLinecap && caps[strokeLinecap]) || 0, - strokeLinejoin: (strokeLinejoin && joins[strokeLinejoin]) || 0, - strokeDasharray: - strokeDash && strokeDash.length % 2 === 1 - ? strokeDash.concat(strokeDash) - : strokeDash, - strokeWidth: strokeWidth != null ? strokeWidth : 1, - strokeDashoffset: - strokeDasharray && strokeDashoffset ? +strokeDashoffset || 0 : null, - strokeMiterlimit: + o.strokeMiterlimit = (strokeMiterlimit && typeof strokeMiterlimit !== 'number' ? parseFloat(strokeMiterlimit) - : strokeMiterlimit) || 4, - vectorEffect: (vectorEffect && vectorEffects[vectorEffect]) || 0, - }; + : strokeMiterlimit) || 4; + } + if (vectorEffect != null) { + o.vectorEffect = (vectorEffect && vectorEffects[vectorEffect]) || 0; + } } diff --git a/src/lib/extract/extractTransform.ts b/src/lib/extract/extractTransform.ts index 5984c1fe6..c66fb5646 100644 --- a/src/lib/extract/extractTransform.ts +++ b/src/lib/extract/extractTransform.ts @@ -67,9 +67,11 @@ function universal2axis( return [x || defaultValue || 0, y || defaultValue || 0]; } -export function props2transform(props: TransformProps): TransformedProps { +export function props2transform( + props: TransformProps, +): TransformedProps | null { const { - rotation = 0, + rotation, translate, translateX, translateY, @@ -85,6 +87,25 @@ export function props2transform(props: TransformProps): TransformedProps { x, y, } = props; + if ( + rotation == null && + translate == null && + translateX == null && + translateY == null && + origin == null && + originX == null && + originY == null && + scale == null && + scaleX == null && + scaleY == null && + skew == null && + skewX == null && + skewY == null && + x == null && + y == null + ) { + return null; + } if (Array.isArray(x) || Array.isArray(y)) { console.warn( @@ -101,7 +122,7 @@ export function props2transform(props: TransformProps): TransformedProps { const sk = universal2axis(skew, skewX, skewY); return { - rotation: +rotation || 0, + rotation: rotation == null ? 0 : +rotation || 0, originX: or[0], originY: or[1], scaleX: sc[0], @@ -114,11 +135,14 @@ export function props2transform(props: TransformProps): TransformedProps { } export function transformToMatrix( - props: TransformedProps, - transform: number[] | string | TransformProps | void | undefined, -): [number, number, number, number, number, number] { + props: TransformedProps | null, + transform: number[] | string | TransformProps | void | null | undefined, +): [number, number, number, number, number, number] | null { + if (!props && !transform) { + return null; + } reset(); - appendTransformProps(props); + props && appendTransformProps(props); if (transform) { if (Array.isArray(transform)) { @@ -141,7 +165,8 @@ export function transformToMatrix( console.error(e); } } else { - appendTransformProps(props2transform(transform)); + const transformProps = props2transform(transform); + transformProps && appendTransformProps(transformProps); } } diff --git a/src/lib/extract/types.ts b/src/lib/extract/types.ts index db29879e4..eaa054548 100644 --- a/src/lib/extract/types.ts +++ b/src/lib/extract/types.ts @@ -1,3 +1,4 @@ +import { Component } from 'react'; import { GestureResponderEvent } from 'react-native'; export type NumberProp = string | number; @@ -93,3 +94,19 @@ export type ClipProps = { clipPath?: string; clipRule?: 'evenodd' | 'nonzero'; }; +export type extractedProps = { + name?: string; + mask?: string; + opacity?: number; + matrix?: number[]; + propList?: string[]; + onLayout?: () => void; + ref?: (instance: Component | null) => void; + markerStart?: string; + markerMid?: string; + markerEnd?: string; + clipPath?: string; + clipRule?: number; + display?: string; + [touchableProperty: string]: unknown; +};