-
Notifications
You must be signed in to change notification settings - Fork 841
Charts: Add TrendIndicator component #46213
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
base: trunk
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: added | ||
|
|
||
| Charts: adds trend indicator |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { TrendIndicator } from './trend-indicator'; | ||
| export type { TrendIndicatorProps, TrendDirection } from './types'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import { Meta, StoryObj } from '@storybook/react'; | ||
| import { TrendIndicator } from '../trend-indicator'; | ||
| import type { TrendIndicatorProps } from '../types'; | ||
|
|
||
| const meta: Meta< TrendIndicatorProps > = { | ||
| title: 'JS Packages/Charts/Components/Trend Indicator', | ||
| component: TrendIndicator, | ||
| parameters: { | ||
| layout: 'centered', | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj< TrendIndicatorProps >; | ||
|
|
||
| export const Up: Story = { | ||
| args: { | ||
| direction: 'up', | ||
| value: '+14%', | ||
| }, | ||
| }; | ||
|
|
||
| export const Down: Story = { | ||
| args: { | ||
| direction: 'down', | ||
| value: '-5%', | ||
| }, | ||
| }; | ||
|
|
||
| export const Neutral: Story = { | ||
| args: { | ||
| direction: 'neutral', | ||
| value: '0%', | ||
| }, | ||
| }; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,49 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { render, screen } from '@testing-library/react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { TrendIndicator } from '../trend-indicator'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| describe( 'TrendIndicator', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it( 'renders up trend with value', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render( <TrendIndicator direction="up" value="+14%" /> ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect( screen.getByText( '+14%' ) ).toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it( 'renders down trend with value', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render( <TrendIndicator direction="down" value="-5%" /> ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect( screen.getByText( '-5%' ) ).toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it( 'renders neutral trend with value', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render( <TrendIndicator direction="neutral" value="0%" /> ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect( screen.getByText( '0%' ) ).toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it( 'renders icon for up direction', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render( <TrendIndicator direction="up" value="+10%" /> ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect( screen.getByRole( 'img', { hidden: true } ) ).toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it( 'renders icon for down direction', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render( <TrendIndicator direction="down" value="-10%" /> ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect( screen.getByRole( 'img', { hidden: true } ) ).toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| it( 'does not render icon for neutral direction', () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render( <TrendIndicator direction="neutral" value="0%" /> ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expect( screen.queryByRole( 'img', { hidden: true } ) ).not.toBeInTheDocument(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+24
to
+38
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| render( <TrendIndicator direction="up" value="+10%" /> ); | |
| expect( screen.getByRole( 'img', { hidden: true } ) ).toBeInTheDocument(); | |
| } ); | |
| it( 'renders icon for down direction', () => { | |
| render( <TrendIndicator direction="down" value="-10%" /> ); | |
| expect( screen.getByRole( 'img', { hidden: true } ) ).toBeInTheDocument(); | |
| } ); | |
| it( 'does not render icon for neutral direction', () => { | |
| render( <TrendIndicator direction="neutral" value="0%" /> ); | |
| expect( screen.queryByRole( 'img', { hidden: true } ) ).not.toBeInTheDocument(); | |
| const { container } = render( <TrendIndicator direction="up" value="+10%" /> ); | |
| expect( container.querySelector('svg') ).toBeInTheDocument(); | |
| } ); | |
| it( 'renders icon for down direction', () => { | |
| const { container } = render( <TrendIndicator direction="down" value="-10%" /> ); | |
| expect( container.querySelector('svg') ).toBeInTheDocument(); | |
| } ); | |
| it( 'does not render icon for neutral direction', () => { | |
| const { container } = render( <TrendIndicator direction="neutral" value="0%" /> ); | |
| expect( container.querySelector('svg') ).not.toBeInTheDocument(); |
Copilot
AI
Dec 6, 2025
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.
Tests are missing for the accessibility concern of providing semantic meaning to screen reader users. Consider adding a test to verify that the component provides appropriate accessible text:
it( 'provides accessible label for screen readers', () => {
const { container } = render(
<TrendIndicator direction="up" value="+14%" />
);
const indicator = container.firstChild as HTMLElement;
expect( indicator ).toHaveAttribute( 'aria-label' );
expect( indicator.getAttribute( 'aria-label' ) ).toMatch( /increase/i );
} );This would ensure the component remains accessible as it evolves.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| .trend-indicator { | ||
| display: inline-flex; | ||
| align-items: center; | ||
| gap: 0.125em; | ||
| font-size: 0.875rem; | ||
| font-weight: 500; | ||
| line-height: 1; | ||
|
|
||
| &__icon { | ||
| width: 1em; | ||
| height: 1em; | ||
| flex-shrink: 0; | ||
| } | ||
|
|
||
| &__value { | ||
| white-space: nowrap; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||
| import clsx from 'clsx'; | ||||
| import styles from './trend-indicator.module.scss'; | ||||
| import type { TrendIndicatorProps, TrendDirection } from './types'; | ||||
|
|
||||
| const COLORS: Record< TrendDirection, string > = { | ||||
| up: '#1a8917', | ||||
| down: '#d63638', | ||||
| neutral: '#646970', | ||||
| }; | ||||
|
Comment on lines
+5
to
+9
|
||||
|
|
||||
| const Icon = ( { direction }: { direction: TrendDirection } ) => { | ||||
| if ( direction === 'neutral' ) { | ||||
| return null; | ||||
| } | ||||
|
|
||||
| const isUp = direction === 'up'; | ||||
| return ( | ||||
| <svg | ||||
| className={ styles[ 'trend-indicator__icon' ] } | ||||
| viewBox="0 0 16 16" | ||||
| fill="none" | ||||
| role="img" | ||||
|
||||
| role="img" |
Copilot
AI
Dec 6, 2025
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.
The component lacks an aria-label or accessible text to convey the meaning of the trend to screen reader users. Users relying on assistive technologies would only hear the value (e.g., "+14%") without understanding whether it represents an increase, decrease, or neutral trend.
Consider adding an aria-label that describes the full meaning, such as:
<span
className={ clsx( styles[ 'trend-indicator' ], className ) }
style={ { ...style, color: COLORS[ direction ] } }
aria-label={ `${ direction === 'up' ? 'Increase' : direction === 'down' ? 'Decrease' : 'No change' }: ${ value }` }
>Alternatively, use a visually hidden text element to provide context alongside the visual indicator.
Copilot
AI
Dec 6, 2025
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.
The component should use internationalization (i18n) for the accessible label text. Other components in this package use the @wordpress/i18n library's __() function for translatable strings (see bar-chart.tsx, line-chart.tsx).
Example implementation:
import { __ } from '@wordpress/i18n';
// In the component:
const getDirectionLabel = ( direction: TrendDirection ): string => {
switch ( direction ) {
case 'up':
return __( 'Increase', 'jetpack-charts' );
case 'down':
return __( 'Decrease', 'jetpack-charts' );
case 'neutral':
return __( 'No change', 'jetpack-charts' );
}
};This ensures the component works correctly for non-English users.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import type { CSSProperties } from 'react'; | ||
|
|
||
| /** | ||
| * The direction of the trend | ||
| */ | ||
| export type TrendDirection = 'up' | 'down' | 'neutral'; | ||
|
|
||
| /** | ||
| * Props for the TrendIndicator component | ||
| */ | ||
| export type TrendIndicatorProps = { | ||
| /** | ||
| * The direction of the trend (up, down, or neutral) | ||
| */ | ||
| direction: TrendDirection; | ||
|
|
||
| /** | ||
| * The value to display (e.g., "14%", "+$500", "2.5k") | ||
| */ | ||
| value: string | number; | ||
|
|
||
| /** | ||
| * Additional CSS class name | ||
| */ | ||
| className?: string; | ||
|
|
||
| /** | ||
| * Inline styles | ||
| */ | ||
| style?: CSSProperties; | ||
| }; |
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.
This test is querying for
role="img"which should be removed from the component (see the accessibility issue in the main component file). After removingrole="img"from the SVG, these tests will need to be updated to use a different query method.Consider using
container.querySelector('svg')instead: