Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/purescript-concur/purescript-concur-react

Concur UI Framework for Purescript
https://github.com/purescript-concur/purescript-concur-react

Last synced: about 2 months ago
JSON representation

Concur UI Framework for Purescript

Awesome Lists containing this project

README

        


Purescript Concur







Join the chat at https://gitter.im/concurhaskell


Join the chat at https://gitter.im/concurhaskell


Purescript-Concur-React on Pursuit


[Concur UI Lib](https://github.com/ajnsit/concur) is a brand new client side Web UI framework that explores an entirely new paradigm. It does not follow FRP (think Reflex or Reactive Banana), or Elm architecture, but aims to combine the best parts of both. This repo contains the Concur implementation for Purescript, using the React backend.

## Documentation

Work in progress tutorials are published in the [Concur Documentation site](https://github.com/ajnsit/concur-documentation/blob/master/README.md)

API documentation is [published on Pursuit](https://pursuit.purescript.org/packages/purescript-concur-react).

## Performance

Purescript-Concur is reasonably light. The [entire *uncompressed* JS bundle](https://github.com/purescript-concur/purescript-concur-react/blob/master/docs/index.prod.minified.0dfb135e.js), including react and all libraries, for the entire example application in this repo clocks in at 180KB. You can build this bundle yourself with the command `npm run prod` (*currently broken* due to the move to spago).

This leads to pretty fast initial load times. Running the Chrome audit on https://purescript-concur.github.io/purescript-concur-react/ produces -



## Ports to other languages

Concur's model translates well to other platforms.

1. [Concur for Haskell](https://github.com/ajnsit/concur) - The original version of Concur written in Haskell.
2. [Concur for Javascript](https://github.com/ajnsit/concur-js) - An official but experimental port to Javascript.
3. [Concur for Python](https://github.com/potocpav/python-concur) - An unofficial and experimental port to Python. Uses ImgUI for graphics. Created and Maintained by [potocpav](https://github.com/potocpav).

## Installation

You can quickly get a production setup going (using Spago and Parcel) by cloning the [Purescript Concur Starter](https://github.com/purescript-concur/purescript-concur-starter).

Else, if you use Spago -

```bash
spago install concur-react
```

## Building examples from source

```bash
git clone https://github.com/purescript-concur/purescript-concur-react.git
cd purescript-concur-react
npm install
# Build library sources
npm run build
# Build examples
npm run examples
# Start a local server
npm run examples-start
# Check examples
open localhost:1234 in the browser
```

## Exporting Concur Widgets to React JS

Concur Widgets can be exported as both React classes or React Elements, which would allow them to be used within Javascript code.

Let's say you have a `counter :: Int -> Widget HTML a` concur widget that you want to expose to React.

*Step 1*: Convert the `Widget` to a `ReactClass` using `Concur.React.toReactClass`. Here you would like the react class to accept a `{conut :: Int}` as props.

Here, the `"Counter"` is the name of the component that will be visible to React. You can use any name.
And `mempty` represents the initial view shown until the widget has finished initialising. We can leave this empty (views have a `Monoid` instance).

```purescript
-- Counter.purs
counterReactClass :: ReactClass { count :: Int }
counterReactClass = toReactClass "Counter" mempty \ {count: i} -> counter i
```

*Step 2*: Import the class from within Javascript, and give it a name starting with an uppercase letter as required by React.

```javascript
// MyApp.jsx
import {counterClass} from '/Counter/index.js';

// React requires all component names to start with an uppercase letter -
let Counter = counterClass;
```

*Step 3*: Now you can use it normally from within React.

```javascript
// MyApp.jsx
class ReactComponent extends Component {
render(props) {
let {count} = this.state;
return (


The counter below was imported from a Concur widget. The starting count of 10 was passed from within React




);
}
```

## Using External React Components

It's easy to add external React components to Concur. Usually all you would require to wrap an external component is to import it as a `ReactClass`, and then wrapping it with one of the `el` functions.

For example, let's say you want to wrap the `Button` component provided by the material-ui library.

*Step 1*: First write an FFI module that exposes the `ReactClass` component -

```javascript
// Button.js
exports.classButton = require('@material-ui/core/Button').default
```

And import it into your purescript program

```purescript
-- Button.purs
foreign import classButton :: forall a. ReactClass a
```

If you are using the [Purescript React MUI bindings](https://github.com/doolse/purescript-react-mui), then you can simply import the class component from the library without defining the FFI module -

```purescript
import MaterialUI.Button (classButton)
```

*Step 2*: Then wrap up the imported `ReactClass` into a widget to make it usable within Concur -

```purescript
import Concur.React.DOM (El, el')
import React (unsafeCreateElement)
import React.DOM.Props (unsafeFromPropsArray)

button :: El
button = el' (unsafeCreateElement classButton <<< unsafeFromPropsArray)
```

*Step 3*: Now you can use `button` normally within Concur. For example -

```purescript
import Concur.React.DOM as D
import Concur.React.Props as P

helloButton = button [P.onClick] [D.text "Hello World!"]
```

Note that you can mix in the default widgets and props with the MUI ones.

## Examples

[Demo](https://purescript-concur.github.io/purescript-concur-react/) and [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Main.purs) for composing all the examples in one page.

Individual example sources -

1. **Hello World!** Shows simple effectful widgets with state using StateT. [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/Hello.purs).
2. **A simple counter widget** without using StateT. [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/Counter.purs).
3. **Focus counter** demonstrates a stateful widget, with multiple event handlers, and no action types needed! [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/FocusCount.purs).
4. **Virtual Keyboard** An onscreen virtual keyboard. Demonstrates FFI as well as handling document level events inside nested widgets. [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/Keyboard.purs).
5. **A login widget**. [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/Login.purs).
6. Concur has Signals! Sample **counting widget implemented with Signals**! [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/Signals.purs).
7. A **Full-featured TodoMVC implementation with LocalStorage Persistence** built with Signals. [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/Todos.purs).
8. A **Fully editable tree** in ~30 lines of code (with Signals). [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/EditHeadingsSignals.purs).
9. A **Postfix calculator**. [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/Calc.purs).
10. Using **AJAX and handling JSON** responses. [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/Ajax.purs).
11. A small widget to **Visualise CSS color codes**. [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/Color.purs).
12. **Asynchronous timers** which can be cancelled. [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/Timers.purs).
13. A **Routed widget** which demonstrates routing. [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/Routing.purs).
14. **The Elm Architecture example** demonstrates how Concur subsumes "The Elm Architecture". [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/TheElmArchitecture.purs).
15. **Performance test** - A huge list of 50 thousand parallel buttons. This has two variants, fast (uses slightly lower level interface) and slow (idiomatic concur code). [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/SlowButtonList.purs).
16. **Tail Recursion demo** - Since Concur is purely functional in nature, its primary mode of iteration is via recursion. Purescript in general is NOT stack stafe with tail recursion; It uses tricks like tailRec and tailRecM. However, Concur performs trampolining to make monadic recursion completely stack safe. This example demonstrates that by making a huge number of tail recursive calls in a short span of time. [Source](https://github.com/purescript-concur/purescript-concur-react/blob/master/examples/src/Test/TailRec.purs).