|
1 |
| -# Relocation |
| 1 | +# relocation |
2 | 2 |
|
3 |
| -[](https://www.npmjs.com/package/relocation) |
| 3 | +Complex portal behaviour for `react^16.3`. |
4 | 4 |
|
5 |
| -Relocation works with [React Redux](https://github.com/rackt/react-redux) to manage modals, lightboxes, alerts, and overlays. |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +## Examples |
| 12 | + |
| 13 | +### Modal Dialogs |
| 14 | + |
| 15 | +```jsx |
| 16 | +import createRelocation from 'relocation'; |
| 17 | + |
| 18 | +const ModalRelocation = createRelocation(); |
| 19 | + |
| 20 | +const ModalRoot = () => ( |
| 21 | + <div style={{position: 'fixed'}}> |
| 22 | + <ModalRelocation.Consumer> |
| 23 | + {(modals) => modals[0] && modals[0].child} |
| 24 | + </ModalRelocation.Consumer> |
| 25 | + </div> |
| 26 | +); |
| 27 | + |
| 28 | +const Modal = ({children}) => ( |
| 29 | + <ModalRelocation.Component> |
| 30 | + <div style={{width: 500, background: '#fff'}}> |
| 31 | + Modal! |
| 32 | + {children} |
| 33 | + </div> |
| 34 | + </ModalRelocation.Component> |
| 35 | +); |
| 36 | + |
| 37 | +const App = ( |
| 38 | + <ModalRelocation.Provider> |
| 39 | + <div> |
| 40 | + <ModalRoot/> |
| 41 | + <Modal>Modal A</Modal> |
| 42 | + <Modal>Modal B</Modal> |
| 43 | + </div> |
| 44 | + </ModalRelocation.Provider> |
| 45 | +); |
| 46 | +``` |
| 47 | + |
| 48 | +### Tooltips |
| 49 | + |
| 50 | + |
| 51 | +```jsx |
| 52 | +import createRelocation from 'relocation'; |
| 53 | + |
| 54 | +const TooltipRelocation = createRelocation(); |
| 55 | + |
| 56 | +const TooltipDisplay = ({id}) => ( |
| 57 | + <TooltipRelocation.Consumer> |
| 58 | + {(tooltips) => { |
| 59 | + const result = tooltips.find(({meta}) => meta.id === id); |
| 60 | + if (result) { |
| 61 | + return result.child; |
| 62 | + } |
| 63 | + return null; |
| 64 | + }} |
| 65 | + </TooltipRelocation.Consumer> |
| 66 | +); |
| 67 | + |
| 68 | +class TooltipTarget { |
| 69 | + id = cuid(); |
| 70 | + state = {element: null} |
| 71 | + open = (element = this.props.tooltip) => { |
| 72 | + this.setState({element}); |
| 73 | + } |
| 74 | + close = () => { |
| 75 | + this.setState({element: null}); |
| 76 | + } |
| 77 | + toggle = (element = this.props.tooltip) => { |
| 78 | + this.setState((state) => state.element ? null : element) |
| 79 | + } |
| 80 | + render() { |
| 81 | + const {element} = this.state; |
| 82 | + return ( |
| 83 | + <React.Fragment> |
| 84 | + <TooltipDisplay id={this.id}/> |
| 85 | + {children({open, close, toggle})} |
| 86 | + {element && ( |
| 87 | + <TooltipRelocation.Component meta={{id}}> |
| 88 | + {element} |
| 89 | + </TooltipRelocation.Component> |
| 90 | + )} |
| 91 | + </React.Fragment> |
| 92 | + ) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | + |
| 97 | +const App = ( |
| 98 | + <TooltipRelocation.Provider> |
| 99 | + <div> |
| 100 | + <TooltipTarget tooltip={<div>My tooltip</div>}> |
| 101 | + {(toggle) => ( |
| 102 | + <button onClick={() => toggle()}>Click me!</button> |
| 103 | + )} |
| 104 | + </TooltipTarget> |
| 105 | + </div> |
| 106 | + </TooltipRelocation.Provider> |
| 107 | +); |
| 108 | +``` |
| 109 | + |
| 110 | +### Managing `head` Element |
| 111 | + |
| 112 | +```jsx |
| 113 | +import createRelocation from 'relocation'; |
| 114 | + |
| 115 | +const HeadRelocation = createRelocation(); |
| 116 | + |
| 117 | +const head = |
| 118 | + typeof document !== 'undefined' |
| 119 | + ? document.getElementsByTagName('head')[0] |
| 120 | + : null; |
| 121 | + |
| 122 | +class HeadPortal extends React.Component<*> { |
| 123 | + componentDidMount() { |
| 124 | + if (head) { |
| 125 | + // Strip out server-side components |
| 126 | + head.querySelectorAll('[data-rh=true]').forEach((e) => { |
| 127 | + e.remove(); |
| 128 | + }); |
| 129 | + } |
| 130 | + } |
| 131 | + render() { |
| 132 | + return head ? ( |
| 133 | + <HeadRelocation.Consumer> |
| 134 | + {(children) => ReactDOM.createPortal(mapChildren(children), head)} |
| 135 | + </HeadRelocation.Consumer> |
| 136 | + ) : null; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +const Head = ({render}) => ( |
| 141 | + <HeadRelocation.Component> |
| 142 | + {render({'data-rh': 'true'})} |
| 143 | + </HeadRelocation.Component> |
| 144 | +); |
| 145 | + |
| 146 | +const Title = ({children}) => ( |
| 147 | + <Head> |
| 148 | + {(props)} => ( |
| 149 | + <title {...props}>{children}</title> |
| 150 | + )} |
| 151 | + </Head> |
| 152 | +); |
| 153 | + |
| 154 | +const App = ({head}) => ( |
| 155 | + <HeadRelocation.Provider collector={head}> |
| 156 | + <HeadPortal/> |
| 157 | + <Title>My Page</Title> |
| 158 | + </HeadRelocation.Provider> |
| 159 | +); |
| 160 | +``` |
| 161 | + |
| 162 | +And on the server: |
| 163 | + |
| 164 | +```jsx |
| 165 | +const render = () => { |
| 166 | + const headCollector = new HeadRelocation.Collector(); |
| 167 | + |
| 168 | + const markup = React.renderToString(<App head={headCollector}/>); |
| 169 | + return React.renderToStaticMarkup(( |
| 170 | + <html> |
| 171 | + <head> |
| 172 | + {head.getElements()} |
| 173 | + </head> |
| 174 | + <body> |
| 175 | + {markup} |
| 176 | + </body> |
| 177 | + </html> |
| 178 | + )); |
| 179 | +}; |
| 180 | +``` |
0 commit comments