Package

purescript-yoga-react-dom

Repository
rowtype-yoga/purescript-yoga-react-dom
License
WTFPL
Uploaded by
pacchettibotti
Published on
2024-12-14T18:00:40Z

After using react-basic-hooks for a while I've gathered a small wishlist for things that I'd like to work a little differently.

What it looks like:

module Example where

import Prelude hiding (div)
import Yoga.React.DOM.HTML (div, a, span)
import Yoga.React.DOM (text)

example =
  div { "data-testid": "example" }
    [ a { href: "https://example.com" }
        $ span { "aria-label": "somelabel" } "Example"
    , span {} " and so on..."
    ]

vs.

module Example where

import Prelude hiding (div)
import React.Basic.DOM (div, a, span, text)
import Foreign.Object (singleton) as Object

example =
  div
    { _data: Object.singleton "testid" "example"
    , children:
        [ a { href: "https://exmaple.com", children: [ span { _aria: Object.singleton "label" "somelabel", children: [ text "Example" ] } ] }
        , span { children: [ text  " and so on..." ] }
        ]
    }

Trying it out / Installation

This is a library that works on its own and in conjunction with the existing react-basic-hooks-dom library.

Then run:

spago install yoga-react-dom

Features and design goals

Documentation with examples for each component

Since there's no tooling in PureScript that will autocomplete possible record keys, I've decided to work around this limitation by providing some short documentation with the most important attributes for each component and a copy-pasteable exmaple.

There are also certain relationships between HTML elements that are very difficult to enforce in the type system, such as how to nest tags correctly (e.g. table, contains thead, and tbody which contains tr, which contains td or th, ...). I hope that the provided examples remedy these problems a lot since the documentation will popup when autocompleting the HTML tag's name.

Ability to pass any "aria-" or "data-" prop directly

This makes working with Hooks that return these props and expect the user to spread them onto your own component's props a lot easier and less error-prone.

aria-* props are typechecked

In contrast to data-* props, aria-* props are finite and can't have arbitrary names. This is an improvement that could be made in the current DOM as well, but I decided to skip it and do it here.

Improved readability by separating the children prop out

This is in line with HTML where the children are not part of the attributes and aren't named.

Allows passing single children and the empty array without runtime casts

React's createElement may be invoked without any children, or with zero to many children. The only way to approximate this in PureScript was via a typeclass as in the Simplified DOM.

The downside of this is that this stops working within Arrays because PureScript requires arrays to be of the same shape. Here is a workaround:

Manually casting the text to be JSX via text:

module Example where
import Yoga.React.DOM (text)
import Yoga.React.DOM.HTML.P (p)

x = [ text "A string", p {} "A p" ]
Modules
Yoga.React.DOM
Yoga.React.DOM.Attributes
Yoga.React.DOM.Attributes.AutoCapitalize
Yoga.React.DOM.Attributes.Dir
Yoga.React.DOM.Attributes.InputMode
Yoga.React.DOM.Attributes.Target
Yoga.React.DOM.Attributes.YesOrNo
Yoga.React.DOM.Client
Yoga.React.DOM.Event
Yoga.React.DOM.HTML
Yoga.React.DOM.HTML.A
Yoga.React.DOM.HTML.Abbr
Yoga.React.DOM.HTML.Address
Yoga.React.DOM.HTML.Area
Yoga.React.DOM.HTML.Article
Yoga.React.DOM.HTML.Aside
Yoga.React.DOM.HTML.Audio
Yoga.React.DOM.HTML.B
Yoga.React.DOM.HTML.Base
Yoga.React.DOM.HTML.Bdi
Yoga.React.DOM.HTML.Bdo
Yoga.React.DOM.HTML.Blockquote
Yoga.React.DOM.HTML.Body
Yoga.React.DOM.HTML.Br
Yoga.React.DOM.HTML.Button
Yoga.React.DOM.HTML.Canvas
Yoga.React.DOM.HTML.Caption
Yoga.React.DOM.HTML.Cite
Yoga.React.DOM.HTML.Code
Yoga.React.DOM.HTML.Col
Yoga.React.DOM.HTML.ColGroup
Yoga.React.DOM.HTML.Data
Yoga.React.DOM.HTML.DataList
Yoga.React.DOM.HTML.Dd
Yoga.React.DOM.HTML.Del
Yoga.React.DOM.HTML.Details
Yoga.React.DOM.HTML.Dfn
Yoga.React.DOM.HTML.Dialog
Yoga.React.DOM.HTML.Div
Yoga.React.DOM.HTML.Dl
Yoga.React.DOM.HTML.Dt
Yoga.React.DOM.HTML.Em
Yoga.React.DOM.HTML.Embed
Yoga.React.DOM.HTML.FieldSet
Yoga.React.DOM.HTML.Figcaption
Yoga.React.DOM.HTML.Figure
Yoga.React.DOM.HTML.Footer
Yoga.React.DOM.HTML.Form
Yoga.React.DOM.HTML.H
Yoga.React.DOM.HTML.HTML
Yoga.React.DOM.HTML.Head
Yoga.React.DOM.HTML.Header
Yoga.React.DOM.HTML.Hr
Yoga.React.DOM.HTML.I
Yoga.React.DOM.HTML.IFrame
Yoga.React.DOM.HTML.Img
Yoga.React.DOM.HTML.Input
Yoga.React.DOM.HTML.Kbd
Yoga.React.DOM.HTML.Label
Yoga.React.DOM.HTML.Legend
Yoga.React.DOM.HTML.Li
Yoga.React.DOM.HTML.Link
Yoga.React.DOM.HTML.Main
Yoga.React.DOM.HTML.Map
Yoga.React.DOM.HTML.Mark
Yoga.React.DOM.HTML.Menu
Yoga.React.DOM.HTML.Meta
Yoga.React.DOM.HTML.Meter
Yoga.React.DOM.HTML.Nav
Yoga.React.DOM.HTML.NoScript
Yoga.React.DOM.HTML.Object
Yoga.React.DOM.HTML.Ol
Yoga.React.DOM.HTML.Optgroup
Yoga.React.DOM.HTML.Option
Yoga.React.DOM.HTML.Output
Yoga.React.DOM.HTML.P
Yoga.React.DOM.HTML.Param
Yoga.React.DOM.HTML.Picture
Yoga.React.DOM.HTML.Pre
Yoga.React.DOM.HTML.Progress
Yoga.React.DOM.HTML.Q
Yoga.React.DOM.HTML.Rp
Yoga.React.DOM.HTML.Rt
Yoga.React.DOM.HTML.Ruby
Yoga.React.DOM.HTML.S
Yoga.React.DOM.HTML.Samp
Yoga.React.DOM.HTML.Script
Yoga.React.DOM.HTML.Section
Yoga.React.DOM.HTML.Select
Yoga.React.DOM.HTML.Small
Yoga.React.DOM.HTML.Source
Yoga.React.DOM.HTML.Span
Yoga.React.DOM.HTML.Strong
Yoga.React.DOM.HTML.Style
Yoga.React.DOM.HTML.Sub
Yoga.React.DOM.HTML.Summary
Yoga.React.DOM.HTML.Sup
Yoga.React.DOM.HTML.Table
Yoga.React.DOM.HTML.Tbody
Yoga.React.DOM.HTML.Td
Yoga.React.DOM.HTML.Template
Yoga.React.DOM.HTML.Textarea
Yoga.React.DOM.HTML.Tfoot
Yoga.React.DOM.HTML.Th
Yoga.React.DOM.HTML.Thead
Yoga.React.DOM.HTML.Time
Yoga.React.DOM.HTML.Title
Yoga.React.DOM.HTML.Tr
Yoga.React.DOM.HTML.Track
Yoga.React.DOM.HTML.U
Yoga.React.DOM.HTML.Ul
Yoga.React.DOM.HTML.Var
Yoga.React.DOM.HTML.Video
Yoga.React.DOM.HTML.Wbr
Yoga.React.DOM.Handler
Yoga.React.DOM.Internal
Yoga.React.DOM.Server
Dependencies