Skip to content

Commit c563e8b

Browse files
authoredJan 4, 2022
feat: allow setting a custom iframe title (#317)
This allows for better accessibility especially for screen readers. See https://www.w3.org/TR/WCAG20-TECHS/H64.html.
1 parent d551cc1 commit c563e8b

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed
 

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ $ npm install react-youtube
2424
id={string} // defaults -> null
2525
className={string} // defaults -> null
2626
containerClassName={string} // defaults -> ''
27+
title={string} // defaults -> null
2728
opts={obj} // defaults -> {}
2829
onReady={func} // defaults -> noop
2930
onPlay={func} // defaults -> noop

‎index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export interface YouTubeProps {
3737
id?: string;
3838
className?: string;
3939
containerClassName?: string;
40+
title?: string;
4041
loading?: 'lazy' | 'eager' | 'auto';
4142
opts?: Options;
4243
onReady?(event: { target: YouTubePlayer }): void;

‎src/YouTube.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ function shouldUpdatePlayer(prevProps, props) {
7070
prevProps.id !== props.id ||
7171
prevProps.className !== props.className ||
7272
prevProps.opts.width !== props.opts.width ||
73-
prevProps.opts.height !== props.opts.height
73+
prevProps.opts.height !== props.opts.height ||
74+
prevProps.title !== props.title
7475
);
7576
}
7677

@@ -226,6 +227,8 @@ class YouTube extends React.Component {
226227
else iframe.removeAttribute('width');
227228
if (this.props.opts && this.props.opts.height) iframe.setAttribute('height', this.props.opts.height);
228229
else iframe.removeAttribute('height');
230+
if (typeof this.props.title === 'string') iframe.setAttribute('title', this.props.title);
231+
else iframe.setAttribute('title', 'YouTube video player');
229232
});
230233
};
231234

@@ -294,6 +297,8 @@ YouTube.propTypes = {
294297
className: PropTypes.string,
295298
// custom class name for player container element
296299
containerClassName: PropTypes.string,
300+
// custom title for the iFrame, see https://www.w3.org/TR/WCAG20-TECHS/H64.html
301+
title: PropTypes.string,
297302

298303
// custom loading for player element
299304
loading: PropTypes.oneOf(['lazy', 'eager', 'auto']),
@@ -327,6 +332,7 @@ YouTube.defaultProps = {
327332
onStateChange: () => {},
328333
onPlaybackRateChange: () => {},
329334
onPlaybackQualityChange: () => {},
335+
title: null,
330336
};
331337

332338
export default YouTube;

‎src/Youtube.test.js

+16
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,22 @@ describe('YouTube', () => {
4444
expect(playerMock.getIframe).toHaveBeenCalled();
4545
});
4646

47+
it('should update the title when modified', () => {
48+
const { rerender } = render(<YouTube title="Video about a cat" videoId="XxVg_s8xAms" />);
49+
50+
rerender(<YouTube title="Video about a dancing cat" videoId="XxVg_s8xAms" />);
51+
52+
expect(playerMock.getIframe).toHaveBeenCalled();
53+
});
54+
55+
it('should update the title when removed', () => {
56+
const { rerender } = render(<YouTube title="Video about a cat" videoId="XxVg_s8xAms" />);
57+
58+
rerender(<YouTube videoId="XxVg_s8xAms" />);
59+
60+
expect(playerMock.getIframe).toHaveBeenCalled();
61+
});
62+
4763
it('should not update id and className when no change in them', () => {
4864
const className = 'custom-class';
4965
const videoId = 'XxVg_s8xAms';

0 commit comments

Comments
 (0)
Please sign in to comment.