Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@ export const WithLongLabel = {
label: '£10.99 for a 5 x 5 x 50cm sheet at Amazon',
},
} satisfies Story;

export const WithStruckThroughLabel = {
args: {
label: '~£10~ £5 at Amazon',
},
} satisfies Story;
16 changes: 13 additions & 3 deletions dotcom-rendering/src/components/Button/ProductLinkButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import type {
import { LinkButton } from '@guardian/source/react-components';
import { SKIMLINK_REL } from '../../lib/affiliateLinksUtils';
import { palette } from '../../palette';
import {
createAccessibleProductLabel,
createStrikeThroughProductLabel,
} from './productUtils';
import { heightAutoStyle, wrapButtonTextStyle } from './styles';
import { getPropsForLinkUrl } from './utils';

Expand Down Expand Up @@ -37,6 +41,12 @@ const minimisePaddingStyle = css`
}
`;

const strikeThroughStyle = css`
s {
font-weight: normal;
}
`;

export const theme: Partial<ThemeButton> = {
backgroundPrimary: palette('--product-button-primary-background'),
backgroundPrimaryHover: palette(
Expand Down Expand Up @@ -67,7 +77,7 @@ export const ProductLinkButton = ({

return (
<LinkButton
{...getPropsForLinkUrl(label)}
{...getPropsForLinkUrl(createAccessibleProductLabel(label))}
href={url}
rel={SKIMLINK_REL}
priority={priority}
Expand All @@ -82,9 +92,9 @@ export const ProductLinkButton = ({
>
<span
style={fullWidthText ? { width: '100%' } : {}}
css={wrapButtonTextStyle}
css={[wrapButtonTextStyle, strikeThroughStyle]}
>
{label}
{createStrikeThroughProductLabel(label)}
</span>
</LinkButton>
);
Expand Down
37 changes: 37 additions & 0 deletions dotcom-rendering/src/components/Button/productUtils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { render } from '@testing-library/react';

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disclosure: copilot mostly wrote these tests

import {
createAccessibleProductLabel,
createStrikeThroughProductLabel,
} from './productUtils';

describe('createStrikeThroughProductLabel', () => {
it('returns the label unchanged when there is no struck-through text', () => {
const { container } = render(
<>{createStrikeThroughProductLabel('£5 at Shop')}</>,
);

expect(container.textContent).toBe('£5 at Shop');
expect(container.querySelector('s')).toBeNull();
});

it('renders the old price in an s element and preserves the rest', () => {
const { container } = render(
<>{createStrikeThroughProductLabel('~£10~ £5 at Shop')}</>,
);

expect(container.querySelector('s')).toHaveTextContent('£10');
expect(container.textContent).toBe('£10 £5 at Shop');
});
});

describe('createAccessibleProductLabel', () => {
it('returns the label unchanged when there is no struck-through text', () => {
expect(createAccessibleProductLabel('£5 at Shop')).toBe('£5 at Shop');
});

it('describes the old and new prices accessibly', () => {
expect(createAccessibleProductLabel('~£10~ £5 at Shop')).toBe(
'Was £10, now £5 at Shop',
);
});
Comment on lines +32 to +36

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is "now £5" meant to have the double space?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good spot, addressed in 306c1df.
This is what happens when you trust copilot

});
68 changes: 68 additions & 0 deletions dotcom-rendering/src/components/Button/productUtils.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { isUndefined } from '@guardian/libs';
import type { ProductCta } from '../../types/content';

const strikeThroughRegex = /~([^~]+)~(.*)/;

type ParsedProductLabel = {
struckThrough: string;
restOfLabel: string;
};

const parseProductLabel = (label: string): ParsedProductLabel | undefined => {
const match = label.match(strikeThroughRegex);
const struckThrough = match?.[1];
const restOfLabel = match?.[2];

if (isUndefined(struckThrough) || isUndefined(restOfLabel)) {
return undefined;
}

return { struckThrough, restOfLabel };
};

/**
* Create a struck through React component from a label
* for a product link button or product CTA
* that may contain a strikethroughed price
* @param label the label text that may contain strikethrough eg '~£10~ £5 at Shop'
*/
export const createStrikeThroughProductLabel = (label: string) => {
const parsedLabel = parseProductLabel(label);

if (isUndefined(parsedLabel)) {
return label;
} else {
return (
<>
<s>{parsedLabel.struckThrough}</s>
{parsedLabel.restOfLabel}
</>
);
}
};

/**
* Create accessible label text
* for a product link button or product CTA
* that may contain a struck through price
* @param label the label text that may contain strikethrough eg '~£10~ £5 at Shop'
*/
export const createAccessibleProductLabel = (label: string): string => {
const parsedLabel = parseProductLabel(label);
if (isUndefined(parsedLabel)) {
return label;
} else {
return `Was ${parsedLabel.struckThrough}, now ${parsedLabel.restOfLabel.trimStart()}`;
}
};

export const getProductLinkLabelWithoutPrice = (
cardCta: ProductCta,
): string => {
return cardCta.text !== '' ? cardCta.text : `Buy at ${cardCta.retailer}`;
};

export const getProductLinkLabelWithPrice = (cta: ProductCta): string => {
const overrideLabel = cta.text.trim().length > 0;
return overrideLabel ? cta.text : `${cta.price} at ${cta.retailer}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
textSansBold17,
} from '@guardian/source/foundations';
import { Link } from '@guardian/source/react-components';
import { getProductLinkLabelWithoutPrice } from '../lib/affiliateLinksUtils';
import type { ArticleFormat } from '../lib/articleFormat';
import { palette } from '../palette';
import type { SummaryProduct } from '../types/content';
import { ProductLinkButton } from './Button/ProductLinkButton';
import { getProductLinkLabelWithoutPrice } from './Button/productUtils';
import { ProductCardImage } from './ProductCardImage';

const horizontalCard = css`
Expand Down
2 changes: 1 addition & 1 deletion dotcom-rendering/src/components/ProductCardButtons.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ThemeButton } from '@guardian/source/react-components';
import { getProductLinkLabelWithPrice } from '../lib/affiliateLinksUtils';
import type { ProductCta } from '../types/content';
import { ProductLinkButton } from './Button/ProductLinkButton';
import { getProductLinkLabelWithPrice } from './Button/productUtils';

export const ProductCardButtons = ({
productCtas,
Expand Down
15 changes: 15 additions & 0 deletions dotcom-rendering/src/components/ProductCardInline.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,18 @@ export const ProductCardOnlyDisplayCredit = meta.story({
image: { ...productImage, displayCredit: true },
},
});

export const WithStrikeThroughPrice = meta.story({
args: {
...meta.input.args,
productCtas: [
{
url: 'https://www.theguardian.com',
retailer: 'Amazon',
text: '',
price: '~£95.99~ £89.99',
},
...meta.input.args.productCtas.slice(1),
],
},
});
2 changes: 1 addition & 1 deletion dotcom-rendering/src/components/ProductCarouselCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
textSansBold17,
} from '@guardian/source/foundations';
import { Link } from '@guardian/source/react-components';
import { getProductLinkLabelWithoutPrice } from '../lib/affiliateLinksUtils';
import type { ArticleFormat } from '../lib/articleFormat';
import { palette } from '../palette';
import type { SummaryProduct } from '../types/content';
import { ProductLinkButton } from './Button/ProductLinkButton';
import { getProductLinkLabelWithoutPrice } from './Button/productUtils';
import { ProductCardImage } from './ProductCardImage';

export type ProductCarouselCardProps = {
Expand Down
2 changes: 1 addition & 1 deletion dotcom-rendering/src/components/ProductCtaList.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { css } from '@emotion/react';
import { article17, palette, remSpace } from '@guardian/source/foundations';
import { getProductLinkLabelWithPrice } from '../lib/affiliateLinksUtils';
import type { ArticleFormat } from '../lib/articleFormat';
import type { SummaryProduct } from '../types/content';
import { ProductLinkButton } from './Button/ProductLinkButton';
import { getProductLinkLabelWithPrice } from './Button/productUtils';
import { Subheading } from './Subheading';

const listStyles = css`
Expand Down
12 changes: 0 additions & 12 deletions dotcom-rendering/src/lib/affiliateLinksUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ABParticipations } from '../experiments/lib/ab-tests';
import type { ProductCta } from '../types/content';

export const SKIMLINK_REL = 'sponsored noreferrer noopener';

Expand Down Expand Up @@ -138,14 +137,3 @@ export const buildXcustParamForAffiliateLink = ({
xcustComponentId,
});
};

export const getProductLinkLabelWithoutPrice = (
cardCta: ProductCta,
): string => {
return cardCta.text !== '' ? cardCta.text : `Buy at ${cardCta.retailer}`;
};

export const getProductLinkLabelWithPrice = (cta: ProductCta): string => {
const overrideLabel = cta.text.trim().length > 0;
return overrideLabel ? cta.text : `${cta.price} at ${cta.retailer}`;
};
Loading