Skip to content

Commit

Permalink
Merge pull request #13 from G-nizam-A/Feat-Embed-video-trailer-youtub…
Browse files Browse the repository at this point in the history
…e-#12

Feat: Embed video trailer youtube #12
  • Loading branch information
nnivxix authored Mar 25, 2024
2 parents 6560d46 + 2391084 commit fc98969
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 21 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"preview": "vite preview"
},
"dependencies": {
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-slot": "^1.0.2",
"class-variance-authority": "^0.7.0",
Expand Down Expand Up @@ -41,4 +43,4 @@
"typescript": "^5.2.2",
"vite": "^5.1.6"
}
}
}
70 changes: 70 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/components/PopupYoutubeTrailer.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Dialog, DialogTrigger, DialogContent } from "@/components/ui/dialog";

export default function PopupYoutubeTrailer({ video }: { video: string }) {
return (
<Dialog>
<DialogTrigger>
<div className="px-2 py-2 w-auto col-span-2 lg:col-span-1 text-center rounded-md bg-red-700 cursor-pointer">
Watch Trailer
</div>
</DialogTrigger>
<DialogContent className="bg-transparent p-0 border-none">
<iframe
width={610}
height={365}
className="w-full"
src={`https://www.youtube.com/embed/${video}`}
title="YouTube video player"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerPolicy="strict-origin-when-cross-origin"
allowFullScreen={true}
/>
</DialogContent>
</Dialog>
);
}
120 changes: 120 additions & 0 deletions src/components/ui/dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import * as React from "react"
import * as DialogPrimitive from "@radix-ui/react-dialog"
import { X } from "lucide-react"

import { cn } from "@/lib/utils"

const Dialog = DialogPrimitive.Root

const DialogTrigger = DialogPrimitive.Trigger

const DialogPortal = DialogPrimitive.Portal

const DialogClose = DialogPrimitive.Close

const DialogOverlay = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Overlay
ref={ref}
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
/>
))
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName

const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => (
<DialogPortal>
<DialogOverlay />
<DialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
{...props}
>
{children}
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
<X className="h-4 w-4" />
<span className="sr-only">Close</span>
</DialogPrimitive.Close>
</DialogPrimitive.Content>
</DialogPortal>
))
DialogContent.displayName = DialogPrimitive.Content.displayName

const DialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-1.5 text-center sm:text-left",
className
)}
{...props}
/>
)
DialogHeader.displayName = "DialogHeader"

const DialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
DialogFooter.displayName = "DialogFooter"

const DialogTitle = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Title
ref={ref}
className={cn(
"text-lg font-semibold leading-none tracking-tight",
className
)}
{...props}
/>
))
DialogTitle.displayName = DialogPrimitive.Title.displayName

const DialogDescription = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
DialogDescription.displayName = DialogPrimitive.Description.displayName

export {
Dialog,
DialogPortal,
DialogOverlay,
DialogClose,
DialogTrigger,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
}
15 changes: 5 additions & 10 deletions src/pages/Show/Movie.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Link, useParams } from "react-router-dom";
import { useParams } from "react-router-dom";
import useFetch from "@/hooks/useFetch";
import type { Movie as MovieType } from "@/types/movie";
import type { Images, Media, Video } from "@/types/media";
Expand All @@ -18,6 +18,7 @@ import {
} from "@/components/ui/carousel";
import SimilarCardItem from "@/components/SimilarCardItem";
import WatchProviderContainer from "@/components/WatchProviderContainer";
import PopupYoutubeTrailer from "@/components/PopupYoutubeTrailer";

export default function Movie() {
const params = useParams();
Expand Down Expand Up @@ -106,15 +107,9 @@ export default function Movie() {
</Carousel>
)}
{!!videos?.results.length && (
<Link
className="px-2 py-2 w-auto col-span-2 lg:col-span-1 text-center rounded-md bg-red-700"
to={`https://www.youtube.com/watch?v=${
getVideo(videos?.results)?.key
}`}
target="_blank"
>
Watch Trailer
</Link>
<PopupYoutubeTrailer
video={getVideo(videos.results)?.key as string}
/>
)}
</div>

Expand Down
15 changes: 5 additions & 10 deletions src/pages/Show/Tv.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Link, useParams } from "react-router-dom";
import { useParams } from "react-router-dom";
import useFetch from "@/hooks/useFetch";
import type { Response, SimilarMixed, SimilarTv } from "@/types/response";
import type { Season, Tv as TvType } from "@/types/tv";
Expand All @@ -17,6 +17,7 @@ import imageUrl from "@/utils/image-url";
import pickRandomImages from "@/utils/pick-random-images";
import SeasonCardItem from "@/components/SeasonCardItem";
import WatchProviderContainer from "@/components/WatchProviderContainer";
import PopupYoutubeTrailer from "@/components/PopupYoutubeTrailer";

export default function Tv() {
const params = useParams();
Expand Down Expand Up @@ -98,15 +99,9 @@ export default function Tv() {
</Carousel>
)}
{!!videos?.results.length && (
<Link
className="px-2 py-2 w-auto col-span-2 lg:col-span-1 text-center rounded-md bg-red-700"
to={`https://www.youtube.com/watch?v=${
getVideo(videos?.results)?.key
}`}
target="_blank"
>
Watch Trailer
</Link>
<PopupYoutubeTrailer
video={getVideo(videos.results)?.key as string}
/>
)}
</div>

Expand Down

0 comments on commit fc98969

Please sign in to comment.