react-rx 1.0.0-beta.3 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/.idea/codeStyles/Project.xml +0 -6
  2. package/.idea/inspectionProfiles/Project_Default.xml +0 -4
  3. package/.idea/workspace.xml +62 -49
  4. package/dist/cjs/WithObservable.d.ts +3 -3
  5. package/dist/cjs/WithObservable.js +3 -3
  6. package/dist/cjs/index.d.ts +6 -2
  7. package/dist/cjs/index.js +18 -11
  8. package/dist/cjs/reactiveComponent.d.ts +6 -6
  9. package/dist/cjs/reactiveComponent.js +8 -26
  10. package/dist/cjs/useAsObservable.d.ts +9 -0
  11. package/dist/cjs/useAsObservable.js +31 -0
  12. package/dist/cjs/useIsomorphicEffect.d.ts +2 -0
  13. package/dist/cjs/useIsomorphicEffect.js +5 -0
  14. package/dist/cjs/useObservable.d.ts +6 -15
  15. package/dist/cjs/useObservable.js +17 -73
  16. package/dist/cjs/useObservableCallback.d.ts +3 -0
  17. package/dist/cjs/useObservableCallback.js +24 -0
  18. package/dist/cjs/useWithObservable.d.ts +9 -0
  19. package/dist/cjs/useWithObservable.js +11 -0
  20. package/dist/cjs/utils.d.ts +7 -3
  21. package/dist/cjs/utils.js +29 -5
  22. package/dist/cjs/withPropsStream.js +3 -3
  23. package/dist/es2015/WithObservable.d.ts +3 -3
  24. package/dist/es2015/index.d.ts +6 -2
  25. package/dist/es2015/index.js +7 -2
  26. package/dist/es2015/reactiveComponent.d.ts +6 -6
  27. package/dist/es2015/reactiveComponent.js +9 -6
  28. package/dist/es2015/useAsObservable.d.ts +9 -0
  29. package/dist/es2015/useAsObservable.js +27 -0
  30. package/dist/es2015/useIsomorphicEffect.d.ts +2 -0
  31. package/dist/es2015/useIsomorphicEffect.js +2 -0
  32. package/dist/es2015/useObservable.d.ts +6 -15
  33. package/dist/es2015/useObservable.js +15 -46
  34. package/dist/es2015/useObservableCallback.d.ts +3 -0
  35. package/dist/es2015/useObservableCallback.js +19 -0
  36. package/dist/es2015/useWithObservable.d.ts +9 -0
  37. package/dist/es2015/useWithObservable.js +7 -0
  38. package/dist/es2015/utils.d.ts +7 -3
  39. package/dist/es2015/utils.js +24 -3
  40. package/dist/esm/WithObservable.d.ts +3 -3
  41. package/dist/esm/index.d.ts +6 -2
  42. package/dist/esm/index.js +7 -2
  43. package/dist/esm/reactiveComponent.d.ts +6 -6
  44. package/dist/esm/reactiveComponent.js +7 -6
  45. package/dist/esm/useAsObservable.d.ts +9 -0
  46. package/dist/esm/useAsObservable.js +27 -0
  47. package/dist/esm/useIsomorphicEffect.d.ts +2 -0
  48. package/dist/esm/useIsomorphicEffect.js +2 -0
  49. package/dist/esm/useObservable.d.ts +6 -15
  50. package/dist/esm/useObservable.js +15 -48
  51. package/dist/esm/useObservableCallback.d.ts +3 -0
  52. package/dist/esm/useObservableCallback.js +20 -0
  53. package/dist/esm/useWithObservable.d.ts +9 -0
  54. package/dist/esm/useWithObservable.js +7 -0
  55. package/dist/esm/utils.d.ts +7 -3
  56. package/dist/esm/utils.js +24 -3
  57. package/package.json +9 -4
  58. package/src/WithObservable.tsx +4 -3
  59. package/src/index.ts +12 -12
  60. package/src/reactiveComponent.ts +28 -20
  61. package/src/useAsObservable.ts +43 -0
  62. package/src/useIsomorphicEffect.ts +3 -0
  63. package/src/useObservable.ts +36 -77
  64. package/src/useObservableCallback.ts +30 -0
  65. package/src/useWithObservable.ts +23 -0
  66. package/src/utils.ts +39 -3
  67. package/website/.babelrc +21 -0
  68. package/website/.prettierrc +7 -0
  69. package/website/README.mdx +1 -0
  70. package/website/package.json +84 -0
  71. package/website/src/@types/babel.d.ts +4 -0
  72. package/website/src/@types/mdx.d.ts +11 -0
  73. package/website/src/@types/react-css-burger.d.ts +4 -0
  74. package/website/src/App.tsx +68 -0
  75. package/website/src/components/Header.tsx +118 -0
  76. package/website/src/components/Link.tsx +18 -0
  77. package/website/src/components/LoadPage.tsx +60 -0
  78. package/website/src/components/Sidebar.tsx +88 -0
  79. package/website/src/components/logos/Github.tsx +10 -0
  80. package/website/src/components/logos/React.tsx +27 -0
  81. package/website/src/components/logos/ReactRxLogo.tsx +39 -0
  82. package/website/src/components/logos/RxJs.tsx +69 -0
  83. package/website/src/components/repl/Checkerboard.tsx +25 -0
  84. package/website/src/components/repl/CodeBlock.tsx +147 -0
  85. package/website/src/components/repl/CodeMirror.tsx +37 -0
  86. package/website/src/components/repl/CodeMirrorMode.tsx +67 -0
  87. package/website/src/components/repl/EvalCode.tsx +55 -0
  88. package/website/src/components/repl/Prelude.tsx +50 -0
  89. package/website/src/components/repl/ShowError.tsx +27 -0
  90. package/website/src/components/repl/codemirror-lib.ts +8 -0
  91. package/website/src/components/repl/codemirror-theme.css +131 -0
  92. package/website/src/components/repl/compile/babel.ts +21 -0
  93. package/website/src/components/repl/eval/evalCallback.ts +27 -0
  94. package/website/src/components/repl/eval/evalCode.ts +7 -0
  95. package/website/src/components/repl/eval/evalExpression.ts +4 -0
  96. package/website/src/components/repl/eval/evalReactDomRender.ts +23 -0
  97. package/website/src/components/repl/index.ts +1 -0
  98. package/website/src/components/repl/runMode.ts +42 -0
  99. package/website/src/datastores/location.ts +14 -0
  100. package/website/src/datastores/page.ts +11 -0
  101. package/website/src/datastores/pageTransition.ts +97 -0
  102. package/website/src/entries/main.css +2 -0
  103. package/website/src/entries/main.tsx +6 -0
  104. package/website/src/examples/FizzBuzz/FizzBuzzExample.tsx +39 -0
  105. package/website/src/examples/FizzBuzz/index.tsx +9 -0
  106. package/website/src/examples/_utils/globalScope.ts +45 -0
  107. package/website/src/examples/animation/AnimationExample.tsx +152 -0
  108. package/website/src/examples/animation/easings.ts +0 -0
  109. package/website/src/examples/animation/index.tsx +15 -0
  110. package/website/src/examples/context/Context.example.jsx +67 -0
  111. package/website/src/examples/context/index.tsx +9 -0
  112. package/website/src/examples/errors/Errors.example.jsx +69 -0
  113. package/website/src/examples/errors/index.tsx +9 -0
  114. package/website/src/examples/event-handlers/EventHandlersExample.tsx +48 -0
  115. package/website/src/examples/event-handlers/index.tsx +9 -0
  116. package/website/src/examples/fetch/FetchExample.jsx +43 -0
  117. package/website/src/examples/fetch/a.txt +1 -0
  118. package/website/src/examples/fetch/b.txt +1 -0
  119. package/website/src/examples/fetch/index.tsx +9 -0
  120. package/website/src/examples/form-data/FormDataExample.jsx +119 -0
  121. package/website/src/examples/form-data/index.tsx +12 -0
  122. package/website/src/examples/form-data/storage.ts +27 -0
  123. package/website/src/examples/forward-ref/ForwardRefExample.tsx +55 -0
  124. package/website/src/examples/forward-ref/index.tsx +9 -0
  125. package/website/src/examples/hello-world/HelloWorld.example.jsx +6 -0
  126. package/website/src/examples/hello-world/index.tsx +9 -0
  127. package/website/src/examples/hooks-interop/HooksInterop.example.tsx +42 -0
  128. package/website/src/examples/hooks-interop/index.tsx +9 -0
  129. package/website/src/examples/passthrough-props/PassThroughPropsExample.jsx +57 -0
  130. package/website/src/examples/passthrough-props/index.tsx +11 -0
  131. package/website/src/examples/reactive-state/ReactiveState.example.jsx +43 -0
  132. package/website/src/examples/reactive-state/index.ts +9 -0
  133. package/website/src/examples/readme-examples/README.mdx +0 -0
  134. package/website/src/examples/readme-examples/index.ts +11 -0
  135. package/website/src/examples/shared-state/SharedState.example.tsx +59 -0
  136. package/website/src/examples/shared-state/index.tsx +9 -0
  137. package/website/src/examples/simple/Counter.example.tsx +14 -0
  138. package/website/src/examples/simple/index.tsx +9 -0
  139. package/website/src/examples/sync/Sync.example.tsx +9 -0
  140. package/website/src/examples/sync/index.tsx +9 -0
  141. package/website/src/examples/tick/TickExample.jsx +51 -0
  142. package/website/src/examples/tick/index.ts +9 -0
  143. package/website/src/examples/todo-app/TodoApp.example.tsx +88 -0
  144. package/website/src/examples/todo-app/index.tsx +12 -0
  145. package/website/src/examples/todo-app/storage.ts +27 -0
  146. package/website/src/examples/use-element/UseElementExample.tsx +70 -0
  147. package/website/src/examples/use-element/index.tsx +9 -0
  148. package/website/src/examples/use-observable/search/SearchExample.tsx +82 -0
  149. package/website/src/examples/use-observable/search/index.tsx +8 -0
  150. package/website/src/examples/use-observable/use-observable/UseObservableExample.tsx +70 -0
  151. package/website/src/examples/use-observable/use-observable/index.tsx +9 -0
  152. package/website/src/index.html +10 -0
  153. package/website/src/mdx-components.tsx +74 -0
  154. package/website/src/pages/Api/Api.mdx +134 -0
  155. package/website/src/pages/Api/ApiPage.tsx +25 -0
  156. package/website/src/pages/Examples/Examples.tsx +105 -0
  157. package/website/src/pages/Examples/ExamplesPage.tsx +13 -0
  158. package/website/src/pages/Guide/Guide.mdx +275 -0
  159. package/website/src/pages/Guide/GuidePage.tsx +25 -0
  160. package/website/src/pages/Index/IndexPage.tsx +50 -0
  161. package/website/src/pages/Index/ReactRxReadme.md +20 -0
  162. package/website/src/pages/NotFound.tsx +3 -0
  163. package/website/src/pages/pages.ts +46 -0
  164. package/website/src/pages/styles.tsx +24 -0
  165. package/website/src/theme.ts +61 -0
  166. package/website/src/utils/parseCodeFenceHeader.ts +32 -0
  167. package/website/tsconfig.json +30 -0
  168. package/website/vercel.json +4 -0
  169. package/website/webpack/config.js +5 -0
  170. package/website/webpack/createConfig.js +95 -0
  171. package/website/webpack/mdx-toc-loader.js +38 -0
@@ -0,0 +1,88 @@
1
+ import {ChangeEvent, FormEvent} from 'react'
2
+ import {
3
+ combineLatest,
4
+ filter,
5
+ map,
6
+ mapTo,
7
+ merge,
8
+ operators,
9
+ React,
10
+ ReactDOM,
11
+ rxComponent,
12
+ scan,
13
+ startWith,
14
+ tap,
15
+ handler
16
+ } from '../_utils/globalScope'
17
+ import styled from 'styled-components'
18
+ //@endimport
19
+
20
+ const {withLatestFrom} = operators
21
+
22
+ interface TodoItem {
23
+ id: number
24
+ text: string
25
+ }
26
+
27
+ const TodoApp = rxComponent(() => {
28
+ const [onInput$, handleInput] = handler<ChangeEvent<HTMLInputElement>>()
29
+ const [onSubmit$, handleSubmit] = handler<FormEvent<HTMLFormElement>>()
30
+
31
+ const text$ = onInput$.pipe(
32
+ map(e => e.currentTarget.value),
33
+ startWith('')
34
+ )
35
+
36
+ const items$ = onSubmit$.pipe(
37
+ tap(e => e.preventDefault()),
38
+ withLatestFrom(text$),
39
+ map(([_, text]) => text),
40
+ filter(text => text.length > 0),
41
+ map(text => ({text, id: Date.now()})),
42
+ scan((items: TodoItem[], item) => items.concat(item), []),
43
+ startWith([])
44
+ )
45
+
46
+ const inputValue$ = merge(text$, onSubmit$.pipe(mapTo('')))
47
+
48
+ return combineLatest([inputValue$, items$]).pipe(
49
+ map(([text, items]) => (
50
+ <Wrapper>
51
+ <h3>TODO</h3>
52
+ <TodoList items={items} />
53
+ <form onSubmit={handleSubmit}>
54
+ <label htmlFor="new-todo">What needs to be done?</label>
55
+ <input id="new-todo" onChange={handleInput} value={text} />
56
+ <button>Add #{items.length + 1}</button>
57
+ </form>
58
+ </Wrapper>
59
+ ))
60
+ )
61
+ })
62
+
63
+ interface ListProps {
64
+ items: TodoItem[]
65
+ }
66
+
67
+ function TodoList(props: ListProps) {
68
+ return (
69
+ <ul>
70
+ {props.items.map(item => (
71
+ <li key={item.id}>{item.text}</li>
72
+ ))}
73
+ </ul>
74
+ )
75
+ }
76
+
77
+ ReactDOM.render(<TodoApp />, document.getElementById('todo-app-example'))
78
+
79
+ const Wrapper = styled.div`
80
+ label {
81
+ display: block;
82
+ margin-top: 10px;
83
+ }
84
+ input {
85
+ width: 100%;
86
+ padding: 5px;
87
+ }
88
+ `
@@ -0,0 +1,12 @@
1
+ import {Example} from '../../pages/Examples/Examples'
2
+ import storage from './storage'
3
+ import styled from 'styled-components'
4
+
5
+ const fs = require('fs')
6
+
7
+ export const TodoAppExample: Example = {
8
+ id: 'todo-app',
9
+ title: 'An Application',
10
+ scope: {storage, styled},
11
+ source: fs.readFileSync(`${__dirname}/TodoApp.example.tsx`, 'utf-8')
12
+ }
@@ -0,0 +1,27 @@
1
+ import {Observable, of} from 'rxjs'
2
+
3
+ const tryParse = <T>(val: any, defaultValue: T): T => {
4
+ try {
5
+ return JSON.parse(val)
6
+ } catch (err) {
7
+ return defaultValue
8
+ }
9
+ }
10
+
11
+ const get = <T>(key: string, defValue: T): Observable<T> => {
12
+ const val = localStorage.getItem(key)
13
+ return of(val === null ? defValue : tryParse<T>(val, defValue))
14
+ }
15
+
16
+ const set = <T>(key: string, nextValue: T): Observable<T> => {
17
+ // Can't stringify undefined, and nulls are what
18
+ // `getItem` returns when key does not exist
19
+ if (typeof nextValue === 'undefined' || nextValue === null) {
20
+ localStorage.removeItem(key)
21
+ } else {
22
+ localStorage.setItem(key, JSON.stringify(nextValue))
23
+ }
24
+ return of(nextValue)
25
+ }
26
+
27
+ export default {get, set}
@@ -0,0 +1,70 @@
1
+ import {
2
+ map,
3
+ elementRef,
4
+ operators,
5
+ React,
6
+ ReactDOM,
7
+ rxComponent,
8
+ timer
9
+ } from '../_utils/globalScope'
10
+
11
+ //@endimport
12
+
13
+ const {withLatestFrom} = operators
14
+
15
+ const STYLE: React.CSSProperties = {
16
+ textAlign: 'center',
17
+ border: '1px dashed'
18
+ }
19
+
20
+ const SPEED = 2
21
+ const unpx = (v: string) => Number(v.replace(/px$/, ''))
22
+
23
+ const UseElementExample = rxComponent(() => {
24
+ const [element$, ref] = elementRef<HTMLElement>()
25
+
26
+ const count$ = timer(0, 16).pipe(
27
+ map((n) => n % 400),
28
+ map((n) => (n > 200 ? -1 : 1))
29
+ )
30
+
31
+ return count$.pipe(
32
+ withLatestFrom(element$),
33
+ map(([direction, element]) => {
34
+ const Tag = direction === 1 ? 'section' : 'article'
35
+ return (
36
+ <div style={{height: '20em', display: 'flex', alignItems: 'center'}}>
37
+ <Tag
38
+ // this connects the element$ observable to the actual element
39
+ ref={ref}
40
+ style={{
41
+ ...STYLE,
42
+ backgroundColor: Tag == 'article' ? '#335186' : '#90441a',
43
+ borderRadius:
44
+ unpx(element?.style.borderRadius || '20px') +
45
+ direction * SPEED * -0.1,
46
+ height: (element?.clientHeight || 0) + direction * SPEED,
47
+ width: (element?.clientWidth || 0) + direction * SPEED
48
+ }}
49
+ >
50
+ <div
51
+ style={{
52
+ ...(element && {fontSize: element.clientHeight / 5}),
53
+ width: '100%',
54
+ height: '100%',
55
+ lineHeight: '500%'
56
+ }}
57
+ >
58
+ {`<${Tag}>`}
59
+ </div>
60
+ </Tag>
61
+ </div>
62
+ )
63
+ })
64
+ )
65
+ })
66
+
67
+ ReactDOM.render(
68
+ <UseElementExample />,
69
+ document.getElementById('use-element-example')
70
+ )
@@ -0,0 +1,9 @@
1
+ import {Example} from '../../pages/Examples/Examples'
2
+
3
+ const fs = require('fs')
4
+
5
+ export const UseElementExample: Example = {
6
+ id: 'use-element',
7
+ title: 'Use element',
8
+ source: fs.readFileSync(`${__dirname}/UseElementExample.tsx`, 'utf-8')
9
+ }
@@ -0,0 +1,82 @@
1
+ import {
2
+ map,
3
+ React,
4
+ ReactDOM,
5
+ timer,
6
+ useMemoObservable,
7
+ of,
8
+ operators,
9
+ filter,
10
+ switchMap
11
+ } from '../../_utils/globalScope'
12
+ import {Observable} from 'rxjs'
13
+ //@endimport
14
+
15
+ const {distinctUntilChanged, debounceTime} = operators
16
+
17
+ interface SearchResult {
18
+ keyword: string
19
+ hits: Hit[]
20
+ }
21
+
22
+ interface Hit {
23
+ title: string
24
+ }
25
+
26
+ const range = (len: number) => {
27
+ const res = []
28
+ for (let i = 0; i <= len; i++) {
29
+ res.push(null)
30
+ }
31
+ return res
32
+ }
33
+
34
+ // A search function that takes longer time to complete for shorter keywords
35
+ // e.g. a keyword of one character takes 9 seconds while a keyword of 9 characters takes 1 second
36
+ const search = (keyword: string): Observable<SearchResult> => {
37
+ const delay = Math.max(1, Math.round(10 - keyword.length))
38
+ return timer(delay * 200).pipe(
39
+ map(() => range(delay).map((_, i) => ({title: `Hit #${i}`}))),
40
+ map((hits) => ({keyword, hits}))
41
+ )
42
+ }
43
+
44
+ function SearchExample() {
45
+ const [keyword, setKeyword] = React.useState('')
46
+ return (
47
+ <>
48
+ <input
49
+ type="search"
50
+ style={{width: '100%'}}
51
+ value={keyword}
52
+ placeholder="Type a keyword to search"
53
+ onChange={(event) => setKeyword(event.target.value)}
54
+ />
55
+ <div>
56
+ The more characters you type, the faster the results will appear
57
+ </div>
58
+ {useMemoObservable(() =>
59
+ of(keyword).pipe(
60
+ debounceTime(200),
61
+ distinctUntilChanged(),
62
+ filter((v) => v !== ''),
63
+ switchMap((kw: string) => search(kw)),
64
+ map((result: SearchResult) => (
65
+ <>
66
+ <h1>Searched for {result.keyword}</h1>
67
+ <div>Got {result.hits.length} hits</div>
68
+ <ul>
69
+ {result.hits.map((hit, i) => (
70
+ <li key={i}>{hit.title}</li>
71
+ ))}
72
+ </ul>
73
+ </>
74
+ ))
75
+ ),
76
+ [keyword]
77
+ )}
78
+ </>
79
+ )
80
+ }
81
+
82
+ ReactDOM.render(<SearchExample />, document.getElementById('search-example'))
@@ -0,0 +1,8 @@
1
+ import {Example} from '../../pages/Examples/Examples'
2
+ const fs = require('fs')
3
+
4
+ export const SearchExample: Example = {
5
+ id: 'search',
6
+ title: 'Search',
7
+ source: fs.readFileSync(`${__dirname}/SearchExample.tsx`, 'utf-8')
8
+ }
@@ -0,0 +1,70 @@
1
+ import * as React from 'react'
2
+ import {Observable, of, timer} from 'rxjs'
3
+ import {ReactDOM, useMemoObservable} from '../../_utils/globalScope'
4
+ import {observableCallback} from 'observable-callback'
5
+ import {map, startWith, switchMap} from 'rxjs/operators'
6
+ //@endimport
7
+
8
+ const justNumbers$ = timer(0, 500)
9
+
10
+ const numberReactElement$ = justNumbers$.pipe(
11
+ map((num) => <div>The number is {num}!</div>)
12
+ )
13
+
14
+ const [onSpeedChange$, onSpeedChange] = observableCallback<
15
+ React.SyntheticEvent<HTMLInputElement>
16
+ >()
17
+
18
+ const speed$: Observable<number> = onSpeedChange$.pipe(
19
+ map((event) => Number(event.currentTarget.value)),
20
+ startWith(1000)
21
+ )
22
+
23
+ const UseObservableExample = () => (
24
+ <>
25
+ <h2>An observable of react elements</h2>
26
+ {useMemoObservable(of(<div>HELLO SYNC</div>), [])}
27
+ {useMemoObservable(numberReactElement$.pipe(startWith(2)), [])}
28
+ <h2>An observable with child as render func</h2>
29
+ {useMemoObservable(
30
+ timer(0, 100).pipe(
31
+ startWith(2),
32
+ map((num) => <>The number is {num}</>)
33
+ ),
34
+ []
35
+ )}
36
+
37
+ <h2>Nested</h2>
38
+ <p>You can adjust the update speed by changing update interval below</p>
39
+ {useMemoObservable(
40
+ () =>
41
+ speed$.pipe(
42
+ switchMap(
43
+ (speed: number): Observable<[number, number]> =>
44
+ timer(0, speed).pipe(
45
+ map((update): [number, number] => [speed, update])
46
+ )
47
+ ),
48
+ map(([speed, update]: [number, number]) => (
49
+ <>
50
+ Update interval:
51
+ <input
52
+ type="number"
53
+ value={speed}
54
+ onChange={onSpeedChange}
55
+ step={200}
56
+ min={0}
57
+ />
58
+ <div>Update {update}</div>
59
+ </>
60
+ ))
61
+ ),
62
+ []
63
+ )}
64
+ </>
65
+ )
66
+
67
+ ReactDOM.render(
68
+ <UseObservableExample />,
69
+ document.getElementById('use-observable-example')
70
+ )
@@ -0,0 +1,9 @@
1
+ import {Example} from '../../../pages/Examples/Examples'
2
+
3
+ const fs = require('fs')
4
+
5
+ export const UseObservableExample: Example = {
6
+ id: 'use-observable',
7
+ title: 'Use observable',
8
+ source: fs.readFileSync(`${__dirname}/UseObservableExample.tsx`, 'utf-8')
9
+ }
@@ -0,0 +1,10 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <title>ReactRx</title>
5
+ <meta content="width=device-width, initial-scale=1.0" name="viewport" />
6
+ </head>
7
+ <body>
8
+ <div id="app"></div>
9
+ </body>
10
+ </html>
@@ -0,0 +1,74 @@
1
+ import {CodeMirrorMode} from './components/repl/CodeMirrorMode'
2
+
3
+ import * as React from 'react'
4
+ import styled from 'styled-components'
5
+ import {rxComponent} from 'react-rx'
6
+ import {map} from 'rxjs/operators'
7
+ import {combineLatest} from 'rxjs'
8
+ import {location$} from './datastores/location'
9
+ import {parseCodeFenceHeader} from './utils/parseCodeFenceHeader'
10
+ import {ModeSpec} from './components/repl/runMode'
11
+
12
+ const ModeWrapper = styled.div`
13
+ color: #fff;
14
+ `
15
+
16
+ const InlineCode = styled.code`
17
+ font-size: 0.9em;
18
+ background-color: #e4e4e4;
19
+ padding: 1px 4px;
20
+ border-radius: 2px;
21
+ `
22
+
23
+ const CODEMIRROR_TSX_MODE: ModeSpec = {
24
+ name: 'jsx',
25
+ base: {name: 'javascript', typescript: true}
26
+ }
27
+
28
+ const TSX_MODE_TYPES = ['js', 'jsx', 'tsx', 'ts']
29
+
30
+ interface InlineCodeProps {
31
+ children: string
32
+ }
33
+
34
+ interface CodeProps {
35
+ children: string
36
+ className: 'language-js' | 'language-jsx' | 'language-tsx'
37
+ }
38
+
39
+ export const components = {
40
+ inlineCode: InlineCode,
41
+ code: (props: CodeProps) => {
42
+ const [lang, range] = parseCodeFenceHeader(
43
+ props.className.replace(/^language-/, '')
44
+ )
45
+ const mode =
46
+ lang && TSX_MODE_TYPES.includes(lang) ? CODEMIRROR_TSX_MODE : null
47
+ return (
48
+ <ModeWrapper>
49
+ <CodeMirrorMode highlighted={range} mode={mode}>
50
+ {props.children}
51
+ </CodeMirrorMode>
52
+ </ModeWrapper>
53
+ )
54
+ }
55
+ }
56
+
57
+ const hash$ = location$.pipe(map(location => location.hash))
58
+
59
+ const BookmarkedLink = rxComponent<{
60
+ href: string
61
+ children: React.ReactNode
62
+ }>(props$ =>
63
+ combineLatest([props$, hash$]).pipe(
64
+ map(([props, hash]) => (
65
+ <a href={props.href} className={hash === props.href ? 'selected' : ''}>
66
+ {props.children}
67
+ </a>
68
+ ))
69
+ )
70
+ )
71
+
72
+ export const tocComponents = {
73
+ a: BookmarkedLink
74
+ }
@@ -0,0 +1,134 @@
1
+ # Reactive Component
2
+
3
+ ## component()
4
+
5
+ **Signature**
6
+
7
+ ```ts
8
+ function component<Props>(
9
+ observable: Observable<Props>
10
+ ): React.FunctionComponent<{}>
11
+ function component<Props>(
12
+ component: Component<Props>
13
+ ): React.FunctionComponent<Props>
14
+ ```
15
+
16
+ **Usage**
17
+
18
+ ```ts
19
+ import * as ReactRx from 'react-rx'
20
+
21
+ export const MyComponent = ReactRx.component(props$ =>
22
+ props$.pipe(map(props => <div>...</div>))
23
+ )
24
+ ```
25
+
26
+ ## forwardRef()
27
+
28
+ The `React.forwardRef()` equivalent for a Reactive component.
29
+
30
+ ** Signature **
31
+
32
+ ```ts
33
+ function forwardRef<Props, Ref>(
34
+ component: (
35
+ input$: Observable<Props>,
36
+ ref: React.RefObject<Ref>
37
+ ) => React.FunctionComponent<Props>
38
+ )
39
+ ```
40
+
41
+ **Usage**
42
+
43
+ ```js
44
+ import * as ReactRx from 'react-rx'
45
+
46
+ const MyInputWrapper = ReactRx.forwardRef((props$, ref) =>
47
+ props$.pipe(
48
+ map(props => (
49
+ <input
50
+ ref={ref}
51
+ type="text"
52
+ value={props.value}
53
+ onChange={props.onChange}
54
+ />
55
+ ))
56
+ )
57
+ )
58
+ ```
59
+
60
+ ## useState()
61
+
62
+ Equivalent to `React.useState()` with the only difference that instead of the stateful value, it returns an observable representing the state changes (e.g. instead of `[T, update(value: T)]`, it returns `[Observable<T>, update(value: T)]`.
63
+
64
+ **Signature**
65
+
66
+ ```ts
67
+ function useState<T = undefined>(): [
68
+ Observable<T | undefined>,
69
+ React.Dispatch<React.SetStateAction<T | undefined>>
70
+ ]
71
+ function useState<T>(
72
+ initial: T | (() => T)
73
+ ): [Observable<T>, React.Dispatch<React.SetStateAction<T>>]
74
+ ```
75
+
76
+ ## useContext()
77
+
78
+ Equivalent to `React.useContext()` with the only difference that instead of a stateful context value, it returns an observable representing the context value changes.
79
+
80
+ ** Signature **
81
+
82
+ ```ts
83
+ function useContext<T>(context: React.Context<T>): Observable<T>
84
+ ```
85
+
86
+ ## useEvent()
87
+
88
+ This has no React API equivalent, but creates a `[Observable<Event>, (Event) => void]` tuple, where the first value is an observable of the arguments the second function is called with.
89
+
90
+ **Signature**
91
+
92
+ ```ts
93
+ function useObservableEvent<Event>(): [
94
+ Observable<Event>,
95
+ (event: Event) => void
96
+ ]
97
+ ```
98
+
99
+ ## toObservable()
100
+
101
+ Converts any value inside a Functional React component to an observable of changes to that value.
102
+
103
+ ```ts
104
+ function toObservable<T>(value: T): Observable<T>
105
+ ```
106
+
107
+ # React hooks
108
+
109
+ ## useObservable()
110
+
111
+ A React hook that returns the current/latest value from an observable
112
+
113
+ **Signature**
114
+
115
+ ```ts
116
+ function useObservable<T>(observable$: Observable<T>): T | null
117
+ function useObservable<T>(observable$: Observable<T>, initialValue: T): T
118
+ ```
119
+
120
+ ## useObservableState()
121
+
122
+ Same as `useState()` described in the Reactive Component section above, exported under a different name for clarity when using in regular React components
123
+
124
+ ## useObservableContext()
125
+
126
+ Same as `useContext()` described in the Reactive Component section above, exported under a different name for clarity when using in regular React components
127
+
128
+ ## useObservableEvent()
129
+
130
+ Same as `useEvent()` described in the Reactive Component section above, exported under a different name for clarity when using in regular React components
131
+
132
+ ## toObservable()
133
+
134
+ Same as `toObservable()` described in the Reactive Component section above
@@ -0,0 +1,25 @@
1
+ import {Header} from '../../components/Header'
2
+ import {Container, Content} from '../styles'
3
+ import MDXContent, {Toc} from './Api.mdx'
4
+ import * as React from 'react'
5
+ import {MDXProvider} from '@mdx-js/react'
6
+ import {components, tocComponents} from '../../mdx-components'
7
+ import {Sidebar} from '../../components/Sidebar'
8
+
9
+ export const ApiPage = () => (
10
+ <>
11
+ <Header />
12
+ <Container>
13
+ <Sidebar heading="API Reference">
14
+ <MDXProvider components={tocComponents}>
15
+ <Toc />
16
+ </MDXProvider>
17
+ </Sidebar>
18
+ <Content>
19
+ <MDXProvider components={components}>
20
+ <MDXContent />
21
+ </MDXProvider>
22
+ </Content>
23
+ </Container>
24
+ </>
25
+ )