Skip to content

Commit c414797

Browse files
Refactor.
1 parent 61e9391 commit c414797

24 files changed

+8627
-391
lines changed

.babelrc

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
{
22
"presets": [
3-
"metalab"
3+
["@babel/preset-env", {
4+
"shippedProposals": true
5+
}],
6+
"@babel/preset-react",
7+
"@babel/preset-flow"
8+
],
9+
"plugins": [
10+
"@babel/plugin-proposal-class-properties"
411
]
512
}

.eslintignore

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
*.js
2-
!src/**/*.js
1+
/flow-typed
2+
/*.js
3+
match/**/*
4+
internal/**/*
5+
test/*.js
6+
lib/**/*
7+
node_modules
8+
/coverage

.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
extends:
22
- metalab
3-
- metalab/react
3+
env:
4+
node: true

.flowconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[ignore]
2+
.*\.flow
3+
[include]
4+
5+
[libs]
6+
7+
[lints]
8+
all=error
9+
10+
[options]
11+
emoji=true
12+
include_warnings=true
13+
suppress_comment=\\(.\\|\n\\)*\\$ExpectError

.gitignore

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
node_modules
22
*.log
3-
*.js
4-
*.js.map
5-
!src/**/*.js
6-
coverage
3+
/*.js
4+
/*.js.map
5+
/coverage

.prettierrc

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parser: flow
2+
semi: true
3+
singleQuote: true
4+
trailingComma: all
5+
bracketSpacing: false
6+
arrowParens: always

README.md

+178-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,180 @@
1-
# Relocation
1+
# relocation
22

3-
[![npm version](https://img.shields.io/npm/v/relocation.svg)](https://www.npmjs.com/package/relocation)
3+
Complex portal behaviour for `react^16.3`.
44

5-
Relocation works with [React Redux](https://github.com/rackt/react-redux) to manage modals, lightboxes, alerts, and overlays.
5+
![build status](http://img.shields.io/travis/metalabdesign/relocation/master.svg?style=flat)
6+
![coverage](https://img.shields.io/codecov/c/github/metalabdesign/relocation/master.svg?style=flat)
7+
![license](http://img.shields.io/npm/l/relocation.svg?style=flat)
8+
![version](http://img.shields.io/npm/v/relocation.svg?style=flat)
9+
![downloads](http://img.shields.io/npm/dm/relocation.svg?style=flat)
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+
```

flow-typed/npm/cuid_vx.x.x.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// flow-typed signature: 52ccdf49283c60a4552ab3bff8fcd227
2+
// flow-typed version: <<STUB>>/cuid_v1.3.8/flow_v0.70.0
3+
4+
/**
5+
* This is an autogenerated libdef stub for:
6+
*
7+
* 'cuid'
8+
*
9+
* Fill this stub out by replacing all the `any` types.
10+
*
11+
* Once filled out, we encourage you to share your work with the
12+
* community by sending a pull request to:
13+
* https://github.com/flowtype/flow-typed
14+
*/
15+
16+
declare module 'cuid' {
17+
declare module.exports: () => string;
18+
}

0 commit comments

Comments
 (0)