From d95b14a70b52dc559722b851e1ce035d7c31a065 Mon Sep 17 00:00:00 2001 From: Alwyn Tan Date: Wed, 16 Aug 2023 17:01:41 -0400 Subject: [PATCH] Fix: Copy SVG properties into the child in react-native-svg (#1485) --- .../src/createLucideIcon.ts | 30 +++++++++++++------ .../src/defaultAttributes.ts | 12 +++++++- .../lucide-react-native.spec.tsx.snap | 12 ++++---- .../tests/lucide-react-native.spec.tsx | 21 +++++++++++++ 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/packages/lucide-react-native/src/createLucideIcon.ts b/packages/lucide-react-native/src/createLucideIcon.ts index fe1ea1301..af7d79935 100644 --- a/packages/lucide-react-native/src/createLucideIcon.ts +++ b/packages/lucide-react-native/src/createLucideIcon.ts @@ -1,7 +1,7 @@ import { forwardRef, createElement, ReactSVG, ReactNode, FunctionComponent } from 'react'; import PropTypes from 'prop-types'; import * as NativeSvg from 'react-native-svg'; -import defaultAttributes from './defaultAttributes'; +import defaultAttributes, { childDefaultAttributes } from './defaultAttributes'; import type { SvgProps } from 'react-native-svg'; type IconNode = [elementName: keyof ReactSVG, attrs: Record][] @@ -9,32 +9,44 @@ type IconNode = [elementName: keyof ReactSVG, attrs: Record][] export interface LucideProps extends SvgProps { size?: string | number absoluteStrokeWidth?: boolean + 'data-testid'?: string } const createLucideIcon = (iconName: string, iconNode: IconNode) => { const Component = forwardRef( - ({ color = 'currentColor', size = 24, strokeWidth = 2, absoluteStrokeWidth, children, ...rest }: LucideProps, ref) => - createElement( + ({ color = 'currentColor', size = 24, strokeWidth = 2, absoluteStrokeWidth, children, 'data-testid': dataTestId, ...rest }: LucideProps, ref) => { + const customAttrs = { + stroke: color, + strokeWidth: absoluteStrokeWidth ? (Number(strokeWidth) * 24) / Number(size) : strokeWidth, + ...rest, + }; + + return createElement( NativeSvg.Svg as unknown as string, { ref, ...defaultAttributes, width: size, height: size, - stroke: color, - strokeWidth: absoluteStrokeWidth ? Number(strokeWidth) * 24 / Number(size) : strokeWidth, - ...rest, + 'data-testid': dataTestId, + ...customAttrs, }, [ ...iconNode.map(([tag, attrs]) => { - const upperCasedTag = (tag.charAt(0).toUpperCase() + tag.slice(1)) as keyof (typeof NativeSvg); - return createElement(NativeSvg[upperCasedTag] as FunctionComponent>, attrs); + const upperCasedTag = (tag.charAt(0).toUpperCase() + + tag.slice(1)) as keyof typeof NativeSvg; + // duplicating the attributes here because generating the OTA update bundles don't inherit the SVG properties from parent (codepush, expo-updates) + return createElement( + NativeSvg[upperCasedTag] as FunctionComponent>, + { ...childDefaultAttributes, ...customAttrs, ...attrs }, + ); }), ...( (Array.isArray(children) ? children : [children]) || [] ), ], - ), + ); + } ); Component.propTypes = { diff --git a/packages/lucide-react-native/src/defaultAttributes.ts b/packages/lucide-react-native/src/defaultAttributes.ts index ce97fb058..3fae7d973 100644 --- a/packages/lucide-react-native/src/defaultAttributes.ts +++ b/packages/lucide-react-native/src/defaultAttributes.ts @@ -1,4 +1,4 @@ -export default { +const defaultAttributes = { xmlns: 'http://www.w3.org/2000/svg', width: 24, height: 24, @@ -9,3 +9,13 @@ export default { strokeLinecap: 'round', strokeLinejoin: 'round', }; + +export const childDefaultAttributes = { + fill: defaultAttributes.fill, + stroke: defaultAttributes.stroke, + strokeWidth: defaultAttributes.strokeWidth, + strokeLinecap: defaultAttributes.strokeLinecap, + strokeLinejoin: defaultAttributes.strokeLinejoin, +}; + +export default defaultAttributes; diff --git a/packages/lucide-react-native/tests/__snapshots__/lucide-react-native.spec.tsx.snap b/packages/lucide-react-native/tests/__snapshots__/lucide-react-native.spec.tsx.snap index 5a96c8bab..bacef291e 100644 --- a/packages/lucide-react-native/tests/__snapshots__/lucide-react-native.spec.tsx.snap +++ b/packages/lucide-react-native/tests/__snapshots__/lucide-react-native.spec.tsx.snap @@ -1,11 +1,13 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html -exports[`Using lucide icon components > should adjust the size, stroke color and stroke width 1`] = `""`; +exports[`Using lucide icon components > should adjust the size, stroke color and stroke width 1`] = `""`; -exports[`Using lucide icon components > should not scale the strokeWidth when absoluteStrokeWidth is set 1`] = `""`; +exports[`Using lucide icon components > should duplicate properties to children components 1`] = `""`; -exports[`Using lucide icon components > should render an component 1`] = `""`; +exports[`Using lucide icon components > should not scale the strokeWidth when absoluteStrokeWidth is set 1`] = `""`; -exports[`Using lucide icon components > should work with a single child component 1`] = `""`; +exports[`Using lucide icon components > should render an component 1`] = `""`; -exports[`Using lucide icon components > should work with several children components 1`] = `""`; +exports[`Using lucide icon components > should work with a single child component 1`] = `""`; + +exports[`Using lucide icon components > should work with several children components 1`] = `""`; diff --git a/packages/lucide-react-native/tests/lucide-react-native.spec.tsx b/packages/lucide-react-native/tests/lucide-react-native.spec.tsx index 7debf73ae..1be83b746 100644 --- a/packages/lucide-react-native/tests/lucide-react-native.spec.tsx +++ b/packages/lucide-react-native/tests/lucide-react-native.spec.tsx @@ -116,4 +116,25 @@ describe('Using lucide icon components', () => { expect(child2).toEqual(getByTestId(childId2)); expect(container.innerHTML).toMatchSnapshot(); }) + + it('should duplicate properties to children components', () => { + const testId = 'multiple-children'; + + const fill = 'red'; + const color = 'white'; + const strokeWidth = 10; + + const { container, getByTestId } = render( + + ); + const { children = [] } = getByTestId(testId) as unknown as { children: HTMLCollection }; + for (let i = 0; i < children.length; i++) { + const child = children[i]; + expect(child.getAttribute('fill')).toBe(fill); + expect(child.getAttribute('stroke')).toBe(color); + expect(child.getAttribute('stroke-width')).toBe(`${strokeWidth}`); + } + + expect(container.innerHTML).toMatchSnapshot(); + }) })