Skip to content

Commit

Permalink
Add opt-in support for setting the "nonce" attribute on the global cu…
Browse files Browse the repository at this point in the history
…rsor style (#386)

Resolves #384

```js
import { setNonce } from "react-resizable-panels";

setNonce("whatever");
```

cc @asual
  • Loading branch information
bvaughn authored Aug 15, 2024
1 parent b2e4241 commit 6d9e971
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/react-resizable-panels/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Panel } from "./Panel";
import { PanelGroup } from "./PanelGroup";
import { PanelResizeHandle } from "./PanelResizeHandle";
import { assert } from "./utils/assert";
import { setNonce } from "./utils/csp";
import { getPanelElement } from "./utils/dom/getPanelElement";
import { getPanelElementsForGroup } from "./utils/dom/getPanelElementsForGroup";
import { getPanelGroupElement } from "./utils/dom/getPanelGroupElement";
Expand Down Expand Up @@ -64,4 +65,7 @@ export {
getResizeHandleElementIndex,
getResizeHandleElementsForGroup,
getResizeHandlePanelIds,

// CSP (see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce)
setNonce,
};
9 changes: 9 additions & 0 deletions packages/react-resizable-panels/src/utils/csp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let nonce: string | null;

export function getNonce(): string | null {
return nonce;
}

export function setNonce(value: string | null) {
nonce = value;
}
6 changes: 6 additions & 0 deletions packages/react-resizable-panels/src/utils/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
EXCEEDED_VERTICAL_MAX,
EXCEEDED_VERTICAL_MIN,
} from "../PanelResizeHandleRegistry";
import { getNonce } from "./csp";

type CursorState = "horizontal" | "intersection" | "vertical";

Expand Down Expand Up @@ -77,6 +78,11 @@ export function setGlobalCursorStyle(
if (styleElement === null) {
styleElement = document.createElement("style");

const nonce = getNonce();
if (nonce) {
styleElement.setAttribute("nonce", nonce);
}

document.head.appendChild(styleElement);
}

Expand Down

0 comments on commit 6d9e971

Please sign in to comment.