-
Notifications
You must be signed in to change notification settings - Fork 734
Support SVG image #1226
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
Merged
Merged
Support SVG image #1226
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
aa918ef
Support SVG image
M-i-k-e-l 336e348
Merge branch 'master' of https://github.com/wix/react-native-ui-lib i…
M-i-k-e-l 2aa0f13
Merge branch 'master' into Infra/support-svg-image
M-i-k-e-l 55a57b8
Remove packages from peerDependencies
M-i-k-e-l f6167e7
Move SVG image to assets configuration
M-i-k-e-l eb43f36
Merge branch 'master' into Infra/support-svg-image
M-i-k-e-l cd5fcf3
Add docs for SVG support
M-i-k-e-l 1bcc2f6
Add reanimated (removed in merge)
M-i-k-e-l e1239c1
Move SVG example to ImageScreen and move return statement
M-i-k-e-l File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,59 @@ | ||
import React, {Component} from 'react'; | ||
import {Image, View, Text} from 'react-native-ui-lib'; | ||
// @ts-ignore | ||
import {renderBooleanOption, renderRadioGroup} from '../ExampleScreenPresenter'; | ||
const file = require('../../../../uilib-docs/src/images/newDesign/headerLogo.svg').default; | ||
const xml = ` | ||
<svg width="32" height="32" viewBox="0 0 32 32"> | ||
<path | ||
fill-rule="evenodd" | ||
clip-rule="evenodd" | ||
fill="url(#gradient)" | ||
d="M4 0C1.79086 0 0 1.79086 0 4V28C0 30.2091 1.79086 32 4 32H28C30.2091 32 32 30.2091 32 28V4C32 1.79086 30.2091 0 28 0H4ZM17 6C17 5.44772 17.4477 5 18 5H20C20.5523 5 21 5.44772 21 6V25C21 25.5523 20.5523 26 20 26H18C17.4477 26 17 25.5523 17 25V6ZM12 11C11.4477 11 11 11.4477 11 12V25C11 25.5523 11.4477 26 12 26H14C14.5523 26 15 25.5523 15 25V12C15 11.4477 14.5523 11 14 11H12ZM6 18C5.44772 18 5 18.4477 5 19V25C5 25.5523 5.44772 26 6 26H8C8.55228 26 9 25.5523 9 25V19C9 18.4477 8.55228 18 8 18H6ZM24 14C23.4477 14 23 14.4477 23 15V25C23 25.5523 23.4477 26 24 26H26C26.5523 26 27 25.5523 27 25V15C27 14.4477 26.5523 14 26 14H24Z" | ||
/> | ||
<defs> | ||
<linearGradient | ||
id="gradient" | ||
x1="0" | ||
y1="0" | ||
x2="8.46631" | ||
y2="37.3364" | ||
gradient-units="userSpaceOnUse"> | ||
<stop offset="0" stop-color="#FEA267" /> | ||
<stop offset="1" stop-color="#E75A4C" /> | ||
</linearGradient> | ||
</defs> | ||
</svg> | ||
`; | ||
|
||
enum SizeType { | ||
None = '', | ||
Fixed = '50', | ||
Percentage = '50%' | ||
} | ||
|
||
export default class SvgImageScreen extends Component { | ||
state = { | ||
isFile: false, | ||
sizeType: SizeType.None | ||
}; | ||
|
||
render() { | ||
const {isFile, sizeType} = this.state; | ||
const size: any = Number(sizeType) || sizeType; | ||
return ( | ||
<View flex padding-page> | ||
<Text h1 marginB-s4> | ||
SVG Image | ||
</Text> | ||
{renderBooleanOption.call(this, isFile ? 'Load from file' : 'Use xml const', 'isFile')} | ||
{renderRadioGroup.call(this, 'Size Type', 'sizeType', SizeType, {isRow: true})} | ||
{size ? ( | ||
<Image source={isFile ? file : xml} width={size} height={size}/> | ||
) : ( | ||
<Image source={isFile ? file : xml}/> | ||
)} | ||
</View> | ||
); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,9 @@ | ||
/// <reference types="react" /> | ||
export interface SvgImageProps { | ||
data: any; | ||
} | ||
declare function SvgImage(props: SvgImageProps): JSX.Element | null; | ||
declare namespace SvgImage { | ||
var displayName: string; | ||
} | ||
export default SvgImage; |
This file contains hidden or 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 hidden or 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,2 @@ | ||
declare let SvgPackage: any; | ||
export default SvgPackage; |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,17 +1,28 @@ | ||
const {getDefaultConfig} = require('metro-config'); | ||
|
||
/** | ||
* Metro configuration for React Native | ||
* https://github.com/facebook/react-native | ||
* | ||
* @format | ||
*/ | ||
|
||
module.exports = { | ||
transformer: { | ||
getTransformOptions: async () => ({ | ||
transform: { | ||
experimentalImportSupport: false, | ||
inlineRequires: false, | ||
}, | ||
}), | ||
}, | ||
}; | ||
module.exports = (async () => { | ||
const { | ||
resolver: {sourceExts, assetExts} | ||
} = await getDefaultConfig(); | ||
return { | ||
transformer: { | ||
getTransformOptions: async () => ({ | ||
transform: { | ||
experimentalImportSupport: false, | ||
inlineRequires: false | ||
} | ||
}), | ||
babelTransformerPath: require.resolve('react-native-svg-transformer') | ||
}, | ||
resolver: { | ||
assetExts: assetExts.filter(ext => ext !== 'svg'), | ||
sourceExts: [...sourceExts, 'svg'] | ||
} | ||
}; | ||
})(); | ||
ethanshar marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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 hidden or 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,31 @@ | ||
import React from 'react'; | ||
import {SvgPackage} from '../../optionalDependencies'; | ||
const SvgXml = SvgPackage?.SvgXml; | ||
// const SvgProps = SvgPackage?.SvgProps; TODO: not sure how (or if) we can use their props | ||
|
||
export interface SvgImageProps { | ||
data: any; // TODO: I thought this should be string | React.ReactNode but it doesn't work properly | ||
} | ||
|
||
function SvgImage(props: SvgImageProps) { | ||
const {data, ...others} = props; | ||
|
||
if (!SvgXml) { | ||
// eslint-disable-next-line max-len | ||
console.error(`RNUILib Image "svg" prop requires installing "react-native-svg" and "react-native-svg-transformer" dependencies`); | ||
return null; | ||
} | ||
|
||
if (typeof data === 'string') { | ||
return <SvgXml xml={data} {...others}/>; | ||
} else if (data) { | ||
const File = data; // Must be with capital letter | ||
return <File {...others}/>; | ||
} else { | ||
return null; | ||
} | ||
ethanshar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
SvgImage.displayName = 'IGNORE'; | ||
|
||
export default SvgImage; |
This file contains hidden or 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 hidden or 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,7 @@ | ||
let SvgPackage: any; | ||
try { | ||
SvgPackage = require('react-native-svg'); | ||
// Sadly we cannot verify with require('react-native-svg-transformer'); | ||
} catch (error) {} | ||
|
||
export default SvgPackage; |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.