-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(feedback): Add setTheme() to dynamically update feedback widget color scheme
#19430
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
...ackages/e2e-tests/test-applications/nextjs-16-userfeedback/app/examples/themeSwitcher.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| 'use client'; | ||
| import * as Sentry from '@sentry/nextjs'; | ||
| import { useEffect, useState } from 'react'; | ||
|
|
||
| export default function ThemeSwitcher() { | ||
| const [feedback, setFeedback] = useState<ReturnType<typeof Sentry.getFeedback>>(); | ||
| useEffect(() => { | ||
| setFeedback(Sentry.getFeedback()); | ||
| }, []); | ||
|
|
||
| return ( | ||
| <div> | ||
| <button | ||
| className="hover:bg-hover px-4 py-2 rounded-md" | ||
| type="button" | ||
| data-testid="set-light-theme" | ||
| onClick={() => feedback?.setTheme('light')} | ||
| > | ||
| Set Light Theme | ||
| </button> | ||
| <button | ||
| className="hover:bg-hover px-4 py-2 rounded-md" | ||
| type="button" | ||
| data-testid="set-dark-theme" | ||
| onClick={() => feedback?.setTheme('dark')} | ||
| > | ||
| Set Dark Theme | ||
| </button> | ||
| <button | ||
| className="hover:bg-hover px-4 py-2 rounded-md" | ||
| type="button" | ||
| data-testid="set-system-theme" | ||
| onClick={() => feedback?.setTheme('system')} | ||
| > | ||
| Set System Theme | ||
| </button> | ||
| </div> | ||
| ); | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /** | ||
| * @vitest-environment jsdom | ||
| */ | ||
| import { getCurrentScope } from '@sentry/core'; | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { buildFeedbackIntegration } from '../../src/core/integration'; | ||
| import { mockSdk } from './mockSdk'; | ||
|
|
||
| describe('setTheme', () => { | ||
| beforeEach(() => { | ||
| getCurrentScope().setClient(undefined); | ||
| document.body.innerHTML = ''; | ||
| }); | ||
|
|
||
| it('updates colorScheme and replaces the stylesheet in the shadow DOM', () => { | ||
| const feedbackIntegration = buildFeedbackIntegration({ lazyLoadIntegration: vi.fn() }); | ||
| const integration = feedbackIntegration({ colorScheme: 'light', autoInject: false }); | ||
| mockSdk({ sentryOptions: { integrations: [integration] } }); | ||
|
|
||
| // Force shadow DOM creation | ||
| integration.createWidget(); | ||
|
|
||
| const host = document.querySelector('#sentry-feedback') as HTMLElement; | ||
| const shadow = host?.shadowRoot; | ||
| expect(shadow).toBeTruthy(); | ||
|
|
||
| // Verify initial light scheme | ||
| const initialStyle = shadow?.querySelector('style'); | ||
| expect(initialStyle?.textContent).toContain('color-scheme: only light'); | ||
|
|
||
| // Switch to dark | ||
| integration.setTheme('dark'); | ||
|
|
||
| const updatedStyle = shadow?.querySelector('style'); | ||
| expect(updatedStyle?.textContent).toContain('color-scheme: only dark'); | ||
| }); | ||
|
|
||
| it("setTheme('system') sets system mode", () => { | ||
| const feedbackIntegration = buildFeedbackIntegration({ lazyLoadIntegration: vi.fn() }); | ||
| const integration = feedbackIntegration({ colorScheme: 'light', autoInject: false }); | ||
| mockSdk({ sentryOptions: { integrations: [integration] } }); | ||
|
|
||
| integration.createWidget(); | ||
|
|
||
| integration.setTheme('system'); | ||
|
|
||
| const host = document.querySelector('#sentry-feedback') as HTMLElement; | ||
| const shadow = host?.shadowRoot; | ||
| const style = shadow?.querySelector('style'); | ||
| // System mode uses a media query for dark, not a forced color-scheme at the :host level | ||
| expect(style?.textContent).toContain('prefers-color-scheme'); | ||
| // Should not force light color scheme | ||
| expect(style?.textContent).not.toContain('color-scheme: only light'); | ||
| }); | ||
|
|
||
| it('does not throw when setTheme is called before shadow DOM is created', () => { | ||
| const feedbackIntegration = buildFeedbackIntegration({ lazyLoadIntegration: vi.fn() }); | ||
| const integration = feedbackIntegration({ colorScheme: 'light', autoInject: false }); | ||
| mockSdk({ sentryOptions: { integrations: [integration] } }); | ||
|
|
||
| // Call setTheme before any widget is created | ||
| expect(() => integration.setTheme('dark')).not.toThrow(); | ||
|
|
||
| // Now create a widget — it should pick up the updated colorScheme | ||
| integration.createWidget(); | ||
|
|
||
| const host = document.querySelector('#sentry-feedback') as HTMLElement; | ||
| const shadow = host?.shadowRoot; | ||
| const style = shadow?.querySelector('style'); | ||
| expect(style?.textContent).toContain('color-scheme: only dark'); | ||
| }); | ||
|
|
||
| it('replaces (not accumulates) style elements on multiple setTheme calls', () => { | ||
| const feedbackIntegration = buildFeedbackIntegration({ lazyLoadIntegration: vi.fn() }); | ||
| const integration = feedbackIntegration({ colorScheme: 'light', autoInject: false }); | ||
| mockSdk({ sentryOptions: { integrations: [integration] } }); | ||
|
|
||
| integration.createWidget(); | ||
|
|
||
| const host = document.querySelector('#sentry-feedback') as HTMLElement; | ||
| const shadow = host?.shadowRoot; | ||
| const countAfterCreate = shadow?.querySelectorAll('style').length ?? 0; | ||
|
|
||
| // Multiple setTheme calls should not accumulate additional style elements | ||
| integration.setTheme('dark'); | ||
| integration.setTheme('light'); | ||
| integration.setTheme('system'); | ||
|
|
||
| expect(shadow?.querySelectorAll('style').length).toBe(countAfterCreate); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
careful with this! i think multiple styles can be added into the shadow root, base stuff for the button, and then more and more as the modal gets opened
edit: yeah checkout this: https://github.com/getsentry/sentry-javascript/blob/develop/packages/feedback/src/modal/integration.tsx#L39-L44 it's a 2nd
<style>insertedUh oh!
There was an error while loading. Please reload this page.
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.
Fixed by storing a direct reference to the main style element
(_mainStyle)when it's created in_createShadow, instead of relying on querySelector('style'). Would this work?