-
Notifications
You must be signed in to change notification settings - Fork 210
/
index.jsx
152 lines (137 loc) · 3.58 KB
/
index.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import React, { useContext } from 'react';
import { shape, node, string, number } from 'prop-types';
import styled from '@emotion/styled';
import pick from 'ramda/src/pick';
import { getSerifRegular } from '@bbc/psammead-styles/font-styles';
import { GEL_GROUP_2_SCREEN_WIDTH_MIN } from '@bbc/gel-foundations/breakpoints';
import {
GEL_SPACING,
GEL_SPACING_HLF_TRPL,
GEL_SPACING_DBL,
} from '@bbc/gel-foundations/spacings';
import useClickTrackerHandler from '#hooks/useClickTrackerHandler';
import { ServiceContext } from '#contexts/ServiceContext';
import FrostedGlassPanel from './FrostedGlassPanel';
import ImageWithPlaceholder from '../../containers/ImageWithPlaceholder';
import withData from './withData';
const Wrapper = styled.div`
display: inline-block;
position: relative;
width: 100%;
max-width: 400px;
text-decoration: none;
&:hover {
a {
text-decoration: underline;
}
}
`;
// This is an unfocusable element, hidden from screenreaders
// This is to allow mouse users to click anywhere on the promo
// But for keyboard and AT users, and scenarios where there is no CSS,
// only the link inside the H3 should be interactive
const ClickableArea = styled.a`
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: 5;
`;
const H3 = styled.h3`
margin: 0;
`;
const A = styled.a`
display: inline-block;
${({ service }) => service && getSerifRegular(service)}
text-decoration: none;
color: white;
font-size: 0.9375rem;
line-height: 1.33;
margin: 0.625rem ${GEL_SPACING} 0 ${GEL_SPACING};
@media (min-width: ${GEL_GROUP_2_SCREEN_WIDTH_MIN}) {
font-size: 1rem;
line-height: 1.25;
margin: ${GEL_SPACING_HLF_TRPL} ${GEL_SPACING_DBL} 0 ${GEL_SPACING_DBL};
}
&:focus {
text-decoration: underline;
}
`;
const FrostedGlassPromo = ({
image,
children,
footer,
url,
eventTrackingData,
index,
}) => {
const { script, service } = useContext(ServiceContext);
const clickTracker = useClickTrackerHandler({
...(eventTrackingData || {}),
url,
});
const onClick = eventTrackingData ? clickTracker : () => {};
// The ClickableArea component is an anchor ("a") element
// Anchors cannot be self-closing under the HTML spec
/* eslint-disable react/self-closing-comp */
return (
<Wrapper data-testid={`frosted-promo-${index}`}>
<ClickableArea
href={url}
onClick={onClick}
aria-hidden="true"
tabIndex="-1"
></ClickableArea>
<ImageWithPlaceholder
darkMode
{...pick(
[
'src',
'srcset',
'fallbackSrcset',
'primaryMimeType',
'fallbackMimeType',
'sizes',
'alt',
'ratio',
'width',
'height',
],
image,
)}
/>
<FrostedGlassPanel image={image.smallSrc || image.src}>
<H3>
<A script={script} service={service} href={url} onClick={onClick}>
{children}
</A>
</H3>
{footer}
</FrostedGlassPanel>
</Wrapper>
);
};
FrostedGlassPromo.propTypes = {
children: node.isRequired,
url: string.isRequired,
footer: node,
eventTrackingData: shape({}),
index: number,
image: shape({
src: string.isRequired,
alt: string.isRequired,
ratio: number.isRequired,
width: number.isRequired,
height: number.isRequired,
smallSrc: string,
srcset: string,
sizes: string,
}).isRequired,
};
FrostedGlassPromo.defaultProps = {
footer: null,
eventTrackingData: null,
index: 0,
};
export default withData(FrostedGlassPromo);