-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Stats: Flesh out highlight card components (#94084)
* Replace custom code with standard function (Intl) * Add missing return invocation * Refactor number manipulation into a lib module * Add CountCard as an exported component * Add tooltip and note functionality to CountCard * Remove unused note prop from CountComparisonCard * Use CountCard where appropriate Replace unnecessary use of CountComparisonCards * Simplify tooltip for CountCard * Simplify tooltip for CountComparisonCard * Add explicit percentage formatting * Undo unnecessary change * Make precise small percentages toggleable * Remove unused props * Update count comparison card tooltip logic Always render the tooltip, but only render the trend in the tooltip if there's a difference * Update stories to align with existing components * Update CountCard usage on WordAds dashboard --------- Co-authored-by: Jason Moon <jsnmoon@users.noreply.github.com>
- Loading branch information
Showing
17 changed files
with
370 additions
and
246 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,42 +1,67 @@ | ||
import { Icon } from '@wordpress/icons'; | ||
import { useMemo } from 'react'; | ||
import BaseCard from './base-card'; | ||
import clsx from 'clsx'; | ||
import { useRef, useState } from 'react'; | ||
import { Card } from '../'; | ||
import Popover from '../popover'; | ||
import { formatNumber } from './lib/numbers'; | ||
|
||
interface CountCardProps { | ||
heading: React.ReactNode; | ||
icon: JSX.Element; | ||
value: number | string; | ||
heading?: React.ReactNode; | ||
icon?: JSX.Element; | ||
note?: string; | ||
showValueTooltip?: boolean; | ||
value: number | string | null; | ||
} | ||
|
||
function useDisplayValue( value: CountCardProps[ 'value' ] ) { | ||
return useMemo( () => { | ||
if ( typeof value === 'string' ) { | ||
return value; | ||
} | ||
if ( typeof value === 'number' ) { | ||
return value.toLocaleString(); | ||
} | ||
return '-'; | ||
}, [ value ] ); | ||
function TooltipContent( { value }: CountCardProps ) { | ||
return ( | ||
<div className="highlight-card-tooltip-content"> | ||
<span className="highlight-card-tooltip-counts"> | ||
{ formatNumber( value as number, false ) } | ||
</span> | ||
</div> | ||
); | ||
} | ||
|
||
export default function CountCard( { heading, icon, value }: CountCardProps ) { | ||
const displayValue = useDisplayValue( value ); | ||
export default function CountCard( { | ||
heading, | ||
icon, | ||
note, | ||
value, | ||
showValueTooltip, | ||
}: CountCardProps ) { | ||
const textRef = useRef( null ); | ||
const [ isTooltipVisible, setTooltipVisible ] = useState( false ); | ||
|
||
// Tooltips are used to show the full number instead of the shortened number. | ||
// Non-numeric values are not shown in the tooltip. | ||
const shouldShowTooltip = showValueTooltip && typeof value === 'number'; | ||
|
||
return ( | ||
<BaseCard | ||
heading={ | ||
<> | ||
<div className="highlight-card-icon"> | ||
<Icon icon={ icon } /> | ||
</div> | ||
<div className="highlight-card-title">{ heading }</div> | ||
</> | ||
} | ||
> | ||
<div className="highlight-card-count"> | ||
<span className="highlight-card-count-value">{ displayValue }</span> | ||
<Card className="highlight-card"> | ||
{ icon && <div className="highlight-card-icon">{ icon }</div> } | ||
{ heading && <div className="highlight-card-heading">{ heading }</div> } | ||
<div | ||
className={ clsx( 'highlight-card-count', { | ||
'is-pointer': showValueTooltip, | ||
} ) } | ||
onMouseEnter={ () => setTooltipVisible( true ) } | ||
onMouseLeave={ () => setTooltipVisible( false ) } | ||
> | ||
<span className="highlight-card-count-value" ref={ textRef }> | ||
{ typeof value === 'number' ? formatNumber( value, true ) : value } | ||
</span> | ||
</div> | ||
</BaseCard> | ||
{ shouldShowTooltip && ( | ||
<Popover | ||
className="tooltip tooltip--darker highlight-card-tooltip" | ||
isVisible={ isTooltipVisible } | ||
position="bottom right" | ||
context={ textRef.current } | ||
> | ||
<TooltipContent value={ value } /> | ||
{ note && <div className="highlight-card-tooltip-note">{ note }</div> } | ||
</Popover> | ||
) } | ||
</Card> | ||
); | ||
} |
This file contains 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.