Skip to content

Commit

Permalink
Change "react" imports to use * syntax (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
bvaughn authored Apr 2, 2023
2 parents 4b73ea5 + f9d3ad5 commit b1556c6
Show file tree
Hide file tree
Showing 17 changed files with 994 additions and 21 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/eslint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: "ESLint"
on: [pull_request]
jobs:
tests-e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- name: Initialize Yarn and install package dependencies
run: yarn
- name: Run ESLint
run: yarn lint
12 changes: 12 additions & 0 deletions .github/workflows/prettier.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: "Prettier"
on: [pull_request]
jobs:
tests-e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- name: Initialize Yarn and install package dependencies
run: yarn
- name: Run Prettier
run: yarn run prettier:ci
14 changes: 14 additions & 0 deletions .github/workflows/typescript.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: "TypeScript"
on: [pull_request]
jobs:
tests-e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- name: Initialize Yarn and install package dependencies
run: yarn
- name: Build NPM package
run: yarn prerelease
- name: Run TypeScript
run: yarn typescript
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
],
"scripts": {
"docs": "cd packages/react-resizable-panels-website && yarn build",
"lint": "cd packages/react-resizable-panels && yarn lint",
"prerelease": "rm -rf ./.parcel-cache && cd packages/react-resizable-panels && rm -rf ./dist && yarn build",
"prettier": "prettier --write \"**/*.{css,html,js,json,jsx,ts,tsx}\"",
"prettier:ci": "prettier --check \"**/*.{css,html,js,json,jsx,ts,tsx}\"",
"typescript": "tsc --noEmit",
"typescript:watch": "tsc --noEmit --watch"
},
"dependencies": {
"devDependencies": {
"@parcel/packager-ts": "2.8.2",
"@parcel/transformer-typescript-types": "2.8.2",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.10",
"@types/react-virtualized-auto-sizer": "^1.0.1",
"@typescript-eslint/eslint-plugin": "^5.57.0",
"@typescript-eslint/parser": "^5.57.0",
"parcel": "latest",
"prettier": "latest",
"process": "^0.11.10",
"typescript": ">=3.0.0"
},
"devDependencies": {
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.10",
"@types/react-virtualized-auto-sizer": "^1.0.1"
}
}
22 changes: 22 additions & 0 deletions packages/react-resizable-panels/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"ignorePatterns": [".parcel-cache", "dist", "node_modules"],
"parser": "@typescript-eslint/parser",
"parserOptions": { "project": ["../../tsconfig.json"] },
"plugins": ["@typescript-eslint", "no-restricted-imports", "react-hooks"],
"root": true,
"rules": {
"no-restricted-imports": [
"error",
{
"paths": ["react"]
}
],
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": [
"warn",
{
"additionalHooks": "(useIsomorphicLayoutEffect)"
}
]
}
}
3 changes: 3 additions & 0 deletions packages/react-resizable-panels/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 0.0.39
* [#118](https://github.com/bvaughn/react-resizable-panels/issues/118): Fix import regression from 0.0.38.

## 0.0.38
* [#117](https://github.com/bvaughn/react-resizable-panels/issues/117): `Panel` collapse behavior works better near viewport edges.
* [#115](https://github.com/bvaughn/react-resizable-panels/pull/115): `PanelResizeHandle` logic calls `event.preventDefault` for events it handles.
Expand Down
4 changes: 4 additions & 0 deletions packages/react-resizable-panels/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
"types": "dist/react-resizable-panels.d.ts",
"scripts": {
"build": "parcel build",
"lint": "eslint \"src/**/*.{ts,tsx}\"",
"watch": "parcel watch"
},
"devDependencies": {
"eslint": "^8.37.0",
"eslint-plugin-no-restricted-imports": "^0.0.0",
"eslint-plugin-react-hooks": "^4.6.0",
"react": "latest",
"react-dom": "latest"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/react-resizable-panels/src/Panel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
useEffect,
useImperativeHandle,
useRef,
} from "react";
} from "./vendor/react";
import useIsomorphicLayoutEffect from "./hooks/useIsomorphicEffect";
import useUniqueId from "./hooks/useUniqueId";

Expand Down
2 changes: 1 addition & 1 deletion packages/react-resizable-panels/src/PanelContexts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CSSProperties, createContext } from "react";
import { CSSProperties, createContext } from "./vendor/react";

import { PanelData, ResizeEvent, ResizeHandler } from "./types";

Expand Down
8 changes: 4 additions & 4 deletions packages/react-resizable-panels/src/PanelGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
useMemo,
useRef,
useState,
} from "react";
} from "./vendor/react";

import { PanelGroupContext } from "./PanelContexts";
import {
Expand Down Expand Up @@ -250,7 +250,7 @@ export function PanelGroup({
})
);
}
}, [autoSaveId, panels]);
}, [autoSaveId, panels, storage]);

useEffect(() => {
// If this panel has been configured to persist sizing information, save sizes to local storage.
Expand All @@ -263,7 +263,7 @@ export function PanelGroup({

savePanelGroupLayoutDebounced(autoSaveId, panelsArray, sizes, storage);
}
}, [autoSaveId, panels, sizes]);
}, [autoSaveId, panels, sizes, storage]);

const getPanelStyle = useCallback(
(id: string): CSSProperties => {
Expand Down Expand Up @@ -301,7 +301,7 @@ export function PanelGroup({
: undefined,
};
},
[activeHandleId, direction, sizes]
[activeHandleId, disablePointerEventsDuringResize, sizes]
);

const registerPanel = useCallback((id: string, panel: PanelData) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react-resizable-panels/src/PanelResizeHandle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
useEffect,
useRef,
useState,
} from "react";
} from "./vendor/react";
import useUniqueId from "./hooks/useUniqueId";

import { useWindowSplitterResizeHandlerBehavior } from "./hooks/useWindowSplitterBehavior";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useLayoutEffect } from "react";
import { useLayoutEffect } from "../vendor/react";

const canUseEffectHooks = !!(
typeof window !== "undefined" &&
Expand Down
7 changes: 5 additions & 2 deletions packages/react-resizable-panels/src/hooks/useUniqueId.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { useId, useRef } from "react";
import { useId, useRef } from "../vendor/react";

const wrappedUseId: () => string | null =
typeof useId === "function" ? useId : (): null => null;

let counter = 0;

export default function useUniqueId(
idFromParams: string | null = null
): string {
const idFromUseId = typeof useId === "function" ? useId() : null;
const idFromUseId = wrappedUseId();

const idRef = useRef<string | null>(idFromParams || idFromUseId || null);
if (idRef.current === null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RefObject, useEffect } from "react";
import { RefObject, useEffect } from "../vendor/react";
import { PRECISION } from "../constants";

import { CommittedValues, PanelDataMap } from "../PanelGroup";
Expand Down Expand Up @@ -150,7 +150,14 @@ export function useWindowSplitterPanelGroupBehavior({
return () => {
cleanupFunctions.forEach((cleanupFunction) => cleanupFunction());
};
}, [groupId, panels, sizes]);
}, [
committedValuesRef,
groupId,
panels,
panelSizeBeforeCollapse,
setSizes,
sizes,
]);
}

export function useWindowSplitterResizeHandlerBehavior({
Expand Down
2 changes: 1 addition & 1 deletion packages/react-resizable-panels/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RefObject } from "react";
import { RefObject } from "./vendor/react";

export type Direction = "horizontal" | "vertical";

Expand Down
61 changes: 61 additions & 0 deletions packages/react-resizable-panels/src/vendor/react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// This module exists to work around Webpack issue https://github.com/webpack/webpack/issues/14814
// and limitations with ParcelJS parsing of the useId workaround (used below).
// For the time being, all react-resizable-panels must import "react" with the "* as React" syntax.
// To avoid mistakes, we use the ESLint "no-restricted-imports" to prevent "react" imports except in this file.
// See https://github.com/bvaughn/react-resizable-panels/issues/118

// eslint-disable-next-line no-restricted-imports
import * as React from "react";

// eslint-disable-next-line no-restricted-imports
import type {
CSSProperties,
ElementType,
ForwardedRef,
MouseEvent,
ReactNode,
RefObject,
TouchEvent,
} from "react";

const {
createElement,
createContext,
forwardRef,
useCallback,
useContext,
useEffect,
useImperativeHandle,
useLayoutEffect,
useMemo,
useRef,
useState,
} = React;

// `toString()` prevents bundlers from trying to `import { useId } from 'react'`
const useId = (React as any)["useId".toString()] as () => string;

export {
createElement,
createContext,
forwardRef,
useCallback,
useContext,
useEffect,
useId,
useImperativeHandle,
useLayoutEffect,
useMemo,
useRef,
useState,
};

export type {
CSSProperties,
ElementType,
ForwardedRef,
MouseEvent,
ReactNode,
RefObject,
TouchEvent,
};
Loading

1 comment on commit b1556c6

@vercel
Copy link

@vercel vercel bot commented on b1556c6 Apr 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.