Skip to content
This repository was archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #82 from danestves/v2
Browse files Browse the repository at this point in the history
feat: get types to pass params for iframe
  • Loading branch information
danestves authored Mar 1, 2022
2 parents 7648615 + 7b10308 commit fa31ca1
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 6 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

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

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"url": "git+https://github.com/danestves/react-youtube-lite.git"
},
"license": "MIT",
"author": "Daniel Esteves <[email protected]> (@danestves)",
"author": "Daniel Esteves <[email protected]> (https://danestves.com)",
"main": "dist/index.js",
"module": "dist/react-youtube-lite.mjs",
"source": "src/index.ts",
Expand All @@ -30,7 +30,7 @@
"scripts": {
"prebuild": "npm run clean",
"build": "NODE_ENV=production parcel build",
"clean": "rimraf .parcel-cache dist",
"clean": "rimraf .parcel-cache dist types",
"prestart": "npm run clean",
"start": "parcel watch",
"test": "jest"
Expand All @@ -44,6 +44,7 @@
"@parcel/packager-ts": "^2.3.2",
"@parcel/transformer-typescript-types": "^2.3.2",
"@types/react": "^17.0.39",
"@types/youtube-player": "^5.5.5",
"parcel": "^2.3.2",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",
Expand Down
12 changes: 8 additions & 4 deletions src/components/react-youtube-lite/react-youtube-lite.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { StyledAspectRatio, StyledYouTubeIcon } from './react-youtube-lite.style
import { addPrefetch } from '../../utils/add-prefetch';
import { getYouTubeId } from '../../utils/get-youtube-id';
import type { ReactYouTubeLiteProps } from '../../types';
import { getSrcSearch } from '../../utils/get-src-search';

function RenderReactYouTubeLite(
{
Expand All @@ -15,6 +16,7 @@ function RenderReactYouTubeLite(
customThumbnail,
iframeProps,
noCookie,
playerParameters,
playlist,
poster,
title,
Expand All @@ -29,9 +31,7 @@ function RenderReactYouTubeLite(
let posterUrl =
typeof customThumbnail === 'string' ? customThumbnail : `https://i.ytimg.com/vi/${videoId}/${poster}.jpg`;
let youtubeUrl = noCookie ? 'https://www.youtube-nocookie.com' : 'https://www.youtube.com';
let iframeSrc = !playlist
? `${youtubeUrl}/embed/${videoId}?autoplay=1`
: `${youtubeUrl}/embed/videoseries?list=${videoId}`;
let iframeSrc = !playlist ? `${youtubeUrl}/embed/${videoId}` : `${youtubeUrl}/embed/videoseries`;

const warmConnections = () => {
if (preconnected) return;
Expand Down Expand Up @@ -73,7 +73,11 @@ function RenderReactYouTubeLite(
width={560}
height={315}
title={title}
src={iframeSrc}
src={getSrcSearch({
url: iframeSrc,
videoId,
isPlaylist: playlist,
})}
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
{...iframeProps}
Expand Down
12 changes: 12 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Dependencies
import type * as React from 'react';
import type * as Stitches from '@stitches/react';
import type * as YouTube from 'youtube-player/dist/types';

interface ReactYouTubeLiteProps extends React.ComponentPropsWithoutRef<'div'> {
/**
Expand Down Expand Up @@ -33,6 +34,17 @@ interface ReactYouTubeLiteProps extends React.ComponentPropsWithoutRef<'div'> {
* @default false
*/
noCookie?: boolean;
/**
* By appending parameters to the IFrame URL, you can customize the playback experience in your application.
* For example, you can automatically play videos using the [`autoplay`](https://developers.google.com/youtube/player_parameters#autoplay)
* parameter or cause a video to play repeatedly using the [`loop`](https://developers.google.com/youtube/player_parameters#loop) parameter.
* You can also use the [`enablejsapi`](https://developers.google.com/youtube/player_parameters#enablejsapi) parameter to enable the player
* to be controlled via the [IFrame Player API](https://developers.google.com/youtube/iframe_api_reference).
*
* List of available parameters:
* https://developers.google.com/youtube/player_parameters#Parameters
*/
playerParameters?: YouTube.Options['playerVars'];
/**
* If the video URL contains a playlist or not
*
Expand Down
29 changes: 29 additions & 0 deletions src/utils/get-src-search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Dependencies
import type * as YouTube from 'youtube-player/dist/types';

type GetSrcSearchProps = {
isPlaylist?: boolean;
url: string;
videoId: string;
opts?: YouTube.Options['playerVars'];
};

function getSrcSearch({ url, videoId, isPlaylist, opts }: GetSrcSearchProps) {
let options: YouTube.Options['playerVars'] = {
...(!isPlaylist
? {
autoplay: 1,
}
: {
list: videoId,
}),
...opts,
};
// @ts-ignore: we can use numbers on the values
let params = new URLSearchParams(options);

return `${url}?${params.toString()}`;
}

export type { GetSrcSearchProps };
export { getSrcSearch };

0 comments on commit fa31ca1

Please sign in to comment.