How to use the svg fill url in react - reactjs

Here's the code:
<div className="flex flex-wrap -mx-2 mb-8">
{props &&
props.item.map((data, i) => (
<div
key={i}
className="w-full md:w-1/2 lg:w-1/4 px-2 mb-4 flex rounded justify-center items-center p-2 m-1"
>
<h1>abc</h1>
<svg
xmlns="http://www.w3.org/2000/svg"
width="100px"
height="140"
fill={url(`#color-${i}`)}
viewBox="0 0 30 45"
aria-hidden="true"
style={{ transform: `translate(0, -20px)` }}
>
<defs>
<linearGradient id={`color-${i}`} x1="0%" y1="100%" x2="0%" y2="0%">
<stop offset={`${min(7, threshold)}%`} stop-color="rgb(40, 134, 248)" />
<stop offset={`${data.humidity}%`} stop-color="rgb(255, 0, 0)" />
<stop stop-color="rgb(227, 227, 227)" />
</linearGradient>
</defs>
<path stroke="#2886f8" d="M15 6
Q 15 6, 25 18
A 12.8 12.8 0 1 1 5 18
Q 15 6 15 6z" />
</svg>
</div>
))}
</div>
What I'm trying to do here is to fill the linearGradient. but the problem is when I try to do this:
<svg fill={url(#color-${i})}>
it doesn't work. the error is Cannot find name 'url'. the choices to import is the import { url } from 'inspector';.
cause the svg it looks like this.

You should interpolate the string templates correctly.
fill={`url(#color-${i})`}
The url is a reference to another identifier in the SVG, in this case, the linear gradient
and
offset={`${Math.min(7, data.threshold)}%`}
min alone isn't a function, be sure to fully quantify it by calling Math.min
Code
<div className="flex flex-wrap -mx-2 mb-8">
{props.item.map((data, i) => (
<div
key={i}
className="w-full md:w-1/2 lg:w-1/4 px-2 mb-4 flex rounded justify-center items-center p-2 m-1"
>
<h1>abc</h1>
<svg
xmlns="http://www.w3.org/2000/svg"
width="100px"
height="140"
fill={`url(#color-${i})`}
viewBox="0 0 30 45"
aria-hidden="true"
style={{ transform: `translate(0, -20px)` }}
>
<defs>
<linearGradient
id={`color-${i}`}
x1="0%"
y1="100%"
x2="0%"
y2="0%"
>
<stop
offset={`${Math.min(7, data.threshold)}%`}
stop-color="rgb(40, 134, 248)"
/>
<stop
offset={`${data.humidity}%`}
stop-color="rgb(255, 0, 0)"
/>
<stop stop-color="rgb(227, 227, 227)" />
</linearGradient>
</defs>
<path
stroke="#2886f8"
d="M15 6
Q 15 6, 25 18
A 12.8 12.8 0 1 1 5 18
Q 15 6 15 6z"
/>
</svg>
</div>
))}
</div>

Related

How to make the footer not take up half of the page?

So I have this svg as the footer and when I check in the developer tools it shows that it is taking up half of the page. I want the svg to only contain in the expected space without taking up extra space in the page since other elements cannot be positioned due to this issue.
<footer className='absolute bottom-0 w-full'>
<svg
className='fill-green'
viewBox='0 0 500 150'
preserveAspectRatio='none'
xmlns='http://www.w3.org/2000/svg'
>
<defs>
<filter id='shadow'>
<feDropShadow
dx='0'
dy='0'
stdDeviation='2'
flood-color='#159F68'
/>
</filter>
<path
id='footer-shadow'
className='stroke-none'
d='M29.86,179.11 C100.86,200.36 200.94,60.48 450.84,180.09 L363.96,855.44 L0.00,190.00 Z'
></path>
</defs>
<g>
<use href='#footer-shadow' filter='url(#shadow)'></use>
<text
className='fill-modern-green text-sm'
x='52%'
y='95%'
dominant-baseline='middle'
text-anchor='middle'
>
All rights reserved.
</text>
</g>
</svg>
</footer>
You can define width and height through the styles attribute for footer and svg. For example:
{/* Height of footer is 30px */}
<footer className='absolute bottom-0 w-full' style={{width: '100%', height: '30px'}}>
{/* SVG Height based on the footer height */}
<svg
style={{width: '100%', height: '100%'}}
className='fill-green'
viewBox='0 0 500 150'
preserveAspectRatio='none'
xmlns='http://www.w3.org/2000/svg'
>
<defs>
<filter id='shadow'>
<feDropShadow
dx='0'
dy='0'
stdDeviation='2'
flood-color='#159F68'
/>
</filter>
<path
id='footer-shadow'
className='stroke-none'
d='M29.86,179.11 C100.86,200.36 200.94,60.48 450.84,180.09 L363.96,855.44 L0.00,190.00 Z'
></path>
</defs>
<g>
<use href='#footer-shadow' filter='url(#shadow)'></use>
<text
className='fill-modern-green text-sm'
x='52%'
y='95%'
dominant-baseline='middle'
text-anchor='middle'
>
All rights reserved.
</text>
</g>
</svg>
</footer>

Adjusting number of cards per row

I would like to know how to define number of cards per row. I want to have 4 cards maximum in a row currently all new cards are added into the row.
It's whole bootstrap.
import Card from "react-bootstrap/Card";
import { Link } from 'react-router-dom';
// import DetailComponent from "../Detail/Detail";
import "./ListingCard.css";
export default function ListingCardComponent({handleShow, property}) {
return (
<div className="container">
<div class="col">
<Card style={{ height: "30rem", width: "18rem" }} onClick={handleShow} className="box">
{/* <a href="/user/listingdetail"> */}
<Link href="/user/listingdetail" params={property._id}>
<Card.Img variant="top" src={property.image} height="255px" />
<Card.Body>
<Card.Title>{property.address}</Card.Title>
<Card.Subtitle className="mb-2 text-muted">Price:${property.rent}</Card.Subtitle>
<Card.Text>Bedroom {property.bedrooms} Bathroom {property.bathrooms}</Card.Text>
</Card.Body>
</Link>
{/* </a> */}
<button type="button" class="btn btn-default btn-lg">
<svg
xmlns="http://www.w3.org/2000/svg"
width="40"
height="40"
fill="currentColor"
class="bi bi-bookmark-heart"
viewBox="0 0 16 16"
>
<path
fill-rule="evenodd"
d="M8 4.41c1.387-1.425 4.854 1.07 0 4.277C3.146 5.48 6.613 2.986 8 4.412z"
/>
<path d="M2 2a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v13.5a.5.5 0 0 1-.777.416L8 13.101l-5.223 2.815A.5.5 0 0 1 2 15.5V2zm2-1a1 1 0 0 0-1 1v12.566l4.723-2.482a.5.5 0 0 1 .554 0L13 14.566V2a1 1 0 0 0-1-1H4z" />
</svg>
</button>
</Card>
{/* <DetailComponent property={property}/> */}
</div>
</div>
);
}

What's the best way go about animating this SVG?

I've been scrolling through looking various dev pages, many many answers but I think I'm missing the entire concept. It gets very technical and I'm at a loss for understanding. Most the pages I've read turn up things on the burger icon itself - which is what it is just broken down; so really I'm just at a lost for how to clone that. Strokes and Stroke-disarrays... The following is the SVG component (Open Icon):
export default function OpenIcon() {
return (
<svg
width="24"
height="20"
viewBox="0 0 24 20"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M0.800049 1.9999C0.800049 1.11625 1.51639 0.399902 2.40005 0.399902H21.6C22.4837 0.399902 23.2001 1.11625 23.2001 1.9999C23.2001 2.88356 22.4837 3.5999 21.6 3.5999H2.40005C1.51639 3.5999 0.800049 2.88356 0.800049 1.9999Z"
fill="#c91249"
/>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M0.800049 9.9999C0.800049 9.11625 1.51639 8.3999 2.40005 8.3999H21.6C22.4837 8.3999 23.2001 9.11625 23.2001 9.9999C23.2001 10.8836 22.4837 11.5999 21.6 11.5999H2.40005C1.51639 11.5999 0.800049 10.8836 0.800049 9.9999Z"
fill="#c91249"
/>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M0.800049 17.9999C0.800049 17.1162 1.51639 16.3999 2.40005 16.3999H21.6C22.4837 16.3999 23.2001 17.1162 23.2001 17.9999C23.2001 18.8836 22.4837 19.5999 21.6 19.5999H2.40005C1.51639 19.5999 0.800049 18.8836 0.800049 17.9999Z"
fill="#c91249"
/>
</svg>
);
}
Standard. Non-descript (intense descriptions lol). It is called under <OpenFocusTrap> on a MobileNavagation.jsx to operate as an App. It is then likewise displayed on a normal Header.jsx (see following):
MobileNavagation.jsx
<OpenFocusTrap>
<button
type="button"
className="flex justify-center items-center w-7 h-full"
onClick={() => setIsOpen((previousIsOpen) => !previousIsOpen)}
>
<span className="sr-only">{isOpen ? 'Close' : 'Open'} Menu</span>
{isOpen ? <CloseIcon /> : <OpenIcon />}
</button>.........
/* Additionally */
function CloseIcon() {
return (
<svg
width="18"
height="18"
viewBox="0 0 18 18"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M1 17L17 1M1 1L17 17"
stroke="#c91249"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
);
}
and...
Header.jsx
<Headroom>
<header className="h-14 lg:h-15" role="banner">
<div
className={`fixed z-20 h-14 lg:h-15 w-full bg-realative px-3 md:px-4 md:py-2 lg:pt-2 lg:pb-0 mx-auto ${
isMobileNavOpen ? '' : 'bg-opacity-95'
}`}
> ...........
They're cut short, didn't think the full pages necessary. If anyone have any solid sources for animating the paths? And/or a barney style explanation as to how...
Thanks for the assistance.
PS. Current "index.css" for <header> is as follows:
header {
position: sticky;
transition-property: all;
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1);
transition-duration: 500ms;
background-color: #191c1b;
top: 0;
left: 0;
height: 80px;
width: 100%;
margin-bottom: 10px;
z-index: 1;
font-family: 'Times New Roman', Times, serif;
}
#layer header {
.logo {
letter-spacing: -.16em;
}
.shadow {
box-shadow: 0 9px 9px -9px rgba(0, 0, 0, 0.13);
}
.hidden {
transform: translateY(-110%);
}
Note: Some details that might matter...
I'm using VSCode, WSL as a workspace for Ubuntu.
"Prettier" and "Tailwind" are included within.
"Hydrogen" is the app.
Yarn and github are used primary for repository downloads (npm gives me lip)

SVG icon doesn`t load

Some svg icons in my project are displayed and some dont. Heres an example of one that has issue
<svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" id="windows">
<defs>
<linearGradient id="windows_svg__b" x1="0%" x2="100%" y1="0%" y2="100%">
<stop offset="0%" stop-color="#FFF" stop-opacity="0.1"></stop>
<stop offset="100%" stop-color="#0087C7"></stop>
</linearGradient>
<path id="windows_svg__a"
d="M6.5 14.62l-5.656-.892A1 1 0 010 12.74V8.288h6.5v6.333zm1 .159V8.288H16v6.662a1 1 0 01-1.156.988L7.5 14.778zm-1-13.367v5.876H0V3.241a1 1 0 01.853-.989l5.647-.84zm1-.148L14.853.17A1 1 0 0116 1.16v6.128H7.5V1.264z">
</path>
</defs>
<g fill="none" fill-rule="evenodd">
<use fill="#0087C7" xlink:href="#windows_svg__a"></use>
<use fill="url(#windows_svg__b)" fill-opacity="0.8" xlink:href="#windows_svg__a"></use>
</g>
</svg>
https://codesandbox.io/s/practical-satoshi-o68rnt
I guess that`s something with tags but not sure and have no idea how to fix it
There are 2 issues with your code first issue is with your SVG have some issues please use the below one.
`<svg width="16" height="16" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" id="windows">
<defs>
<linearGradient id="windows_svg__b" x1="0%" x2="100%" y1="0%" y2="100%">
<stop offset="0%" stop-color="#FFF" stop-opacity="0.1"></stop>
<stop offset="100%" stop-color="#0087C7"></stop>
</linearGradient>
<path id="windows_svg__a"
d="M6.5 14.62l-5.656-.892A1 1 0 010 12.74V8.288h6.5v6.333zm1 .159V8.288H16v6.662a1 1 0 01-1.156.988L7.5 14.778zm-1-13.367v5.876H0V3.241a1 1 0 01.853-.989l5.647-.84zm1-.148L14.853.17A1 1 0 0116 1.16v6.128H7.5V1.264z">
</path>
</defs>
<g fill="none" fill-rule="evenodd">
<use fill="#0087C7" href="#windows_svg__a"></use>
<use fill="url(#windows_svg__b)" fill-opacity="0.8" href="#windows_svg__a"></use>
</g>
</svg>`
Second, you have to import that image first, and then you can use it in your code.
import "./styles.css";
import icon from"../public/icons/icon.svg";
export default function App() {
return (
<div className="App">
<img src={icon} alt="sorry" />
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
Try to use
import { ReactComponent as Icon } from "path/icon.svg";
And then you can use it as component in React
<div>
<Icon />
</div>

SVG stroke not showing inside of clipPath

I have the following Codepen, where I'm trying to animate a stroke of a circle around an image.
So far, I've got a circle SVG which is clipping an image, but it doesn't show the stroke inside of clipPath.
How do I get the border to show?
class App extends React.Component {
render() {
return (
<svg width='48' height='48'>
<defs>
<clipPath id='circleView'>
<circle cx='24' cy='24' r='23' fill='none' stroke='red' strokeWidth='2' />
</clipPath>
</defs>
<image width='48' height='48' xlinkHref={'https://source.unsplash.com/random'} clipPath='url(#circleView)' />
</svg>
)
}
}
As Robert said, the child SVG figures in the clip-path line are not displayed.
Therefore, for the animation you need to add another circle outside the clip-path
<circle cx="25" cy="24" r="14" fill="none" stroke="red" strokeWidth="2" />
.container {
width:25%;
height:25%;
}
<div class="container">
<svg viewBox="0 0 48 48" >
<defs>
<clipPath id='circleView'>
<circle cx='24' cy='22' r='16' fill='none' stroke='red' stroke-width='2' />
</clipPath>
</defs>
<image width="100%" height="100%" xlink:href='https://i.stack.imgur.com/O9eO8.jpg'
clip-path='url(#circleView)' />
<circle cx='24' cy='22' r='16' fill='none' stroke='red' strokeWidth='2' />
</svg>
</div>
To animate a circle, use the change in the stroke-dashoffset attribute from maximum to zero. values="(100.48;0)"
Animation starts after click
.container {
width:25%;
height:25%;
}
<div class="container">
<svg id="svg1" viewBox="0 0 48 48">
<defs>
<clipPath id='circleView'>
<circle cx='24' cy='22' r='16' fill='none' stroke='red' stroke-width='2' />
</clipPath>
</defs>
<image width="100%" height="100%" xlink:href='https://i.stack.imgur.com/O9eO8.jpg' clip-path='url(#circleView)' />
<circle transform="rotate(-90 24 22)" cx="24" cy="22" r="16" fill='none' stroke='red' strokeWidth='2'
stroke-dasharray="100.48" stroke-dashoffset="100.48" >
<animate
attributeName="stroke-dashoffset"
dur="1s"
begin="svg1.click"
values="100.48;0"
fill="freeze"/>
</circle>
</svg>
</div>
Variant of animation with CSS
I added transparency animation to the circle animation.
The animation begins when you hover the mouse cursor.
.container {
width:25%;
height:25%;
}
#crc1 {
fill:skyblue;
stroke-width:1;
stroke:red;
stroke-dasharray:100.48;
stroke-dashoffset:100.48;
fill-opacity:0.9;
}
#crc1:hover {
animation: dash 1.5s ease-out forwards;
}
#keyframes dash {
0% { stroke-dashoffset: 100.48; fill-opacity:0.9; }
50% { fill-opacity:0.45;}
100% { stroke-dashoffset: 0; fill-opacity:0; }
}
#img1 {
clip-path: url(#circleView);
}
<div class="container">
<svg id="svg1" viewBox="0 0 48 48">
<defs>
<clipPath id='circleView'>
<circle cx='24' cy='22' r='16'/>
</clipPath>
</defs>
<image width="100%" height="100%" xlink:href='https://i.stack.imgur.com/O9eO8.jpg'
clip-path='url(#circleView)' />
<circle id="crc1" cx="24" cy="22" r="16" />
</svg>
</div>

Resources