-
Notifications
You must be signed in to change notification settings - Fork 34
Support strike through in product buttons #16705
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e754316
feat: support strike through in product buttons
andrewHEguardian f6b079e
Merge branch 'main' into ahe/product-button-strike-through
andrewHEguardian 306c1df
fix accessible product label double spacing
andrewHEguardian c36d7c3
Merge branch 'main' into ahe/product-button-strike-through
andrewHEguardian 4729bfb
Merge branch 'main' into ahe/product-button-strike-through
andrewHEguardian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
dotcom-rendering/src/components/Button/productUtils.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { render } from '@testing-library/react'; | ||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good spot, addressed in 306c1df. |
||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}`; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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