Skip to content

Commit

Permalink
Support disabling global cursor styles (#387)
Browse files Browse the repository at this point in the history
```js
import { disableGlobalCursorStyles } from "react-resizable-panels";

disableGlobalCursorStyles();
```

cc @PabloCorso
  • Loading branch information
bvaughn authored Aug 15, 2024
1 parent 6d9e971 commit 30b63d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/react-resizable-panels/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { PanelGroup } from "./PanelGroup";
import { PanelResizeHandle } from "./PanelResizeHandle";
import { assert } from "./utils/assert";
import { setNonce } from "./utils/csp";
import {
enableGlobalCursorStyles,
disableGlobalCursorStyles,
} from "./utils/cursor";
import { getPanelElement } from "./utils/dom/getPanelElement";
import { getPanelElementsForGroup } from "./utils/dom/getPanelElementsForGroup";
import { getPanelGroupElement } from "./utils/dom/getPanelGroupElement";
Expand Down Expand Up @@ -66,6 +70,8 @@ export {
getResizeHandleElementsForGroup,
getResizeHandlePanelIds,

// CSP (see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce)
// Styles and CSP (see https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce)
enableGlobalCursorStyles,
disableGlobalCursorStyles,
setNonce,
};
13 changes: 13 additions & 0 deletions packages/react-resizable-panels/src/utils/cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@ import { getNonce } from "./csp";
type CursorState = "horizontal" | "intersection" | "vertical";

let currentCursorStyle: string | null = null;
let enabled: boolean = true;
let styleElement: HTMLStyleElement | null = null;

export function disableGlobalCursorStyles() {
enabled = false;
}

export function enableGlobalCursorStyles() {
enabled = true;
}

export function getCursorStyle(
state: CursorState,
constraintFlags: number
Expand Down Expand Up @@ -67,6 +76,10 @@ export function setGlobalCursorStyle(
state: CursorState,
constraintFlags: number
) {
if (!enabled) {
return;
}

const style = getCursorStyle(state, constraintFlags);

if (currentCursorStyle === style) {
Expand Down

0 comments on commit 30b63d5

Please sign in to comment.