SVG with path fill="url(xxx)" not working in Nuxt.js - vue.js

The SVG (with animation) works perfect on .HTML file. But when I use it in Nuxt.js,
<path fill="url(#SVGID_82_)" ............. />
Everything tag that uses URL() for the fill attribute doesn't show up.
For example, the one with fill="#504D75" works fine, but the one with url(#SVGID_82_) is not visible.
This is how it should look like:
This is what it looks like in Nuxt.js:
This is part of the code:
<g>
<linearGradient
id="SVGID_82_"
gradientUnits="userSpaceOnUse"
x1="5022.084"
y1="484.3875"
x2="5051.5962"
y2="535.5038"
gradientTransform="matrix(-1 0 0 -1 5733.2881 656)"
>
<stop offset="0.2119" style="stop-color: #282447" />
<stop offset="0.2376" style="stop-color: #3f3b5f" />
<stop offset="0.2759" style="stop-color: #646085" />
<stop offset="0.2969" style="stop-color: #747195" />
</linearGradient>
<path
fill="url(#SVGID_82_)"
d="M695.75,129.58c0.73-0.4,1.81-0.48,2.7,0.14l25.91,14.94c1.18,0.68,2.14,2.34,2.14,3.7
l-0.09,30.43c0,0.68-0.24,1.15-0.63,1.37l-29.15,16.94c0.38-0.22,0.62-0.7,0.63-1.37l-0.3-30.46c0-1.36-0.81-2.61-2-3.29
l-26.24-15.67c-0.6-0.34-1.14-0.38-1.52-0.15L695.75,129.58z"
/>
<path
fill="#504D75"
d="M668.72,146.31c-1.18-0.68-2.15-0.13-2.15,1.23l-0.09,30.43c0,1.36,0.95,3.02,2.14,3.7l26.48,15.29
c1.18,0.68,2.15,0.13,2.15-1.23l0.09-30.43c0-1.36-0.95-3.02-2.14-3.7L668.72,146.31z"
/>
<g>
<path
fill="#282447"
d="M692.58,192.59l-21.68-12.52c-1.06-0.61-1.88-2.04-1.88-3.26l0.07-24.91c0-0.63,0.23-1.11,0.65-1.35
c0.42-0.24,0.95-0.2,1.51,0.12l-0.13,0.23l0.13-0.23l21.68,12.52c1.06,0.61,1.88,2.04,1.88,3.26l-0.07,24.91
c0,0.63-0.23,1.11-0.65,1.35c-0.18,0.1-0.38,0.15-0.59,0.15C693.21,192.87,692.9,192.77,692.58,192.59z M670.98,151.13
c-0.39-0.22-0.74-0.27-0.99-0.12c-0.25,0.14-0.38,0.46-0.38,0.9l-0.07,24.91c0,1.03,0.72,2.29,1.62,2.8l21.68,12.52
c0.39,0.22,0.74,0.27,0.98,0.12c0.25-0.14,0.38-0.46,0.38-0.9l0.07-24.91c0-1.03-0.72-2.29-1.62-2.8L670.98,151.13
L670.98,151.13z"
/>
</g>
</g>

I stumbled upon this issue once. Not sure if your use case is similar but should be.
Here, you do have an #SVGID_82_, and you are maybe having another element with this id (looping somewhere maybe), but you only can have one id per page in HTML. Hence, some are appearing while the other are not. Nonetheless to say that inspecting thanks to devtools is the best option so far here (try finding the missing paths).
As for my experience and solution.
worklife_wording.vue
<template>
<svg fill="none" viewBox="0 0 161 24">
<defs />
<path
fill="#32BFA3"
d="M.197 6.353a3.714 3.714 0 017.036-2.38l5.154 15.242a3.714 3.714 0 11-7.037 2.38L.197 6.352z"
/>
<path
fill="#FA9856"
fill-rule="evenodd"
d="M23.814 1.437a3.708 3.708 0 00-4.701 2.322l-1.328 3.919 3.867 11.434.044.14 4.441-13.113a3.708 3.708 0 00-2.323-4.702z"
clip-rule="evenodd"
/>
<path
:fill="`url(#prefix__prefix__paint${uniqueId}_linear)`" <------ here !
d="M8.724 6.145a3.714 3.714 0 117.037-2.38l5.153 15.242a3.714 3.714 0 11-7.036 2.379L8.724 6.145z"
/>
<path
fill="#144862"
d="M62....."
/>
<defs>
<linearGradient
:id="`prefix__prefix__paint${uniqueId}_linear`" <------- and here !
x1="7.584"
x2="22.122"
y1=".247"
y2="24.897"
gradientUnits="userSpaceOnUse"
>
<stop stop-color="#144862" />
<stop offset="1" stop-color="#3B7694" />
</linearGradient>
</defs>
</svg>
</template>
<script>
export default {
name: 'SvgWorklifeWording',
props: {
uniqueId: {
type: String,
default: '0',
},
},
}
</script>
I basically migrated my SVG into a .vue file and made it dynamic on the id part thanks to a prop. Then, I'm using it like this in my pages.
fancy-page.vue
<lazy-svg-unique-worklife-wording
unique-id="1"
></lazy-svg-unique-worklife-wording>
I do use native Nuxt lazy here and have a prefix of svg-unique for those specific component directory, on top of using nuxt-svg-loader.
Here is the config that I do use for those SVG in my nuxt.config.js components' section:
{
path: '~/assets/svg/unique_ids_needed',
prefix: 'svg-unique',
extensions: ['vue'],
},
That way, I do not have any collision when a unique ID is needed because I can basically loop on an array and display unique SVG urls thanks to v-for's index!
If my answer is not helping, I guess that we will need some reproduction on codesandbox to go further.

Related

SVG icon showing cutoff on android in react native

I am using lib react-native-svg to display SVG in my mobile app. In iOS, it is showing properly but on android, it shows cutoff. I will also attach a screenshot for reference, Can someone please help me how to solve this problem?
iOS Screenshot
Android Screenshot
SVG snap
export const SVGIcon = (props: SvgProps) => (
<Svg
width={170}
height={150}
style={{ marginRight: 12, marginTop: 1 }}
fill="none"
viewbox="0 0 46 46"
xmlns="http://www.w3.org/2000/svg"
{...props}>
<Mask
id="a"
style={{
maskType: 'alpha',
}}
maskUnits="userSpaceOnUse"
x={21}
y={7}
width={136}
height={135}>
<Rect
x={21.25}
y={7.25}
width={135.5}
height={134.5}
rx={19.75}
fill="#fff"
stroke="#FAB405"
strokeWidth={0.5}
/>
</Mask>
<G mask="url(#a)">
<Path d="M129.103 84.136h-84.34v31.103h84.34V84.136Z" fill="#3E3E3E" />
<Path
d="M105.994 130.267c9.806 0 17.756-7.957 17.756-17.773 0-9.815-7.95-17.772-17.756-17.772-9.806 0-17.755 7.957-17.755 17.772 0 9.816 7.95 17.773 17.755 17.773Z"
fill="#111"
/>
<Path
d="M105.994 122.295c5.408 0 9.792-4.388 9.792-9.801s-4.384-9.801-9.792-9.801c-5.407 0-9.791 4.388-9.791 9.801s4.384 9.801 9.791 9.801Z"
fill="#F1F1F1"
/>
<Path
d="M105.994 120.727c4.543 0 8.225-3.686 8.225-8.233 0-4.547-3.682-8.233-8.225-8.233-4.542 0-8.225 3.686-8.225 8.233 0 4.547 3.683 8.233 8.225 8.233Z"
fill="#BCBCBC"
/>
<Path
d="m-33.047 96.682 2.61 21.693H84.845s-1.305-21.301 13.84-26.398c15.144-5.096 30.68 5.358 28.46 25.614l16.189-.392s1.958.261 1.567-3.136c-.392-3.398 0-14.245 0-14.245l-1.567-1.96-1.828-20.648s-.652-3.397-8.616-5.75c-7.964-2.352-36.817-8.494-36.817-8.494s-2.611-1.046-3.394-2.222c-.784-1.176-15.798-26.267-15.798-26.267s-3.002-1.96-4.177-2.744c0 0-6.92-15.943-31.464-22.477C16.694 2.722-13.203.239-13.203.239h-4.047v93.045h-14.883l-.914 3.398Z"
fill="#F5A81F"
/>
<Path
d="M-33.047 96.682h84.73l4.178 6.534h30.811v18.034h-119.72l-3.133-24.568h3.134Z"
fill="#3E3E3E"
/>
<Mask
id="b"
style={{
maskType: 'alpha',
}}
maskUnits="userSpaceOnUse"
x={-33}
y={-1}
width={179}
height={120}>
<Path
d="m-32.917 96.42 2.611 21.694h115.28s-1.305-21.302 13.84-26.398c15.144-5.097 30.68 5.358 28.461 25.614l16.189-.392s1.958.261 1.566-3.137c-.391-3.398 0-14.244 0-14.244l-1.566-1.96-1.828-20.648s-.653-3.398-8.617-5.75-36.816-8.494-36.816-8.494-2.611-1.046-3.395-2.222c-.783-1.176-15.797-26.267-15.797-26.267s-3.003-1.96-4.178-2.744c0 0-6.92-15.944-31.464-22.478C16.825 2.46-13.072-.023-13.072-.023h-4.047v93.046h-14.884l-.914 3.398Z"
fill="#F5A81F"
/>
</Mask>
<G mask="url(#b)">
<Path
d="M144.769 103.216s-3.655-15.682-22.977-18.426c-19.323-2.745-30.681-.262-41.778 18.426l-2.35 14.636 69.325-1.045-2.22-13.591Z"
fill="#FFC533"
/>
</G>
<Path
d="M52.858 35h17.756L86.28 61.136 78.84 72.898h-25.98V35Z"
fill="#DEDEDC"
/>
<Path
d="M22.047 9.517v83.636"
stroke="#FFC533"
strokeWidth={0.25}
strokeMiterlimit={10}
/>
<Path
d="M22.047 101.909v12.807"
stroke="#1E1E1E"
strokeWidth={0.25}
strokeMiterlimit={10}
/>
<Path
d="M78.055 93.153H-12.81"
stroke="#FFC533"
strokeWidth={0.25}
strokeMiterlimit={10}
/>
<Path d="M78.055 112.494H-34.614v5.228h112.67v-5.228Z" fill="#111" />
<Mask
id="c"
style={{
maskType: 'alpha',
}}
maskUnits="userSpaceOnUse"
x={46}
y={24}
width={41}
height={50}>
<Path
d="m50.639 73.16-3.917-46.393.653-1.96 32.639 3.267 6.528 33.324-7.834 11.761h-28.07Z"
fill="#C4C4C4"
/>
</Mask>
<G mask="url(#c)">
<Path
d="m55.73 49.767 4.831 3.006c.783-.915 1.306-2.091 1.697-3.006 0-.13.13-.261.13-.523.393-.914.523-1.83.784-2.483 0-.13 0-.261.13-.261.131-.523-2.61-1.568-4.177-2.222-.522-.261-.914-.392-1.044-.522 0 0 .26.653.26 1.045 0 1.046-.26 1.83-.783 2.483-1.044 1.699-1.828 2.483-1.828 2.483Z"
fill="#FFAE73"
/>
<Path
d="M57.95 44.932s1.697 4.051 4.439 4.835c0-.13.13-.261.13-.523.392-.914.523-1.83.784-2.483-.392-.522-2.742-1.437-4.178-2.09-.783.13-1.175.26-1.175.26Z"
fill="#F9924F"
/>
<Path
d="M63.694 47.415s-6.136 1.83-7.18-2.091c-1.045-3.92-2.611-6.273 1.305-7.58 3.917-1.306 5.092 0 5.745 1.176 1.828 3.137.914 6.012.13 8.495Z"
fill="#FFAE73"
/>
<Path
d="M64.87 58.784s8.616 9.279 15.797 11.108c0 0 2.22 0 2.61-2.875.262-1.83-14.1-14.767-18.538-14.898-4.308-.13-1.175 4.705.13 6.665Z"
fill="#427C4D"
/>
<Path
d="M57.558 84.006c.523 1.437 1.436 2.483 2.611 2.875 1.306.522 3.134.522 5.092 0 2.35-.523 4.7-1.569 6.658-2.483 1.045-.523 1.959-1.176 2.48-1.568 2.743-2.222-7.18-4.836-5.613-12.546 3.133-15.551-3.133-17.25-4.308-18.818-2.22-2.744-8.747-.392-8.747-.392-1.175 1.699-2.09 3.92-2.35 7.318-.131 1.307 0 3.136.391 5.227.914 6.273 3.003 14.375 3.134 16.335 0 1.569.26 2.876.652 4.052Z"
fill="#759F7E"
/>
<Path
d="M55.992 47.938s-.523 2.874-.261 3.267c0 0 6.136-.654 9.008.522 0 0-1.306-2.352-2.48-2.875-1.045-.392-6.267-.914-6.267-.914ZM54.817 62.051c6.92 6.142 14.491-4.966 14.491-4.966-.653-1.045-1.697-1.83-3.003-2.352-3.263 1.568-7.31 4.313-8.486 4.705-.652.13-2.48 2.09-3.002 2.613Z"
fill="#427C4D"
/>
<Path
d="M55.861 52.12s-3.003 8.886-4.57-2.222c-.13-1.307 1.045-6.142.784-8.103-.261-2.09-1.567-3.79.783-5.358 2.22-1.568 2.22-2.221 3.656-2.613 1.436-.392 3.655.523 4.83.13 1.175-.392 5.484 4.313 3.134 5.75 0 0-2.48-2.613-3.525-.522-.392.784-1.828.261-1.567 1.176 1.567 7.58-2.089 11.761-3.525 11.761Z"
fill="#282828"
/>
<Path
d="M55.992 63.358s11.75-4.574 17.755-9.017c0 0 .914-2.222-.392-3.529-1.305-1.176-15.536.654-18.408 4.052-2.872 3.397-1.436 8.363 1.045 8.494Z"
fill="#759F7E"
/>
<Path
d="M73.356 36.699s.913-1.046.391-1.568c-.522-.523-1.175-1.7-.914-2.875.261-1.176 1.436-3.267 2.22-2.483.783.784.26 2.875.26 3.397 0 .523.523 1.046.393 1.96-.131.915-.653 1.96-.653 1.96l-1.697-.391Z"
fill="#FFAE73"
/>
<Path
d="m73.094 35.392-3.786 15.16 4.44 3.789s1.305-3.92 2.088-9.54c.914-6.011 0-8.364 0-8.364s-1.567-2.744-2.742-1.045Z"
fill="#759F7E"
/>
</G>
</G>
</Svg>
);
Your SVG has a couple of problems. One is that the viewBox is the wrong size for your picture. It is a lot smaller than the SVG contents. But since the SVG mostly displays, I don't think that's your main problem.
Your SVG uses mask-type: alpha masks. It might be that that is the problem. Maybe they are not properly supported on Android. Or perhaps the parsing and rendering of masks is a bit broken in the Android version of react-native-svg.
Things to try:
Those two masks don't need to be type alpha masks. Remove the style="mask-type: alpha" from both masks, and the SVG will still look the same. Maybe they'll also render correctly then.
Try is modifying your SVG to not use masks. One mask just creates the rounded rectangle outline shape of the icon. That could be changed to a clip. The other mask just cuts out the bottom arch from the bus fender. You could remove the mask by modifying the fender shape, and colour.

Svg Icon in expo react native appearing half or only a part of the icon

import * as React from 'react';
import { SvgXml } from 'react-native-svg';
export default function SvgComponent() {
const svgcode = `<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<g id="Iconly/Light-Outline/Profile">
<g id="Profile">
<g id="Group 3">
<mask id="mask0_33437_4900" style="mask-type:alpha" maskUnits="userSpaceOnUse" x="4" y="14" width="16" height="8">
<path id="Clip 2" fill-rule="evenodd" clip-rule="evenodd" d="M4 14.4961H19.8399V21.8701H4V14.4961Z" fill="white"/>
</mask>
<g mask="url(#mask0_33437_4900)">
<path id="Fill 1" fill-rule="evenodd" clip-rule="evenodd" d="M11.9209 15.9961C7.65988 15.9961 5.49988 16.7281 5.49988 18.1731C5.49988 19.6311 7.65988 20.3701 11.9209 20.3701C16.1809 20.3701 18.3399 19.6381 18.3399 18.1931C18.3399 16.7351 16.1809 15.9961 11.9209 15.9961M11.9209 21.8701C9.96188 21.8701 3.99988 21.8701 3.99988 18.1731C3.99988 14.8771 8.52088 14.4961 11.9209 14.4961C13.8799 14.4961 19.8399 14.4961 19.8399 18.1931C19.8399 21.4891 15.3199 21.8701 11.9209 21.8701" fill="black"/>
</g>
</g>
<g id="Group 6">
<mask id="mask1_33437_4900" style="mask-type:alpha" maskUnits="userSpaceOnUse" x="6" y="2" width="12" height="11">
<path id="Clip 5" fill-rule="evenodd" clip-rule="evenodd" d="M6.60986 2.00012H17.2299V12.6187H6.60986V2.00012Z" fill="white"/>
</mask>
<g mask="url(#mask1_33437_4900)">
<path id="Fill 4" fill-rule="evenodd" clip-rule="evenodd" d="M11.9209 3.42776C9.77989 3.42776 8.03789 5.16876 8.03789 7.30976C8.03089 9.44376 9.75989 11.1838 11.8919 11.1918L11.9209 11.9058V11.1918C14.0609 11.1918 15.8019 9.44976 15.8019 7.30976C15.8019 5.16876 14.0609 3.42776 11.9209 3.42776M11.9209 12.6188H11.8889C8.9669 12.6098 6.59989 10.2268 6.60989 7.30676C6.60989 4.38176 8.99189 1.99976 11.9209 1.99976C14.8489 1.99976 17.2299 4.38176 17.2299 7.30976C17.2299 10.2378 14.8489 12.6188 11.9209 12.6188" fill="black"/>
</g>
</g>
</g>
</g>
</svg>
`;
const Svg = () => (
<SvgXml
xml={svgcode}
width='set the width here'
height='set the height here'
/>
);
return <Svg />;
}
I am importing this and using it but the icon is appearing half what's the issue? The SVG is appearing properly in the browser. I have searched a lot but no help. Also tell if there is any other way to use svg in expo app
Not really sure but just a suggestion. Check if you have any other component getting rendered above your SVG.
Why I am guessing this is because since your phone is a relatively smaller device than your browser on a computer, any other component might get squashed thus forcing it to display above your SVG and you might be seeing it as if it's appearing half. On computer screens that might not be the case.
Let me know more about the issue so that I'm more clear about it.

hover on svg in cypress and test tooltip

Scenario:
I have something similar
<svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
<!-- Using g to inherit presentation attributes -->
<g fill="white" stroke="green" stroke-width="5">
<circle cx="40" cy="40" r="25" />
<circle cx="60" cy="60" r="25" />
</g>
</svg>
Hovering over svg will show a tooltip and I need to test whether tooltip is displayed and the content is correct.
Any suggestions? Tried trigger(mouseover) which in not working
cy.get(svg).trigger('focus')
worked for me

LinearGradient in react-native-svg not working

I am trying to use SVG in my react native project. This is the code for my component:
<Svg xmlns="http://www.w3.org/2000/svg" width="100%" height="507" viewBox="0 0 375 507" style={{position:'absolute', bottom:0}}>
<Defs>
<ClipPath id="a">
<Rect class="a" fill='#fff' stroke='#707070' width="375" height="507" transform="translate(0 160)" d="M0 0h375v507H0z"/>
</ClipPath>
<LinearGradient id="b" clipPath='url(#a)' x1="0.5" x2="-0.031" y2="1.477" gradientUnits="objectBoundingBox">
<Stop offset="0" stopColor="rgb(76,209,149)" />
<Stop offset="1" stopColor="rgb(170,221,100)" />
</LinearGradient>
</Defs>
<G class="b" transform="translate(0 -160)">
<Circle class="c" cx="334.249" cy="334.249" r="334.249" transform="translate(-146.354 164.646)" fill='url(#b)' />
</G>
</Svg>
The Output I'm getting is:
https://i.stack.imgur.com/mGaBg.png
I think #enxaneta is right, clip-path seems to not exist on react-native-svg please refer to the documentation, you may find on the docs here react-native-svg #LinearGradient
I think you should have to reference it like this:
<Rect class="a" fill="url(#yourGradientId)" stroke='#707070' width="375" height="507" transform="translate(0 160)" d="M0 0h375v507H0z"/>
where fill should be reference to your gradient id i.e. fill=url(#b)
I've used <Path> to achieve the results below
Note: I have achieve this by using two shapes on top of each other:
A component with background gradient using react-native-linear-gradient
And the LinearGradient from react-native-linear-gradient
Please refer to the codes below.
Code of example above
import Svg, { Path, Defs, LinearGradient, Stop } from 'react-native-svg';
import Gradient from 'react-native-linear-gradient'
const { width, height } = Dimensions.get('window')
<Gradient style={{height: height * .25,}}
colors={ ['#FFD080', 'red'] }
start={{ x: 0, y: 0}}
end={{ x:1, y: 1}}
locations={[0.18, 1, 1]}>
<Svg
height={height * .44}
width={width}
viewBox="0 0 1440 320"
style={{ position: 'relative', top: height * .069 }}
>
<Defs>
<LinearGradient id="path" x1="0" y1="0" x2="1" y2="1">
<Stop offset="0" stopColor="#FFD080" stopOpacity="1" />
<Stop offset="1" stopColor="red" stopOpacity="1" />
</LinearGradient>
</Defs>
<Path fill="url(#path)"
d="M0,96L48,133.3C96,171,192,245,288,229.3C384,213,480,107,576,74.7C672,43,768,85,864,133.3C960,181,1056,235,1152,234.7C1248,235,1344,181,1392,154.7L1440,128L1440,0L1392,0C1344,0,1248,0,1152,0C1056,0,960,0,864,0C768,0,672,0,576,0C480,0,384,0,288,0C192,0,96,0,48,0L0,0Z"/>
</Svg>
</Gradient>
I found a solution that instead of using the svgs having gradient,I converted the SVG into a Lottie file. that works great and as an extra advantage, we can transform the SVG into a simple animation :)

Import String and convert to JSX in Expo

I would like to import a file as a String. Edit it. Then convert it to a JSX element.
Specifically, one use-case would be to import an svg file. Edit it so that it's compatible with React Native. Then convert it to a JSX element that can be reused.
I can't seem to get a string from importing .svg files and I would prefer not to change it to .txt as other projects already use these files and we also need to keep things DRY.
How can I import this:
<svg xmlns="http://www.w3.org/2000/svg" width="24.25" height="26.52">
<defs>
<style>
.circle, .line {
stroke: #77bc1f;
stroke-linecap: round;
stroke-width: 3.48px;
}
.circle {
fill: none;
}
.line {
fill: #9f9;
}
</style>
</defs>
<title>Logo Icon</title>
<path class="circle" fill="none" d="m19.12166,7a10.27,10.27 0 1 1
-14,0"/>
<line class="line" fill="#9f9" x1="12.06166" y1="1.74"
x2="12.06166" y2="14.88"/>
</svg>
And convert it into this:
<Svg width={24.25} height={26.52}>
<Path
class="circle"
fill="none"
d="m19.12166,7a10.27,10.27 0 1 1 -14,0"
stroke="#77bc1f"
strokeLinecap="round"
strokeWidth="3.48px"
/>
<Line
class="line"
stroke="#77bc1f"
strokeLinecap="round"
strokeWidth="3.48px"
fill="#000"
x1="12.06166"
y1="1.74"
x2="12.06166"
y2="14.88"
/>
</Svg>
Dynamically?
Great question. I never thought about with react native. I know with react you can do dangerouslySetInnerHTML. Please let me know if this works for you with rwact-native.
If doesn't may be try this from
https://medium.com/#remarkablemark/an-alternative-to-dangerously-set-innerhtml-64a12a7c1f96
Now to import the svg, there are multiple options to do that
Option 1. Use react-native-remote-svg
For example
import Image from 'react-native-remote-svg';
const SVGComponent = '<svg width="48px" height="1px" viewBox="0 0 48 1" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><rect id="Rectangle-5" x="25" y="36" width="48" height="1"></rect></svg>';
<Image
source={{
uri: "data:image/svg+xml;utf8," + SVGComponent
}}/>
Option 2:
Use WebView
<WebView
source={{ html: svgstring }}
/>