• Stars
    star
    769
  • Rank 59,078 (Top 2 %)
  • Language
    Elm
  • License
    BSD 3-Clause "New...
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Write React components in Elm

Write React components in Elm

This package makes it easy to turn Elm code into React components.

Companies that use Elm in production usually start with a single component. So if you want to use Elm at work, start with a small experiment. Do people think it is nice? Do more! Do people think it sucks? Do less!

Read more about how to use Elm at work here.

Example

Usage

After you have compiled an Elm program to JavaScript, you can embed it in React like this:

import Elm from 'react-elm-components'
import { Todo } from '../dist/elm/todomvc.js'

function render() {
	return <Elm src={Todo} />
}

Flags

Sometimes you want to give your Elm program some flags on start up. For example, maybe your Todo module needs to get an array of todos. You would write something like this:

import Elm from 'react-elm-components'
import { Todo } from '../dist/elm/todomvc.js'

function render() {
	var flags = { todos: ["Get Milk", "Do Laundry"] };
	return <Elm src={Todo} flags={flags} />
}

These flags will be given to the Elm program, allowing you to do some setup work in JS first.

JavaScript/Elm Interop

As your Elm program gets fancier, you will probably need to interact with JavaScript. We do this with ports. Think of these as holes in the side of an Elm program that let you pass messages back-and-forth.

So maybe we extend our Todo app to allow outsiders to register new tasks through the todos port. And maybe we also expose numActiveTodos so that the outsider can know how much work you have left. You would set it up like this:

import Elm from 'react-elm-components'
import { Todo } from '../dist/elm/todomvc.js'

function render() {
	return <Elm src={Todo} ports={setupPorts} />
}

function setupPorts(ports) {
	ports.numActiveTodos.subscribe(function(n) {
		console.log(n);
	});

	ports.todos.send("Invent the Universe");
	ports.todos.send("Bake an Apple Pie");
}

In the setupPorts function, we first subscribe to the numActiveTodos port. Whenever the number of active todos changes, we will run that function and log the number on the console. After that, we send two values through the todos port. This will add both of these into the model and trigger the numActiveTodos callback twice.

Advanced Usage

Once the Elm component is initialized, changing the flags and ports properties will do nothing. So here are some tricks that may help you out:

  1. If you want to reinitialize your Elm component, add a different key to the old and new components. This way old one is destroyed and replaced by the new one.
  2. If you want to mess with ports, you can save the ports object into your state and access it later.
  3. This package is super simple. Fewer than 20 lines. Check out the implementation and do it different if you want!

Angular, Ember, etc.

If you want to embed Elm in Angular or Ember or whatever else, you are in luck!

The implementation is under 20 lines, mostly React-related. The important lines are basically running the following program at the correct time:

var Elm = require('../dist/elm/todomvc.js');
var app = Elm.Todo.embed(node, flags);
setupPorts(app.ports)

So if you are interested in embedding Elm in something else, do the same trick! You can get more complete docs on embedding Elm in HTML here and JavaScript interop here. Let the community know if you make something!


react-elm-components is maintained by the Front End Capability Team at Culture Amp.

More Repositories

1

kaizen-design-system

Culture Amp's Kaizen Design System 🌱
TypeScript
159
star
2

elm-css-modules-loader

Reference CSS modules in Elm source files with Webpack
Elm
71
star
3

kestrel

Kotlin Framework for running event-sourced services
Kotlin
60
star
4

stencil-elm-output-target

Plugin for Stencil (https://stenciljs.com) to output a proxy module to consume web components with type safety in Elm
TypeScript
21
star
5

event_framework

The Culture Amp Ruby Event Framework ❀️
Ruby
10
star
6

local-saml-idp

Hard fork of mcguinness/saml-idp; a local SAML Identity Provider (IdP) library to test SAML 2.0 Service Providers (SPs).
JavaScript
10
star
7

aws-assume-role-buildkite-plugin

Buildkite plugin to assume IAM role before running command
Shell
9
star
8

cultureamp-style-guide

Culture Amp’s Component Library and Living Style Guide
JavaScript
6
star
9

visual-testing-api

This is a serverless REST Api that stores images and does image comparison. It uses Looks Same algo to compare the images
JavaScript
6
star
10

ca-go

A Go library with multiple packages to be shared by services.
Go
5
star
11

browser-testing

Tool to do browser automation
JavaScript
5
star
12

elm-storybook

⚠️ Experimental Elm/Storybook tooling
Elm
4
star
13

parameter-store-exec

Execute a command with environment loaded from AWS Parameter Store
Go
4
star
14

s3dotenv

Command wrapper to load environment variables from S3
Go
4
star
15

glamplify

Go Amplify Module of useful common tools (eg. logging, config, etc)
Go
4
star
16

ecr-scan-results-buildkite-plugin

Buildkite plugin to retrieve ECR scan results
Go
3
star
17

web-team-scripts-to-rule-them-all

Boilerplate scripts fulfilling the normalized script pattern
Shell
2
star
18

rich-text-toolkit

Deprecated: moved to kaizen-design-system
TypeScript
2
star
19

homebrew-web-team-devtools

A collection of helper scripts for system and application setup
Shell
2
star
20

danger-systems-github-action

Shell
2
star
21

datadog-cdk

CDK constructs for DataDog Cloudformation Resource Types.
TypeScript
2
star
22

kafka-connect-plugins

A set of generic plugins for Kafka Connect that complement the built-in transformations, config providers, and connectors.
Kotlin
2
star
23

terraform-provider-schemaregistry

Terraform Provider for Confluent Schema Registry
Go
2
star
24

cultureamp-front-end-scripts

Kanso: Build scripts for Culture Amp front end projects
JavaScript
2
star
25

splunk-ruby

Ruby gem for logging to splunk conforming to sensible defaults
Ruby
1
star
26

elm-css-modules-plugin

Stand-alone babel plugin powering elm-css-modules-loader
TypeScript
1
star
27

nearest-color-shade

JavaScript utility to identify the brand color and tint (white added) or shade (black added) of a CSS color value
JavaScript
1
star
28

poetry-codeartifact-auth

poetry-codeartifact-auth
Python
1
star
29

skip-checkout-buildkite-plugin

Skips git checkout in a Buildkite step.
Shell
1
star
30

devbox-extras

Plugins for internal use at Culture Amp
Shell
1
star
31

elm-upgrade-shims

Culture Amp's Elm 0.18-to-0.19 API shims 🌈
Elm
1
star
32

aws-nquire

Tools to interact with AWS APIs to discover resources
Go
1
star
33

web-team-dotfiles

Culture Amp web team dotfiles
Shell
1
star
34

gostudy

1
star
35

kotlin-kafka-http-sink-connector

A basic Kafka Connect HTTP Sink Connector written in Kotlin
Kotlin
1
star
36

changelog-github

Changelog generator based on @atlassian/changelog-github πŸ¦‹
TypeScript
1
star
37

cfparams

Wrangle parameters for AWS CloudFormation
Go
1
star
38

deploy-templates-buildkite-plugin

A public custom buildkite plugin to handle the deployment configuration
Shell
1
star
39

babel-elm-assets-plugin

A babel plugin that allows you to turn some Elm code into `require()` statements so you can use webpack assets
TypeScript
1
star
40

cultureamp-front-end-example

Example project of a separate Culture Amp front end
JavaScript
1
star
41

step-templates-buildkite-plugin

A Buildkite plugin that allows steps to be injected into the pipeline based on a common template.
Shell
1
star