Skip to content

Commit e4f5a39

Browse files
committed
Initial commit
0 parents  commit e4f5a39

File tree

10 files changed

+144
-0
lines changed

10 files changed

+144
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.cache
2+
bower_components
3+
dist
4+
node_modules
5+
6+
.spago
7+
generated-docs
8+
output

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2020, Nonbili Inc.
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

example/index.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<h1>Use Halogen component as a custom element</h1>
2+
3+
<p>The following text is rendered by a Halogen component</p>
4+
5+
<halogen-hello></halogen-hello>
6+
7+
<script src="main.js"></script>

example/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import Main from "./output/Main";
2+
3+
Main.main();

example/packages.dhall

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let upstream =
2+
https://github.com/purescript/package-sets/releases/download/psc-0.13.6-20200127/packages.dhall sha256:06a623f48c49ea1c7675fdf47f81ddb02ae274558e29f511efae1df99ea92fb8
3+
4+
let overrides = {=}
5+
6+
let additions = {=}
7+
8+
in upstream // overrides // additions

example/spago.dhall

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{ name = "purescript-halogen-custom-element"
2+
, dependencies = [ "console", "effect", "halogen" ]
3+
, packages = ./packages.dhall
4+
, sources = [ "../src/**/*.purs", "src/**/*.purs" ]
5+
}

example/src/Hello.purs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Hello
2+
( Message(..)
3+
, Query
4+
, Action
5+
, component
6+
) where
7+
8+
import Prelude
9+
10+
import Data.Const (Const)
11+
import Effect.Aff (Aff)
12+
import Halogen as H
13+
import Halogen.HTML as HH
14+
15+
type Message = Void
16+
17+
type Query = Const Void
18+
19+
type Action = Void
20+
21+
type HTML = H.ComponentHTML Action () Aff
22+
23+
type DSL = H.HalogenM State Action () Message Aff
24+
25+
type State = Unit
26+
27+
initialState :: State
28+
initialState = unit
29+
30+
render :: State -> HTML
31+
render state =
32+
HH.text "hello"
33+
34+
component :: H.Component HH.HTML Query Unit Message Aff
35+
component = H.mkComponent
36+
{ initialState: const initialState
37+
, render
38+
, eval: H.mkEval $ H.defaultEval
39+
}

example/src/Main.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Main where
2+
3+
import Prelude
4+
5+
import Effect (Effect)
6+
import Halogen.CustomElement as CustomElement
7+
import Hello (component)
8+
9+
main :: Effect Unit
10+
main = do
11+
CustomElement.define "halogen-hello" component

src/CustomElement.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
exports.define_ = name => render => () => {
2+
class HalogenElement extends HTMLElement {
3+
connectedCallback() {
4+
render(this)();
5+
}
6+
}
7+
customElements.define(name, HalogenElement);
8+
};

src/CustomElement.purs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Halogen.CustomElement
2+
( define
3+
) where
4+
5+
import Prelude
6+
7+
import Effect (Effect)
8+
import Effect.Aff (Aff)
9+
import Halogen as H
10+
import Halogen.Aff as HA
11+
import Halogen.HTML as HH
12+
import Halogen.VDom.Driver as Driver
13+
import Web.HTML (HTMLElement)
14+
15+
foreign import define_ :: String -> (HTMLElement -> Effect Unit) -> Effect Unit
16+
17+
define
18+
:: forall query output
19+
. String
20+
-> H.Component HH.HTML query Unit output Aff
21+
-> Effect Unit
22+
define name component = do
23+
define_ name render
24+
where
25+
render el =
26+
HA.runHalogenAff $ Driver.runUI component unit el

0 commit comments

Comments
 (0)