Skip to content

Commit 9f3b876

Browse files
omgovichVlad Shilov
authored andcommitted
Tweak variable naming and types
1 parent 0afe804 commit 9f3b876

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
lines changed

src/components/common/AlphaColorPicker.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ export const AlphaColorPicker = <T extends AnyColor>({
2020
onChange,
2121
...rest
2222
}: Props<T>): JSX.Element => {
23-
const containerRef = useRef<HTMLDivElement>(null);
24-
useStyleSheet(containerRef);
23+
const nodeRef = useRef<HTMLDivElement>(null);
24+
useStyleSheet(nodeRef);
2525

2626
const [hsva, updateHsva] = useColorManipulation<T>(colorModel, color, onChange);
2727

2828
const nodeClassName = formatClassName(["react-colorful", className]);
2929

3030
return (
31-
<div {...rest} ref={containerRef} className={nodeClassName}>
31+
<div {...rest} ref={nodeRef} className={nodeClassName}>
3232
<Saturation hsva={hsva} onChange={updateHsva} />
3333
<Hue hue={hsva.h} onChange={updateHsva} />
3434
<Alpha hsva={hsva} onChange={updateHsva} className="react-colorful__last-control" />

src/components/common/ColorPicker.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ export const ColorPicker = <T extends AnyColor>({
1919
onChange,
2020
...rest
2121
}: Props<T>): JSX.Element => {
22-
const containerRef = useRef<HTMLDivElement>(null);
23-
useStyleSheet(containerRef);
22+
const nodeRef = useRef<HTMLDivElement>(null);
23+
useStyleSheet(nodeRef);
2424

2525
const [hsva, updateHsva] = useColorManipulation<T>(colorModel, color, onChange);
2626

2727
const nodeClassName = formatClassName(["react-colorful", className]);
2828

2929
return (
30-
<div {...rest} ref={containerRef} className={nodeClassName}>
30+
<div {...rest} ref={nodeRef} className={nodeClassName}>
3131
<Saturation hsva={hsva} onChange={updateHsva} />
3232
<Hue hue={hsva.h} onChange={updateHsva} className="react-colorful__last-control" />
3333
</div>

src/components/common/Interactive.tsx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useRef, useMemo, useEffect } from "react";
22

33
import { useEventCallback } from "../../hooks/useEventCallback";
44
import { clamp } from "../../utils/clamp";
5+
56
export interface Interaction {
67
left: number;
78
top: number;
@@ -18,6 +19,11 @@ const getTouchPoint = (touches: TouchList, touchId: null | number): Touch => {
1819
return touches[0];
1920
};
2021

22+
// Finds the proper window object to fix iframe embedding issues
23+
const getParentWindow = (node?: HTMLDivElement | null): Window => {
24+
return (node && node.ownerDocument.defaultView) || self;
25+
};
26+
2127
// Returns a relative position of the pointer inside the node's bounding box
2228
const getRelativePosition = (
2329
node: HTMLDivElement,
@@ -28,11 +34,10 @@ const getRelativePosition = (
2834

2935
// Get user's pointer position from `touches` array if it's a `TouchEvent`
3036
const pointer = isTouch(event) ? getTouchPoint(event.touches, touchId) : (event as MouseEvent);
31-
const nodeWindow = node.ownerDocument.defaultView || window;
3237

3338
return {
34-
left: clamp((pointer.pageX - (rect.left + nodeWindow.pageXOffset)) / rect.width),
35-
top: clamp((pointer.pageY - (rect.top + nodeWindow.pageYOffset)) / rect.height),
39+
left: clamp((pointer.pageX - (rect.left + getParentWindow(node).pageXOffset)) / rect.width),
40+
top: clamp((pointer.pageY - (rect.top + getParentWindow(node).pageYOffset)) / rect.height),
3641
};
3742
};
3843

@@ -122,12 +127,10 @@ const InteractiveBase = ({ onMove, onKey, ...rest }: Props) => {
122127
function toggleDocumentEvents(state?: boolean) {
123128
const touch = hasTouch.current;
124129
const el = container.current;
125-
const containerWindow = (el && el.ownerDocument.defaultView) || self;
130+
const parentWindow = getParentWindow(el);
126131

127-
// add or remove additional pointer event listeners
128-
const toggleEvent = state
129-
? containerWindow.addEventListener
130-
: containerWindow.removeEventListener;
132+
// Add or remove additional pointer event listeners
133+
const toggleEvent = state ? parentWindow.addEventListener : parentWindow.removeEventListener;
131134
toggleEvent(touch ? "touchmove" : "mousemove", handleMove);
132135
toggleEvent(touch ? "touchend" : "mouseup", handleMoveEnd);
133136
}

src/hooks/useStyleSheet.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1+
import { RefObject } from "react";
2+
13
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
24
import { getNonce } from "../utils/nonce";
35

46
// Bundler is configured to load this as a processed minified CSS-string
57
import styles from "../css/styles.css";
6-
import { RefObject } from "react";
78

89
const styleElementMap: Map<Document, HTMLStyleElement> = new Map();
910

1011
/**
1112
* Injects CSS code into the document's <head>
1213
*/
13-
export const useStyleSheet = (containerRef?: RefObject<HTMLDivElement>): void => {
14+
export const useStyleSheet = (nodeRef: RefObject<HTMLDivElement>): void => {
1415
useIsomorphicLayoutEffect(() => {
15-
const containerDocument =
16-
containerRef && containerRef.current ? containerRef.current.ownerDocument : document;
17-
if (typeof containerDocument !== "undefined" && !styleElementMap.has(containerDocument)) {
18-
const styleElement = containerDocument.createElement("style");
16+
const parentDocument = nodeRef.current ? nodeRef.current.ownerDocument : document;
17+
18+
if (typeof parentDocument !== "undefined" && !styleElementMap.has(parentDocument)) {
19+
const styleElement = parentDocument.createElement("style");
1920
styleElement.innerHTML = styles;
20-
styleElementMap.set(containerDocument, styleElement);
21+
styleElementMap.set(parentDocument, styleElement);
2122

2223
// Conform to CSP rules by setting `nonce` attribute to the inline styles
2324
const nonce = getNonce();
2425
if (nonce) styleElement.setAttribute("nonce", nonce);
2526

26-
containerDocument.head.appendChild(styleElement);
27+
parentDocument.head.appendChild(styleElement);
2728
}
2829
}, []);
2930
};

0 commit comments

Comments
 (0)