|
| 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 | +``` |
0 commit comments