@comicrelief/component-library 7.21.0 → 7.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. package/dist/components/Molecules/Promo/Promo.js +87 -12
  2. package/dist/components/Molecules/Promo/Promo.md +266 -4
  3. package/dist/components/Molecules/Promo/Promo.style.js +24 -6
  4. package/dist/components/Molecules/Promo/Promo.test.js +2 -2
  5. package/dist/components/Molecules/Promo/_ProgressRing.js +57 -0
  6. package/dist/components/Molecules/Promo/_PromoVideo.js +116 -0
  7. package/dist/components/Molecules/Promo/_PromoVideoButton.js +31 -0
  8. package/dist/components/Molecules/Promo/_PromoVideoButton.style.js +110 -0
  9. package/dist/components/Molecules/Promo/assets/Pause--black.svg +3 -0
  10. package/dist/components/Molecules/Promo/assets/Pause--white.svg +3 -0
  11. package/dist/components/Molecules/Promo/assets/Play--black.svg +3 -0
  12. package/dist/components/Molecules/Promo/assets/Play--white.svg +3 -0
  13. package/dist/styleguide/assets/mobileVideoPosterImage.png +0 -0
  14. package/package.json +1 -1
  15. package/src/components/Molecules/Promo/Promo.js +89 -13
  16. package/src/components/Molecules/Promo/Promo.md +266 -4
  17. package/src/components/Molecules/Promo/Promo.style.js +23 -2
  18. package/src/components/Molecules/Promo/Promo.test.js +2 -2
  19. package/src/components/Molecules/Promo/_ProgressRing.js +43 -0
  20. package/src/components/Molecules/Promo/_PromoVideo.js +105 -0
  21. package/src/components/Molecules/Promo/_PromoVideoButton.js +26 -0
  22. package/src/components/Molecules/Promo/_PromoVideoButton.style.js +121 -0
  23. package/src/components/Molecules/Promo/assets/Pause--black.svg +3 -0
  24. package/src/components/Molecules/Promo/assets/Pause--white.svg +3 -0
  25. package/src/components/Molecules/Promo/assets/Play--black.svg +3 -0
  26. package/src/components/Molecules/Promo/assets/Play--white.svg +3 -0
  27. package/src/styleguide/assets/mobileVideoPosterImage.png +0 -0
@@ -13,7 +13,7 @@ it('renders Promo correctly', () => {
13
13
  imageSet={defaultData.promoImage}
14
14
  image={defaultData.promoImage}
15
15
  imageAltText=""
16
- copyFirst={false}
16
+ copyLeft={false}
17
17
  >
18
18
  <Text
19
19
  tag="h1"
@@ -47,7 +47,7 @@ it('renders Promo correctly end position', () => {
47
47
  imageSet={defaultData.promoImage}
48
48
  image={defaultData.promoImage}
49
49
  imageAltText=""
50
- copyFirst={false}
50
+ copyLeft={false}
51
51
  position="end"
52
52
  >
53
53
  <Text
@@ -0,0 +1,43 @@
1
+ import React, { useState, useEffect } from 'react';
2
+ import PropTypes from 'prop-types';
3
+
4
+ import {
5
+ ProgressRingWrapper, ProgressRingSVG, ProgressRingCircle
6
+ } from './_PromoVideoButton.style';
7
+
8
+ const ProgressRing = ({
9
+ videoProgress, ...rest
10
+ }) => {
11
+ const thisStroke = 4;
12
+ const thisRadius = 28;
13
+ const initNormRadius = thisRadius - thisStroke * 2;
14
+ const thisCircumference = initNormRadius * 2 * Math.PI;
15
+ const [thisDashOffset, setThisDashOffset] = useState(initNormRadius * 2 * Math.PI);
16
+
17
+ useEffect(() => {
18
+ const offset = thisCircumference - ((videoProgress / 100) * thisCircumference);
19
+ setThisDashOffset(offset);
20
+ }, [videoProgress, thisCircumference]);
21
+
22
+ return (
23
+ <ProgressRingWrapper videoProgress={videoProgress}>
24
+ <ProgressRingSVG height={thisRadius * 2} width={thisRadius * 2}>
25
+ <ProgressRingCircle
26
+ strokeDasharray={`${thisCircumference} ${thisCircumference}`}
27
+ strokeDashOffsetStyle={thisDashOffset}
28
+ strokeWidth={`${thisStroke}`}
29
+ r={`${initNormRadius}`}
30
+ cx={`${thisRadius}`}
31
+ cy={`${thisRadius}`}
32
+ {...rest}
33
+ />
34
+ </ProgressRingSVG>
35
+ </ProgressRingWrapper>
36
+ );
37
+ };
38
+
39
+ ProgressRing.propTypes = {
40
+ videoProgress: PropTypes.number.isRequired
41
+ };
42
+
43
+ export default ProgressRing;
@@ -0,0 +1,105 @@
1
+ import React, { useEffect, useRef, useState } from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { Video } from './Promo.style';
4
+ import PromoVideoButton from './_PromoVideoButton';
5
+
6
+ const PromoVideo = ({
7
+ lightColouredVideo,
8
+ copyLeft,
9
+ thisVideoSrc,
10
+ thisPoster,
11
+ autoPlay,
12
+ loop,
13
+ showPosterAfterPlaying
14
+ }) => {
15
+ const [isPlaying, setIsPlaying] = useState(false);
16
+ const [isRestarting, setIsRestarting] = useState(false);
17
+ const [videoProgress, setVideoProgress] = useState(0);
18
+ const videoEl = useRef(null);
19
+
20
+ const togglePlay = () => {
21
+ if (isPlaying) videoEl.current.pause();
22
+ else videoEl.current.play();
23
+ setIsPlaying(!isPlaying);
24
+ };
25
+
26
+ const updateTime = () => {
27
+ if (videoEl.current.duration) {
28
+ // Calculate the percentage of the video played:
29
+ const percentage = Math.round((videoEl.current.currentTime / videoEl.current.duration) * 100);
30
+ // Because a completely smooth animation is impossible with the 250ms-ish frequency of
31
+ // the 'timeupdate' event, we're electing to just update the ring every 25%.
32
+ const nearest = 25;
33
+ const roundedPercentage = (percentage + nearest / 2) - ((percentage + nearest / 2) % nearest);
34
+ setVideoProgress(roundedPercentage);
35
+ }
36
+ };
37
+
38
+ // Only loads once the initial screensize check is complete
39
+ useEffect(() => {
40
+ // Use truthy comparison so either a null OR undefined value won't work
41
+ if (thisVideoSrc != null) {
42
+ videoEl.current.addEventListener('timeupdate', updateTime);
43
+ // Trigger on-load autoplay if apppropriate
44
+ if (autoPlay && !isPlaying) {
45
+ togglePlay();
46
+ }
47
+
48
+ videoEl.current.addEventListener('ended', () => {
49
+ // Used purely to halt the CSS animation:
50
+ setIsRestarting(true);
51
+
52
+ // If this is a non-looping video, add a listener to update our local state
53
+ // once the video's ended, to let the user retrigger it manually:
54
+ if (!loop) {
55
+ setIsPlaying(false);
56
+ setVideoProgress(0);
57
+ // Reload the video to show the poster image:
58
+ if (showPosterAfterPlaying) videoEl.current.load();
59
+ } else {
60
+ // Rather than using the Video 'loop' property, we retrigger
61
+ // it in *code* as there's no 'restarted' to hook into:
62
+ togglePlay();
63
+ }
64
+
65
+ // Grace period to allow the animation to reset
66
+ setTimeout(() => { setIsRestarting(false); }, 100);
67
+ });
68
+ }
69
+ // CERTAINLY don't want this re-running for EACH of these variable updates, sorry Lint...
70
+ // eslint-disable-next-line react-hooks/exhaustive-deps
71
+ }, [thisVideoSrc]);
72
+
73
+ return (
74
+ <>
75
+ <Video
76
+ ref={videoEl}
77
+ src={thisVideoSrc}
78
+ poster={thisPoster}
79
+ muted
80
+ >
81
+ Your browser does not support this video filetype.
82
+ </Video>
83
+ <PromoVideoButton
84
+ copyLeft={copyLeft}
85
+ videoProgress={videoProgress}
86
+ togglePlay={togglePlay}
87
+ isPlaying={isPlaying}
88
+ lightColouredVideo={lightColouredVideo}
89
+ isRestarting={isRestarting}
90
+ />
91
+ </>
92
+ );
93
+ };
94
+
95
+ PromoVideo.propTypes = {
96
+ copyLeft: PropTypes.bool.isRequired,
97
+ lightColouredVideo: PropTypes.bool.isRequired,
98
+ thisVideoSrc: PropTypes.string.isRequired,
99
+ thisPoster: PropTypes.string.isRequired,
100
+ autoPlay: PropTypes.bool.isRequired,
101
+ loop: PropTypes.bool.isRequired,
102
+ showPosterAfterPlaying: PropTypes.bool.isRequired
103
+ };
104
+
105
+ export default PromoVideo;
@@ -0,0 +1,26 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import ProgressRing from './_ProgressRing';
4
+ import { PlayButtonWrapper, PlayButton, Icon } from './_PromoVideoButton.style';
5
+
6
+ const PromoVideoButton = ({
7
+ togglePlay, ...rest
8
+ }) => (
9
+ <PlayButtonWrapper>
10
+ <PlayButton
11
+ onClick={() => { togglePlay(); }}
12
+ {...rest}
13
+ >
14
+ <Icon {...rest} />
15
+ <ProgressRing
16
+ {...rest}
17
+ />
18
+ </PlayButton>
19
+ </PlayButtonWrapper>
20
+ );
21
+
22
+ PromoVideoButton.propTypes = {
23
+ togglePlay: PropTypes.func.isRequired
24
+ };
25
+
26
+ export default PromoVideoButton;
@@ -0,0 +1,121 @@
1
+ import styled, { css } from 'styled-components';
2
+ import playWhite from './assets/Play--white.svg';
3
+ import pauseWhite from './assets/Pause--white.svg';
4
+ import playBlack from './assets/Play--black.svg';
5
+ import pauseBlack from './assets/Pause--black.svg';
6
+
7
+ const PlayButton = styled.button`
8
+ display: flex;
9
+ width: 50px;
10
+ height: 50px;
11
+ position: absolute;
12
+ top: 10px;
13
+ right: 10px;
14
+ align-items: center;
15
+ justify-content: center;
16
+ border: 0;
17
+ padding: 0;
18
+ margin: 0;
19
+ border-radius: 50%;
20
+ background: ${({ theme }) => theme.color('black')};
21
+ opacity: 0.75;
22
+ transition: opacity 0.2s;
23
+
24
+ &:hover,
25
+ &:focus {
26
+ opacity: 1.0;
27
+ }
28
+
29
+ &:focus-visible {
30
+ outline-style: outset;
31
+ outline-width: medium;
32
+ }
33
+
34
+ // Non-light coloured video:
35
+ ${({ lightColouredVideo }) => !lightColouredVideo && css`
36
+ background: ${({ theme }) => theme.color('white')};
37
+ `}
38
+
39
+ ${({ copyLeft }) => !copyLeft && css`
40
+ right: auto;
41
+ left: 10px;
42
+ `}
43
+
44
+ @media ${({ theme }) => theme.breakpoint('medium')} {
45
+ top: 20px;
46
+ right: 20px;
47
+
48
+ ${({ copyLeft }) => !copyLeft && css`
49
+ right: auto;
50
+ left: 20px;
51
+ `}
52
+ }
53
+ `;
54
+
55
+ const PlayButtonWrapper = styled.div`
56
+ width: 100%;
57
+ height: 100%;
58
+ position: absolute;
59
+ top: 0;
60
+ left: 0;
61
+ `;
62
+
63
+ const ProgressRingWrapper = styled.span`
64
+ position: absolute;
65
+ width: 100%;
66
+ height: 100%;
67
+ display: flex;
68
+ `;
69
+
70
+ const ProgressRingSVG = styled.svg`
71
+ //
72
+ `;
73
+
74
+ const ProgressRingCircle = styled.circle`
75
+ z-index: 100;
76
+ transition: stroke-dashoffset 0.5s;
77
+
78
+ transform: rotate(-90deg);
79
+ transform-origin: 50% 50%;
80
+ stroke-dashoffset: ${({ strokeDashOffsetStyle }) => strokeDashOffsetStyle};
81
+ stroke: white; // need to changed based on Promo settings
82
+ fill: transparent;
83
+
84
+ // Non-light coloured video, white button, black stroke
85
+ ${({ lightColouredVideo }) => !lightColouredVideo && css`
86
+ stroke: ${({ theme }) => theme.color('black')};
87
+ `}
88
+
89
+ // Cancel the animation
90
+ ${({ isRestarting }) => (isRestarting) && css`
91
+ transition: none;
92
+ `}
93
+ `;
94
+
95
+ const Icon = styled.span`
96
+ height: 50px;
97
+ width: 50px;
98
+ background: no-repeat center/50% url(${playWhite}) transparent;
99
+
100
+ // Light-coloured video, black button, white icons
101
+ ${({ lightColouredVideo, isPlaying }) => (lightColouredVideo && !isPlaying) && css`
102
+ background-image: url(${playWhite});
103
+ `}
104
+
105
+ ${({ lightColouredVideo, isPlaying }) => (lightColouredVideo && isPlaying) && css`
106
+ background-image: url(${pauseWhite});
107
+ `}
108
+
109
+ // Non-light coloured video, white button, black icons
110
+ ${({ lightColouredVideo, isPlaying }) => (!lightColouredVideo && !isPlaying) && css`
111
+ background-image: url(${playBlack});
112
+ `}
113
+
114
+ ${({ lightColouredVideo, isPlaying }) => (!lightColouredVideo && isPlaying) && css`
115
+ background-image: url(${pauseBlack});
116
+ `}
117
+ `;
118
+
119
+ export {
120
+ ProgressRingWrapper, ProgressRingSVG, ProgressRingCircle, PlayButton, PlayButtonWrapper, Icon
121
+ };
@@ -0,0 +1,3 @@
1
+ <svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M12 6C10.8954 6 10 6.89543 10 8V40C10 41.1046 10.8954 42 12 42H17C18.1046 42 19 41.1046 19 40V8C19 6.89543 18.1046 6 17 6H12ZM31 6C29.8954 6 29 6.89543 29 8V40C29 41.1046 29.8954 42 31 42H36C37.1046 42 38 41.1046 38 40V8C38 6.89543 37.1046 6 36 6H31Z" fill="#222222"/>
3
+ </svg>
@@ -0,0 +1,3 @@
1
+ <svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path fill-rule="evenodd" clip-rule="evenodd" d="M12 6C10.8954 6 10 6.89543 10 8V40C10 41.1046 10.8954 42 12 42H17C18.1046 42 19 41.1046 19 40V8C19 6.89543 18.1046 6 17 6H12ZM31 6C29.8954 6 29 6.89543 29 8V40C29 41.1046 29.8954 42 31 42H36C37.1046 42 38 41.1046 38 40V8C38 6.89543 37.1046 6 36 6H31Z" fill="#FFFFFF"/>
3
+ </svg>
@@ -0,0 +1,3 @@
1
+ <svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path d="M39.6845 22.5846L11.457 6.22056C10.9505 5.92706 10.326 5.92656 9.819 6.21856C9.3125 6.51056 9 7.05156 9 7.63656V40.3641C9 40.9491 9.3125 41.4901 9.8195 41.7821C10.0725 41.9276 10.3545 42.0001 10.6365 42.0001C10.92 42.0001 11.2035 41.9266 11.4575 41.7796L39.685 25.4161C40.189 25.1231 40.5 24.5836 40.5 24.0001C40.5 23.4166 40.189 22.8771 39.6845 22.5846Z" fill="#222222"/>
3
+ </svg>
@@ -0,0 +1,3 @@
1
+ <svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path d="M39.6845 22.5846L11.457 6.22056C10.9505 5.92706 10.326 5.92656 9.819 6.21856C9.3125 6.51056 9 7.05156 9 7.63656V40.3641C9 40.9491 9.3125 41.4901 9.8195 41.7821C10.0725 41.9276 10.3545 42.0001 10.6365 42.0001C10.92 42.0001 11.2035 41.9266 11.4575 41.7796L39.685 25.4161C40.189 25.1231 40.5 24.5836 40.5 24.0001C40.5 23.4166 40.189 22.8771 39.6845 22.5846Z" fill="#FFFFFF"/>
3
+ </svg>