Skip to content

Commit a2bb47f

Browse files
authored
Merge pull request #144 from basemate/add_youtube_component
Add youtube component
2 parents 082b669 + c2fc8c8 commit a2bb47f

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
%iframe{@tag_attributes}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Matestack::Ui::Core::Youtube
2+
class Youtube < Matestack::Ui::Core::Component::Static
3+
4+
REQUIRED_KEYS = [:yt_id, :height, :width]
5+
6+
def setup
7+
url = 'https://www.youtube.com/embed/'
8+
url = 'https://www.youtube-nocookie.com/embed/' if options[:privacy_mode] == true
9+
url << options[:yt_id]
10+
url << '?' unless options[:no_controls] != true and options[:start_at].nil?
11+
url << 'controls=0' if options[:no_controls] == true
12+
url << '&amp;' if (options[:no_controls] != nil) and (options[:start_at] != nil)
13+
url << "start=#{options[:start_at]}" unless options[:start_at].nil?
14+
15+
@tag_attributes.merge!({
16+
'src': url,
17+
'allow': 'accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture',
18+
'allowfullscreen': '',
19+
'frameborder': 0,
20+
'height': options[:height],
21+
'width': options[:width]
22+
})
23+
end
24+
25+
end
26+
end

docs/components/youtube.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# matestack core component: Youtube
2+
3+
Show [specs](../../spec/usage/components/youtube_spec.rb)
4+
5+
A youtube video, embedded into an `iFrame` HTML tag.
6+
7+
## Parameters
8+
The video tag takes mandatory *youtube id*, *height* and *width* arguments and can take a number of optional configuration params.
9+
10+
#### # yt_id
11+
Expects a string with the id to the youtube video you want to embed.
12+
13+
#### # id, class (optional)
14+
Like most of the core components, you can give a video an id and a class.
15+
16+
#### # height
17+
Expects an integer with the height of the iFrame in px.
18+
19+
#### # width
20+
Expects an integer with the width of the iFrame in px.
21+
22+
#### # no_controls (optional)
23+
Expects a boolean. If set to true, no controls will be displayed with the embedded youtube video.
24+
25+
#### # privacy_mode (optional)
26+
Expects a boolean. If set to true, the video gets loaded from 'www.youtube-nocookie.com' instead of the default 'www.youtube.com'.
27+
28+
#### # start_at (optional)
29+
Expects an integer that indicates at what second the video should start.
30+
31+
## Example 1
32+
33+
```ruby
34+
youtube height: 360, width: 360, yt_id: 'OY5AeGhgK7I'
35+
```
36+
37+
gets rendered into
38+
39+
```HTML
40+
<iframe allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' allowfullscreen='' frameborder='0' height='360' src='https://www.youtube.com/embed/OY5AeGhgK7I' width='360'></iframe>
41+
```
42+
43+
## Example 2
44+
45+
```ruby
46+
youtube height: 360, width: 360, yt_id: 'OY5AeGhgK7I', start_at: 30, no_controls: true
47+
youtube height: 360, width: 360, yt_id: 'OY5AeGhgK7I', start_at: 30, privacy_mode: true
48+
```
49+
50+
gets rendered into
51+
52+
```HTML
53+
<iframe allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' allowfullscreen='' frameborder='0' height='360' src='https://www.youtube.com/embed/OY5AeGhgK7I?controls=0&amp;start=30' width='360'></iframe>
54+
<iframe allow='accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture' allowfullscreen='' frameborder='0' height='360' src='https://www.youtube-nocookie.com/embed/OY5AeGhgK7I?start=30' width='360'></iframe>
55+
```

spec/usage/components/youtube_spec.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require_relative '../../support/utils'
2+
include Utils
3+
4+
describe 'Youtube Component', type: :feature, js: true do
5+
6+
it 'Example 1' do
7+
8+
class ExamplePage < Matestack::Ui::Page
9+
10+
def response
11+
components {
12+
# simple youtube video
13+
youtube height: 360, width: 360, yt_id: 'OY5AeGhgK7I', class: 'iframe'
14+
# youtube video with start_at and no_controls
15+
youtube height: 360, width: 360, yt_id: 'OY5AeGhgK7I', start_at: 30, no_controls: true
16+
# youtube video with start_at and privacy_mode
17+
youtube height: 360, width: 360, yt_id: 'OY5AeGhgK7I', start_at: 30, privacy_mode: true
18+
}
19+
end
20+
21+
end
22+
23+
visit '/example'
24+
25+
static_output = page.html
26+
27+
expect(page).to have_selector("iframe[src='https://www.youtube.com/embed/OY5AeGhgK7I'][class='iframe']")
28+
expect(page).to have_selector("iframe[src='https://www.youtube.com/embed/OY5AeGhgK7I?controls=0&start=30']")
29+
expect(page).to have_selector("iframe[src='https://www.youtube-nocookie.com/embed/OY5AeGhgK7I?start=30']")
30+
31+
end
32+
33+
end

0 commit comments

Comments
 (0)