Skip to content

Commit

Permalink
Interactivity: Fix broken react usage in published package (#58258)
Browse files Browse the repository at this point in the history
@wordpress/interactivity depends on preact, not react.

- Enable the "no react in scope" eslint rule for the interactivity packages.
- Add pragmas to set the JSX transform the use createElement.
- Import Preact's `h` as `createElement` explicitly.

This setup ensures that `@wordpress/babel-plugin-import-jsx-pragma` does not add a
`createElement` import from React because it detects createElement is already in scope.
  • Loading branch information
sirreal authored and cbravobernal committed Jan 30, 2024
1 parent 5313e53 commit abd9132
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,5 +386,11 @@ module.exports = {
],
},
},
{
files: [ 'packages/interactivity*/src/**' ],
rules: {
'react/react-in-jsx-scope': 'error',
},
},
],
};
4 changes: 4 additions & 0 deletions packages/interactivity/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- Ensure Preact is used in published packages ([58258](https://github.com/WordPress/gutenberg/pull/58258).

## 4.0.0 (2024-01-24)

### Enhancements
Expand Down
3 changes: 3 additions & 0 deletions packages/interactivity/src/directives.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* @jsx createElement */

/**
* External dependencies
*/
import { h as createElement } from 'preact';
import { useContext, useMemo, useRef } from 'preact/hooks';
import { deepSignal, peek } from 'deepsignal';

Expand Down
13 changes: 10 additions & 3 deletions packages/interactivity/src/hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/* @jsx createElement */

/**
* External dependencies
*/
import { h, options, createContext, cloneElement } from 'preact';
import {
h as createElement,
options,
createContext,
cloneElement,
} from 'preact';
import { useRef, useCallback, useContext } from 'preact/hooks';
import type { VNode, Context, RefObject } from 'preact';

Expand Down Expand Up @@ -59,7 +66,7 @@ interface Scope {
evaluate: Evaluate;
context: Context< any >;
ref: RefObject< HTMLElement >;
attributes: h.JSX.HTMLAttributes;
attributes: createElement.JSX.HTMLAttributes;
}

interface Evaluate {
Expand Down Expand Up @@ -372,7 +379,7 @@ options.vnode = ( vnode: VNode< any > ) => {
priorityLevels,
originalProps: props,
type: vnode.type,
element: h( vnode.type as any, props ),
element: createElement( vnode.type as any, props ),
top: true,
};
vnode.type = Directives;
Expand Down
12 changes: 3 additions & 9 deletions tools/webpack/interactivity.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ module.exports = {
...baseConfig,
name: 'interactivity',
entry: {
index: `./packages/interactivity/src/index.js`,
router: `./packages/interactivity-router/src/index.js`,
index: './packages/interactivity',
router: './packages/interactivity-router',
navigation: './packages/block-library/src/navigation/view.js',
query: './packages/block-library/src/query/view.js',
image: './packages/block-library/src/image/view.js',
Expand Down Expand Up @@ -57,13 +57,7 @@ module.exports = {
configFile: false,
presets: [
'@babel/preset-typescript',
[
'@babel/preset-react',
{
runtime: 'automatic',
importSource: 'preact',
},
],
'@babel/preset-react',
],
},
},
Expand Down

0 comments on commit abd9132

Please sign in to comment.