I'm currently working on a react component that aims to replicate the functionality of Wikipedia's page preview feature minus the text. Upon hovering over the link, I want the image to appear either above or below the text.
The ImgCard
component I've created includes an a
link that displays the image when you hover over it. However, I feel like the component may be too complex, so any suggestions for simplification would be appreciated. The example below utilizes TailwindCSS.
Some issues encountered:
- If there isn't enough space, the image doesn't render properly. For instance, hovering over the
spongebob 5
link showcases this issue. It would be ideal if the component could intelligently determine when to display the image above the text in such cases. - The images consistently appear on the left side.
How can these challenges be best addressed?
const {useState} = React;
function ImgCard({ src, alt, text }) {
const [isShown, setIsShown] = useState(false);
return (
<React.Fragment>
<a
href={src}
className="text-blue-600 visited:text-purple-600"
onMouseEnter={() => setIsShown(true)}
onMouseLeave={() => setIsShown(false)}
>
{text}
</a>
{isShown && (
<img
className="max-w-xs rounded overflow-hidden shadow-lg absolute"
src={src}
alt={alt}
/>
)}
</React.Fragment>
);
}
function Page(){
return(
<div className="max-w-xl mx-auto px-8">
<h1 className="text-2xl text-gray-900 font-semibold">The Best Lorem Ever
</h1>
<div className="grid grid-cols-1 gap-4">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit <ImgCard text="spongebob 1" src="https://i.imgur.com/TYHtyCe.png" />. Nam imperdiet magna quis consectetur gravida. Nam maximus consectetur rhoncus. Nulla facilisi. Ut convallis risus at odio euismod, <ImgCard text="spongebob 2" src="https://i.imgur.com/tnOglmb.png" /> sodales tincidunt augue bibendum. Sed hendrerit arcu ut ipsum mattis, id eleifend sem dictum. Etiam finibus elementum vulputate. Suspendisse nec sem ex.
</p>
<p>
<ImgCard text="spongebob 3" src="https://i.imgur.com/m8oAgs8.jpg" /> in porttitor sapien, ac egestas libero. Suspendisse potenti. Aliquam sed tempus ligula. Praesent efficitur aliquam varius. Proin ex libero, hendrerit sit amet massa ac, mollis semper nulla. Vestibulum ultrices rhoncus metus, vel pellentesque erat iaculis in. Duis ut ligula in lacus volutpat mattis maximus nec nunc. Sed euismod tortor non mauris porta fringilla. Cras aliquam quis odio sit amet dapibus. In et eros venenatis, interdum dui at, accumsan mauris. Pro...
</p>
<p>
<ImgCard text="spongebob 4" src="https://i.imgur.com/vYCijFL.jpg" /> Mauris vitae iaculis turpis, nec sodales diam. Duis euismod, velit tincidunt laoreet porta, sem nulla lobortis tellus, non fringilla ipsum mauris et massa. Pellentesque vel ante sem. Integer ut mauris aliquet dolor auctor porttitor ut eget sem. Vestibulum egestas tellus ut mi dignissim fringilla. Phasellus ut gravida quam, nec rutrum lectus. Fusce ut volutpat diam, vitae dignissim enim. Vivamus dapibus nunc eu neque porta, ut luct...
</p>
</div>
</div>
)
}
// Render it
ReactDOM.render(
<Page />,
document.getElementById("react")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react"></div>
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet">