@comicrelief/component-library 8.85.0 → 8.86.1

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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@comicrelief/component-library",
3
3
  "author": "Comic Relief Engineering Team",
4
- "version": "8.85.0",
4
+ "version": "8.86.1",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.es.js",
7
7
  "files": [
@@ -22,7 +22,7 @@ const StyledLink = styled.a`
22
22
  }
23
23
 
24
24
  // 'isHeader' is only set and passed in Header2025, so the pre-existing Header isn't affected:
25
- ${({ $isHeader }) => $isHeader && css`
25
+ ${({ $isHeader, theme }) => $isHeader && css`
26
26
  overflow: hidden;
27
27
  padding-right: 0px;
28
28
  transition: padding-right ${RevealTextSpeed}s cubic-bezier(0.5, 1.5, 0.5, 0.75);
@@ -33,7 +33,7 @@ const StyledLink = styled.a`
33
33
  }
34
34
 
35
35
  // No hover state for mobile, so targetting Medium+:
36
- @media ${({ theme }) => theme.breakpoints2026('M')} {
36
+ @media ${theme.breakpoints2026('M')} {
37
37
  &:hover,
38
38
  &:focus {
39
39
  img {
@@ -42,7 +42,7 @@ const StyledLink = styled.a`
42
42
  }
43
43
  }
44
44
 
45
- @media ${({ theme }) => theme.allBreakpoints('NavWide')} {
45
+ @media ${theme.allBreakpoints('NavWide')} {
46
46
  &:hover,
47
47
  &:focus {
48
48
  // Default
@@ -80,14 +80,17 @@ const StyledLink = styled.a`
80
80
  width: 100%;
81
81
  height: 100%;
82
82
 
83
- ${springScaleAnimation(true, 1.15, 2)}
84
-
85
83
  &:hover,
86
84
  &:focus {
87
85
  background-color: ${theme.color('grey_4_hover')};
88
86
  opacity: 1;
89
87
  }
90
88
  `}
89
+
90
+ // springScaleAnimation must stay a sibling — nesting it inside the $newStyle css block
91
+ // breaks flattening so rules, it bugs out and compiles as global :hover, :focus
92
+ // when you try to combine them
93
+ ${({ $newStyle }) => $newStyle && springScaleAnimation(true, 1.15, 2)}
91
94
  `;
92
95
 
93
96
  const RevealText = styled(Text)`
@@ -89,13 +89,13 @@ exports[`does not render copyright text when not supplied 1`] = `
89
89
 
90
90
  .c29:hover,
91
91
  .c29:focus {
92
- transform: scale(1.15);
92
+ background-color: #3A3A3A;
93
+ opacity: 1;
93
94
  }
94
95
 
95
96
  .c29:hover,
96
97
  .c29:focus {
97
- background-color: #3A3A3A;
98
- opacity: 1;
98
+ transform: scale(1.15);
99
99
  }
100
100
 
101
101
  .c31 {
@@ -1429,13 +1429,13 @@ exports[`renders correctly 1`] = `
1429
1429
 
1430
1430
  .c29:hover,
1431
1431
  .c29:focus {
1432
- transform: scale(1.15);
1432
+ background-color: #3A3A3A;
1433
+ opacity: 1;
1433
1434
  }
1434
1435
 
1435
1436
  .c29:hover,
1436
1437
  .c29:focus {
1437
- background-color: #3A3A3A;
1438
- opacity: 1;
1438
+ transform: scale(1.15);
1439
1439
  }
1440
1440
 
1441
1441
  .c31 {
@@ -0,0 +1,129 @@
1
+ import React, {
2
+ useCallback, useEffect, useMemo, useRef
3
+ } from 'react';
4
+ import PropTypes from 'prop-types';
5
+ import { Splide, SplideSlide } from '@splidejs/react-splide';
6
+ import '../../../vendor/splide/splide.min.css';
7
+
8
+ import { CarouselCard, SlideCopy, Wrapper } from './MoneybuysCarousel.style';
9
+
10
+ // Safeguard incase number is passed as a string
11
+ const normalizeAmount = value => {
12
+ if (value === '' || value === null || value === undefined) {
13
+ return null;
14
+ }
15
+
16
+ const parsed = Number(value);
17
+ return Number.isFinite(parsed) ? parsed : null;
18
+ };
19
+
20
+ const findMatchingIndex = (moneybuys, currentAmount) => {
21
+ const normalizedAmount = normalizeAmount(currentAmount);
22
+
23
+ if (normalizedAmount === null) {
24
+ return -1;
25
+ }
26
+
27
+ return moneybuys.findIndex(
28
+ ({ amount }) => Number(amount) === normalizedAmount
29
+ );
30
+ };
31
+
32
+ const MoneybuysCarousel = ({
33
+ moneybuys,
34
+ currentAmount,
35
+ currency = '£'
36
+ }) => {
37
+ const splideRef = useRef(null);
38
+ const matchedIndex = useMemo(
39
+ () => findMatchingIndex(moneybuys, currentAmount),
40
+ [moneybuys, currentAmount]
41
+ );
42
+ const hasMultipleSlides = moneybuys.length > 1;
43
+ const shouldAutoplay = matchedIndex < 0 && hasMultipleSlides;
44
+
45
+ const splideOptions = useMemo(() => ({
46
+ speed: 750,
47
+ rewindSpeed: 750,
48
+ interval: 5000,
49
+ arrows: hasMultipleSlides,
50
+ pagination: hasMultipleSlides,
51
+ drag: 'free',
52
+ flickPower: 50,
53
+ perMove: 1,
54
+ dragMinThreshold: { mouse: 50, touch: 50 },
55
+ updateOnMove: true,
56
+ snap: true,
57
+ autoplay: shouldAutoplay,
58
+ pauseOnHover: true,
59
+ pauseOnFocus: true,
60
+ perPage: 1,
61
+ rewind: true,
62
+ padding: { left: '1rem', right: '1rem' }
63
+ }), [hasMultipleSlides, shouldAutoplay]);
64
+
65
+ const goToMatchedSlide = useCallback(splideInstance => {
66
+ if (matchedIndex < 0 || !splideInstance) {
67
+ return;
68
+ }
69
+
70
+ if (splideInstance.index !== matchedIndex) {
71
+ splideInstance.go(matchedIndex);
72
+ }
73
+ }, [matchedIndex]);
74
+
75
+ // The useEffect is only for changes, this is necessary
76
+ // to get it to the right slide on mount
77
+ const handleMounted = useCallback(splide => {
78
+ goToMatchedSlide(splide);
79
+ }, [goToMatchedSlide]);
80
+
81
+ useEffect(() => {
82
+ const splideInstance = splideRef.current?.splide;
83
+ goToMatchedSlide(splideInstance);
84
+ }, [goToMatchedSlide]);
85
+
86
+ if (!moneybuys?.length) {
87
+ return null;
88
+ }
89
+
90
+ return (
91
+ <Wrapper data-testid="moneybuys-carousel">
92
+ <CarouselCard>
93
+ <Splide
94
+ ref={splideRef}
95
+ className="moneybuys-carousel"
96
+ data-testid="moneybuys-carousel--splide"
97
+ aria-label="What your money buys"
98
+ options={splideOptions}
99
+ onMounted={handleMounted}
100
+ >
101
+ {moneybuys.map(({ amount, description }) => (
102
+ <SplideSlide
103
+ key={`${amount}-${description}`}
104
+ data-testid="moneybuys-carousel--slide"
105
+ data-amount={amount}
106
+ aria-label={`${currency}${amount}: ${description}`}
107
+ >
108
+ <SlideCopy data-testid="moneybuys-carousel--copy">
109
+ <strong>{`${currency}${amount}`}</strong>
110
+ {` ${description}`}
111
+ </SlideCopy>
112
+ </SplideSlide>
113
+ ))}
114
+ </Splide>
115
+ </CarouselCard>
116
+ </Wrapper>
117
+ );
118
+ };
119
+
120
+ MoneybuysCarousel.propTypes = {
121
+ moneybuys: PropTypes.arrayOf(PropTypes.shape({
122
+ amount: PropTypes.oneOfType([PropTypes.number, PropTypes.string]).isRequired,
123
+ description: PropTypes.string.isRequired
124
+ })).isRequired,
125
+ currentAmount: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
126
+ currency: PropTypes.string
127
+ };
128
+
129
+ export default MoneybuysCarousel;
@@ -0,0 +1,127 @@
1
+ import styled from 'styled-components';
2
+ import spacing from '../../../theme/shared/spacing';
3
+
4
+ const Wrapper = styled.div`
5
+ width: 100%;
6
+ `;
7
+
8
+ const CarouselCard = styled.div`
9
+ position: relative;
10
+ width: 100%;
11
+ margin-bottom: ${spacing('l')};
12
+
13
+ .splide {
14
+ position: relative;
15
+ width: 100%;
16
+ }
17
+
18
+ .splide__track {
19
+ overflow: hidden;
20
+ background-color: ${({ theme }) => theme.color('white')};
21
+ border: 1px solid ${({ theme }) => theme.color('grey_medium')};
22
+ border-radius: 10px;
23
+ /*
24
+ Can only set vertical padding on the track;
25
+ inline padding has to be set via splide's padding option.
26
+ */
27
+ padding-top: ${spacing('l')};
28
+ padding-bottom: ${spacing('l')};
29
+ box-sizing: border-box;
30
+ }
31
+
32
+ .splide__slide {
33
+ display: flex;
34
+ align-items: center;
35
+ justify-content: center;
36
+ text-align: center;
37
+ }
38
+
39
+ .splide__arrows {
40
+ position: absolute;
41
+ top: 0;
42
+ left: 0;
43
+ width: 100%;
44
+ height: 100%;
45
+ pointer-events: none;
46
+ }
47
+
48
+ button.splide__arrow {
49
+ pointer-events: auto;
50
+ background-color: ${({ theme }) => theme.color('grey_light')};
51
+ border: 1px solid ${({ theme }) => theme.color('grey_medium')};
52
+ opacity: 1;
53
+ width: 1.5rem;
54
+ height: 1.5rem;
55
+ transform: translateY(-50%);
56
+
57
+ svg {
58
+ fill: ${({ theme }) => theme.color('black')};
59
+ height: 0.8rem;
60
+ width: 0.8rem;
61
+ }
62
+
63
+ &:hover {
64
+ background-color: ${({ theme }) => theme.color('grey_3')};
65
+ }
66
+ }
67
+
68
+ .splide__arrow--prev {
69
+ left: 1rem;
70
+ }
71
+
72
+ .splide__arrow--next {
73
+ right: 1rem;
74
+ }
75
+
76
+ .splide__pagination {
77
+ position: absolute;
78
+ top: calc(100% + ${spacing('sm')});
79
+ left: 0;
80
+ right: 0;
81
+ padding: 0;
82
+ margin: 0;
83
+ background: transparent;
84
+ pointer-events: auto;
85
+
86
+ & li > button {
87
+ height: 6px;
88
+ width: 6px;
89
+ }
90
+ }
91
+
92
+ .splide__pagination__page {
93
+ background: transparent;
94
+ border: 1px solid ${({ theme }) => theme.color('black')};
95
+ height: 10px;
96
+ width: 10px;
97
+ margin: 4px;
98
+ opacity: 1;
99
+ transform: none;
100
+
101
+ &.is-active {
102
+ background: ${({ theme }) => theme.color('black')};
103
+ }
104
+
105
+ &:hover {
106
+ cursor: pointer;
107
+ opacity: 0.85;
108
+ }
109
+ }
110
+ `;
111
+
112
+ const SlideCopy = styled.p`
113
+ margin: 0;
114
+ padding: 0 4.5rem;
115
+ font-size: ${({ theme }) => theme.fontSize('s')};
116
+ font-family: ${({ theme }) => theme.fontFamilies('Montserrat')};
117
+ line-height: 20px;
118
+ color: ${({ theme }) => theme.color('black')};
119
+
120
+ strong {
121
+ font-weight: 700;
122
+ }
123
+ `;
124
+
125
+ export {
126
+ Wrapper, CarouselCard, SlideCopy
127
+ };
@@ -0,0 +1,50 @@
1
+ import React, { useState } from 'react';
2
+ import styled from 'styled-components';
3
+ import MoneybuysCarousel from './MoneybuysCarousel';
4
+ import Input from '../../Atoms/Input/Input';
5
+ import { ExampleContainer } from '../../../demos/SharedStyles';
6
+
7
+ const exampleMoneybuys = [
8
+ {
9
+ amount: 20,
10
+ description: 'could help stock a community pantry, so local people can access essential support closer to home.'
11
+ },
12
+ {
13
+ amount: 50,
14
+ description: 'could support a young person experiencing homelessness to access somewhere safe and warm to sleep for the night.'
15
+ },
16
+ {
17
+ amount: 100,
18
+ description: 'could help youth workers run workshops for young people, helping them learn new skills and realise their potential.'
19
+ }
20
+ ];
21
+
22
+ const DemoWidth = styled.div`
23
+ width: 500px;
24
+ max-width: 100%;
25
+ margin-top: 1rem;
26
+ `;
27
+
28
+ export default function MoneybuysCarouselExample() {
29
+ const [currentAmount, setCurrentAmount] = useState('');
30
+
31
+ return (
32
+ <ExampleContainer>
33
+ <Input
34
+ label="Use this input to simulate the amount being input elsewhere"
35
+ showLabel
36
+ type="number"
37
+ name="moneybuys-carousel-current-amount"
38
+ id="moneybuys-carousel-current-amount"
39
+ value={currentAmount}
40
+ onChange={(event) => setCurrentAmount(event.target.value)}
41
+ />
42
+ <DemoWidth>
43
+ <MoneybuysCarousel
44
+ moneybuys={exampleMoneybuys}
45
+ currentAmount={currentAmount}
46
+ />
47
+ </DemoWidth>
48
+ </ExampleContainer>
49
+ );
50
+ }
@@ -147,6 +147,8 @@ import { ImpactSliderExample1, ImpactSliderExample2 } from '../components/Organi
147
147
 
148
148
  import WYMDCarouselExample from '../components/Organisms/WYMDCarousel/WYMDCarouselExample.jsx';
149
149
  import wymdCarouselExampleSrc from '../components/Organisms/WYMDCarousel/WYMDCarouselExample.jsx?raw';
150
+ import MoneybuysCarouselExample from '../components/Organisms/MoneybuysCarousel/MoneybuysCarouselExample.jsx';
151
+ import moneybuysCarouselExampleSrc from '../components/Organisms/MoneybuysCarousel/MoneybuysCarouselExample.jsx?raw';
150
152
 
151
153
  import RichtextCarouselExample from '../components/Organisms/RichtextCarousel/RichtextCarouselExample.jsx';
152
154
  import richtextCarouselExampleSrc from '../components/Organisms/RichtextCarousel/RichtextCarouselExample.jsx?raw';
@@ -249,6 +251,7 @@ export const organisms = [
249
251
  { name: 'ImpactSlider', examples: [<ImpactSliderExample1 />, <ImpactSliderExample2 />] },
250
252
  { name: 'MarketingPreferencesDSForm', demo: demo(MarketingPreferencesDSExample, marketingPreferencesDSExampleSrc) },
251
253
  { name: 'Membership', examples: [<MembershipExample1 />, <MembershipExample2 />] },
254
+ { name: 'MoneybuysCarousel', demo: demo(MoneybuysCarouselExample, moneybuysCarouselExampleSrc) },
252
255
  { name: 'RichtextCarousel', demo: demo(RichtextCarouselExample, richtextCarouselExampleSrc) },
253
256
  { name: 'WYMDCarousel', demo: demo(WYMDCarouselExample, wymdCarouselExampleSrc) }
254
257
  ];
package/src/index.js CHANGED
@@ -97,3 +97,4 @@ export { default as Membership } from './components/Organisms/Membership/Members
97
97
  export { default as RichtextCarousel } from './components/Organisms/RichtextCarousel/RichtextCarousel';
98
98
  export { default as WYMDCarousel } from './components/Organisms/WYMDCarousel/WYMDCarousel';
99
99
  export { default as FilterCard } from './components/Organisms/FilterCard/FilterCard';
100
+ export { default as MoneybuysCarousel } from './components/Organisms/MoneybuysCarousel/MoneybuysCarousel';