Skip to content

Commit d42f974

Browse files
feat: add parameters prop (#29)
* feat: add parameters prop * feat: correct parameters type * chore(demo): test parameters * test: fix parameters prop tests * fix: do not incluide enablejsapi=0 by default * fix: setInterval types * docs: provide better parameters prop description * docs: doing linkable parameters prop in the readme * docs: remove deprecated messages Co-authored-by: andrewvasilchuk <[email protected]>
1 parent 83cc58c commit d42f974

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ The list of available `props` (with their types, default values and descriptions
139139
[2]: https://developers.google.com/youtube/player_parameters#enablejsapi
140140
[3]: https://developers.google.com/youtube/iframe_api_reference#Getting_Started
141141
[4]: https://developers.google.com/youtube/player_parameters#IFrame_Player_API
142+
[5]: https://developers.google.com/youtube/player_parameters#Parameters
142143

143144
| Property | Required | Type | Default | Description |
144145
| -------------------- | -------- | ---------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
@@ -155,6 +156,7 @@ The list of available `props` (with their types, default values and descriptions
155156
| `enablejsapi` | `false` | `boolean` | `false` | Include [`'enablejapi=1'`][2] parameter to the generated `src` attribute of the `<iframe />` element. This will allow you to listen to the `init:player` event as well as access the `YT.Player` instance via the `getPlayerInstance` method. **Make sure** you have injected the proper YouTube [`<script />`](https://developers.google.com/youtube/player_parameters#IFrame_Player_API) tag or passed the `injectPlayerScript` prop |
156157
| `playerOptions` | `false` | `Partial<YT.PlayerOptions>` | `{}` | Options the will be passed to the [`YT.Player`][3] constructor |
157158
| `injectPlayerScript` | `false` | `boolean` | `false` | Will auto inject the proper YouTube [`<script />`][4] when `enablejapi` is passed and there is no `window.YT.Player` |
159+
| `parameters` | `false` | `YT.Parameters` | `{}` | [YouTube Parameters][5] that will be injected into the generated `<iframe />` `src` attribute |
158160

159161
### Events
160162

demo/App.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ export default Vue.extend({
5151
src: 'https://www.youtube.com/embed/4JS70KB9GS0',
5252
aspectRatio: '1:1',
5353
previewImageSize: 'default',
54+
parameters: {
55+
start: 32,
56+
},
5457
},
5558
}),
5659
]),

src/VueLazyYoutubeVideo.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,16 @@ export default (Vue as WithRefs<Refs, WithEvents<Events>>).extend({
7676
type: Boolean,
7777
default: false,
7878
},
79+
parameters: {
80+
type: Object as PropType<YT.PlayerVars>,
81+
default: () => ({}),
82+
},
7983
},
8084
data() {
8185
return {
8286
activated: this.autoplay,
8387
playerInstance: null as YT.Player | null,
84-
__interval__: null as NodeJS.Timeout | null,
88+
__interval__: null as number | null,
8589
}
8690
},
8791
computed: {
@@ -95,11 +99,15 @@ export default (Vue as WithRefs<Refs, WithEvents<Events>>).extend({
9599
}
96100
},
97101
srcAttribute(): string {
98-
const hasQuestionMark =
99-
typeof this.src === 'string' && this.src.indexOf('?') !== -1
100-
return `${this.src}${hasQuestionMark ? '&' : '?'}autoplay=1${
102+
const hasQuestionMark = this.src.indexOf('?') !== -1
103+
const src = `${this.src}${hasQuestionMark ? '&' : '?'}autoplay=1${
101104
this.enablejsapi ? '&enablejsapi=1' : ''
102105
}`
106+
107+
return Object.entries(this.parameters).reduce(
108+
(acc, [key, value]) => acc + `&${key}=${value}`,
109+
src
110+
)
103111
},
104112
styleObj(): object {
105113
return {
@@ -179,7 +187,7 @@ export default (Vue as WithRefs<Refs, WithEvents<Events>>).extend({
179187
script.setAttribute('src', PLAYER_SCRIPT_SRC)
180188

181189
script.onload = () => {
182-
this.__interval__ = setInterval(() => {
190+
this.__interval__ = window.setInterval(() => {
183191
this.checkPlayer()
184192
}, PLAYER_CHECK_MS)
185193
}

test/index.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,37 @@ describe('VueLazyYoutubeVideo', () => {
478478
})
479479
})
480480
})
481+
482+
describe('parameters', () => {
483+
it('should correctly set `parameters` to `src` attribute of the `<iframe />` element', async () => {
484+
const parameters = { rel: 0, color: 'white' as const }
485+
const wrapper = TestManager.createWrapper({
486+
propsData: { parameters },
487+
})
488+
const iframe = await TestManager.clickAndGetIframe(wrapper)
489+
expect(iframe.element.getAttribute('src')).toBe(
490+
`${defaultProps.src}?autoplay=1&rel=0&color=white`
491+
)
492+
})
493+
494+
it('should be `Object` and have default value', (done) => {
495+
const definition = TestManager.getPropDefinition('parameters')
496+
expect(definition).toMatchObject({
497+
type: Object,
498+
})
499+
if (typeof definition === 'object' && !Array.isArray(definition)) {
500+
const { default: d } = definition
501+
if (d && typeof d === 'function') {
502+
expect(d()).toMatchObject({})
503+
done()
504+
} else {
505+
done.fail('Invalid prop definition')
506+
}
507+
} else {
508+
done.fail('Invalid prop definition')
509+
}
510+
})
511+
})
481512
})
482513

483514
describe('events', () => {

types/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ export interface Props {
2323
enablejsapi?: boolean
2424
playerOptions?: Partial<YT.PlayerOptions>
2525
injectPlayerScript?: boolean
26+
parameters?: YT.PlayerVars
2627
}

0 commit comments

Comments
 (0)