|
| 1 | +# 📚 React Layers Manager |
| 2 | + |
| 3 | +Manage layers and `z-index` in React applications effectively. |
| 4 | + |
| 5 | +No more |
| 6 | + |
| 7 | +```css |
| 8 | +z-index: 10000; |
| 9 | +z-index: 10001; |
| 10 | +z-index: 999; |
| 11 | +z-index: 99999; |
| 12 | +``` |
| 13 | + |
| 14 | +That's right 👦 |
| 15 | + |
| 16 | +## 👩🏫 The issue with z-index: 9999 |
| 17 | + |
| 18 | +Some [CSS properties create stacking contextes](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/The_stacking_context). |
| 19 | + |
| 20 | +Within a stacking context, child elements are stacked according to [some rules](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Positioning/Understanding_z_index/Adding_z-index) and in general one on top of each other based on their z-index value. |
| 21 | + |
| 22 | +The problem though is that elements cannot escape their parent stackig context. |
| 23 | + |
| 24 | +What this means in practice is that an element with `z-index: 9999` inside of a stacking context with `z-index: 1` will always be below a stacking context sibling of the latter with `z-index: 2` for example: |
| 25 | + |
| 26 | +```html |
| 27 | +<div style="position: absolute; z-index: 2; top: 0;"> |
| 28 | + I am on top and I don't care about your 9999 |
| 29 | +</div> |
| 30 | + |
| 31 | +<div style="position: absolute; z-index: 1; top: 0;"> |
| 32 | + <div style="position: absolute; z-index: 9999;"> |
| 33 | + I want to be in front of you |
| 34 | + </div> |
| 35 | +</div> |
| 36 | +``` |
| 37 | + |
| 38 | +`react-layers-manager` solves this issue. |
| 39 | + |
| 40 | +## 💪 How it works |
| 41 | + |
| 42 | +`react-layers-manager` leverages the power of the new React Context API introduced in React 16.3 and Portals to render your layers as siblings of your application root. |
| 43 | + |
| 44 | +This way layers are guaranteed to always be on top of your application! |
| 45 | + |
| 46 | +## Installation |
| 47 | + |
| 48 | +``` |
| 49 | +npm i react-layers-manager |
| 50 | +``` |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +`react-layers-manager` exposes two components: |
| 55 | + |
| 56 | +* `LayersManager` that is just a wrapper for your app |
| 57 | +* `Layer` to be used contextually in your components when you want to render something in a layer |
| 58 | + |
| 59 | +```jsx |
| 60 | +import React from 'react' |
| 61 | +import ReactDOM from 'react-dom' |
| 62 | +import { LayersManager, Layer } from 'react-layers-manager' |
| 63 | + |
| 64 | +const SampleModal = () => ( |
| 65 | + <Layer> |
| 66 | + <Modal>We have updated our privacy policy :trollface:</Modal> |
| 67 | + </Layer> |
| 68 | +) |
| 69 | + |
| 70 | +const App = () => ( |
| 71 | + <h1>Hello folks</h1> |
| 72 | + <SampleModal /> |
| 73 | + <SampleModal /> |
| 74 | +) |
| 75 | + |
| 76 | +ReactDOM.render( |
| 77 | + <LayersManager> |
| 78 | + <App /> |
| 79 | + </LayersManager>, |
| 80 | + document.getElementById('root') |
| 81 | +) |
| 82 | +``` |
| 83 | + |
| 84 | +### Layer |
| 85 | + |
| 86 | +The `Layer` component accepts a few optional `props`: |
| 87 | + |
| 88 | +```js |
| 89 | +type Props = { |
| 90 | + // Renders a layer at a specific index. |
| 91 | + // By default layers are appended to the layers manager container. |
| 92 | + index?: number, |
| 93 | + |
| 94 | + // Invoked when the layer mounts with the app element. |
| 95 | + // This is useful to set aria-hidden="true" for example when showing a modal |
| 96 | + // or disable scrolling. |
| 97 | + onMount?: (root: HTMLElement) => void, |
| 98 | + |
| 99 | + // Invoked when the layer unmounts with the app element. |
| 100 | + onUnmount?: (root: HTMLElement) => void, |
| 101 | +} |
| 102 | +``` |
0 commit comments