-
Notifications
You must be signed in to change notification settings - Fork 30
Football mini match stats component #14947
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
Open
jamesmockett
wants to merge
14
commits into
main
Choose a base branch
from
jm/football-stats-mini
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7b918d0
Basic layout and styling for match stat
jamesmockett fab66db
Add divider to chart
jamesmockett 6e670b9
Move hardcoded values to props
jamesmockett 23e5650
Calculate bar width
jamesmockett 110a9bd
Include team name for accessibility
jamesmockett 01cef86
Add FootballMiniMatchStats components
jamesmockett fb333c8
Update match stat stories
jamesmockett a78b44f
Simplified live grid layout for stories
jamesmockett 430e440
Rename stories
jamesmockett 07be145
Support layout variations for use in different contexts
jamesmockett 9b6420b
Hide chart from assistive technology
jamesmockett c042bad
Add football stat colours to palette
jamesmockett bf48cbd
Update dark mode palette based on latest designs
jamesmockett 4a82926
Add button hover colours to palette
jamesmockett 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
79 changes: 79 additions & 0 deletions
79
dotcom-rendering/src/components/FootballMatchStat.stories.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,79 @@ | ||
| import { css } from '@emotion/react'; | ||
| import { space } from '@guardian/source/foundations'; | ||
| import type { Meta, StoryObj } from '@storybook/react-webpack5'; | ||
| import { palette } from '../palette'; | ||
| import { FootballMatchStat } from './FootballMatchStat'; | ||
|
|
||
| const meta = { | ||
| title: 'Components/Football Match Stat', | ||
| component: FootballMatchStat, | ||
| decorators: [ | ||
| (Story) => ( | ||
| <div | ||
| css={css` | ||
| padding: ${space[4]}px; | ||
| background-color: ${palette( | ||
| '--football-live-blog-background', | ||
| )}; | ||
| `} | ||
| > | ||
| <Story /> | ||
| </div> | ||
| ), | ||
| ], | ||
| parameters: { | ||
| viewport: { | ||
| defaultViewport: 'mobileMedium', | ||
| }, | ||
| }, | ||
| } satisfies Meta<typeof FootballMatchStat>; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| export const Default = { | ||
| args: { | ||
| label: 'Goal Attempts', | ||
| home: { | ||
| teamName: 'Manchester United', | ||
| teamColour: '#da020e', | ||
| value: 7, | ||
| }, | ||
| away: { | ||
| teamName: 'Arsenal', | ||
| teamColour: '#023474', | ||
| value: 4, | ||
| }, | ||
| }, | ||
| } satisfies Story; | ||
|
|
||
| export const ShownAsPercentage = { | ||
| args: { | ||
| label: 'Possession', | ||
| home: { | ||
| teamName: 'West Ham', | ||
| teamColour: '#722642', | ||
| value: 39, | ||
| }, | ||
| away: { | ||
| teamName: 'Newcastle', | ||
| teamColour: '#383838', | ||
| value: 61, | ||
| }, | ||
| showPercentage: true, | ||
| }, | ||
| } satisfies Story; | ||
|
|
||
| export const RaisedLabelOnDesktop = { | ||
| args: { | ||
| ...Default.args, | ||
| raiseLabelOnDesktop: true, | ||
| }, | ||
| } satisfies Story; | ||
|
|
||
| export const LargeNumbersOnDesktop = { | ||
| args: { | ||
| ...Default.args, | ||
| largeNumbersOnDesktop: true, | ||
| }, | ||
| } satisfies Story; |
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,164 @@ | ||
| import { css } from '@emotion/react'; | ||
| import { | ||
| from, | ||
| textSansBold14, | ||
| textSansBold15, | ||
| textSansBold20, | ||
| textSansBold28, | ||
| visuallyHidden, | ||
| } from '@guardian/source/foundations'; | ||
| import { palette } from '../palette'; | ||
|
|
||
| const containerCss = css` | ||
| position: relative; | ||
| padding: 5px 10px 10px; | ||
| border: 1px solid ${palette('--football-match-stat-border')}; | ||
| border-radius: 6px; | ||
| &::before { | ||
| position: absolute; | ||
| content: ''; | ||
| left: 50%; | ||
| bottom: 0; | ||
| width: 1px; | ||
| height: 24px; | ||
| background-color: ${palette('--football-match-stat-border')}; | ||
| } | ||
| `; | ||
|
|
||
| const headerCss = css` | ||
| display: grid; | ||
| grid-template-columns: auto 1fr auto; | ||
| grid-template-areas: 'home-stat label away-stat'; | ||
| `; | ||
|
|
||
| const raiseLabelCss = css` | ||
| ${from.desktop} { | ||
| grid-template-areas: | ||
| 'label label label' | ||
| 'home-stat . away-stat'; | ||
| } | ||
| `; | ||
|
|
||
| const labelCss = css` | ||
| ${textSansBold14}; | ||
| grid-area: label; | ||
| justify-self: center; | ||
| color: ${palette('--football-match-stat-name')}; | ||
| ${from.desktop} { | ||
| ${textSansBold15}; | ||
| } | ||
| `; | ||
|
|
||
| const numberCss = css` | ||
| ${textSansBold20}; | ||
| grid-area: home-stat; | ||
| color: var(--match-stat-team-colour); | ||
| `; | ||
|
|
||
| const largeNumberCss = css` | ||
| ${from.desktop} { | ||
| ${textSansBold28} | ||
| } | ||
| `; | ||
|
|
||
| const awayStatCss = css` | ||
| grid-area: away-stat; | ||
| justify-self: end; | ||
| `; | ||
|
|
||
| const chartCss = css` | ||
| position: relative; | ||
| display: flex; | ||
| gap: 10px; | ||
| `; | ||
|
|
||
| const barCss = css` | ||
| height: 8px; | ||
| width: var(--match-stat-percentage); | ||
| background-color: var(--match-stat-team-colour); | ||
| border-radius: 8px; | ||
| `; | ||
|
|
||
| type MatchStatistic = { | ||
| teamName: string; | ||
| teamColour: string; | ||
| value: number; | ||
| }; | ||
|
|
||
| type Props = { | ||
| label: string; | ||
| home: MatchStatistic; | ||
| away: MatchStatistic; | ||
| showPercentage?: boolean; | ||
| raiseLabelOnDesktop?: boolean; | ||
| largeNumbersOnDesktop?: boolean; | ||
| }; | ||
|
|
||
| const formatValue = (value: number, showPercentage: boolean) => | ||
| `${value}${showPercentage ? '%' : ''}`; | ||
|
|
||
| export const FootballMatchStat = ({ | ||
| label, | ||
| home, | ||
| away, | ||
| showPercentage = false, | ||
| raiseLabelOnDesktop = false, | ||
| largeNumbersOnDesktop = false, | ||
| }: Props) => { | ||
| const homePercentage = (home.value / (home.value + away.value)) * 100; | ||
| const awayPercentage = (away.value / (home.value + away.value)) * 100; | ||
|
|
||
| return ( | ||
| <div css={containerCss}> | ||
| <div css={[headerCss, raiseLabelOnDesktop && raiseLabelCss]}> | ||
| <span css={labelCss}>{label}</span> | ||
|
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. It might make sense for this to be a heading? (Although the heading level would need to be configurable as it can appear in different places.) |
||
| <span | ||
| css={[numberCss, largeNumbersOnDesktop && largeNumberCss]} | ||
| style={{ '--match-stat-team-colour': home.teamColour }} | ||
| > | ||
| <span | ||
| css={css` | ||
| ${visuallyHidden} | ||
| `} | ||
| > | ||
| {away.teamName} | ||
| </span> | ||
| {formatValue(home.value, showPercentage)} | ||
| </span> | ||
| <span | ||
| css={[ | ||
| numberCss, | ||
| awayStatCss, | ||
| largeNumbersOnDesktop && largeNumberCss, | ||
| ]} | ||
| style={{ '--match-stat-team-colour': away.teamColour }} | ||
| > | ||
| <span | ||
| css={css` | ||
| ${visuallyHidden} | ||
| `} | ||
| > | ||
| {away.teamName} | ||
| </span> | ||
| {formatValue(away.value, showPercentage)} | ||
| </span> | ||
| </div> | ||
| <div aria-hidden="true" css={chartCss}> | ||
| <div | ||
| css={barCss} | ||
| style={{ | ||
| '--match-stat-percentage': `${homePercentage}%`, | ||
| '--match-stat-team-colour': home.teamColour, | ||
| }} | ||
| ></div> | ||
| <div | ||
| css={barCss} | ||
| style={{ | ||
| '--match-stat-percentage': `${awayPercentage}%`, | ||
| '--match-stat-team-colour': away.teamColour, | ||
| }} | ||
| ></div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
75 changes: 75 additions & 0 deletions
75
dotcom-rendering/src/components/FootballMiniMatchStats.stories.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,75 @@ | ||
| import { css } from '@emotion/react'; | ||
| import { breakpoints, from } from '@guardian/source/foundations'; | ||
| import type { Meta, StoryObj } from '@storybook/react-webpack5'; | ||
| import { palette } from '../palette'; | ||
| import { FootballMiniMatchStats as FootballMiniMatchStatsComponent } from './FootballMiniMatchStats'; | ||
|
|
||
| const gridCss = css` | ||
| background-color: ${palette('--football-live-blog-background')}; | ||
| /** | ||
| * Extremely simplified live blog grid layout as we're only interested in | ||
| * the 240px wide left column added at the desktop breakpoint. | ||
| * dotcom-rendering/src/layouts/LiveLayout.tsx | ||
| */ | ||
| ${from.desktop} { | ||
| display: grid; | ||
| grid-column-gap: 20px; | ||
| grid-template-columns: 240px 1fr; | ||
| } | ||
| `; | ||
|
|
||
| const containerCss = css` | ||
| padding: 10px; | ||
| ${from.desktop} { | ||
| padding-left: 20px; | ||
| padding-right: 0; | ||
| } | ||
| `; | ||
|
|
||
| const meta = { | ||
| title: 'Components/Football Mini Match Stats', | ||
| component: FootballMiniMatchStatsComponent, | ||
| decorators: [ | ||
| (Story) => ( | ||
| <div css={gridCss}> | ||
| <div css={containerCss}> | ||
| <Story /> | ||
| </div> | ||
| </div> | ||
| ), | ||
| ], | ||
| parameters: { | ||
| chromatic: { | ||
| viewports: [ | ||
| breakpoints.mobileMedium, | ||
| breakpoints.tablet, | ||
| breakpoints.wide, | ||
| ], | ||
| }, | ||
| }, | ||
| } satisfies Meta<typeof FootballMiniMatchStatsComponent>; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof meta>; | ||
|
|
||
| export const FootballMiniMatchStats = { | ||
| args: { | ||
| homeTeam: { | ||
| name: 'Manchester United', | ||
| colour: '#da020e', | ||
| }, | ||
| awayTeam: { | ||
| name: 'Arsenal', | ||
| colour: '#023474', | ||
| }, | ||
| stats: [ | ||
| { | ||
| label: 'Possession', | ||
| homeValue: 39, | ||
| awayValue: 61, | ||
| showPercentage: true, | ||
| }, | ||
| { label: 'Goal Attempts', homeValue: 7, awayValue: 4 }, | ||
| ], | ||
| }, | ||
| } satisfies Story; |
Oops, something went wrong.
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.
I don't love these prop names so I'm open to suggestions