Skip to content
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

Make css and className optional for element and elementKeyed #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
49 changes: 37 additions & 12 deletions src/React/Basic/Emotion.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module React.Basic.Emotion
, style
, class IsStyleProperty
, prop
, class StyledInput
, class StyledInputRl
, element
, elementKeyed
, css
Expand Down Expand Up @@ -82,9 +84,12 @@ import Data.Number.Format (toString) as Number
import Data.String as String
import Foreign as F
import Foreign.Object (Object, fromHomogeneous)
import Prim.Row as Row
import Prim.RowList (class RowToList, Cons, Nil, RowList)
import Prim.TypeError (class Warn, Text)
import React.Basic (JSX, ReactComponent)
import Type.Row.Homogeneous (class Homogeneous)
import Type.RowList (class ListToRow)
import Unsafe.Coerce (unsafeCoerce)
import Web.HTML.History (URL(..))

Expand Down Expand Up @@ -140,40 +145,60 @@ class IsStyleProperty a where
instance isStylePropertyStyleProperty :: IsStyleProperty StyleProperty where
prop = identity

class StyledInput (input :: Row Type) (base :: Row Type) | input -> base
instance styledInputI ::
( RowToList input inputRl
, StyledInputRl inputRl baseRl
, ListToRow baseRl base1
, Row.Cons "className" String base1 base2
, Row.Nub base2 base
) => StyledInput input base

class StyledInputRl (inputRl :: RowList Type) (baseRl :: RowList Type) | inputRl -> baseRl

instance styledInputRlNil :: StyledInputRl Nil Nil
else instance styledInputRlConsCss ::
StyledInputRl inputTl base =>
StyledInputRl (Cons "css" Style inputTl) base
else instance styledInputRlConsOther ::
StyledInputRl inputTl baseTl =>
StyledInputRl (Cons name v inputTl) (Cons name v baseTl)

-- | Create a `JSX` node from a `ReactComponent`, by providing the props.
-- |
-- | This function is identical to `React.Basic.element` plus Emotion's
-- | `css` prop.
element ::
forall props.
ReactComponent { className :: String | props } ->
{ className :: String, css :: Style | props } ->
forall props' props.
StyledInput props' props =>
ReactComponent { | props } ->
{ | props' } ->
JSX
element = runFn2 element_

foreign import element_ ::
forall props.
forall props' props.
Fn2
(ReactComponent { className :: String | props })
{ className :: String, css :: Style | props }
(ReactComponent { | props })
{ | props' }
JSX

-- | Create a `JSX` node from a `ReactComponent`, by providing the props.
-- |
-- | This function is identical to `React.Basic.element` plus Emotion's
-- | `css` prop.
elementKeyed ::
forall props.
ReactComponent { className :: String | props } ->
{ key :: String, className :: String, css :: Style | props } ->
forall props' props.
ReactComponent { | props } ->
{ key :: String | props' } ->
JSX
elementKeyed = runFn2 elementKeyed_

foreign import elementKeyed_ ::
forall props.
forall props' props.
Fn2
(ReactComponent { className :: String | props })
{ key :: String, className :: String, css :: Style | props }
(ReactComponent { | props })
{ key :: String | props'}
JSX

foreign import global :: ReactComponent { styles :: Style }
Expand Down