Skip to content

Commit

Permalink
feat: add web implementation SvgXml and others component (#2382)
Browse files Browse the repository at this point in the history
# Summary
We want to make available SvgXml and other components in the web
platform.

## Test Plan
We can easily check how that works by opening `Test1813`.

## Compatibility

| OS      | Implemented |
| ------- | :---------: |
| Web     |    ✅     |

---------

Co-authored-by: Jakub Grzywacz <[email protected]>
  • Loading branch information
bohdanprog and jakex7 authored Jul 31, 2024
1 parent a27e17f commit a2e843b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
17 changes: 9 additions & 8 deletions src/css/LocalSvg.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import * as React from 'react';
import { useState, useEffect, Component } from 'react';
import type { ImageSourcePropType } from 'react-native';
import { Platform, Image } from 'react-native';

import { fetchText } from 'react-native-svg';
import { Image, Platform, type ImageSourcePropType } from 'react-native';
import { fetchText, type SvgProps } from 'react-native-svg';
import { resolveAssetUri } from '../lib/resolveAssetUri';
import { SvgCss, SvgWithCss } from './css';
import type { SvgProps } from 'react-native-svg';

export function getUriFromSource(source: ImageSourcePropType) {
const resolvedAssetSource = Image.resolveAssetSource(source);
return resolvedAssetSource.uri;
const resolvedAssetSource =
Platform.OS === 'web'
? resolveAssetUri(source)
: Image.resolveAssetSource(source);
return resolvedAssetSource?.uri;
}

export function loadLocalRawResourceDefault(source: ImageSourcePropType) {
Expand Down Expand Up @@ -39,7 +40,7 @@ export async function loadAndroidRawResource(uri: string) {

export function loadLocalRawResourceAndroid(source: ImageSourcePropType) {
const uri = getUriFromSource(source);
if (isUriAnAndroidResourceIdentifier(uri)) {
if (uri && isUriAnAndroidResourceIdentifier(uri)) {
return loadAndroidRawResource(uri);
} else {
return fetchText(uri);
Expand Down
10 changes: 9 additions & 1 deletion src/xml.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ export function SvgXml(props: XmlProps) {
}
}

export async function fetchText(uri: string) {
export async function fetchText(uri?: string) {
if (!uri) {
return null;
}
const response = await fetch(uri);
if (response.ok || (response.status === 0 && uri.startsWith('file://'))) {
return await response.text();
Expand Down Expand Up @@ -207,6 +210,11 @@ export function astToReact(
): JSX.Element | string {
if (typeof value === 'object') {
const { Tag, props, children } = value;
if (props?.class) {
props.className = props.class;
delete props.class;
}

return (
<Tag key={index} {...props}>
{(children as (AST | string)[]).map(astToReact)}
Expand Down

0 comments on commit a2e843b

Please sign in to comment.