Files
react/packages/react-interactions/events
Sebastian Markbåge 60016c448b Export React as Named Exports instead of CommonJS (#18106)
* Add options for forked entry points

We currently fork .fb.js entry points. This adds a few more options.

.modern.fb.js - experimental FB builds
.classic.fb.js - stable FB builds
.fb.js - if no other FB build, use this for FB builds
.experimental.js - experimental builds
.stable.js - stable builds
.js - used if no other override exists

This will be used to have different ES exports for different builds.

* Switch React to named exports

* Export named exports from the export point itself

We need to re-export the Flow exported types so we can use them in our code.

We don't want to use the Flow types from upstream since it doesn't have the non-public APIs that we have.

This should be able to use export * but I don't know why it doesn't work.

This actually enables Flow typing of React which was just "any" before.
This exposed some Flow errors that needs fixing.

* Create forks for the react entrypoint

None of our builds expose all exports and they all differ in at least one
way, so we need four forks.

* Set esModule flag to false

We don't want to emit the esModule compatibility flag on our CommonJS
output. For now we treat our named exports as if they're CommonJS.

This is a potentially breaking change for scheduler (but all those apis
are unstable), react-is and use-subscription. However, it seems unlikely
that anyone would rely on this since these only have named exports.

* Remove unused Feature Flags

* Let jest observe the stable fork for stable tests

This lets it do the negative test by ensuring that the right tests fail.

However, this in turn will make other tests that are not behind
__EXPERIMENTAL__ fail. So I need to do that next.

* Put all tests that depend on exports behind __EXPERIMENTAL__

Since there's no way to override the exports using feature flags
in .intern.js anymore we can't use these APIs in stable.

The tradeoff here is that we can either enable the negative tests on
"stable" that means experimental are expected to fail, or we can disable
tests on stable. This is unfortunate since some of these APIs now run on
a "stable" config at FB instead of the experimental.

* Switch ReactDOM to named exports

Same strategy as React.

I moved the ReactDOMFB runtime injection to classic.fb.js

Since we only fork the entrypoint, the `/testing` entrypoint needs to
be forked too to re-export the same things plus `act`. This is a bit
unfortunate. If it becomes a pattern we can consider forking in the
module resolution deeply.

fix flow

* Fix ReactDOM Flow Types

Now that ReactDOM is Flow type checked we need to fix up its types.

* Configure jest to use stable entry for ReactDOM in non-experimental

* Remove additional FeatureFlags that are no longer needed

These are only flagging the exports and no implementation details so we
can control them fully through the export overrides.
2020-02-25 13:54:27 -08:00
..

react-interactions/events

This package is experimental. It is intended for use with the experimental React events API that is not available in open source builds.

Event Responders attach to a host node. They listen to native browser events dispatched on the host node of their child and transform those events into high-level events for applications.

The core API is documented below. Documentation for individual Event Responders can be found here.

Event Responder Interface

Note: React Responders require the internal React flag enableDeprecatedFlareAPI.

An Event Responder Interface is defined using an object. Each responder can define DOM events to listen to, handle the synthetic responder events, dispatch custom events, and implement a state machine.

// types
type ResponderEventType = string;

type ResponderEvent = {|
  nativeEvent: any,
  target: Element | Document,
  pointerType: string,
  type: string,
  passive: boolean,
|};

type CustomEvent = {
  type: string,
  target: Element,
  ...
}

getInitialState?: (props: null | Object) => Object

The initial state of that the Event Responder is created with.

onEvent?: (event: ResponderEvent, context: ResponderContext, props, state)

Called during the bubble phase of the targetEventTypes dispatched on DOM elements within the Event Responder.

onMount?: (context: ResponderContext, props, state)

Called after an Event Responder in mounted.

onRootEvent?: (event: ResponderEvent, context: ResponderContext, props, state)

Called when any of the rootEventTypes are dispatched on the root of the app.

onUnmount?: (context: ResponderContext, props, state)

Called before an Event Responder in unmounted.

rootEventTypes?: Array

Defines the DOM events to listen to on the root of the app.

targetEventTypes?: Array

Defines the DOM events to listen to within the Event Responder subtree.

ResponderContext

The Event Responder Context is exposed via the context argument for certain methods on the EventResponder object.

addRootEventTypes(eventTypes: Array)

This can be used to dynamically listen to events on the root of the app only when it is necessary to do so.

dispatchEvent(propName: string, event: CustomEvent, { discrete: boolean })

Dispatches a custom synthetic event. The type and target are required fields if the event is an object, but any other fields can be defined on the event that will be passed to the listener. You can also pass a value that is not an object, but a boolean. For example:

const event = { type: 'press', target, pointerType, x, y };
context.dispatchEvent('onPress', event, DiscreteEvent);

isTargetWithinNode(target: Element, element: Element): boolean

Returns true if target is a child of element.

isTargetWithinResponder(target: Element): boolean

Returns true is the target element is within the subtree of the Event Responder.

isTargetWithinResponderScope(target: Element): boolean

Returns true is the target element is within the current Event Responder's scope. If the target element is within the scope of the same responder, but owned by another Event Responder instance, this will return false.

removeRootEventTypes(eventTypes: Array)

Remove the root event types added with addRootEventTypes.