Skip to content

Implement <svg> support #272

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions apps/examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"react": "~18.3.1",
"react-dom": "~18.3.1",
"react-native": "~0.76.1",
"react-native-svg": "15.8.0",
"react-native-web": "~0.19.13",
"react-strict-dom": "*"
},
Expand Down
94 changes: 63 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/react-strict-dom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
"peerDependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-native": ">=0.75.2"
"react-native": ">=0.75.2",
"react-native-svg": "^15.8.0"
},
"engines": {
"node": ">=20.11.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/react-strict-dom/src/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type {
} from '@stylexjs/stylex';

import * as html from './html';
import * as svg from './svg';
import * as css from '@stylexjs/stylex';

type StyleTheme<V, T> = Theme<V, T>;
Expand All @@ -25,4 +26,4 @@ type StylesWithout<T> = StyleXStylesWithout<T>;

export type { StaticStyles, StyleTheme, StyleVars, Styles, StylesWithout };

export { css, html };
export { css, html, svg };
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
*/

import type { CompiledStyles } from '@stylexjs/stylex/lib/StyleXTypes';
import type { ReactDOMStyleProps } from '../../types/renderer.web';
import type { StrictSvgProps } from '../../../dist/types/StrictSvgProps';

import * as React from 'react';
import * as stylex from '@stylexjs/stylex';
import { errorMsg } from '../../shared/logUtils';
import { isSvgPropAllowed } from '../../shared/isSvgPropAllowed';

// $FlowFixMe[unclear-type]
function validateStrictProps(props: any) {
Object.keys(props).forEach((key) => {
const isValid = isSvgPropAllowed(key);
if (!isValid) {
errorMsg(`invalid prop "${key}"`);
delete props[key];
}
});
}

export function createStrictDOMSvgComponent<T, P: StrictSvgProps>(
TagName: string,
defaultStyle: StrictSvgProps['style']
): component(ref?: React.RefSetter<T>, ...P) {
// NOTE: `debug-style` is not generated by `stylex.create`
// so it needs a type-cast
const debugStyle: CompiledStyles = {
$$css: true,
'debug::name': `svg-${TagName}` as $FlowFixMe
};

const component: React.AbstractComponent<P, T> = React.forwardRef(
function (props, forwardedRef) {
/**
* get host props
*/
const { style, ...hostProps } = props;
validateStrictProps(hostProps);

if (props.role != null) {
// "presentation" synonym has wider browser support
// $FlowFixMe
hostProps.role = props.role === 'none' ? 'presentation' : props.role;
}

/**
* get host style props
*/
// Waiting on a diff so we can remove this indirection.
const hostStyleProps: ReactDOMStyleProps = stylex.props([
debugStyle,
defaultStyle,
style
]);

/**
* Construct tree
*
* Intentional flow error as we are asking for a more specific type
* than React itself.
*/
const element = (
<TagName
{...hostProps}
{...hostStyleProps}
ref={forwardedRef as $FlowFixMe}
/>
);
return element;
}
);

component.displayName = `svg.${TagName}`;
return component;
}
2 changes: 2 additions & 0 deletions packages/react-strict-dom/src/dom/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ const span: StrictReactDOMPropsStyle = styles.inline;
const strong: StrictReactDOMPropsStyle = [styles.inline, styles.strong];
const sub: StrictReactDOMPropsStyle = styles.inline;
const sup: StrictReactDOMPropsStyle = styles.inline;
const svg: StrictReactDOMPropsStyle = styles.img;
const textarea: StrictReactDOMPropsStyle = [
styles.inlineblock,
styles.textarea
Expand Down Expand Up @@ -174,6 +175,7 @@ export const defaultStyles = {
strong: strong as typeof strong,
sub: sub as typeof sub,
sup: sup as typeof sup,
svg: svg as typeof svg,
textarea: textarea as typeof textarea,
u: u as typeof u,
ul: ul as typeof ul
Expand Down
Loading
Loading