Skip to content

Commit a7e44b3

Browse files
committed
docs: add invert() docs
1 parent c7c127e commit a7e44b3

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const MyComponent = mock();
2525
- [`loadable()`](./docs/loadable.md)
2626
- [`lazy()`](./docs/lazy.md)
2727
- [`delayed()`](./docs/delayed.md)
28+
- [`invert()`](./docs/invert.md)
2829
- Sensors
2930
- [`<SizeSensor>`](./docs/SizeSensor.md)
3031
- [`<WidthSensor>`](./docs/WidthSensor.md)

docs/invert.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# `invert()`
2+
3+
A utility function that inverts DOM element by creating a FaCC out of it. Allows you to easily
4+
create stateless DOM compnents with reference to a DOM element and invertd control flow.
5+
6+
## Usage
7+
8+
```jsx
9+
import {invert} from 'mol-fe-react/lib/invert';
10+
11+
const Div = invert('div');
12+
const Audio = invert('audio');
13+
const Video = invert('video');
14+
// etc...
15+
16+
<Div
17+
onClick={(event, comp) => {
18+
19+
}}
20+
render={(div, comp) => div}
21+
>{(comp) =>
22+
23+
}</Div>
24+
```
25+
26+
27+
## Reference
28+
29+
```tsx
30+
invert: (tag: string) => React.ComponentClass;
31+
```
32+
33+
, where
34+
35+
- `tag` - a DOM element tag name.
36+
37+
Returns an inverted FaCC of that tag, whose props are passed through to the DOM element and
38+
it has these additional props:
39+
40+
- `render` - optional, wrapper renderer that you can use to wrap your element in extra markup,
41+
defaults to `(element) => element`.
42+
- `onMounted(el, comp)` - called when component mounts; `el` - DOM element reference; `comp` - React
43+
component instance.
44+
- `onUnmount(comp)` - called when component unmounts; `comp` - React component instance.
45+
46+
Children of the created component can be a function that recieves a React component as its only argument.
47+
48+
The created react component instance has `.el` property which is a reference to the DOM element.
49+
50+
51+
## Example
52+
53+
```jsx
54+
import {invert} from 'mol-fe-react/lib/invert';
55+
56+
const Audio = invert('audio');
57+
58+
<Audio
59+
src='https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3'
60+
onTimeUpdate={() => {}}
61+
render={(element, comp) =>
62+
<div>
63+
{element}
64+
<button onClick={() => comp.el.play()}>Play</button>
65+
<button onClick={() => comp.el.pause()}>Pause</button>
66+
<button onClick={() => comp.el.currentTime -= 5}>Seek -</button>
67+
<button onClick={() => comp.el.currentTime += 5}>Seek +</button>
68+
<button onClick={() => comp.el.volume -= 0.05}>Volume -</button>
69+
<button onClick={() => comp.el.volume += 0.05}>Volume +</button>
70+
<button onClick={() => comp.el.muted = true}>Mute</button>
71+
<button onClick={() => comp.el.muted = false}>Unmute</button>
72+
<pre style={{fontFamily: 'monospace'}}>
73+
{JSON.stringify({
74+
duration: comp.el && comp.el.duration,
75+
time: comp.el && comp.el.currentTime,
76+
volume: comp.el && comp.el.volume,
77+
muted: comp.el && comp.el.muted
78+
}, null, 4)}
79+
</pre>
80+
</div>
81+
}
82+
/>
83+
```

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './mock';
55
export * from './loadable';
66
export * from './lazy';
77
export * from './delayed';
8+
export * from './invert';
89

910
// Sensors
1011
export * from './SizeSensor';

src/invert.story.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import {linkTo} from '@storybook/addon-links';
55
import {invert} from './invert';
66

77
const Audio = invert('audio');
8+
import ShowDocs from '../.storybook/ShowDocs'
89

9-
storiesOf('invert()', module)
10+
storiesOf('Dummies/invert()', module)
11+
.add('Documentation', () => h(ShowDocs, {name: 'invert'}))
1012
.add('<audio>', () =>
1113
<Audio
1214
src='https://www.soundhelix.com/examples/mp3/SoundHelix-Song-2.mp3'

src/invert.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {h, noop} from './util';
44
export interface IInvertedProps {
55
children?: (...args) => React.ReactElement<any>;
66
onMounted: (el: HTMLElement, comp: React.Component<any>) => void;
7+
onUnmount: (comp: React.Component<any>) => void;
78
render: (element: React.ReactElement<any>, comp: React.Component<any>) => React.ReactElement<any>;
89
}
910

@@ -25,10 +26,12 @@ export const invert: TInvert = (tag: keyof React.ReactHTML) => {
2526

2627
componentDidMount () {
2728
(this.props.onMounted || noop)(this.el, this);
29+
this.forceUpdate();
2830
}
2931

3032
compnentWillUnmount () {
3133
this.el = null;
34+
(this.props.onUnmount || noop)(this);
3235
}
3336

3437
event (name: string, handler: (...args) => void) {

0 commit comments

Comments
 (0)