Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds more scaffolding for experimental event API #15112

Merged
merged 17 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/react-art/src/ReactARTHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ export function handleEventComponent(
}

export function handleEventTarget(
type: string,
type: Symbol | number,
props: Props,
internalInstanceHandle: Object,
) {
Expand Down
27 changes: 25 additions & 2 deletions packages/react-dom/src/client/ReactDOMHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import dangerousStyleValue from '../shared/dangerousStyleValue';

import type {DOMContainer} from './ReactDOM';
import type {ReactEventResponder} from 'shared/ReactTypes';
import {REACT_EVENT_COMPONENT_TYPE} from 'shared/ReactSymbols';

export type Type = string;
export type Props = {
Expand All @@ -65,6 +66,7 @@ export type PublicInstance = Element | Text;
type HostContextDev = {
namespace: string,
ancestorInfo: mixed,
isEventComponent?: boolean,
};
type HostContextProd = string;
export type HostContext = HostContextDev | HostContextProd;
Expand All @@ -73,7 +75,11 @@ export type ChildSet = void; // Unused
export type TimeoutHandle = TimeoutID;
export type NoTimeout = -1;

import {enableSuspenseServerRenderer} from 'shared/ReactFeatureFlags';
import {
enableSuspenseServerRenderer,
enableEventAPI,
} from 'shared/ReactFeatureFlags';
import warning from 'shared/warning';

// Intentionally not named imports because Rollup would
// use dynamic dispatch for CommonJS interop named imports.
Expand Down Expand Up @@ -142,6 +148,9 @@ export function getRootHostContext(
if (__DEV__) {
const validatedTag = type.toLowerCase();
const ancestorInfo = updatedAncestorInfo(null, validatedTag);
if (enableEventAPI) {
return {namespace, ancestorInfo, isEventComponent: false};
}
return {namespace, ancestorInfo};
}
return namespace;
Expand All @@ -159,6 +168,12 @@ export function getChildHostContext(
parentHostContextDev.ancestorInfo,
type,
);
if (enableEventAPI) {
if (type === REACT_EVENT_COMPONENT_TYPE) {
return {namespace, ancestorInfo, isEventComponent: true};
}
return {namespace, ancestorInfo, isEventComponent: false};
}
return {namespace, ancestorInfo};
}
const parentNamespace = ((parentHostContext: any): HostContextProd);
Expand Down Expand Up @@ -296,6 +311,14 @@ export function createTextInstance(
if (__DEV__) {
const hostContextDev = ((hostContext: any): HostContextDev);
validateDOMNesting(null, text, hostContextDev.ancestorInfo);
if (enableEventAPI) {
warning(
!hostContextDev.isEventComponent,
'validateDOMNesting: React event components cannot have text DOM nodes as children. ' +
'Wrap the child text "%s" in an element.',
text,
);
}
}
const textNode: TextInstance = createTextNode(text, rootContainerInstance);
precacheFiberNode(internalInstanceHandle, textNode);
Expand Down Expand Up @@ -804,7 +827,7 @@ export function handleEventComponent(
}

export function handleEventTarget(
type: string,
type: Symbol | number,
props: Props,
internalInstanceHandle: Object,
) {
Expand Down
21 changes: 1 addition & 20 deletions packages/react-dom/src/client/validateDOMNesting.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

import {REACT_EVENT_COMPONENT_TYPE} from 'shared/ReactSymbols';
import warningWithoutStack from 'shared/warningWithoutStack';
// TODO: direct imports like some-package/src/* are bad. Fix me.
import {getCurrentFiberStackInDev} from 'react-reconciler/src/ReactCurrentFiber';

import {enableEventAPI} from 'shared/ReactFeatureFlags';

let validateDOMNesting = () => {};
let updatedAncestorInfo = () => {};

Expand Down Expand Up @@ -162,18 +159,10 @@ if (__DEV__) {
dlItemTagAutoclosing: null,
};

updatedAncestorInfo = function(oldInfo, tag: string | Symbol) {
updatedAncestorInfo = function(oldInfo, tag) {
let ancestorInfo = {...(oldInfo || emptyAncestorInfo)};
let info = {tag};

if (enableEventAPI) {
if (tag === REACT_EVENT_COMPONENT_TYPE) {
ancestorInfo.inEventComponent = true;
return ancestorInfo;
} else {
ancestorInfo.inEventComponent = false;
}
}
if (inScopeTags.indexOf(tag) !== -1) {
ancestorInfo.aTagInScope = null;
ancestorInfo.buttonTagInScope = null;
Expand Down Expand Up @@ -422,14 +411,6 @@ if (__DEV__) {
const parentTag = parentInfo && parentInfo.tag;

if (childText != null) {
if (enableEventAPI) {
warningWithoutStack(
!ancestorInfo.inEventComponent,
'validateDOMNesting: React event components cannot have text DOM nodes as children. ' +
'Wrap the child text "%s" in an element.',
childText,
);
}
warningWithoutStack(
childTag == null,
'validateDOMNesting: when childText is passed, childTag should be null',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ export function handleEventComponent(
}

export function handleEventTarget(
type: string,
type: Symbol | number,
props: Props,
internalInstanceHandle: Object,
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ export function handleEventComponent(
}

export function handleEventTarget(
type: string,
type: Symbol | number,
props: Props,
internalInstanceHandle: Object,
) {
Expand Down
8 changes: 4 additions & 4 deletions packages/react-reconciler/src/ReactFiber.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type {
ReactFragment,
ReactPortal,
RefObject,
ReactEvent,
ReactEventComponent,
ReactEventTarget,
} from 'shared/ReactTypes';
import type {WorkTag} from 'shared/ReactWorkTags';
Expand Down Expand Up @@ -614,15 +614,15 @@ export function createFiberFromFragment(
}

export function createFiberFromEventComponent(
event: ReactEvent,
eventComponent: ReactEventComponent,
pendingProps: any,
mode: TypeOfMode,
expirationTime: ExpirationTime,
key: null | string,
): Fiber {
const fiber = createFiber(EventComponent, pendingProps, key, mode);
fiber.elementType = event;
fiber.type = event;
fiber.elementType = eventComponent;
fiber.type = eventComponent;
fiber.stateNode = new Map();
fiber.expirationTime = expirationTime;
return fiber;
Expand Down
8 changes: 6 additions & 2 deletions packages/react-reconciler/src/ReactFiberBeginWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ import shallowEqual from 'shared/shallowEqual';
import getComponentName from 'shared/getComponentName';
import ReactStrictModeWarnings from './ReactStrictModeWarnings';
import {refineResolvedLazyComponent} from 'shared/ReactLazyComponent';
import {REACT_LAZY_TYPE} from 'shared/ReactSymbols';
import {
REACT_LAZY_TYPE,
REACT_EVENT_TARGET_TOUCH_HIT,
} from 'shared/ReactSymbols';
import warning from 'shared/warning';
import warningWithoutStack from 'shared/warningWithoutStack';
import {
Expand Down Expand Up @@ -1960,8 +1963,9 @@ function updateEventTarget(current, workInProgress, renderExpirationTime) {
parent !== null && parent.tag === Event,
'Event target components must be direct children of event components',
);
trueadm marked this conversation as resolved.
Show resolved Hide resolved
const eventTargetType = nextProps.type;
// These warnings only occur in DEV to reduce overhead in production
if (__DEV__ && nextProps.type === 'touch-hit') {
if (__DEV__ && eventTargetType === REACT_EVENT_TARGET_TOUCH_HIT) {
trueadm marked this conversation as resolved.
Show resolved Hide resolved
let childrenCount = 0;
let child = workInProgress.child;
while (child !== null) {
Expand Down
10 changes: 9 additions & 1 deletion packages/react-reconciler/src/ReactFiberHostContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import type {StackCursor} from './ReactFiberStack';
import type {Container, HostContext} from './ReactFiberHostConfig';

import invariant from 'shared/invariant';
import {EventComponent} from 'shared/ReactWorkTags';

import {getChildHostContext, getRootHostContext} from './ReactFiberHostConfig';
import {createCursor, push, pop} from './ReactFiberStack';

import {enableEventAPI} from 'shared/ReactFeatureFlags';

declare class NoContextT {}
const NO_CONTEXT: NoContextT = ({}: any);

Expand Down Expand Up @@ -79,7 +82,12 @@ function pushHostContext(fiber: Fiber): void {
rootInstanceStackCursor.current,
);
const context: HostContext = requiredContext(contextStackCursor.current);
const nextContext = getChildHostContext(context, fiber.type, rootInstance);
let type = fiber.type;
// For React event components, we get the typeof field (the symbol)
trueadm marked this conversation as resolved.
Show resolved Hide resolved
if (enableEventAPI && fiber.tag === EventComponent) {
type = type.$$typeof;
}
const nextContext = getChildHostContext(context, type, rootInstance);

// Don't push this Fiber's context unless it's unique.
if (context === nextContext) {
trueadm marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion packages/react-test-renderer/src/ReactTestHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export function handleEventComponent(
}

export function handleEventTarget(
type: string,
type: Symbol | number,
props: Props,
internalInstanceHandle: Object,
) {
Expand Down
5 changes: 5 additions & 0 deletions packages/shared/ReactSymbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ export const REACT_EVENT_TARGET_TYPE = hasSymbol
? Symbol.for('react.event_target')
: 0xead6;

// React event targets
export const REACT_EVENT_TARGET_TOUCH_HIT = hasSymbol
? Symbol.for('react.event_target.touch_hit')
: 0xead7;

const MAYBE_ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;
const FAUX_ITERATOR_SYMBOL = '@@iterator';

Expand Down
10 changes: 7 additions & 3 deletions packages/shared/ReactTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type ReactNode =
| ReactFragment
| ReactProvider<any>
| ReactConsumer<any>
| ReactEvent
| ReactEventComponent
| ReactEventTarget;

export type ReactEmpty = null | void | boolean;
Expand Down Expand Up @@ -81,13 +81,17 @@ export type RefObject = {|
current: any,
|};

export type ReactEventResponderEventType =
| string
| {name: string, passive?: boolean, capture?: boolean};

export type ReactEventResponder = {
targetEventTypes: Array<string>,
targetEventTypes: Array<ReactEventResponderEventType>,
createInitialState?: (props: Object) => Object,
handleEvent: (context: Object, props: Object, state: Object) => void,
};

export type ReactEvent = {|
export type ReactEventComponent = {|
$$typeof: Symbol | number,
props: null | Object,
responder: ReactEventResponder,
Expand Down