-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.rkt
205 lines (173 loc) · 6.89 KB
/
main.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#lang racketscript/base
(require (for-syntax racket/base
racket/syntax
syntax/stx
syntax/parse)
racketscript/interop
racket/stxparam
racket/list)
(define React ($/require/* "react"))
(define ReactDOM ($/require/* "react-dom"))
(provide render
<el
<>
$props
define-component
create-context
with-context
use-context
in-context
$ctx
use-state
define-state
use-effect
use-reducer
with-reducer-context
$state
$action
in-reducer-context
$ctx-state
$ctx-dispatch
use-callback
use-memo
use-ref
use-imperative-handle
use-layout-effect
use-debug-value)
;; Basic hooks
(define (use-state default-state)
(apply values (js-array->list (#js.React.useState default-state))))
;; (define-state name val) is shorthand for creating a React State Hook
;; It defines two identifiers:
;; - name: the current value of the state
;; - set-name!: a setter used to change the state's value
(define-syntax define-state
(syntax-parser
[(_ name default-val)
#:with set-name! (format-id #'name "set-~a!" #'name)
#'(define-values (name set-name!) (use-state default-val))]))
(define use-effect #js.React.useEffect)
(define use-context #js.React.useContext)
;; Additional hooks
(define (use-reducer reducer initial-state [init $/undefined])
(apply values (js-array->list (#js.React.useReducer reducer initial-state init))))
(define use-callback #js.React.useCallback)
(define use-memo #js.React.useMemo)
(define use-ref #js.React.useRef)
(define use-imperative-handle #js.React.useImperativeHandle)
(define use-layout-effect #js.React.useLayoutEffect)
(define use-debug-value #js.React.useDebugValue)
;; Basic API
(define create-context #js.React.createContext)
(define create-element
(lambda (component #:props [props null] . children)
(apply #js.React.createElement
(append
(list (racket->js component) props)
(map racket->js (flatten children))))))
(define (racket->js node)
(cond
[(or (string? node) (number? node)) ($/str node)]
[else node]))
;; A small alias for readability
(define <el create-element)
;; a macro version of <el that tries to hide some boilerplate
(define-syntax <>
(syntax-parser
[(_ name #:props ([x:id v] ...) . body)
#'(<el name #:props ($/obj [x v] ...) . body)]
[(_ name . body) #'(<el name . body)]))
(define-syntax-parameter $props
(syntax-parser
[_ #'(error '$props "Warning: $props keyword cannot be used outside Rackt define-component body")]))
(define-syntax define-component
(syntax-parser
[(_ name . body)
#'(define (name props . ..)
(syntax-parameterize
;; $props may be used as an id,
;; or in head position where an implicit $ is inserted
([$props (syntax-parser
[:id #'props]
[(_ . args) #'($ props . args)])])
. body))]))
(define-syntax with-context
(syntax-parser
[(_ ctx-name (~datum =) default-value . body)
#'(<> ($ ctx-name 'Provider) #:props ([value default-value]) . body)]))
(define-syntax-parameter $ctx
(syntax-parser
[_ #'(error '$ctx "Warning: $ctx keyword cannot be used outside Rackt in-context body")]))
(define-syntax in-context
(syntax-parser
[(_ ctx-name . body)
#'(let ([ctx (use-context ctx-name)])
(syntax-parameterize
;; $ctx may be used as an id,
;; or in head position where an implicit $ is inserted
([$ctx (syntax-parser
[:id #'ctx]
[(_ . args) #'($ ctx . args)])])
. body))]))
(define-syntax-parameter $state
(syntax-parser
[_ #'(error '$state "Warning: $state keyword cannot be used outside Rackt with-reducer-context action table")]))
(define-syntax-parameter $action
(syntax-parser
[_ #'(error '$action "Warning: $action keyword cannot be used outside Rackt with-reducer-context action table")]))
(begin-for-syntax
(define (id->str id)
(datum->syntax id (symbol->string (syntax->datum id)))))
;; like with-context, but the given context must be a Reducer,
;; ie, it only has two properties corresponding the Reducer "state" and "dispatch"
(define-syntax with-reducer-context
(syntax-parser
[(_ ctx-name #:init init-state #:actions ([action-type:id e] ...) . body)
#:with (action-str ...) (stx-map id->str #'(action-type ...))
#`(begin
(define-values (_store _dispatch)
(use-reducer
(lambda (state action)
(syntax-parameterize
;; $state and $action may be used as ids,
;; or in head position where an implicit $ is inserted
([$state (syntax-parser
[:id #'state]
[(_ . args) #'($ state . args)])]
[$action (syntax-parser
[:id #'action]
[(_ . args) #'($ action . args)])])
(cond [(eq? ($ action 'type) action-str) e] ... [else state])))
init-state))
(with-context ctx-name = ($/obj [store _store] [dispatch _dispatch]) . body))]))
(define-syntax-parameter $ctx-state
(syntax-parser
[_ #'(error '$ctx-state "Warning: $ctx-state keyword cannot be used outside Rackt in-reducer-context body")]))
(define-syntax-parameter $ctx-dispatch
(syntax-parser
[_ #'(error '$ctx-dispatch "Warning: $ctx-dispatch keyword cannot be used outside Rackt in-reducer-context body")]))
;; like in-context, but the given context must be a Reducer,
;; ie, it only has two properties corresponding the Reducer "state" and "dispatch".
;; There are implicitly bound to $ctx-state and $ctx-dispatch, respectively
(define-syntax in-reducer-context
(syntax-parser
[(_ ctx-name . body)
#'(begin
(in-context ctx-name
(let ([store ($ctx 'store)]
[dispatch ($ctx 'dispatch)])
(syntax-parameterize
;; $store and $dispatch may be used as ids,
;; or in head position where an implicit $ is inserted
([$ctx-state (syntax-parser
[:id #'store]
[(_ . args) #'($ store . args)])]
[$ctx-dispatch
(syntax-parser
[:id #'dispatch]
[(_ obj) #'(dispatch obj)] ; given explicit obj
[(_ action-type:id . props+vals)
#`(dispatch ($/obj [type #,(id->str #'action-type)] . props+vals))])])
. body))))]))
(define (render react-element node-id)
(#js.ReactDOM.render react-element (#js*.document.getElementById node-id)))