Skip to content

Commit 0b73bcc

Browse files
authored
fix(export): make PNG exports adapt to active theme (JhaSourav07#822)
2 parents cad6196 + b386550 commit 0b73bcc

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

components/dashboard/ShareSheet.test.tsx

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ describe('ShareSheet', () => {
7474

7575
afterEach(() => {
7676
vi.restoreAllMocks();
77+
document.documentElement.classList.remove('dark');
7778
});
7879

7980
it('does not render when isOpen is false', () => {
@@ -195,7 +196,10 @@ describe('ShareSheet', () => {
195196
);
196197
});
197198

198-
it('handles Download PNG action', async () => {
199+
it('handles Download PNG action in dark mode', async () => {
200+
// Add 'dark' class to document.documentElement
201+
document.documentElement.classList.add('dark');
202+
199203
render(<ShareSheet {...defaultProps} />);
200204

201205
// Create a mock document element to satisfy the selector
@@ -207,7 +211,41 @@ describe('ShareSheet', () => {
207211
fireEvent.click(downloadButton!);
208212

209213
const { toPng } = await import('html-to-image');
210-
expect(toPng).toHaveBeenCalled();
214+
expect(toPng).toHaveBeenLastCalledWith(
215+
expect.any(HTMLElement),
216+
expect.objectContaining({
217+
backgroundColor: '#050505',
218+
})
219+
);
220+
221+
await waitFor(() => {
222+
expect(screen.getByText('Downloaded!')).toBeDefined();
223+
});
224+
225+
document.body.removeChild(mockRoot);
226+
});
227+
228+
it('handles Download PNG action in light mode', async () => {
229+
// Ensure 'dark' class is NOT on document.documentElement
230+
document.documentElement.classList.remove('dark');
231+
232+
render(<ShareSheet {...defaultProps} />);
233+
234+
// Create a mock document element to satisfy the selector
235+
const mockRoot = document.createElement('div');
236+
mockRoot.id = 'dashboard-root';
237+
document.body.appendChild(mockRoot);
238+
239+
const downloadButton = screen.getByText('Download as PNG').closest('button');
240+
fireEvent.click(downloadButton!);
241+
242+
const { toPng } = await import('html-to-image');
243+
expect(toPng).toHaveBeenLastCalledWith(
244+
expect.any(HTMLElement),
245+
expect.objectContaining({
246+
backgroundColor: '#ffffff',
247+
})
248+
);
211249

212250
await waitFor(() => {
213251
expect(screen.getByText('Downloaded!')).toBeDefined();

hooks/useShareActions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ export function useShareActions(
8080
document.getElementById('dashboard-root') ??
8181
document.querySelector<HTMLElement>('[data-dashboard]') ??
8282
document.body;
83+
const isDark =
84+
typeof document !== 'undefined' && document.documentElement.classList.contains('dark');
8385
const dataUrl = await toPng(node, {
8486
quality: 0.95,
8587
pixelRatio: 2,
86-
backgroundColor: '#050505',
88+
backgroundColor: isDark ? '#050505' : '#ffffff',
8789
filter: (el) => {
8890
if (el instanceof HTMLElement) {
8991
if (el.id === 'share-sheet-overlay') return false;

0 commit comments

Comments
 (0)