Skip to content

Add el helper #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions src/React/Basic/DOM/Simplified/ToJSX.purs
Original file line number Diff line number Diff line change
@@ -1,15 +1,55 @@
module React.Basic.DOM.Simplified.ToJSX
( class ToJSX
, el
, toJSX
) where
)
where

import Prelude

import Data.Array (singleton)
import Data.Maybe (Maybe(..))
import React.Basic (JSX)
import Prim.Row (class Lacks)
import React.Basic (JSX, ReactComponent)
import React.Basic as React
import Record as Record
import Type.Proxy (Proxy(..))
import Unsafe.Coerce (unsafeCoerce)

-- | Helper function to easily use any `ReactComponent` and compose it with the simplified html tags.
-- | E.g. using NextUI:
-- | ```purescript
-- | -- Import the simplified elements
-- | import React.Basic.DOM.Simplified.Generated as R
-- |
-- | ...
-- |
-- | -- Import your react components
-- | foreign import container :: forall props. ReactComponent { | props }
-- | foreign import row :: forall props. ReactComponent { | props }
-- | foreign import col :: forall props. ReactComponent { | props }
-- |
-- | ...
-- |
-- | -- Build your jsx
-- | el container {} $
-- | el row {} $
-- | el col {} $
-- | R.div {} "Some text"
-- | ```
el
∷ ∀ props jsx
. Lacks "children" props
=> ToJSX jsx
⇒ ReactComponent { children ∷ Array JSX | props }
→ Record props
→ jsx
→ JSX
el cmp props children =
(React.element)
cmp
(Record.insert (Proxy ∷ Proxy "children") (toJSX children) props)

class ToJSX jsx where
toJSX :: jsx -> Array JSX

Expand Down