diff --git a/README.md b/README.md index f4a0481..1670c34 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ -[Live demo](https://fckt.github.io/react-layer-stack/) +[Live demo](https://chain-police.github.io/react-layer-stack/) [Chat](https://gitter.im/react-layer-stack/Lobby) ### Rationale + `react`/`react-dom` comes with 2 basic assumptions/ideas: + - every UI is hierarchical naturally. This why we have the idea of "`components` wrap each other" - `react-dom` mounts (physically) child component to its parent DOM node by default @@ -12,36 +14,57 @@ The problem is that sometimes the second property isn't what you want in your sp Canonical example is a Tooltip-like component: at some point, during development process, you could find that you need to add some description for your UI element: it'll be rendered in some fixed layer and it should know its coordinates (which are corresponding UI element coord or mouse coords) and at the same time it needs information whether it should be shown right now or not, its content and some context from parent components. ```javascript -import React, { Component } from 'react'; -import { Layer, LayerToggle } from 'react-layer-stack'; -import FixedLayer from './demo/components/FixedLayer'; +import React, { Component } from "react"; +import { Layer, LayerToggle } from "react-layer-stack"; +import FixedLayer from "./demo/components/FixedLayer"; class Demo extends Component { - render() { - return ( -
- { (_, content) => // Layer should have an unique ID - - { content } - - } - - {({ show, hide }) => ( // Layer is accessible from any part of the tree. - // There could be several Toggles for one Layer. - )} - -
- ) - } + render() { + return ( +
+ + {( + _, + content, // Layer should have an unique ID + ) => ( + + {content} + + )} + + + + {( + { show, hide }, // Layer is accessible from any part of the tree. + ) => ( + // There could be several Toggles for one Layer. + + )} + +
+ ); + } } ``` @@ -49,12 +72,15 @@ Another option could be use one of dozens complete implementations with differen https://js.coach/?search=popover ### More examples -https://github.com/fckt/react-layer-stack/blob/master/demo/src/Demo.js + +https://github.com/chain-police/react-layer-stack/blob/master/demo/src/Demo.js ### Live demo -https://fckt.github.io/react-layer-stack/ + +https://chain-police.github.io/react-layer-stack/ ### Installation + ``` npm install --save react-layer-stack ``` @@ -75,7 +101,7 @@ npm install --save react-layer-stack `defaultShow: Boolean` (optional) -`children: callback({ isActive, show: callback(args), showOnlyMe, hide, hideAll }, ...args): ReactElement` - will be rendered into +`children: callback({ isActive, show: callback(args), showOnlyMe, hide, hideAll }, ...args): ReactElement` - will be rendered into #### `` @@ -93,67 +119,88 @@ This is a mount point for `Layer`s. `children: callback({ views, displaying, show: callback(id, args), hide, hideAll, mountPointId, mountPointArgs }): ReactElement` - you can choose different strategies how to render `Layers` in `LayerStackMountPoint` instead of the default one - ### Real-world usage example Public API consist 2 key components: `Layer`, `LayerStackMountPoint` and 1 additional: `LayerToggle` (sometimes toggle needs to know which popover is open now). Set the `LayerStackMountPoint` somewhere on the top of the tree: ```javascript -import { LayerStackProvider, LayerStackMountPoint } from 'react-layer-stack' +import { LayerStackProvider, LayerStackMountPoint } from "react-layer-stack"; // ... // render() { - return ( - - - - - - {children} - - - - ) +return ( + + + + + {children} + + +); // } ``` Define your `Layer`. This example shows how to propagate variables from lexical context (https://developer.mozilla.org/en/docs/Web/JavaScript/Closures) to the `Layer`, which will be displayed in the `LayerStackMountPoint`. Each layer should have an `id` and `use` properties. `use` property is needed to determine if we should update the lexical context of the anonymous function which renders `Modal` into `Layer` if `Cell` is updated. ```javascript -import { Layer, LayerToggle } from 'react-layer-stack' +import { Layer, LayerToggle } from "react-layer-stack"; // ... for each `object` in array of `objects` -const modalId = 'DeleteObjectConfirmation' + objects[rowIndex].id +const modalId = "DeleteObjectConfirmation" + objects[rowIndex].id; return ( - - // the layer definition. The content will show up in the LayerStackMountPoint when `show(modalId)` be fired in LayerToggle - {({ - hide, // alias for `hide(modalId)` - index } // useful to know to set zIndex, for example - , e) => // access to the arguments (click event data in this example) - - DELETE } - onConfirm={ this.handleDeleteObject.bind(this, objects[rowIndex].name, hide) } // hide after confirmation - close={ hide } /> - } - - - // this is the toggle for Layer with `id === modalId` can be defined everywhere in the components tree - {({show}) => // show is alias for `show(modalId)` -
show(e) }> // additional arguments can be passed (like event) - -
} -
-
) + + // the layer definition. The content will show up in the + LayerStackMountPoint when `show(modalId)` be fired in LayerToggle + + {" "} + {( + { + hide, // alias for `hide(modalId)` + index, + }, // useful to know to set zIndex, for example + e, // access to the arguments (click event data in this example) + ) => ( + + DELETE} + onConfirm={this.handleDeleteObject.bind( + this, + objects[rowIndex].name, + hide, + )} // hide after confirmation + close={hide} + /> + + )} + + // this is the toggle for Layer with `id === modalId` can be defined + everywhere in the components tree + + {" "} + {( + { show }, // show is alias for `show(modalId)` + ) => ( +
show(e)}> + {" "} + // additional arguments can be passed (like event) + +
+ )} +
+
+); // ... ``` ### ReactDOM.unstable_createPortal -Facebook team is working on the similar [feature](https://github.com/facebook/react/blob/d28ac9eea0cad6be949cc9d3f973cf548e89bf97/src/renderers/dom/fiber/__tests__/ReactDOMFiber-test.js#L254) called "portals" (by analogy with https://github.com/tajo/react-portal). That approach uses `ReactDOM` (API) which is fatal if browser is not the only target. There are [other considerations](https://github.com/facebook/react/pull/8386#issuecomment-265628702) also. + +Facebook team is working on the similar [feature](https://github.com/facebook/react/blob/d28ac9eea0cad6be949cc9d3f973cf548e89bf97/src/renderers/dom/fiber/__tests__/ReactDOMFiber-test.js#L254) called "portals" (by analogy with https://github.com/tajo/react-portal). That approach uses `ReactDOM` (API) which is fatal if browser is not the only target. There are [other considerations](https://github.com/facebook/react/pull/8386#issuecomment-265628702) also. ### Alternatives + The is a lot of alternative ways to archive the desirable **bottom-to-up** link b/w components. The most obvious (and naiive as well) way is to use redux (or another flux/data lib) as a transport to send data from one DOM branch to another. It's good and robust solution, but the problem is that it just feels like overkill. It seems not universal also, could consume some additional time to implement and grasp afterwards, not because of complications, but because you have to reinvent the same pattern again and again (slightly different in each case, see https://stackoverflow.com/questions/35623656/how-can-i-display-a-modal-dialog-in-redux-that-performs-asynchronous-actions). @@ -163,16 +210,21 @@ Another solution is to use on of ready-to-use components. But sometimes are you And the last option is to find library like https://github.com/tajo/react-portal or https://react-bootstrap.github.io/react-overlays/, designed to address the needs of **bottom-to-up** communication. These libs are often quite opinionated to their cases and doesn't solve the problem in its roots. The goal of **react-layer-stack** is to give an answer how to organize **bottom-to-up** communication in the most natural, reasonable and flexible way. ### The future -Obviously there is a lot of applications for the Layer API (https://github.com/fckt/react-layer-stack/blob/master/README.md#layer-). So, you can declare the entire React app as a Layer and manage it from the outer app! + +Obviously there is a lot of applications for the Layer API (https://github.com/chain-police/react-layer-stack/blob/master/README.md#layer-). So, you can declare the entire React app as a Layer and manage it from the outer app! ### Images to understand the whole thing + #### View layers stack + ![Symlink](http://cfs6.tistory.com/upload_control/download.blog?fhandle=YmxvZzE1NzczMkBmczYudGlzdG9yeS5jb206L2F0dGFjaC8wLzEzMDAwMDAwMDAyMi5qcGc%3D) #### Layer id and "use" property (sym/soft link) + ![Symlink](http://1.bp.blogspot.com/-gZMz1nF3GC0/UiyehOS_bWI/AAAAAAAABQI/BpYyEtadcEg/s640/profiles1.png) ### Related Stackoverflow q&a + - http://stackoverflow.com/a/40461655/524034 - http://stackoverflow.com/questions/40443160/bootstrap-modal-like-behavior-in-react - http://stackoverflow.com/questions/40444788/rendering-a-modal-in-react diff --git a/package.json b/package.json index 3c44bd0..8753c45 100644 --- a/package.json +++ b/package.json @@ -1,47 +1,47 @@ { - "name": "react-layer-stack", - "version": "4.2.12", - "description": "Simple but ubiquitously powerful and agnostic layering system for React. Useful for any kind of windowing/popover/modals/tooltip application", - "repository": { - "type": "git", - "url": "https://github.com/fckt/react-layer-stack" - }, - "main": "lib/index.js", - "scripts": { - "build": "babel src --out-dir lib --source-maps" - }, - "author": "Alexey Frolov ", - "license": "MIT", - "peerDependencies": { - "react": ">=15.5.0", - "prop-types": "15.6.0" - }, - "devDependencies": { - "babel-cli": "^6.16.0", - "babel-plugin-syntax-flow": "^6.18.0", - "babel-plugin-transform-flow-strip-types": "^6.22.0", - "babel-plugin-transform-object-rest-spread": "^6.16.0", - "babel-preset-es2015": "^6.16.0", - "babel-preset-react": "^6.16.0" - }, - "keywords": [ - "react", - "react-component", - "layer", - "layers", - "tooltip", - "popover", - "window", - "windowing", - "dropdown", - "overlay", - "popup", - "flyout", - "zindex", - "modal", - "lightbox", - "portal", - "transportation", - "bottom-up" - ] + "name": "react-layer-stack", + "version": "4.2.12", + "description": "Simple but ubiquitously powerful and agnostic layering system for React. Useful for any kind of windowing/popover/modals/tooltip application", + "repository": { + "type": "git", + "url": "https://github.com/chain-police/react-layer-stack" + }, + "main": "lib/index.js", + "scripts": { + "build": "babel src --out-dir lib --source-maps" + }, + "author": "Alexey Frolov ", + "license": "MIT", + "peerDependencies": { + "react": ">=15.5.0", + "prop-types": "15.6.0" + }, + "devDependencies": { + "babel-cli": "^6.16.0", + "babel-plugin-syntax-flow": "^6.18.0", + "babel-plugin-transform-flow-strip-types": "^6.22.0", + "babel-plugin-transform-object-rest-spread": "^6.16.0", + "babel-preset-es2015": "^6.16.0", + "babel-preset-react": "^6.16.0" + }, + "keywords": [ + "react", + "react-component", + "layer", + "layers", + "tooltip", + "popover", + "window", + "windowing", + "dropdown", + "overlay", + "popup", + "flyout", + "zindex", + "modal", + "lightbox", + "portal", + "transportation", + "bottom-up" + ] } diff --git a/src/index.js b/src/index.js index 86d9891..252f622 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,6 @@ -import LayerStackProvider from './components/LayerStackProvider' -import LayerStackMountPoint from './components/LayerStackMountPoint' -import Layer from './components/Layer' -import LayerToggle from './components/LayerToggle' +import LayerStackProvider from "./components/LayerStackProvider"; +import LayerStackMountPoint from "./components/LayerStackMountPoint"; +import Layer from "./components/Layer"; +import LayerToggle from "./components/LayerToggle"; -export { Layer, LayerToggle, LayerStackMountPoint, LayerStackProvider } \ No newline at end of file +export { Layer, LayerToggle, LayerStackMountPoint, LayerStackProvider };