From 7320819ff98630bcf43e70b46890281e4b586029 Mon Sep 17 00:00:00 2001 From: Emmanuel Ogbizi-Ugbe Date: Sat, 7 May 2022 00:03:22 -0400 Subject: [PATCH] style: switch indentation to 2 spaces --- .eslintrc.json | 4 +- .prettierrc.json | 10 +- config/mocks/DOMRect.ts | 38 ++-- config/mocks/ResizeObserver.ts | 18 +- demo/src/App.tsx | 56 ++--- demo/src/Clock.tsx | 46 ++-- demo/src/Window.tsx | 36 +-- package.json | 2 +- src/__mocks__.ts | 38 ++-- src/__tests__/index.test.ts | 8 +- src/components/Element.tsx | 32 +-- src/components/Frame.tsx | 70 +++--- src/components/IFrame.tsx | 26 +-- src/components/Mirror.tsx | 64 +++--- src/components/Reflection.tsx | 146 ++++++------ src/components/Styles.tsx | 4 +- src/components/__tests__/Element.test.tsx | 62 ++--- src/components/__tests__/Frame.test.tsx | 34 ++- src/components/__tests__/Mirror.test.tsx | 107 +++++---- src/components/__tests__/Reflection.test.tsx | 228 +++++++++---------- src/hooks/__tests__/useDimensions.test.ts | 118 +++++----- src/hooks/__tests__/useMirror.test.ts | 22 +- src/hooks/__tests__/useObserver.test.ts | 171 +++++++------- src/hooks/__tests__/useRef.test.ts | 24 +- src/hooks/__tests__/useRenderTrigger.test.ts | 18 +- src/hooks/useDimensions.ts | 40 ++-- src/hooks/useMirror.tsx | 4 +- src/hooks/useObserver.ts | 78 +++---- src/hooks/usePortal.ts | 20 +- src/hooks/useRef.ts | 4 +- src/hooks/useRenderTrigger.ts | 18 +- src/utils/dom.ts | 74 +++--- src/utils/index.ts | 18 +- webpack.config.ts | 100 ++++---- 34 files changed, 865 insertions(+), 873 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index ae9816e2..6a8761dd 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -42,8 +42,8 @@ "allowAllPropertiesOnSameLine": true }], "react/jsx-first-prop-new-line": [2, "multiline"], - "react/jsx-indent": [2, 4], - "react/jsx-indent-props": [2, 4], + "react/jsx-indent": [2, 2], + "react/jsx-indent-props": [2, 2], "prettier/prettier": 2 }, "settings": { diff --git a/.prettierrc.json b/.prettierrc.json index eb3f4216..9494cd0a 100644 --- a/.prettierrc.json +++ b/.prettierrc.json @@ -1,7 +1,7 @@ { - "bracketSpacing": true, - "semi": true, - "singleQuote": false, - "tabWidth": 4, - "trailingComma": "all" + "bracketSpacing": true, + "semi": true, + "singleQuote": false, + "tabWidth": 2, + "trailingComma": "all" } diff --git a/config/mocks/DOMRect.ts b/config/mocks/DOMRect.ts index 4b17474d..f90090ff 100644 --- a/config/mocks/DOMRect.ts +++ b/config/mocks/DOMRect.ts @@ -1,23 +1,23 @@ export default class DOMRect { - bottom = 0; - left = 0; - right = 0; - top = 0; - constructor( - // eslint-disable-next-line no-unused-vars - public x = 0, - // eslint-disable-next-line no-unused-vars - public y = 0, - // eslint-disable-next-line no-unused-vars - public width = 0, - // eslint-disable-next-line no-unused-vars - public height = 0, - ) { - this.left = x; - this.top = y; - this.right = x + width; - this.bottom = y + height; - } + bottom = 0; + left = 0; + right = 0; + top = 0; + constructor( + // eslint-disable-next-line no-unused-vars + public x = 0, + // eslint-disable-next-line no-unused-vars + public y = 0, + // eslint-disable-next-line no-unused-vars + public width = 0, + // eslint-disable-next-line no-unused-vars + public height = 0, + ) { + this.left = x; + this.top = y; + this.right = x + width; + this.bottom = y + height; + } } Object.assign(window, { DOMRect }); diff --git a/config/mocks/ResizeObserver.ts b/config/mocks/ResizeObserver.ts index fe7885fa..da466d64 100644 --- a/config/mocks/ResizeObserver.ts +++ b/config/mocks/ResizeObserver.ts @@ -1,13 +1,13 @@ export default class ResizeObserver { - observe() { - // do nothing - } - unobserve() { - // do nothing - } - disconnect() { - // do nothing - } + observe() { + // do nothing + } + unobserve() { + // do nothing + } + disconnect() { + // do nothing + } } Object.assign(window, { ResizeObserver }); diff --git a/demo/src/App.tsx b/demo/src/App.tsx index b1dd69d6..e185cba2 100644 --- a/demo/src/App.tsx +++ b/demo/src/App.tsx @@ -5,34 +5,34 @@ import { Clock } from "./Clock"; import "./styles.css"; export default function App(): JSX.Element { - const [usingPortal, setUsingPortal] = React.useState(false); - const [ref, reflection] = useMirror({ className: "Frame" }); - return ( -
-

React Mirror Demo

- + const [usingPortal, setUsingPortal] = React.useState(false); + const [ref, reflection] = useMirror({ className: "Frame" }); + return ( +
+

React Mirror Demo

+ -
-
- -
Mirror mirror in my dom
- -
- - {usingPortal ? ( - setUsingPortal(false)}> - {reflection} - - ) : ( - reflection - )} -
+
+
+ +
Mirror mirror in my dom
+
- ); + + {usingPortal ? ( + setUsingPortal(false)}> + {reflection} + + ) : ( + reflection + )} +
+
+ ); } diff --git a/demo/src/Clock.tsx b/demo/src/Clock.tsx index 4d34ed6c..0dec6c6b 100644 --- a/demo/src/Clock.tsx +++ b/demo/src/Clock.tsx @@ -2,30 +2,30 @@ import * as React from "react"; import "./clock.css"; function useCurrent(): Date { - const [date, setDate] = React.useState(new Date()); - React.useEffect(() => { - const timeout = setTimeout(() => setDate(new Date()), 300); - return (): void => clearTimeout(timeout); - }, [date, setDate]); - return date; + const [date, setDate] = React.useState(new Date()); + React.useEffect(() => { + const timeout = setTimeout(() => setDate(new Date()), 300); + return (): void => clearTimeout(timeout); + }, [date, setDate]); + return date; } export function Clock(): JSX.Element { - const date = useCurrent(); - const ratios = { - Second: date.getSeconds() / 60, - Minute: date.getMinutes() / 60, - Hour: (date.getHours() % 12) / 12, - }; - return ( -
- {Object.entries(ratios).map(([cls, ratio]) => ( - - ))} -
- ); + const date = useCurrent(); + const ratios = { + Second: date.getSeconds() / 60, + Minute: date.getMinutes() / 60, + Hour: (date.getHours() % 12) / 12, + }; + return ( +
+ {Object.entries(ratios).map(([cls, ratio]) => ( + + ))} +
+ ); } diff --git a/demo/src/Window.tsx b/demo/src/Window.tsx index 453843e6..c64f7f2d 100644 --- a/demo/src/Window.tsx +++ b/demo/src/Window.tsx @@ -2,24 +2,24 @@ import * as React from "react"; import * as ReactDOM from "react-dom"; export function Window({ - onClose, - children, + onClose, + children, }: { - onClose: () => void; - children: React.ReactNode; + onClose: () => void; + children: React.ReactNode; }): JSX.Element { - const portalWindow = React.useMemo(() => { - const _window = window.open( - "", - "React Mirror Portal Test", - "width=400,height=400", - ); - _window?.addEventListener("beforeunload", onClose); - return _window; - }, [onClose]); - const portal = React.useMemo(() => { - if (!portalWindow?.document?.body) return; - return ReactDOM.createPortal(children, portalWindow.document.body); - }, [children, portalWindow]); - return <>{portal}; + const portalWindow = React.useMemo(() => { + const _window = window.open( + "", + "React Mirror Portal Test", + "width=400,height=400", + ); + _window?.addEventListener("beforeunload", onClose); + return _window; + }, [onClose]); + const portal = React.useMemo(() => { + if (!portalWindow?.document?.body) return; + return ReactDOM.createPortal(children, portalWindow.document.body); + }, [children, portalWindow]); + return <>{portal}; } diff --git a/package.json b/package.json index 80f00c12..a07cd590 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "lib" ], "scripts": { - "lint": "eslint . --ext .js,.ts", + "lint": "eslint . --ext .js,.ts,.jsx,.tsx", "build": "webpack --mode=production", "build-watch": "webpack --mode=development --watch", "build-types": "tsc --declaration --emitDeclarationOnly --project tsconfig.prod.json", diff --git a/src/__mocks__.ts b/src/__mocks__.ts index ffca5fe2..a5fbefae 100644 --- a/src/__mocks__.ts +++ b/src/__mocks__.ts @@ -1,25 +1,25 @@ export function addDomNode() { - const domNode = document.createElement("div"); - document.body.appendChild(domNode); - const node0 = document.createComment("comment node"); - document.appendChild(node0); - const node1 = document.createElement("div"); - domNode.appendChild(node1); - node1.className = "class1 one"; - node1.setAttribute("attr", "[value"); - const node2 = document.createElement("input"); - node1.appendChild(node2); - node2.className = "class2 two"; - node2.value = "input-value"; - const node3 = document.createTextNode("text content"); - domNode.appendChild(node3); - return domNode; + const domNode = document.createElement("div"); + document.body.appendChild(domNode); + const node0 = document.createComment("comment node"); + document.appendChild(node0); + const node1 = document.createElement("div"); + domNode.appendChild(node1); + node1.className = "class1 one"; + node1.setAttribute("attr", "[value"); + const node2 = document.createElement("input"); + node1.appendChild(node2); + node2.className = "class2 two"; + node2.value = "input-value"; + const node3 = document.createTextNode("text content"); + domNode.appendChild(node3); + return domNode; } export function addDomStyles() { - const domStyle = document.createElement("style"); - document.head.appendChild(domStyle); - domStyle.innerHTML = ` + const domStyle = document.createElement("style"); + document.head.appendChild(domStyle); + domStyle.innerHTML = ` @charset "utf-8"; @font-face { font-family: "Open Sans"; @@ -58,5 +58,5 @@ export function addDomStyles() { width: 20px; } `; - return domStyle; + return domStyle; } diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 6991c634..c8e18d05 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -1,7 +1,7 @@ describe("Entry", () => { - it("exports expected modules", async () => { - // eslint-disable-next-line @typescript-eslint/no-var-requires - expect(require("../")).toMatchInlineSnapshot(` + it("exports expected modules", async () => { + // eslint-disable-next-line @typescript-eslint/no-var-requires + expect(require("../")).toMatchInlineSnapshot(` Object { "Frame": [Function], "Mirror": [Function], @@ -9,5 +9,5 @@ describe("Entry", () => { "useMirror": [Function], } `); - }); + }); }); diff --git a/src/components/Element.tsx b/src/components/Element.tsx index 944b23d4..2b99469a 100644 --- a/src/components/Element.tsx +++ b/src/components/Element.tsx @@ -1,28 +1,28 @@ import * as React from "react"; export type ElementProps = - T extends keyof JSX.IntrinsicElements - ? JSX.IntrinsicElements[T] - : JSX.IntrinsicElements[keyof JSX.IntrinsicElements]; + T extends keyof JSX.IntrinsicElements + ? JSX.IntrinsicElements[T] + : JSX.IntrinsicElements[keyof JSX.IntrinsicElements]; type DOMElement = T extends keyof HTMLElementTagNameMap - ? HTMLElementTagNameMap[T] - : never; + ? HTMLElementTagNameMap[T] + : never; type DOMElementProps = { - tagName: T; - domRef?: React.Ref>; + tagName: T; + domRef?: React.Ref>; } & ElementProps; export function Element({ - children, - domRef, - tagName, - ...props + children, + domRef, + tagName, + ...props }: DOMElementProps) { - return React.createElement( - tagName.toLowerCase(), - { ...props, ref: domRef }, - ...(Array.isArray(children) ? children : [children]), - ); + return React.createElement( + tagName.toLowerCase(), + { ...props, ref: domRef }, + ...(Array.isArray(children) ? children : [children]), + ); } diff --git a/src/components/Frame.tsx b/src/components/Frame.tsx index 6125bfa2..0cdce356 100644 --- a/src/components/Frame.tsx +++ b/src/components/Frame.tsx @@ -10,54 +10,54 @@ export type FrameProps = Omit; * Used to wrap and isolate reflection from rest of document */ export function Frame({ children, ...frameProps }: FrameProps) { - return ( - - ); + return ( + + ); } /** * Copy of the current document style sheets to be used in framing */ function DocumentStyle() { - const [rules, setRules] = React.useState(getAllStyleRules); + const [rules, setRules] = React.useState(getAllStyleRules); - const onUpdate = React.useCallback( - function updateRules() { - const newRules = getAllStyleRules(); - if (JSON.stringify(rules) === JSON.stringify(newRules)) return; - setRules(newRules); - }, - [rules], - ); + const onUpdate = React.useCallback( + function updateRules() { + const newRules = getAllStyleRules(); + if (JSON.stringify(rules) === JSON.stringify(newRules)) return; + setRules(newRules); + }, + [rules], + ); - useObserver({ - ObserverClass: MutationObserver, - onUpdate, - initOptions: { - attributeFilter: ["class"], - attributes: true, - characterData: true, - childList: true, - subtree: true, - }, - target: window.document, - }); + useObserver({ + ObserverClass: MutationObserver, + onUpdate, + initOptions: { + attributeFilter: ["class"], + attributes: true, + characterData: true, + childList: true, + subtree: true, + }, + target: window.document, + }); - return ; + return ; } diff --git a/src/components/__tests__/Element.test.tsx b/src/components/__tests__/Element.test.tsx index 190992a3..25dec00d 100644 --- a/src/components/__tests__/Element.test.tsx +++ b/src/components/__tests__/Element.test.tsx @@ -3,24 +3,24 @@ import { render } from "@testing-library/react"; import { Element } from "../Element"; describe("Element", () => { - it("creates expected element", () => { - const ref = React.createRef(); - expect(ref.current).toBeNull(); - render(); - expect(ref.current).toMatchInlineSnapshot(` + it("creates expected element", () => { + const ref = React.createRef(); + expect(ref.current).toBeNull(); + render(); + expect(ref.current).toMatchInlineSnapshot(`
`); - }); + }); - it("creates expected element with array of children", () => { - const subject = render( - - {["childA", "childB"]} - , - ); - expect(subject.getByTestId("testId")).toMatchInlineSnapshot(` + it("creates expected element with array of children", () => { + const subject = render( + + {["childA", "childB"]} + , + ); + expect(subject.getByTestId("testId")).toMatchInlineSnapshot(` @@ -28,16 +28,16 @@ describe("Element", () => { childB `); - }); + }); - it("creates element with children multiple nodes", () => { - const subject = render( - - <>{"childA"} - <>{"childB"} - , - ); - expect(subject.getByTestId("testId")).toMatchInlineSnapshot(` + it("creates element with children multiple nodes", () => { + const subject = render( + + <>{"childA"} + <>{"childB"} + , + ); + expect(subject.getByTestId("testId")).toMatchInlineSnapshot(` @@ -45,20 +45,20 @@ describe("Element", () => { childB `); - }); + }); - it("creates element with children single nodes", () => { - const subject = render( - - {"childA"} - , - ); - expect(subject.getByTestId("testId")).toMatchInlineSnapshot(` + it("creates element with children single nodes", () => { + const subject = render( + + {"childA"} + , + ); + expect(subject.getByTestId("testId")).toMatchInlineSnapshot(` childA `); - }); + }); }); diff --git a/src/components/__tests__/Frame.test.tsx b/src/components/__tests__/Frame.test.tsx index 6f091d96..4fe398ec 100644 --- a/src/components/__tests__/Frame.test.tsx +++ b/src/components/__tests__/Frame.test.tsx @@ -4,25 +4,24 @@ import { addDomStyles } from "../../__mocks__"; import { Frame } from "../Frame"; describe("Frame", () => { - it("creates frame element with styles and children", async () => { - // add document styles to be cloned - const domStyle = addDomStyles(); + it("creates frame element with styles and children", async () => { + // add document styles to be cloned + const domStyle = addDomStyles(); - const subject = render( - -
{"child text"}
- , - ); - const iframe = subject.getByTestId("testId") as HTMLIFrameElement; + const subject = render( + +
{"child text"}
+ , + ); + const iframe = subject.getByTestId("testId") as HTMLIFrameElement; - expect(iframe).toMatchInlineSnapshot(` + expect(iframe).toMatchInlineSnapshot(`