Skip to content

Commit b87adf3

Browse files
authored
Merge pull request #648 from adobe/AN-404626
feat: add prop for ChartPopover
2 parents 60ae215 + e0b322c commit b87adf3

File tree

5 files changed

+65
-3
lines changed

5 files changed

+65
-3
lines changed

packages/react-spectrum-charts/src/RscChart.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ RscChart.displayName = 'RscChart';
145145

146146
const ChartDialog = ({ datum, popover, setIsPopoverOpen, targetElement }: ChartDialogProps) => {
147147
const { chartPopoverProps, name } = popover;
148-
const { children, onOpenChange, containerPadding, rightClick, ...dialogProps } = chartPopoverProps;
148+
const { children, onOpenChange, containerPadding, contentMargin, rightClick, ...dialogProps } = chartPopoverProps;
149149
const minWidth = dialogProps.minWidth ?? 0;
150150

151151
return (
@@ -166,7 +166,12 @@ const ChartDialog = ({ datum, popover, setIsPopoverOpen, targetElement }: ChartD
166166
</ActionButton>
167167
{(close) => (
168168
<Dialog data-testid="rsc-popover" UNSAFE_className="rsc-popover" {...dialogProps} minWidth={minWidth}>
169-
<SpectrumView gridColumn="1/-1" gridRow="1/-1" margin={12}>
169+
<SpectrumView
170+
data-testid="rsc-popover-content"
171+
gridColumn="1/-1"
172+
gridRow="1/-1"
173+
margin={contentMargin ?? 12}
174+
>
170175
{datum && datum[COMPONENT_NAME] === name && children?.(datum, close)}
171176
</SpectrumView>
172177
</Dialog>

packages/react-spectrum-charts/src/rscToSbAdapter/chartPopoverAdapter.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ describe('getChartPopoverOptions()', () => {
2424
const options = getChartPopoverOptions({});
2525
expect(options).not.toHaveProperty('height');
2626
});
27+
it('should pass through contentMargin prop', () => {
28+
const options = getChartPopoverOptions({ contentMargin: 24 });
29+
expect(options).toHaveProperty('contentMargin', 24);
30+
});
2731
});

packages/react-spectrum-charts/src/stories/components/ChartPopover/ChartPopover.story.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,13 @@ StackedBarChart.args = { children: dialogContent, width: 'auto' };
170170
const Svg = bindWithProps(ChartPopoverSvgStory);
171171
Svg.args = { children: dialogContent, width: 'auto' };
172172

173+
const ContentMargin = bindWithProps(ChartPopoverSvgStory);
174+
ContentMargin.args = { children: dialogContent, width: 'auto', contentMargin: 24 };
175+
173176
export {
174177
AreaChart,
175178
Canvas,
179+
ContentMargin,
176180
DodgedBarChart,
177181
DonutChart,
178182
LineChart,

packages/react-spectrum-charts/src/stories/components/ChartPopover/ChartPopover.test.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
import '../../../test-utils/__mocks__/matchMedia.mock.js';
3333
import {
3434
Canvas,
35+
ContentMargin,
3536
DodgedBarChart,
3637
DonutChart,
3738
LineChart,
@@ -147,6 +148,52 @@ describe('ChartPopover', () => {
147148
expect(popover).toHaveStyle('width: auto; min-width: 250px;');
148149
});
149150

151+
test('should honor contentMargin', async () => {
152+
render(<ContentMargin {...ContentMargin.args} />);
153+
154+
const chart = await findChart();
155+
expect(chart).toBeInTheDocument();
156+
const bars = getAllMarksByGroupName(chart, 'bar0');
157+
158+
// clicking the bar should open the popover
159+
await clickNthElement(bars, 0);
160+
const popover = await screen.findByTestId('rsc-popover');
161+
await waitFor(() => expect(popover).toBeInTheDocument()); // waitFor to give the popover time to make sure it doesn't close
162+
163+
// Check that the View inside the popover has the correct margin
164+
const view = screen.getByTestId('rsc-popover-content');
165+
// React Spectrum View applies margin using specific margin-* properties
166+
expect(view).toHaveStyle({
167+
marginTop: '24px',
168+
marginRight: '24px',
169+
marginBottom: '24px',
170+
marginLeft: '24px',
171+
});
172+
});
173+
174+
test('should use default contentMargin when not provided', async () => {
175+
render(<StackedBarChart {...StackedBarChart.args} />);
176+
177+
const chart = await findChart();
178+
expect(chart).toBeInTheDocument();
179+
const bars = getAllMarksByGroupName(chart, 'bar0');
180+
181+
// clicking the bar should open the popover
182+
await clickNthElement(bars, 0);
183+
const popover = await screen.findByTestId('rsc-popover');
184+
await waitFor(() => expect(popover).toBeInTheDocument());
185+
186+
// Check that the View inside the popover has the default margin of 12px
187+
const view = screen.getByTestId('rsc-popover-content');
188+
// React Spectrum View applies margin using specific margin-* properties
189+
expect(view).toHaveStyle({
190+
marginTop: '12px',
191+
marginRight: '12px',
192+
marginBottom: '12px',
193+
marginLeft: '12px',
194+
});
195+
});
196+
150197
test('Popover opens on right click, not left click', async () => {
151198
render(<RightClick {...RightClick.args} />);
152199

packages/vega-spec-builder/src/types/dialogs/chartPopoverSpec.types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ export interface ChartPopoverOptions {
3232
rightClick?: boolean;
3333
/** Sets which marks should be highlighted when a popover is visible. This feature is incomplete. */
3434
UNSAFE_highlightBy?: 'series' | 'dimension' | 'item' | string[];
35+
/** The margin that should be applied between the popover and its surrounding container */
36+
contentMargin?: number;
3537
}
3638

3739
type ChartPopoverOptionsWithDefaults = 'UNSAFE_highlightBy';
3840

3941
export interface ChartPopoverSpecOptions
4042
extends PartiallyRequired<ChartPopoverOptions, ChartPopoverOptionsWithDefaults> {
4143
markName: string;
42-
}
44+
}

0 commit comments

Comments
 (0)