Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/devtools/devtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ interface DevtoolsOptions {
* Defaults to 'aside'.
*/
containerElement?: string | any
/**
* nonce for style element for CSP
*/
styleNonce?: string
}

interface DevtoolsPanelOptions {
Expand All @@ -73,6 +77,10 @@ interface DevtoolsPanelOptions {
* A boolean variable indicating whether the panel is open or closed
*/
isOpen?: boolean
/**
* nonce for style element for CSP
*/
styleNonce?: string
/**
* A function that toggles the open and close state of the panel
*/
Expand All @@ -92,6 +100,7 @@ export function ReactQueryDevtools({
toggleButtonProps = {},
position = 'bottom-left',
containerElement: Container = 'aside',
styleNonce,
}: DevtoolsOptions): React.ReactElement | null {
const rootRef = React.useRef<HTMLDivElement>(null)
const panelRef = React.useRef<HTMLDivElement>(null)
Expand Down Expand Up @@ -229,6 +238,7 @@ export function ReactQueryDevtools({
<ThemeProvider theme={theme}>
<ReactQueryDevtoolsPanel
ref={panelRef as any}
styleNonce={styleNonce}
{...otherPanelProps}
style={{
position: 'fixed',
Expand Down Expand Up @@ -375,7 +385,13 @@ export const ReactQueryDevtoolsPanel = React.forwardRef<
HTMLDivElement,
DevtoolsPanelOptions
>(function ReactQueryDevtoolsPanel(props, ref): React.ReactElement {
const { isOpen = true, setIsOpen, handleDragStart, ...panelProps } = props
const {
isOpen = true,
styleNonce,
setIsOpen,
handleDragStart,
...panelProps
} = props

const queryClient = useQueryClient()
const queryCache = queryClient.getQueryCache()
Expand Down Expand Up @@ -467,6 +483,7 @@ export const ReactQueryDevtoolsPanel = React.forwardRef<
{...panelProps}
>
<style
nonce={styleNonce}
dangerouslySetInnerHTML={{
__html: `
.ReactQueryDevtoolsPanel * {
Expand Down
17 changes: 17 additions & 0 deletions src/devtools/tests/devtools.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -391,4 +391,21 @@ describe('ReactQueryDevtools', () => {
expect(queries[1]?.textContent).toEqual(query2Hash)
expect(queries[2]?.textContent).toEqual(query3Hash)
})

it('style should have a nonce', async () => {
const { queryClient } = createQueryClient()

function Page() {
return <div></div>
}

const { container } = renderWithClient(queryClient, <Page />, {
styleNonce: 'test-nonce',
initialIsOpen: false,
})
const styleTag = container.querySelector('style')
expect(styleTag).toHaveAttribute('nonce', 'test-nonce')

await screen.findByRole('button', { name: /react query devtools/i })
})
})