material-motion-views-react
Version:
React support for Material Motion
97 lines (96 loc) • 4.16 kB
TypeScript
/// <reference types="react" />
/** @license
* Copyright 2016 - present The Material Motion Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
import * as React from 'react';
import { StreamDict } from 'material-motion';
export declare type AttachStreamsProps = StreamDict<any> & {
children: React.ReactElement<{
domRef(element: Element): void;
}>;
domRef?: (element: Element | null) => void;
};
export declare type AttachStreamsState = {
[index: string]: any;
};
/**
* `AttachStreams` is a stateful component that listens to any stream it
* receives as a prop. Whenever the stream emits a value, `AttachStreams`
* forwards that value as a prop onto its child component.
*
* If the prop name starts with "on," `AttachStreams` presumes the prop
* represents an event stream, and that the stream it received is a subject.
* Instead of subscribing to the stream and forwarding values to the child
* component, it will subscribe to the appropriate event on the child component
* and forward that event to the stream.
*
* In order for event subscription to work, `AttachStreams` must receive a child
* component that accepts a `domRef` prop. That prop should be set as `ref` on
* whatever DOM node the child tree eventually renders.
*/
export declare class AttachStreams extends React.Component<AttachStreamsProps, AttachStreamsState> {
state: AttachStreamsState;
private _subscriptions;
private _domNode;
/**
* `domRef` is passed as a prop to this component's `children`. `children`
* must ensure that `domRef` will eventually be called with a reference to the
* actual DOM node that this tree represents.
*
* For instance, the children component could look like this:
*
* const SomeComponent = ({ domRef }) => <div ref = { domRef } />;
*/
private _domRef;
/**
* A React lifecycle method that will be called before this component is added
* to the DOM.
*/
componentWillMount(): void;
/**
* A React lifecycle method that will be called before this component will
* receive new props.
*
* Here, we compare the new props to the old props: subscribing to any new
* props and unsubscribing from any props that are no longer present.
*/
componentWillReceiveProps(nextProps: AttachStreamsProps): void;
/**
* `AttachStreams` decides whether to forward events from the stream to the
* child or from the child to the stream based on the prop name. If the prop
* name starts with "on", `AttachStreams` listen for events matching the rest
* of the prop name (e.g. `click` for `onClick`) and forward them to the
* stream.
*
* Otherwise, `AttachStreams` will subscribe to the stream and pass any
* emissions as props to its `children` component.
*/
private _subscribeToProps(props);
/**
* React has a synthetic event system, which exposes events that it knows
* about, e.g. `click`, as `onClick`. Unfortunately, React doesn't know about
* some event types that we might care about, e.g. `pointermove`. Therefore,
* we subscribe to that actual DOM's event system for any prop that looks like
* an event listener (e.g. starts with `on`).
*/
private _subscribeToEvent$(propName, subject);
render(): React.ReactElement<any>;
/**
* A React lifecycle method that will be called just before this component is
* removed from the DOM. Here, we unsubscribe from all current subscriptions.
*/
componentWillUnmount(): void;
}
export default AttachStreams;