Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hooks, support React 18, drop React <16 #117

Open
wants to merge 74 commits into
base: default
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
4aab64d
rewrite it with hooks
goto-bus-stop Jun 30, 2021
6cc2a90
use "real" es modules in node.js
goto-bus-stop Jun 30, 2021
d7d5312
example sourcemaps
goto-bus-stop Jun 30, 2021
f8e5432
generate source map with rollup
goto-bus-stop Jun 30, 2021
fe06529
lint
goto-bus-stop Jun 30, 2021
0086482
ci: use npm 7
goto-bus-stop Jun 30, 2021
6d54d23
ci: test react 17 and 18 only
goto-bus-stop Jun 30, 2021
3f09a8b
ci: fix typo
goto-bus-stop Jun 30, 2021
ee1a039
actually test with react 18
goto-bus-stop Jun 30, 2021
c188a1f
target modern-er browsers
goto-bus-stop Aug 19, 2021
f80fd0d
no need for document.head fallback
goto-bus-stop Aug 30, 2021
9b05de7
Merge branch 'default' into hooks
goto-bus-stop Dec 1, 2021
da41023
jsdoc types
goto-bus-stop Dec 18, 2021
4995d5e
ci: run with react 18 RC
goto-bus-stop Dec 18, 2021
af17792
ci: should do it like this oops
goto-bus-stop Dec 18, 2021
5a750b3
1.0.0-alpha.0
goto-bus-stop Dec 18, 2021
bc4f80c
Merge branch 'default' into hooks
goto-bus-stop Apr 7, 2022
7598ddc
fix types
goto-bus-stop Apr 7, 2022
00f9654
ci: use default npm version
goto-bus-stop Apr 19, 2022
f1e5670
use correct entry point for different react versions
goto-bus-stop Apr 19, 2022
28fbcd4
readme: remove --save
goto-bus-stop Apr 19, 2022
b2b03b6
tweaks
goto-bus-stop Apr 19, 2022
3886b0b
Merge branch 'default' into hooks
goto-bus-stop Apr 20, 2022
7f8a5c4
Merge branch 'default' into hooks
goto-bus-stop Apr 20, 2022
5476e9e
Remove defaultProps, handled by default arguments
goto-bus-stop Apr 20, 2022
13aef5a
1.0.0-alpha.1
goto-bus-stop Apr 20, 2022
bfecfb6
Accept react 17/18 typings
goto-bus-stop Apr 20, 2022
24ba24d
Merge branch 'default' into hooks
goto-bus-stop May 1, 2022
62380eb
Expose all functionality in a hook
goto-bus-stop May 1, 2022
475748b
use player.setSize() method
goto-bus-stop May 1, 2022
73ee1c6
example: do not minify identifiers so the internals are easier to ins…
goto-bus-stop May 1, 2022
31e9e9a
tweak firstRender check
goto-bus-stop May 1, 2022
df22e79
indent
goto-bus-stop May 1, 2022
4b3d539
setPlaybackQuality is removed
goto-bus-stop May 1, 2022
e2ab405
showInfo is removed
goto-bus-stop May 1, 2022
57c4336
1.0.0-alpha.2
goto-bus-stop May 1, 2022
7097029
fix unmount order
goto-bus-stop May 1, 2022
968ed15
load synchronously if possible
goto-bus-stop May 1, 2022
ae7bca9
1.0.0-alpha.3
goto-bus-stop May 1, 2022
c8cd8c3
argument tweaks
goto-bus-stop May 1, 2022
99b8365
build(deps-dev): bump mocha from 9.2.2 to 10.0.0 (#145)
dependabot[bot] May 2, 2022
73fe5af
declare <YouTube /> as a function component
goto-bus-stop May 2, 2022
5d85885
add default values to typescript doc
goto-bus-stop May 2, 2022
bc8501c
use `YT` global
goto-bus-stop May 2, 2022
66c67f0
document hook
goto-bus-stop May 2, 2022
3068dac
pass `mute` in playerVars
goto-bus-stop May 2, 2022
0c47f9f
work around unmount order fun times
goto-bus-stop May 3, 2022
34dc48d
add origin/host configs
goto-bus-stop May 3, 2022
06389ad
1.0.0-alpha.4
goto-bus-stop May 3, 2022
69c173e
do not cache eslint runs
goto-bus-stop May 3, 2022
63cb401
fix getSdk type
goto-bus-stop May 4, 2022
6a51475
Merge branch 'default' into hooks
goto-bus-stop Sep 21, 2022
3715c13
Merge branch 'default' into hooks
goto-bus-stop Oct 11, 2022
9d8a7ee
Merge branch 'default' into hooks
goto-bus-stop Oct 18, 2022
33cb208
deps: update rollup
goto-bus-stop Nov 1, 2022
b350ec2
ci: tweak matrix
goto-bus-stop Nov 12, 2022
17dd127
example: fix logo url
goto-bus-stop Nov 13, 2022
a64d1bc
document host/origin props
goto-bus-stop Dec 9, 2022
cbc952e
Merge branch 'default' into hooks
goto-bus-stop Dec 9, 2022
b957cce
typescript
goto-bus-stop Dec 13, 2022
d123534
make tests work
goto-bus-stop Dec 13, 2022
1e99e41
ci: remove tsd
goto-bus-stop Dec 13, 2022
339758c
compile jsx
goto-bus-stop Dec 13, 2022
2c20657
typescript example
goto-bus-stop Dec 13, 2022
5e0ee81
very specific proptypes
goto-bus-stop Dec 13, 2022
68971fd
example: use react createRoot
goto-bus-stop Dec 13, 2022
73a9120
example: use vite
goto-bus-stop Dec 13, 2022
88d783f
use vitest
goto-bus-stop Dec 13, 2022
9f3a4f0
make linting work again
goto-bus-stop Dec 13, 2022
b69c5d6
remove obsolete deps
goto-bus-stop Dec 13, 2022
db6dd40
remove babel config
goto-bus-stop Dec 13, 2022
5e04aee
Merge branch 'default' into hooks
goto-bus-stop Mar 10, 2023
7e404cc
deps: update vitest
goto-bus-stop Mar 10, 2023
6866ed7
deps: update typescript
goto-bus-stop Apr 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 0 additions & 20 deletions .babelrc.js

This file was deleted.

30 changes: 30 additions & 0 deletions .browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
and_chr 92
and_chr 91
and_ff 90
and_ff 89
and_qq 10.4
and_uc 12.12
android 92
android 91
baidu 7.12
chrome 92
chrome 91
chrome 90
edge 92
edge 91
firefox 90
firefox 89
firefox 78
ios_saf 14.5-14.7
ios_saf 14.0-14.4
ios_saf 13.4-13.7
kaios 2.5
op_mini all
op_mob 77
op_mob 76
opera 77
opera 76
safari 14.1
safari 14
samsung 14.0
samsung 13.0
16 changes: 12 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
module.exports = {
extends: 'airbnb',
parserOptions: {
ecmaVersion: 2021,
},
rules: {
// I disagree
'react/jsx-filename-extension': 'off',
// I disagree
'react/require-default-props': 'off',
// Our babel config doesn't support class properties
'react/state-in-constructor': 'off',
// I disagree
'react/function-component-definition': ['error', {
namedComponents: 'function-declaration',
Expand All @@ -21,4 +20,13 @@ module.exports = {
allowChildren: false,
}],
},
overrides: [
{
files: ['**/*.ts', '**/*.tsx'],
extends: ['airbnb-typescript'],
parserOptions: {
project: './tsconfig.json',
},
},
],
};
28 changes: 5 additions & 23 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,14 @@ name: CI
on: [push, pull_request]

jobs:
types:
name: Types
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v2
with:
node-version: lts/*
- name: Install dependencies
run: npm install
- name: Check types
run: npm run tsd

lint:
name: Code style
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: lts/*
- name: Install dependencies
Expand All @@ -37,25 +22,22 @@ jobs:
name: Tests
strategy:
matrix:
node-version: [14.x, 16.x, 17.x]
react-version: [17.x, 18.x]
include:
- node-version: 12.x
react-version: 16.0.0
node-version: [14.x, 16.x, 18.x]
react-version: [16.x, 17.x, 18.x]
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v3
- name: Install Node.js ${{matrix.node-version}}
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: ${{matrix.node-version}}
- name: Install dependencies
run: npm install
- name: Install React ${{matrix.react-version}}
if: matrix.react-version != '18.x'
run: |
npm install --save-dev \
npm install --force --save-dev \
react@${{matrix.react-version}} \
react-dom@${{matrix.react-version}}
- name: Run tests
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ jobs:
- name: Run tests
run: npm test
- name: Build example
run: |
npm run --prefix example build
mkdir _deploy
cp example/bundle.js example/index.html _deploy
run: npm run --prefix example build
- name: Publish site
if: success()
uses: crazy-max/ghaction-github-pages@v1
with:
target_branch: gh-pages
build_dir: _deploy
build_dir: example/dist
keep_history: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,28 @@ All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](http://semver.org/).

## 1.0.0-alpha.4 - 2022-05-03
* Add docs for the `useYouTube` hook.
* Add props for `origin` / `host` settings.
* Pass-through `muted` to the player initially, so `<YouTube autoplay muted />` works as expected.

## 1.0.0-alpha.3 - 2022-05-01
* Fix unmount order.
* Start player synchronously if the SDK is already loaded.

## 1.0.0-alpha.2 - 2022-05-01
* Expose all functionality as a `useYouTube` hook.
* Remove props no longer supported by YouTube: `showInfo`, `suggestedQuality`.

## 1.0.0-alpha.1 - 2022-04-20
* Improve typings.
* Remove duplicate `defaultProps`.

## 1.0.0-alpha.0 - 2021-12-01
* Use hooks internally.
* Drop support for React 16. This version requires React 17 or 18.
* Target evergreen browsers. If you need to support older browsers, you need to transpile this dependency.

## 0.7.4 - 2022-04-23
* Fix a warning about workspaces when installing with yarn.

Expand Down
90 changes: 74 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
# @u-wave/react-youtube

YouTube player component for React.

[Install][] - [Usage][] - [Demo][] - [Props][]
[Install][] - [Usage][] - [Demo][] - [Component API][] - [Hook API][]

## Install

```
npm install --save @u-wave/react-youtube
npm install @u-wave/react-youtube
```

## Usage

[Demo][] - [Demo source code][]

```js
Expand All @@ -23,15 +20,23 @@ import YouTube from '@u-wave/react-youtube';
/>
```

## Props
<a id="component"></a>
## `<YouTube />`
The `<YouTube />` component renders an iframe and attaches the YouTube player to it. It supports all the
same options as the `useYouTube` hook, plus a few to configure the iframe. If you need to do more with
the iframe than this component provides, consider using the `useYouTube` hook directly.

### Props
| Name | Type | Default | Description |
|:-----|:-----|:-----|:-----|
| video | string | | An 11-character string representing a YouTube video ID.. |
| id | string | | DOM ID for the player element. |
| className | string | | CSS className for the player element. |
| style | object | | Inline style for container element. |
| video | string | | An 11-character string representing a YouTube video ID.. |
| width | number, string | | Width of the player element. |
| height | number, string | | Height of the player element. |
| host | string | https://www.youtube.com | YouTube host to use: 'https://www.youtube.com' or 'https://www.youtube-nocookie.com'. |
| origin | string | | The YouTube API will usually default this value correctly. It is exposed for completeness.<br>https://developers.google.com/youtube/player_parameters#origin |
| paused | bool | | Pause the video. |
| autoplay | bool | false | Whether the video should start playing automatically.<br>https://developers.google.com/youtube/player_parameters#autoplay |
| showCaptions | bool | false | Whether to show captions below the video.<br>https://developers.google.com/youtube/player_parameters#cc_load_policy |
Expand All @@ -47,31 +52,84 @@ import YouTube from '@u-wave/react-youtube';
| showRelatedVideos | bool | true | Whether to show related videos after the video is over.<br>https://developers.google.com/youtube/player_parameters#rel |
| volume | number | | The playback volume, **as a number between 0 and 1**. |
| muted | bool | | Whether the video's sound should be muted. |
| suggestedQuality | string | | The suggested playback quality.<br>https://developers.google.com/youtube/iframe_api_reference#Playback_quality |
| playbackRate | number | | Playback speed.<br>https://developers.google.com/youtube/iframe_api_reference#setPlaybackRate |
| onReady | function | | Sent when the YouTube player API has loaded. |
| onError | function | | Sent when the player triggers an error. |
| onCued | function | () => {} | Sent when the video is cued and ready to play. |
| onBuffering | function | () => {} | Sent when the video is buffering. |
| onPlaying | function | () => {} | Sent when playback has been started or resumed. |
| onPause | function | () => {} | Sent when playback has been paused. |
| onEnd | function | () => {} | Sent when playback has stopped. |
| onCued | function | | Sent when the video is cued and ready to play. |
| onBuffering | function | | Sent when the video is buffering. |
| onPlaying | function | | Sent when playback has been started or resumed. |
| onPause | function | | Sent when playback has been paused. |
| onEnd | function | | Sent when playback has stopped. |
| onStateChange | function | | |
| onPlaybackRateChange | function | | |
| onPlaybackQualityChange | function | | |

## Related
<a id="hook"></a>
## `useYouTube(container, options)`
Create a YouTube player at `container`. `container` must be a ref object.

Returns the `YT.Player` object, or `null` until the player is ready.

```js
import { useYouTube } from '@u-wave/react-youtube';

function Player() {
const container = useRef(null);
const player = useYouTube(container, {
video: 'x2to0hs',
autoplay: true,
});
console.log(player?.getVideoUrl());
return <div ref={container} />;
}
```

### Options
| Name | Type | Default | Description |
|:-----|:-----|:-----|:-----|
| video | string | | An 11-character string representing a YouTube video ID.. |
| width | number, string | | Width of the player element. |
| height | number, string | | Height of the player element. |
| host | string | https://www.youtube.com | YouTube host to use: 'https://www.youtube.com' or 'https://www.youtube-nocookie.com'. |
| origin | string | | The YouTube API will usually default this value correctly. It is exposed for completeness.<br>https://developers.google.com/youtube/player_parameters#origin |
| paused | bool | | Pause the video. |
| autoplay | bool | false | Whether the video should start playing automatically.<br>https://developers.google.com/youtube/player_parameters#autoplay |
| showCaptions | bool | false | Whether to show captions below the video.<br>https://developers.google.com/youtube/player_parameters#cc_load_policy |
| controls | bool | true | Whether to show video controls.<br>https://developers.google.com/youtube/player_parameters#controls |
| disableKeyboard | bool | false | Ignore keyboard controls.<br>https://developers.google.com/youtube/player_parameters#disablekb |
| allowFullscreen | bool | true | Whether to display the fullscreen button.<br>https://developers.google.com/youtube/player_parameters#fs |
| lang | string | | The player's interface language. The parameter value is an ISO 639-1 two-letter language code or a fully specified locale.<br>https://developers.google.com/youtube/player_parameters#hl |
| annotations | bool | true | Whether to show annotations on top of the video.<br>https://developers.google.com/youtube/player_parameters#iv_load_policy |
| startSeconds | number | | Time in seconds at which to start playing the video.<br>https://developers.google.com/youtube/player_parameters#start |
| endSeconds | number | | Time in seconds at which to stop playing the video.<br>https://developers.google.com/youtube/player_parameters#end |
| modestBranding | bool | false | Remove most YouTube logos from the player.<br>https://developers.google.com/youtube/player_parameters#modestbranding |
| playsInline | bool | false | Whether to play the video inline on iOS, instead of fullscreen.<br>https://developers.google.com/youtube/player_parameters#playsinline |
| showRelatedVideos | bool | true | Whether to show related videos after the video is over.<br>https://developers.google.com/youtube/player_parameters#rel |
| volume | number | | The playback volume, **as a number between 0 and 1**. |
| muted | bool | | Whether the video's sound should be muted. |
| playbackRate | number | | Playback speed.<br>https://developers.google.com/youtube/iframe_api_reference#setPlaybackRate |
| onReady | function | | Sent when the YouTube player API has loaded. |
| onError | function | | Sent when the player triggers an error. |
| onCued | function | | Sent when the video is cued and ready to play. |
| onBuffering | function | | Sent when the video is buffering. |
| onPlaying | function | | Sent when playback has been started or resumed. |
| onPause | function | | Sent when playback has been paused. |
| onEnd | function | | Sent when playback has stopped. |
| onStateChange | function | | |
| onPlaybackRateChange | function | | |
| onPlaybackQualityChange | function | | |

## Related
- [react-youtube][] - A widely-used YouTube component. Its API matches the YouTube iframe API more closely, and it doesn't support prop-based volume/quality/playback changes.
- [@u-wave/react-vimeo][] - A Vimeo component with a similar declarative API.

## License

[MIT][]

[Install]: #install
[Usage]: #usage
[Props]: #props
[Component API]: #component
[Hook API]: #hook
[Demo]: https://u-wave.net/react-youtube
[Demo source code]: ./example
[MIT]: ./LICENSE
Expand Down
2 changes: 1 addition & 1 deletion example/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bundle.js
dist
Loading