diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fffcac0933e2..0c1e603b8433 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4208,6 +4208,9 @@ importers: '@automattic/number-formatters': specifier: workspace:* version: link:../../js-packages/number-formatters + '@base-ui/react': + specifier: 1.9.0 + version: 1.9.0(@date-fns/tz@1.5.0)(@types/react@19.3.0)(date-fns@4.4.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0) '@date-fns/tz': specifier: 1.5.0 version: 1.5.0 diff --git a/projects/packages/premium-analytics/changelog/update-card-tabs-primitive b/projects/packages/premium-analytics/changelog/update-card-tabs-primitive new file mode 100644 index 000000000000..db9aee56fcea --- /dev/null +++ b/projects/packages/premium-analytics/changelog/update-card-tabs-primitive @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Metric cards: Draw the cards on their own tabs primitive, taking the design system’s hover, selected and focus states. diff --git a/projects/packages/premium-analytics/eslint.config.mjs b/projects/packages/premium-analytics/eslint.config.mjs index d4c551c4ffa8..e1a9bd9bb634 100644 --- a/projects/packages/premium-analytics/eslint.config.mjs +++ b/projects/packages/premium-analytics/eslint.config.mjs @@ -169,6 +169,14 @@ export default defineConfig( message: 'Import these from @jetpack-premium-analytics/externals instead: it compiles them once into a shared script module rather than into every module that imports them. Missing an export? Add it to packages/externals/src/index.ts.', }, + { + // Base UI is what `@wordpress/ui` builds on, so `externals` + // already compiles it in: importing it anywhere else + // compiles a second copy. + regex: '^@base-ui/react(/.*)?$', + message: + 'Build on @wordpress/ui from @jetpack-premium-analytics/externals, or on CardTabs from @jetpack-premium-analytics/ui. A primitive that needs Base UI itself takes it from externals, which already compiles it in behind @wordpress/ui.', + }, ], }, ], @@ -209,6 +217,12 @@ export default defineConfig( message: 'Import these from @jetpack-premium-analytics/externals instead: it compiles them once into a shared script module rather than into every bundle that imports them. Missing an export? Add it to packages/externals/src/index.ts.', }, + { + // Same as the packages rule above. + regex: '^@base-ui/react(/.*)?$', + message: + 'Build on @wordpress/ui from @jetpack-premium-analytics/externals, or on CardTabs from @jetpack-premium-analytics/ui. A primitive that needs Base UI itself takes it from externals, which already compiles it in behind @wordpress/ui.', + }, ], }, ], diff --git a/projects/packages/premium-analytics/package.json b/projects/packages/premium-analytics/package.json index f175f8fd1773..d8eb3cf199d5 100644 --- a/projects/packages/premium-analytics/package.json +++ b/projects/packages/premium-analytics/package.json @@ -47,6 +47,7 @@ "@automattic/jetpack-connection": "workspace:*", "@automattic/jetpack-script-data": "workspace:*", "@automattic/number-formatters": "workspace:*", + "@base-ui/react": "1.9.0", "@date-fns/tz": "1.5.0", "@jetpack-premium-analytics/data": "link:packages/data", "@jetpack-premium-analytics/datetime": "link:packages/datetime", diff --git a/projects/packages/premium-analytics/packages/externals/src/index.ts b/projects/packages/premium-analytics/packages/externals/src/index.ts index a1b0abd7e83a..08715fd6f56e 100644 --- a/projects/packages/premium-analytics/packages/externals/src/index.ts +++ b/projects/packages/premium-analytics/packages/externals/src/index.ts @@ -103,6 +103,15 @@ export { VisuallyHidden, } from '@wordpress/ui'; +/** + * Base UI + * + * The unstyled parts `@wordpress/ui` is built on, so they are already compiled + * in here. Prefixed because `Tabs` above is the design system's styled one. + */ +export { DirectionProvider as BaseDirectionProvider } from '@base-ui/react/direction-provider'; +export { Tabs as BaseTabs } from '@base-ui/react/tabs'; + /** * DataViews */ diff --git a/projects/packages/premium-analytics/packages/ui/src/card-tabs/__tests__/card-tabs.test.tsx b/projects/packages/premium-analytics/packages/ui/src/card-tabs/__tests__/card-tabs.test.tsx new file mode 100644 index 000000000000..3cb7626f7e64 --- /dev/null +++ b/projects/packages/premium-analytics/packages/ui/src/card-tabs/__tests__/card-tabs.test.tsx @@ -0,0 +1,90 @@ +import { render, screen } from '@testing-library/react'; +import userEvent from '@testing-library/user-event'; +import { isRTL } from '@wordpress/i18n'; +import { act } from 'react'; +import { CardTabs } from '../index'; + +jest.mock( '@wordpress/i18n', () => ( { + ...jest.requireActual( '@wordpress/i18n' ), + isRTL: jest.fn( () => false ), +} ) ); + +const METRICS = [ + { key: 'orders', label: 'Orders' }, + { key: 'visitors', label: 'Visitors' }, + { key: 'revenue', label: 'Revenue' }, +]; + +function renderCardTabs( { disabledKey }: { disabledKey?: string } = {} ) { + const onValueChange = jest.fn(); + + render( + + + { METRICS.map( metric => ( + + { metric.label } + + ) ) } + + { METRICS.map( metric => ( + + { metric.label } chart + + ) ) } + + ); + + return { onValueChange }; +} + +describe( 'CardTabs', () => { + beforeEach( () => { + jest.mocked( isRTL ).mockReturnValue( false ); + } ); + + it( 'shows the selected card’s panel, labelled by the card, and reports a click on another', async () => { + const user = userEvent.setup(); + const { onValueChange } = renderCardTabs(); + + const selected = screen.getByRole( 'tab', { name: 'Visitors' } ); + expect( selected ).toHaveAttribute( 'aria-selected', 'true' ); + expect( screen.getByRole( 'tabpanel' ) ).toHaveAttribute( 'aria-labelledby', selected.id ); + expect( screen.getByRole( 'tabpanel' ) ).toHaveTextContent( 'Visitors chart' ); + expect( screen.queryByText( 'Orders chart' ) ).not.toBeInTheDocument(); + + await user.click( screen.getByRole( 'tab', { name: 'Orders' } ) ); + + expect( onValueChange ).toHaveBeenCalledWith( 'orders', expect.anything() ); + } ); + + it( 'moves between cards with the arrow keys and selects only on Enter', async () => { + const user = userEvent.setup(); + const { onValueChange } = renderCardTabs( { disabledKey: 'revenue' } ); + + act( () => screen.getByRole( 'tab', { name: 'Visitors' } ).focus() ); + await user.keyboard( '{ArrowLeft}' ); + + expect( screen.getByRole( 'tab', { name: 'Orders' } ) ).toHaveFocus(); + expect( onValueChange ).not.toHaveBeenCalled(); + + await user.keyboard( '{Enter}' ); + + expect( onValueChange ).toHaveBeenCalledWith( 'orders', expect.anything() ); + } ); + + it( 'follows the text direction, so the left arrow moves forward in RTL', async () => { + jest.mocked( isRTL ).mockReturnValue( true ); + const user = userEvent.setup(); + renderCardTabs(); + + act( () => screen.getByRole( 'tab', { name: 'Orders' } ).focus() ); + await user.keyboard( '{ArrowLeft}' ); + + expect( screen.getByRole( 'tab', { name: 'Visitors' } ) ).toHaveFocus(); + } ); +} ); diff --git a/projects/packages/premium-analytics/packages/ui/src/card-tabs/card-tabs.module.scss b/projects/packages/premium-analytics/packages/ui/src/card-tabs/card-tabs.module.scss new file mode 100644 index 000000000000..e0915fbf4372 --- /dev/null +++ b/projects/packages/premium-analytics/packages/ui/src/card-tabs/card-tabs.module.scss @@ -0,0 +1,88 @@ +/* A card's narrowest inline size before the row wraps. No WPDS dimension + * token reaches this far; a consumer tunes it through + * `--jpa-card-tabs-min-inline-size`, as it tunes the padding. */ +$card-tabs-min-inline-size: 10rem; + +/* As many cards to a row as fit at their minimum size, every row sharing the + * width evenly. A consumer that balances its rows sets + * `--jpa-card-tabs-columns` instead. */ +.list { + display: grid; + grid-template-columns: repeat(var(--jpa-card-tabs-columns, auto-fit), minmax(min(100%, var(--jpa-card-tabs-min-inline-size, #{$card-tabs-min-inline-size})), 1fr)); + gap: var(--wpds-dimension-gap-sm); + min-inline-size: 0; +} + +/* A card draws its own box, so there is no indicator to hide. The selected + * card's fill marks the selection; the other states take the interactive + * tokens Button and Tabs share. */ +.tab { + display: flex; + align-items: flex-start; + inline-size: 100%; + min-inline-size: 0; + margin: 0; + padding: var(--jpa-card-tabs-padding, var(--wpds-dimension-padding-md)); + overflow: hidden; + border: 0; + border-radius: var(--wpds-border-radius-lg); + background-color: var(--wpds-color-background-interactive-neutral-weak); + color: var(--wpds-color-foreground-interactive-neutral); + font: inherit; + text-align: start; + cursor: var(--wpds-cursor-control); + appearance: none; + + /* Drawn inside the card, so the widget chrome's `overflow` cannot clip it. + * Transparent until focused; forced colors paint it regardless. */ + outline: var(--wpds-border-width-focus) solid transparent; + outline-offset: calc(-1 * var(--wpds-border-width-focus)); + + @media not (prefers-reduced-motion) { + transition: background-color 0.1s linear; + } +} + +.tab:focus-visible { + outline-color: var(--wpds-color-stroke-focus); +} + +.tab:not([data-disabled]):not([aria-selected="true"]):is(:hover, :active) { + background-color: var(--wpds-color-background-interactive-neutral-weak-active); + color: var(--wpds-color-foreground-interactive-neutral-active); +} + +.tab[aria-selected="true"] { + background-color: var(--wpds-color-background-interactive-brand-weak-active); + cursor: default; +} + +.tab[data-disabled] { + color: var(--wpds-color-foreground-interactive-neutral-disabled); + cursor: default; +} + +/* Fills do not survive forced colors, so the selected card doubles its edge. */ +@media (forced-colors: active) { + + .tab[aria-selected="true"] { + outline-width: var(--wpds-border-width-md); + outline-style: double; + } + + .tab[data-disabled] { + color: GrayText; + } +} + +.panel { + min-inline-size: 0; + min-block-size: 0; +} + +/* The panel is a tab stop of its own, so it takes the outset ring `Tabs` + * gives its panel. */ +.panel:focus-visible { + outline: var(--wpds-border-width-focus) solid var(--wpds-color-stroke-focus); + outline-offset: var(--wpds-border-width-focus); +} diff --git a/projects/packages/premium-analytics/packages/ui/src/card-tabs/card-tabs.tsx b/projects/packages/premium-analytics/packages/ui/src/card-tabs/card-tabs.tsx new file mode 100644 index 000000000000..cb52636f251c --- /dev/null +++ b/projects/packages/premium-analytics/packages/ui/src/card-tabs/card-tabs.tsx @@ -0,0 +1,73 @@ +/** + * External dependencies + */ +import { BaseDirectionProvider, BaseTabs } from '@jetpack-premium-analytics/externals'; +import { isRTL } from '@wordpress/i18n'; +import clsx from 'clsx'; +import { forwardRef } from 'react'; +/** + * Internal dependencies + */ +import styles from './card-tabs.module.scss'; +import type { + CardTabsListProps, + CardTabsPanelProps, + CardTabsRootProps, + CardTabsTabProps, +} from './types'; + +/** + * Groups the cards and their panels. Mirrors the `Tabs` API of `@wordpress/ui`, + * with each tab drawn as a card whose fill marks the selection. + */ +export const Root = forwardRef< HTMLDivElement, CardTabsRootProps >( + function CardTabsRoot( props, forwardedRef ) { + return ( + + + + ); + } +); + +/** The row of cards, wrapping as the room runs out. */ +export const List = forwardRef< HTMLDivElement, CardTabsListProps >( function CardTabsList( + { className, ...otherProps }, + forwardedRef +) { + return ( + + ); +} ); + +/** A card: a button that shows its panel when activated. */ +export const Tab = forwardRef< HTMLButtonElement, CardTabsTabProps >( function CardTabsTab( + { className, ...otherProps }, + forwardedRef +) { + return ( + + ); +} ); + +/** The panel shown while its card is selected. */ +export const Panel = forwardRef< HTMLDivElement, CardTabsPanelProps >( function CardTabsPanel( + { className, ...otherProps }, + forwardedRef +) { + return ( + + ); +} ); diff --git a/projects/packages/premium-analytics/packages/ui/src/card-tabs/index.ts b/projects/packages/premium-analytics/packages/ui/src/card-tabs/index.ts new file mode 100644 index 000000000000..ad45b48979a8 --- /dev/null +++ b/projects/packages/premium-analytics/packages/ui/src/card-tabs/index.ts @@ -0,0 +1 @@ +export * as CardTabs from './card-tabs'; diff --git a/projects/packages/premium-analytics/packages/ui/src/card-tabs/stories/card-tabs.stories.tsx b/projects/packages/premium-analytics/packages/ui/src/card-tabs/stories/card-tabs.stories.tsx new file mode 100644 index 000000000000..3e4282a5f106 --- /dev/null +++ b/projects/packages/premium-analytics/packages/ui/src/card-tabs/stories/card-tabs.stories.tsx @@ -0,0 +1,123 @@ +/** + * External dependencies + */ +import { useState } from 'react'; +/** + * Internal dependencies + */ +import { CardTabs } from '../index'; +import type { Meta, StoryObj } from '@storybook/react'; +import type { ComponentProps } from 'react'; + +/** The Store performance cards, as the widget lays them out. */ +const METRICS = [ + { key: 'orders', label: 'Orders', value: '143' }, + { key: 'visitors', label: 'Store visitors', value: '27' }, + { key: 'conversion', label: 'Conversion rate', value: '3.2%' }, + { key: 'revenue', label: 'Revenue', value: '$1,284' }, +]; + +interface CardTabsStoryControls { + containerWidth: number; + activateOnFocus: boolean; + disableLast: boolean; +} + +/** + * Four metric cards over a panel, inside a horizontally resizable container + * that stands in for a widget of that width. + */ +function CardTabsStory( { containerWidth, activateOnFocus, disableLast }: CardTabsStoryControls ) { + const [ value, setValue ] = useState( METRICS[ 0 ].key ); + + return ( +
+ + + { METRICS.map( ( metric, index ) => ( + + + { metric.label } + + { metric.value } + + + + ) ) } + + { METRICS.map( metric => ( + + { metric.label } chart + + ) ) } + +
+ ); +} + +const meta = { + title: 'Packages/Premium Analytics/UI/CardTabs', + component: CardTabs.Root, + tags: [ 'autodocs' ], + argTypes: { + containerWidth: { + control: { type: 'range', min: 240, max: 960, step: 8 }, + description: 'Width of the container, standing in for the widget.', + }, + activateOnFocus: { + control: 'boolean', + description: 'Select a card as the arrow keys reach it, instead of on Enter or Space.', + }, + disableLast: { + control: 'boolean', + description: 'Disable the last card.', + }, + }, + parameters: { + layout: 'padded', + docs: { + description: { + component: + 'Mirrors the `Tabs` API of `@wordpress/ui`, with each tab drawn as a card: Base UI tabs underneath, the selected card marked by its fill rather than an indicator, and the design system’s interactive tokens for every state. The row wraps on its own once the cards would fall under their minimum width; the padding, that width and the column count are set through `--jpa-card-tabs-padding`, `--jpa-card-tabs-min-inline-size` and `--jpa-card-tabs-columns`.', + }, + }, + }, +} satisfies Meta< ComponentProps< typeof CardTabs.Root > & CardTabsStoryControls >; + +export default meta; + +type Story = StoryObj< CardTabsStoryControls >; + +/** Room for every card on one row. */ +export const Default: Story = { + render: args => , + args: { containerWidth: 720, activateOnFocus: false, disableLast: false }, +}; + +/** A container too narrow for one row: the cards wrap into two. */ +export const Wrapping: Story = { + render: args => , + args: { containerWidth: 360, activateOnFocus: false, disableLast: false }, +}; diff --git a/projects/packages/premium-analytics/packages/ui/src/card-tabs/types.ts b/projects/packages/premium-analytics/packages/ui/src/card-tabs/types.ts new file mode 100644 index 000000000000..4ec041e80574 --- /dev/null +++ b/projects/packages/premium-analytics/packages/ui/src/card-tabs/types.ts @@ -0,0 +1,20 @@ +/** + * External dependencies + */ +import type { BaseTabs } from '@jetpack-premium-analytics/externals'; +import type { ComponentPropsWithoutRef, CSSProperties, ElementType, ReactNode } from 'react'; + +/** Base UI's props with `className` and `style` narrowed to plain values, as `@wordpress/ui` narrows its own. */ +type PartProps< Part extends ElementType > = Omit< + ComponentPropsWithoutRef< Part >, + 'className' | 'style' | 'children' +> & { + className?: string; + style?: CSSProperties; + children?: ReactNode; +}; + +export type CardTabsRootProps = PartProps< typeof BaseTabs.Root >; +export type CardTabsListProps = PartProps< typeof BaseTabs.List >; +export type CardTabsTabProps = PartProps< typeof BaseTabs.Tab >; +export type CardTabsPanelProps = PartProps< typeof BaseTabs.Panel >; diff --git a/projects/packages/premium-analytics/packages/ui/src/index.ts b/projects/packages/premium-analytics/packages/ui/src/index.ts index 050a6c6ce52a..7d9cb7541a7d 100644 --- a/projects/packages/premium-analytics/packages/ui/src/index.ts +++ b/projects/packages/premium-analytics/packages/ui/src/index.ts @@ -1,3 +1,4 @@ +export { CardTabs } from './card-tabs'; export { DataViewsDrilldownNative, type DataViewsDrilldownNativeProps, diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-delta/metric-delta.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-delta/metric-delta.tsx index 5cb8e14670ee..5c73cb8978fe 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-delta/metric-delta.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-delta/metric-delta.tsx @@ -35,6 +35,9 @@ export type MetricDeltaProps = { className?: string; + /** The element the delta renders as, for a spot that allows phrasing content only, such as a button. */ + render?: ComponentProps< typeof Stack >[ 'render' ]; + /** * Text alignment * @default 'center' @@ -80,6 +83,7 @@ export function MetricDelta( { hideZero = false, invertColors = false, className, + render, justify = 'center', showAbsolute = false, absoluteFormat = 'number', @@ -90,7 +94,11 @@ export function MetricDelta( { if ( percentageChange === null ) { return ( - + { fallback } ); @@ -117,6 +125,7 @@ export function MetricDelta( { return ( { render( ); expect( - screen.getByRole( 'tablist' ).style.getPropertyValue( '--jpa-metric-tabs-columns' ) + screen.getByRole( 'tablist' ).style.getPropertyValue( '--jpa-card-tabs-columns' ) ).toBe( columns ); } ); @@ -515,6 +515,24 @@ describe( 'MetricTabsChart', () => { } } ); + it( 'draws each card without block elements, which a button cannot hold', () => { + const unavailable = { + ...METRIC, + key: 'likes', + label: 'Likes', + unavailable: "Hourly data isn't available for this metric.", + }; + + render( ); + + const tabs = screen.getAllByRole( 'tab' ); + expect( tabs ).toHaveLength( 2 ); + for ( const tab of tabs ) { + // eslint-disable-next-line testing-library/no-node-access -- A block element has no role to query by. + expect( tab.querySelector( 'div' ) ).toBeNull(); + } + } ); + it( 'emits a single series when the metric has no previous period', () => { const withoutPrevious = { ...METRIC, previous: undefined }; diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-tabs-chart/metric-tabs-chart.module.scss b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-tabs-chart/metric-tabs-chart.module.scss index 1a7c0318f667..bb5b35338e02 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-tabs-chart/metric-tabs-chart.module.scss +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-tabs-chart/metric-tabs-chart.module.scss @@ -15,10 +15,7 @@ min-height: 0; } -/* Keeps the tabs' `z-index` inside the header, so the chart's tooltip, which - comes later in the source, paints above them. */ .header { - isolation: isolate; display: flex; flex-direction: row; flex-wrap: wrap; @@ -28,16 +25,12 @@ gap: var(--wpds-dimension-gap-md); } -/* Equal-width cards fill the room left by the controls. The component sets the - * column count that balances the rows; until it measures, or beside controls, - * as many cards as fit share a row. The minimum matches `MIN_CARD_WIDTH`. */ +/* The cards fill the room left by the controls. Until the component measures, + * or beside controls, as many cards as fit share a row; measured, it sets the + * column count that balances the rows. */ .tabs { - display: grid; flex: 1 1 auto; - grid-template-columns: repeat(var(--jpa-metric-tabs-columns, auto-fit), minmax(min(100%, 160px), 1fr)); - gap: var(--wpds-dimension-gap-sm); min-width: 0; - overflow: visible; } /* Dropdown mode (narrow tiles): the selected-metric card is the trigger. */ @@ -60,9 +53,8 @@ padding-block: var(--wpds-dimension-padding-md); } -/* Draw the focus ring inside the card; the default outset ring gets clipped +/* Draw the focus ring inside the trigger; the default outset ring gets clipped * by the widget chrome's `overflow: auto`. */ -.tab, .metricSelect [class*="trigger-wrapper"] { outline-offset: calc(-1 * var(--wpds-border-width-focus)); } @@ -72,57 +64,19 @@ text-align: start; } +/* The card is the container the value's `cqi` clamp below measures against. */ .tab { container-type: inline-size; - height: auto; - min-width: 0; - overflow: hidden; - padding: var(--wpds-dimension-padding-md); - border-radius: var(--wpds-border-radius-lg); -} - -/* Hide the selection underline; the card fill shows selection instead. Older - * `@wordpress/ui` rendered the indicator as a per-tab ``; newer versions - * render it as a tablist-level presentation span. Hide whichever is present. */ -.tab > svg { - display: none; -} - -.root [role="tablist"] [role="presentation"] { - display: none; -} - -.tab:hover, -.tab[aria-selected="true"] { - background-color: var(--wpds-color-background-surface-brand); -} - -/* The Tab paints its hover fill and focus ring on `::after`, sized for a 48px - * tab. Stretch it to the card so the ring follows the card edge, and drop its - * fill, which the card fill above replaces. */ -.tab::after { - inset: 0; - border-radius: inherit; - outline-offset: calc(-1 * var(--wpds-border-width-focus)); - background-color: transparent; - box-shadow: none; } /* A lone metric has nothing to switch to: it reads as the widget's headline - * figure rather than a control, so it drops the card fill and the pointer. */ + * figure rather than a control, so it keeps the card's inset and no fill. */ .singleMetric .tab { - cursor: default; -} - -/* A static headline has no selected or hover state. */ -.singleMetric .tab:hover { - background-color: transparent; + padding: var(--wpds-dimension-padding-md); } -/* Each tab's content: label stacked over the value + delta, start-aligned. - * Growing to fill is what keeps it against the card's leading edge — the - * tablist centres a tab's children, which shows as soon as a card is wider - * than its text. */ +/* Each card's content: label stacked over the value + delta, start-aligned + * and growing to fill the card. */ .tabContent { display: flex; flex: 1 1 auto; @@ -138,6 +92,7 @@ max-width: 100%; overflow: hidden; text-overflow: ellipsis; + white-space: nowrap; } /* Prefer moving the delta below the value over painting into the next card. */ diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-tabs-chart/metric-tabs-chart.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-tabs-chart/metric-tabs-chart.tsx index 05157a3ea6a1..2d358a1141b5 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-tabs-chart/metric-tabs-chart.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-tabs-chart/metric-tabs-chart.tsx @@ -3,13 +3,13 @@ */ import { SelectControl, - Tabs, Text, VisuallyHidden, mixHexColors, useBarColors, useGlobalChartsContext, } from '@jetpack-premium-analytics/externals'; +import { CardTabs } from '@jetpack-premium-analytics/ui'; import { useResizeObserver } from '@wordpress/compose'; import { __ } from '@wordpress/i18n'; import clsx from 'clsx'; @@ -43,7 +43,7 @@ import type { ComponentProps, CSSProperties, ReactNode } from 'react'; */ const MIN_TAB_WIDTH = 120; -/** Narrowest card (px) before the cards wrap; keep in step with `.tabs` in the stylesheet. */ +/** Narrowest card (px) before the cards wrap, handed to the list as `--jpa-card-tabs-min-inline-size`. */ const MIN_CARD_WIDTH = 160; /** The gap (px) between cards, `--wpds-dimension-gap-sm`. */ @@ -572,7 +572,7 @@ function MetricTabContent( { return ( { hideLabelFromVision ? ( - { metric.label } + }>{ metric.label } ) : ( { metric.label } ) } @@ -594,6 +594,7 @@ function MetricTabContent( { ) : ( } className={ styles.metricComparison } value={ metric.value } previousValue={ metric.previousValue } @@ -603,7 +604,7 @@ function MetricTabContent( { align="flex-end" /> ) } - { note && { note } } + { note && }>{ note } } ); } @@ -830,38 +831,39 @@ export function MetricTabsChart( { } return ( -
- { metrics.map( metric => ( - - + ) ) } - + { controls }
- { /* One panel per tab (WAI-ARIA + @wordpress/ui parity). Only the active - metric's panel renders its chart; the rest stay empty. */ } + { /* One panel per tab, as WAI-ARIA asks; Base UI mounts only the selected one. */ } { metrics.map( metric => ( - - + ) ) } -
+ ); } diff --git a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-with-comparison/metric-with-comparison.tsx b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-with-comparison/metric-with-comparison.tsx index 66e100b691ff..0390c99c0395 100644 --- a/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-with-comparison/metric-with-comparison.tsx +++ b/projects/packages/premium-analytics/packages/widgets-toolkit/src/components/metric-with-comparison/metric-with-comparison.tsx @@ -63,6 +63,9 @@ export type MetricWithComparisonProps = { className?: string; + /** The element the metric and its delta render as, for a spot that allows phrasing content only, such as a button. */ + render?: ComponentProps< typeof Stack >[ 'render' ]; + /** * What to display for delta when calculation is not possible */ @@ -86,6 +89,7 @@ export function MetricWithComparison( { invertDeltaColors = false, hideDeltaOnZero = false, className, + render, deltaFallback, showAbsoluteDelta = false, }: MetricWithComparisonProps ) { @@ -94,11 +98,19 @@ export function MetricWithComparison( { const absoluteFormat = dataFormat.type === 'currency' ? 'currency' : 'number'; return ( - + { showDelta && ( { + const designSystemDir = installedDir( '@wordpress/ui', PACKAGE_ROOT ); + + expect( installedDir( '@base-ui/react', PACKAGE_ROOT ) ).toBe( + installedDir( '@base-ui/react', designSystemDir ) + ); +} ); diff --git a/projects/packages/premium-analytics/tests/single-copy-modules.cjs b/projects/packages/premium-analytics/tests/single-copy-modules.cjs index 575d519bb9ba..a14785290272 100644 --- a/projects/packages/premium-analytics/tests/single-copy-modules.cjs +++ b/projects/packages/premium-analytics/tests/single-copy-modules.cjs @@ -37,8 +37,8 @@ const escapeRegExp = value => value.replace( /[.*+?^${}()|[\]\\]/g, '\\$&' ); * @return {Object} Mapper entries, keyed by an anchored pattern for the bare import. */ function singleCopyModuleMapper( rootDir ) { - // Anything the package does not depend on directly, such as `@base-ui/react`, is reached through - // the library that does. + // Anything the package does not depend on directly, such as `@wordpress/rich-text`, is reached + // through the library that does. const searchDirs = [ rootDir ]; for ( const name of [ '@wordpress/ui', '@wordpress/dataviews', '@wordpress/components' ] ) { try { diff --git a/projects/plugins/jetpack/changelog/update-card-tabs-primitive b/projects/plugins/jetpack/changelog/update-card-tabs-primitive new file mode 100644 index 000000000000..a4dcdbb752b9 --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-card-tabs-primitive @@ -0,0 +1,4 @@ +Significance: patch +Type: enhancement + +Premium Analytics: Give the metric cards the design system’s hover, selected and focus states. diff --git a/projects/plugins/premium-analytics/changelog/update-card-tabs-primitive b/projects/plugins/premium-analytics/changelog/update-card-tabs-primitive new file mode 100644 index 000000000000..e5574021af38 --- /dev/null +++ b/projects/plugins/premium-analytics/changelog/update-card-tabs-primitive @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Metric cards: Give the cards the design system’s hover, selected and focus states.