Comments (9)
Hey there! I made a looooooot of tests and found a solution at last. That's not the best and it's not very sexy but at least it works.
I can't use lightbox or anything like that, I can't use RichText either to display images because when I click on it, I will always have the error message "Not showing lightbox because no image(s) was supplied".
But if I add my images as an attached assets instead of inside my content and wrap it in a , it seems to work. I thought it may interest you :)
<RichText
content={unipopia.node.contenu.raw}
renderers={{
img: ({
src, width, height, altText,
}) => (
<a href={`https://media.graphassets.com/${unipopia.node.images}`}>
<img
src={src}
alt={altText}
width={width}
height={height}
/>
</a>
),
}}
/>
from rich-text.
Hey! No worries. I never used lightroom, but I helped someone with a different library in the past, and it worked. See #71
from rich-text.
Here's an example on how to use it with react-modal-image. I hope it helps!
https://codesandbox.io/s/eager-kirch-2c2m3t?file=/src/App.tsx
from rich-text.
#reopen
Hey there! Thanks a lot for your help @jpedroschmitz but it still doesn't work T____T It drives me crazy!
Maybe I have a mistake somewere else in my code? I'm quite new to coding so... here is my code with yours :
// React imports
import { useState } from 'react';
import PropTypes from 'prop-types';
import { RichText } from '@graphcms/rich-text-react-renderer';
import Lightbox from 'react-awesome-lightbox';
import 'react-awesome-lightbox/build/style.css';
import './style.scss';
function PostCard({ posts }) {
const unipopiaPosts = posts.filter((post) => post.node.categories.some((category) => category.nom === 'Unipopia'));
const [clickedImage, setClickedImage] = useState('');
return (
<article className="post__block">
{unipopiaPosts.map((unipopia) => (
<div key={unipopia.node.slug}>
<div className="post__title">
{unipopia.node.titre}
</div>
<div className="post__excerpt">
{unipopia.node.extrait}
</div>
<div className="post__content">
<RichText
content={unipopia.node.contenu.raw}
renderers={{
image: ({ node }) => {
const imageUrl = node.children[0].src;
return (
<img
alt={node.children[0].title}
src={imageUrl}
height={node.children[0].height}
width={node.children[0].width}
onClick={() => setClickedImage(imageUrl ?? '')}
/>
);
},
}}
/>
{clickedImage && (
<Lightbox
large={clickedImage}
title="Image Title"
onClose={() => setClickedImage(null)}
/>
)}
</div>
</div>
))}
</article>
);
}
PostCard.propTypes = {
posts: PropTypes.arrayOf(
PropTypes.shape({
node: PropTypes.shape({
slug: PropTypes.string.isRequired,
titre: PropTypes.string.isRequired,
extrait: PropTypes.string,
auteur: PropTypes.shape({
nom: PropTypes.string.isRequired,
}).isRequired,
contenu: PropTypes.shape({
raw: PropTypes.shape({
children: PropTypes.array.isRequired,
}).isRequired,
}).isRequired,
}).isRequired,
}),
).isRequired,
};
export default PostCard;
Do you see anything that is wrong by any chance?
from rich-text.
(sorry, the "code" mode didn't work)
from rich-text.
I also tried with ModalImage and cleaned my code but style doesn't work. the weird thing is if I add a console.log to see if the onClick works, it shows nothing.
And if I look at my img src, it looks like this : src="https://media.graphassets.com/resize=width:1754,height:2480/7huO5IwToKhhvYUWcWVA"
Maybe it's because of this type of url?
My component with ModalImage just in case :
import { useState } from 'react';
import PropTypes from 'prop-types';
import { RichText } from '@graphcms/rich-text-react-renderer';
import ModalImage from 'react-modal-image';
import './style.scss';
function PostCard({ posts }) {
const unipopiaPosts = posts.filter((post) => post.node.categories.some((category) => category.nom === 'Unipopia'));
const [clickedImage, setClickedImage] = useState(null);
return (
<article className="post__block">
{unipopiaPosts.map((unipopia) => (
<div key={unipopia.node.slug}>
<div className="post__title">
{unipopia.node.titre}
</div>
<div className="post__excerpt">
{unipopia.node.extrait}
</div>
<div className="post__content">
<RichText
content={unipopia.node.contenu.raw}
renderers={{
image: ({ node }) => (
<div>
<ModalImage
alt={node.title}
small={node.src}
large={node.src}
hideZoom
onClick={() => setClickedImage(node.src)}
/>
</div>
),
}}
/>
{clickedImage && (
<ModalImage
alt="Clicked Image"
small={clickedImage}
large={clickedImage}
onClose={() => setClickedImage(null)}
/>
)}
</div>
</div>
))}
</article>
);
}
PostCard.propTypes = {
posts: PropTypes.arrayOf(
PropTypes.shape({
node: PropTypes.shape({
slug: PropTypes.string.isRequired,
titre: PropTypes.string.isRequired,
extrait: PropTypes.string,
auteur: PropTypes.shape({
nom: PropTypes.string.isRequired,
}).isRequired,
contenu: PropTypes.shape({
raw: PropTypes.shape({
children: PropTypes.array.isRequired,
}).isRequired,
}).isRequired,
}).isRequired,
}),
).isRequired,
};
export default PostCard;
from rich-text.
The problem with your code is that you're rendering the ModalImage
component twice.
You're <RichText />
component should look like this:
<RichText
content={unipopia.node.contenu.raw}
renderers={{
img: ({ src, width, height, altText }) => (
<div>
<img
src={src}
alt={altText}
width={width}
height={height}
onClick={() => setClickedImage(src ?? "")}
/>
</div>
)
}}
/>
If you look at your code, you're using image
instead of img
. You're also rendering ModalImage
instead of an image. Please take a look at the example I shared!
from rich-text.
Hey there! Sorry to bother you with that, I'm very new to all of this.
I fixed my code and mostly copy/past yours.
Image seens to be clickable now but I have another error in my console, now from Lightbow itself.
Not showing lightbox because no image(s) was supplied
at s (http://localhost:8080/js/main.52bd1cb363c583eab43e.js:17367:3086)
at div
at div
at article
at PostCard (http://localhost:8080/js/main.52bd1cb363c583eab43e.js:6602:20)
at div
at Container (http://localhost:8080/js/main.52bd1cb363c583eab43e.js:6096:23)
at div
at Page (http://localhost:8080/js/main.52bd1cb363c583eab43e.js:6493:23)
at Unipopia (http://localhost:8080/js/main.52bd1cb363c583eab43e.js:7439:67)
at RenderedRoute (http://localhost:8080/js/main.52bd1cb363c583eab43e.js:61205:5)
at Routes (http://localhost:8080/js/main.52bd1cb363c583eab43e.js:61826:5)
at div
at App
at Router (http://localhost:8080/js/main.52bd1cb363c583eab43e.js:61764:15)
at BrowserRouter (http://localhost:8080/js/main.52bd1cb363c583eab43e.js:59827:5)
Maybe I'm just lost and should use something easier for a begginer :/
just in case you still have some times to waste, my new component:
import { useState } from 'react';
import PropTypes from 'prop-types';
import { RichText } from '@graphcms/rich-text-react-renderer';
import Lightbox from 'react-awesome-lightbox';
import 'react-awesome-lightbox/build/style.css';
import './style.scss';
function PostCard({ posts }) {
const unipopiaPosts = posts.filter((post) => post.node.categories.some((category) => category.nom === 'Unipopia'));
const [clickedImage, setClickedImage] = useState('');
return (
{unipopiaPosts.map((unipopia) => (
{unipopia.node.titre}
{unipopia.node.extrait}
{console.log('Contenu brut :', unipopia.node.contenu.raw)}
<RichText
content={unipopia.node.contenu.raw}
renderers={{
img: ({
src, width, height, altText,
}) => (
<img
src={src}
alt={altText}
width={width}
height={height}
onClick={() => setClickedImage(src ?? '')}
/>
),
}}
/>
{clickedImage && (
<Lightbox
large={clickedImage}
title="Image Title"
onClose={() => setClickedImage(null)}
/>
)}
))}
);
}
export default PostCard;
from rich-text.
No worries at all. I'm glad it's working, but I'm unsure what's causing this error on the console. Looking at your code, everything seems to be okay.
from rich-text.
Related Issues (20)
- Accessing image properties from renderer isn't working. HOT 1
- Add anchor tag support for Page Navigation HOT 3
- Update @graphcms/html-to-slate-ast to use newer versions of Slate HOT 3
- <p> tag className formatting "overwrites" <a> tag ClassName formatting HOT 2
- Import element fuctions to reuse on my own code HOT 3
- Video Props typings are not correct HOT 5
- Error: Cannot find module './xhr-sync-worker.js' HOT 7
- ClientError: expected RichText raw value to be object: HOT 1
- Newline added by astToHtmlString for link object
- <p></p> returned in empty rich text box
- Feature request: Allow assign class name directly to image in rich text editor
- GraphCMS Marcom website says to use children as prop HOT 1
- React duplicate key issue HOT 3
- [help wanted] How to zoom an image rendered by rich-text-renderer with react-awesome-lightbox library HOT 1
- Is there a way to render iFrame? HOT 1
- Link is not recognized HOT 3
- Images are being parsed as links HOT 1
- RichText rendering nested li's causing nextjs hydration errors HOT 2
- Link embed with Assets not working HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rich-text.