-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for svg in StrapiImage
- Loading branch information
1 parent
077070e
commit b572885
Showing
4 changed files
with
67 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,29 +1,41 @@ | ||
import { CLIENT_STRAPI_URL } from "@/strapi"; | ||
import Image from "next/image"; | ||
import SVG from "react-inlinesvg"; | ||
|
||
/** | ||
* Display an Image fetched from the Strapi instance | ||
* @param img object returned by Strapi when querying the image. | ||
* @param size the prepared size of the image (one of "thumbnail", "small", "medium", "large", "origin") | ||
* @param size the prepared size of the image (one of "thumbnail", "small", "medium", "large", "origin"). This parameter is ignored for SVGs | ||
*/ | ||
export default function StrapiImage({ | ||
img, | ||
size, | ||
className, | ||
}: { | ||
img: any; | ||
size: "thumbnail" | "small" | "medium" | "large" | "origin"; | ||
className?: string; | ||
}) { | ||
let selectedFormat = | ||
size === "origin" ? img.data.attributes : img.data.attributes.formats[size]; | ||
return ( | ||
<> | ||
<Image | ||
alt={selectedFormat.name} | ||
src={selectedFormat.url} | ||
width={selectedFormat.width} | ||
height={selectedFormat.height} | ||
loader={(p) => `${CLIENT_STRAPI_URL}${p.src}`} | ||
/> | ||
</> | ||
); | ||
let image = img.data.attributes; | ||
|
||
if (image.ext !== ".svg") { | ||
let selectedFormat = size === "origin" ? image : image.formats[size]; | ||
|
||
return ( | ||
<> | ||
<Image | ||
alt={selectedFormat.name} | ||
src={selectedFormat.url} | ||
width={selectedFormat.width} | ||
height={selectedFormat.height} | ||
loader={(p) => `${CLIENT_STRAPI_URL}${p.src}`} | ||
className={className} | ||
/> | ||
</> | ||
); | ||
} else { | ||
return ( | ||
<SVG src={`${CLIENT_STRAPI_URL}${image.url}`} className={className} /> | ||
); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,32 @@ | ||
import Background from "@/assets/background.svg"; | ||
import CLICLogo from "@/assets/clic_color_info.svg"; | ||
import NavigationBar from "@/components/NavigationBar"; | ||
import StrapiImage from "@/components/StrapiImage"; | ||
import strapi from "@/strapi"; | ||
import { ApiAssociation } from "@/types/generated/contentTypes"; | ||
import { GetServerSideProps, InferGetServerSidePropsType } from "next"; | ||
|
||
export default function Home() { | ||
export default function Home(props: { association: ApiAssociation }) { | ||
return ( | ||
<div className="main"> | ||
<NavigationBar /> | ||
<div className="background"> | ||
<CLICLogo className="CLICLogo" /> | ||
<StrapiImage | ||
img={props.association.attributes.logo} | ||
size="medium" | ||
className="CLICLogo" | ||
/> | ||
<Background className="background" /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export const getServerSideProps: GetServerSideProps< | ||
InferGetServerSidePropsType<typeof Home> | ||
> = async (context) => { | ||
let association = await strapi.find<ApiAssociation>("association", { | ||
populate: "*", | ||
}); | ||
console.log(JSON.stringify(association)); | ||
return { props: { association: association.data } }; | ||
}; |