-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support local .svg files, fixes #1306
depends on react-native-community/cli#1042 ```jsx import React from 'react'; import {LocalSvg} from 'react-native-svg'; import test from './test.svg'; export default () => <LocalSvg asset={test} />; ```
- Loading branch information
Showing
4 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { NativeModules, Platform } from 'react-native'; | ||
// @ts-ignore | ||
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource'; | ||
|
||
import { fetchText } from './xml'; | ||
import { SvgCss } from './css'; | ||
|
||
const { getRawResource } = NativeModules.RNSVGRenderableManager; | ||
|
||
export function getUriFromSource(source?: string | number) { | ||
const resolvedAssetSource = resolveAssetSource(source); | ||
return resolvedAssetSource.uri; | ||
} | ||
|
||
export function loadLocalRawResourceDefault(source?: string | number) { | ||
const uri = getUriFromSource(source); | ||
return fetchText(uri); | ||
} | ||
|
||
export function isUriAnAndroidResourceIdentifier(uri?: string | number) { | ||
return typeof uri === 'string' && uri.indexOf('/') <= -1; | ||
} | ||
|
||
export async function loadAndroidRawResource(uri?: string | number) { | ||
try { | ||
return await getRawResource(uri); | ||
} catch (e) { | ||
console.error( | ||
'Error in RawResourceUtils while trying to natively load an Android raw resource: ', | ||
e, | ||
); | ||
return null; | ||
} | ||
} | ||
|
||
export function loadLocalRawResourceAndroid(source?: string | number) { | ||
const uri = getUriFromSource(source); | ||
if (isUriAnAndroidResourceIdentifier(uri)) { | ||
return loadAndroidRawResource(uri); | ||
} else { | ||
return fetchText(uri); | ||
} | ||
} | ||
|
||
export const loadLocalRawResource = | ||
Platform.OS !== 'android' | ||
? loadLocalRawResourceDefault | ||
: loadLocalRawResourceAndroid; | ||
|
||
export type LocalProps = { asset?: string | number; override?: Object }; | ||
|
||
export function LocalSvg(props: LocalProps) { | ||
const { asset, ...rest } = props; | ||
const [xml, setXml] = useState(null); | ||
useEffect(() => { | ||
loadLocalRawResource(asset).then(setXml); | ||
}, [asset]); | ||
return <SvgCss xml={xml} {...rest} />; | ||
} | ||
|
||
export default LocalSvg; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters