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

Implement <svg> support #272

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Implement <svg> support #272

wants to merge 1 commit into from

Conversation

efoken
Copy link

@efoken efoken commented Feb 19, 2025

#4

@efoken
Copy link
Author

efoken commented Feb 19, 2025

Let me know what you think @necolas

There are still some $FlowFixMe that I added inside createStrictDOMSvgComponent which can be fixed by changing the NativeProps type, but I was not sure if that's the best place, or if it may be better to have a bit more differentiation between React Native Props and React Native SVG Props.

Also I am willing to add some tests and probably some examples in the Expo examples app.

Copy link
Contributor

@necolas necolas left a comment

Choose a reason for hiding this comment

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

Thanks for sharing this quickly and early.

My initial thoughts are that the HTML and SVG interfaces are too intermingled in the code. Although html.svg seemed to make sense at first, I have doubts after looking through this patch and doing a bit more research.

The <svg> element is type SVGSVGElement. So writing <svg.svg> in RSD would be true to spec. And eventually there might be <html.html> as well.

If we were to put everything SVG (inc the <svg> element) under the svg export, it would help keep a clearer separation between HTML and SVG types and logic. DCE has a better chance of removing all the SVG code too.

So in general, having separate Svg functions and checks seems like the better way forward to me. And we can refactor/rename any code that needs to be shared as and where it's needed.

* "circle"
*/
export const circle: component(
ref?: React.RefSetter<HTMLElement>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to use the correct ref type for these, e.g., SVGCircleElement

Copy link
Author

Choose a reason for hiding this comment

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

Yes correct @necolas, but SVG element types do only exist in TypeScript, sadly Flow does not have them ...

Comment on lines 144 to 160
'alignmentBaseline', // svg
'amplitude', // svg
'azimuth', // svg
'baseFrequency', // svg
'baselineShift', // svg
'bias', // svg
'clipPath', // svg
'clipRule', // svg
'color', // svg
'crossOrigin', // svg
'cx', // svg
'cy', // svg
'd', // svg
'diffuseConstant', // svg
'divisor', // svg
'dx', // svg
'dy', // svg
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably refactor this code and have a separate check for SVG props, as they shouldn't be allowed on the HTML elements.

*/

declare module 'react-native-svg' {
declare module.exports: any;
Copy link
Contributor

Choose a reason for hiding this comment

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

If there are no community Flow types we can use for this package, I can contribute types based on what we use internally

Copy link
Author

Choose a reason for hiding this comment

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

There are no community types available

* "circle"
*/
export const circle: component(
ref?: React.RefSetter<HTMLElement>,
Copy link
Contributor

Choose a reason for hiding this comment

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

The ref types need to be updated to their SVG element equivalents

Comment on lines 37 to 38
'aria-label'?: ?Stringish,
'data-testid'?: ?string,
Copy link
Contributor

Choose a reason for hiding this comment

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

These types are already part of StrictReactDOMProps

Copy link
Author

Choose a reason for hiding this comment

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

Changed it to Pick< StrictReactDOMProps, 'aria-label' | 'data-testid'>, hope that's okay as only these two props are supported on elements, at least not for Native

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah we need to match the web types more closely. Doesn't matter if native doesn't yet have as much support. Same as we do for HTML

@nandorojo
Copy link
Contributor

nandorojo commented Feb 26, 2025

This PR is great timing! I just happened to do something similar in my app via a patch and was playing with it before sending a PR. Thanks moving so quickly on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants