πΊ A lightweight React component package that lets users click a preview image (or any other element like a button) and play a YouTube video inside an overlay. It provides a clean close interaction, simple props and is designed to be easy to drop into modern React projects.
npm install react-click-image-play-youtube-video- Click-to-open YouTube video overlay experience
- Smooth overlay show/hide behavior with close interaction
- Simple API with
embedLink,embedTitle, and optionalonCloseFn - React + TypeScript ready with typed component props
- Lightweight package footprint with minimal runtime dependencies
- Unit testing is implemented with Jest and React Testing Library to test core components and overlay behavior
Note: Always pass embedLink in this format: https://www.youtube.com/embed/<video-id>.
- Basic Usage
/* node modules */
import { useState } from 'react';
import { ReactYouTubeOverlay } from 'react-click-image-play-youtube-video';
/* component */
function App() {
/* local state */
const [openOverlay, setOpenOverlay] = useState(false);
return (
<>
<button type="button" onClick={() => setOpenOverlay(true)}>
Play YouTube Video
</button>
{openOverlay && (
<ReactYouTubeOverlay
embedLink="https://www.youtube.com/embed/dQw4w9WgXcQ"
embedTitle="Sample YouTube Video"
onCloseFn={() => setOpenOverlay(false)}
/>
)}
</>
);
}
/* exports */
export default App;- Open Overlay from an Image Click
/* node modules */
import { useState } from 'react';
import { ReactYouTubeOverlay } from 'react-click-image-play-youtube-video';
/* component */
function VideoPreview() {
/* local state */
const [openOverlay, setOpenOverlay] = useState(false);
return (
<>
<img
src="https://img.youtube.com/vi/dQw4w9WgXcQ/maxresdefault.jpg"
alt="Open video"
style={{ width: 320, cursor: 'pointer' }}
onClick={() => setOpenOverlay(true)}
/>
{openOverlay && (
<ReactYouTubeOverlay
embedLink="https://www.youtube.com/embed/dQw4w9WgXcQ"
embedTitle="Preview Video"
onCloseFn={() => setOpenOverlay(false)}
/>
)}
</>
);
}
/* exports */
export default VideoPreview;PASS src/components/test/main-div.test.tsx
MainDiv
β renders visible overlay with children when show is true
β renders hidden overlay without children when show is false
β renders exactly 2 paragraph children when two paragraphs are provided
PASS src/components/test/close-button.test.tsx
CloseBtn
β renders a button with closeIcon class and type button
β calls handleOnClick when clicked
PASS src/components/test/frame-component.test.tsx
FrameComponent
β renders iframe with provided title
β builds src with autoplay, mute, and rel query params
β sets allow and allowFullScreen attributes
PASS src/components/test/index.test.tsx
ReactYouTubeOverlay
β returns null when embedLink and embedTitle are empty strings
β renders overlay content when embedLink and embedTitle are non-empty
β changes overlay state when close button is clicked
Test Suites: 4 passed, 4 total
Tests: 11 passed, 11 total
Snapshots: 0 total
----------------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
components | 100 | 100 | 100 | 100 |
close-button.tsx | 100 | 100 | 100 | 100 |
frame-component.tsx | 100 | 100 | 100 | 100 |
index.tsx | 100 | 100 | 100 | 100 |
main-div.tsx | 100 | 100 | 100 | 100 |
hooks | 100 | 100 | 100 | 100 |
use-overlay.ts | 100 | 100 | 100 | 100 |
----------------------|---------|----------|---------|---------|-------------------
Contributions, suggestions, and improvements are welcome.
Feel free to open issues or pull requests.
Like this project? Support it with a github star, it would mean a lot to me! Cheers and Happy Coding.
