Skip to content

Commit 7b36b18

Browse files
committed
fix: utilize wistia js api onReady functionfor focusing the video when ready
1 parent 546a2ac commit 7b36b18

1 file changed

Lines changed: 15 additions & 26 deletions

File tree

  • packages/scratch-gui/src/components/cards

packages/scratch-gui/src/components/cards/cards.jsx

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,20 @@ class VideoStep extends React.Component {
9999
script2.async = true;
100100
script2.setAttribute('id', 'wistia-video-api');
101101
document.body.appendChild(script2);
102+
103+
// The Wistia API doesn't provide a callback for when the video is ready,
104+
// so we use the global _wq queue that Wistia provides for this purpose.
105+
// See the below above for more details.
106+
// https://docs.wistia.com/docs/javascript-player-api#with-standard-embeds
107+
window._wq = window._wq || [];
108+
window._wq.push({
109+
id: this.props.video,
110+
onReady: video => {
111+
if (video) {
112+
video.focus();
113+
}
114+
}});
115+
102116
}
103117

104118
// We use the Wistia API here to update or pause the video dynamically:
@@ -136,7 +150,6 @@ class VideoStep extends React.Component {
136150
className={`wistia_embed wistia_async_${this.props.video}`}
137151
id="video-div"
138152
style={{height: `257px`, width: `466px`}}
139-
ref={this.props.videoRef}
140153
>
141154
 
142155
</div>
@@ -147,8 +160,7 @@ class VideoStep extends React.Component {
147160

148161
VideoStep.propTypes = {
149162
expanded: PropTypes.bool.isRequired,
150-
video: PropTypes.string.isRequired,
151-
videoRef: PropTypes.shape({current: PropTypes.instanceOf(Element)})
163+
video: PropTypes.string.isRequired
152164
};
153165

154166
const ImageStep = ({title, image}) => (<Fragment>
@@ -356,28 +368,6 @@ const Cards = props => {
356368

357369
if (activeDeckId === null) return;
358370

359-
const videoRef = useRef(null);
360-
361-
useEffect(() => {
362-
// Only focus in the presence of a video on the card
363-
const steps = content[activeDeckId]?.steps;
364-
const isTutorialStep = steps?.[step]?.video;
365-
366-
if (isTutorialStep) {
367-
// Need the custom focus delay because of the conflict between this logic
368-
// and refocusing the button that opened the library (tutorials in this case)
369-
const FOCUS_DELAY = 500;
370-
371-
const focusVideo = () => {
372-
const target = videoRef.current &&
373-
videoRef.current.querySelector('.w-big-play-button');
374-
if (target) target.focus();
375-
};
376-
requestAnimationFrame(focusVideo);
377-
setTimeout(focusVideo, FOCUS_DELAY);
378-
}
379-
}, [activeDeckId, step, content]);
380-
381371
// Tutorial cards need to calculate their own dragging bounds
382372
// to allow for dragging the cards off the left, right and bottom
383373
// edges of the workspace.
@@ -450,7 +440,6 @@ const Cards = props => {
450440
dragging={dragging}
451441
expanded={expanded}
452442
video={translateVideo(steps[step].video, locale)}
453-
videoRef={videoRef}
454443
/>
455444
) : (
456445
<ImageStep

0 commit comments

Comments
 (0)