Skip to content

Commit d149306

Browse files
authored
Add a simplified version of the dom elements (#41)
* Add a simplified version of the dom elements * Inline unsafeCoerce * Add Maybe instance
1 parent 534f01e commit d149306

File tree

4 files changed

+17115
-1
lines changed

4 files changed

+17115
-1
lines changed

codegen/index.js

+73
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require("fs");
22
const { htmlProps, svgProps, voids, types, typesByElement, reserved } = require("./consts");
33
const changeCase = require('change-case')
44
const htmlGenFile = "../src/React/Basic/DOM/Generated.purs";
5+
const htmlSimplifiedGenFile = "../src/React/Basic/DOM/Simplified/Generated.purs";
56
const svgGenFile = "../src/React/Basic/DOM/SVG.purs";
67

78
const htmlHeader = `-- | ----------------------------------------
@@ -22,6 +23,26 @@ import Web.DOM (Node)
2223
2324
`;
2425

26+
const simplifiedHtmlHeader = `-- | ----------------------------------------
27+
-- | THIS FILE IS GENERATED -- DO NOT EDIT IT
28+
-- | ----------------------------------------
29+
30+
module React.Basic.DOM.Simplified.Generated where
31+
32+
import Data.Nullable (Nullable)
33+
import Effect.Unsafe (unsafePerformEffect)
34+
import Foreign.Object (Object)
35+
import Prim.Row (class Nub, class Union)
36+
import React.Basic (JSX, ReactComponent, Ref, element)
37+
import React.Basic.DOM.Internal (CSS, unsafeCreateDOMComponent)
38+
import React.Basic.DOM.Simplified.ToJSX (class ToJSX, toJSX)
39+
import React.Basic.Events (EventHandler)
40+
import Record as Record
41+
import Unsafe.Coerce (unsafeCoerce)
42+
import Web.DOM (Node)
43+
44+
`;
45+
2546
const propType = (e, p) => {
2647
const elPropTypes = typesByElement[p];
2748
if (elPropTypes) {
@@ -127,15 +148,67 @@ const generatePropTypes = (elements, props, sharedPropType) =>
127148
_${e}'
128149
:: ReactComponent (Record ${propType})
129150
_${e}' = unsafePerformEffect (unsafeCreateDOMComponent "${e}")
151+
`;
152+
}).map(x => x.replace(/^\n\ {4}/, "").replace(/\n\ {4}/g, "\n"))
153+
.join("\n");
154+
155+
const generateSimplifiedPropTypes = (elements, props, sharedPropType) =>
156+
elements.map(e => {
157+
const noChildren = voids.includes(e);
158+
const symbol = reserved.includes(e) ? `${e}'` : e;
159+
160+
const propType = sharedPropType ? `(${sharedPropType} Props_${e})` : `Props_${e}`
161+
162+
return noChildren ? `` : `
163+
type Props_${e} =${printRecord(e,
164+
( reactProps.concat("children")
165+
)
166+
.concat(props[e] || [], props["*"] || [])
167+
.sort()
168+
)}
169+
170+
${symbol}
171+
:: forall attrsNoChildren attrsWithDuplicate attrs attrs_ jsx
172+
. Union attrs attrs_ ${propType}
173+
=> ToJSX jsx
174+
=> Union (children :: Array JSX) attrsNoChildren attrsWithDuplicate
175+
=> Nub (children :: Array JSX | attrsNoChildren) attrs
176+
=> Record attrsNoChildren
177+
-> jsx
178+
-> JSX
179+
${symbol} props children = element _internal${symbol} propsWithChildren
180+
where
181+
propsWithChildren :: { | attrs }
182+
propsWithChildren = Record.merge { children: toJSX children } props
183+
184+
${symbol}' :: forall jsx. ToJSX jsx => jsx -> JSX
185+
${symbol}' = ${symbol} {}
186+
187+
_internal${symbol}
188+
:: forall attrs attrs_
189+
. Union attrs attrs_ ${propType}
190+
=> ReactComponent (Record attrs)
191+
_internal${symbol} = unsafeCoerce _internal${symbol}'
192+
193+
_internal${symbol}'
194+
:: ReactComponent (Record ${propType})
195+
_internal${symbol}' = unsafePerformEffect (unsafeCreateDOMComponent "${symbol}")
196+
130197
`;
131198
}).map(x => x.replace(/^\n\ {4}/, "").replace(/\n\ {4}/g, "\n"))
132199
.join("\n");
133200

134201
const htmlTagTypes = generatePropTypes(htmlProps.elements.html, htmlProps, null);
202+
const htmlSimplifiedTagTypes = generateSimplifiedPropTypes(htmlProps.elements.html, htmlProps, null);
135203
const svgTagTypes = generatePropTypes(Object.keys(camelCaseSvgProps), camelCaseSvgProps, 'SharedSVGProps');
136204

137205
console.log(`Writing "${htmlGenFile}" ...`);
138206
fs.writeFileSync(htmlGenFile, htmlHeader + htmlTagTypes);
207+
208+
console.log(`Writing "${htmlSimplifiedGenFile}" ...`);
209+
fs.writeFileSync(htmlSimplifiedGenFile, simplifiedHtmlHeader + htmlSimplifiedTagTypes);
210+
139211
console.log(`Writing "${svgGenFile}" ...`);
140212
fs.writeFileSync(svgGenFile, svgHeader + svgTagTypes);
213+
141214
console.log("Done.");

spago.dhall

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ You can edit this file as you like.
44
-}
55
{ name = "react-basic-dom"
66
, dependencies =
7-
[ "effect"
7+
[ "arrays"
8+
, "effect"
89
, "foldable-traversable"
910
, "foreign-object"
1011
, "maybe"
1112
, "nullable"
1213
, "prelude"
1314
, "react-basic"
15+
, "record"
1416
, "unsafe-coerce"
1517
, "web-dom"
1618
, "web-events"

0 commit comments

Comments
 (0)