Skip to content
Open
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
c280d86
Fix a typo
BillHally Apr 11, 2022
82c3286
add styles in the Lit functions
AngelMunoz May 17, 2022
4560f60
Remove mention to --runFast in docs
alfonsogarciacaro May 27, 2022
6317d3a
Solutions for #31 and #39
alfonsogarciacaro May 27, 2022
8ae1d78
add extra types to LitElement
AngelMunoz May 29, 2022
33f1d2e
add computation expressions for Class LitElements
AngelMunoz May 29, 2022
def3e96
add class based examples
AngelMunoz May 29, 2022
a143bc0
update to .NET6
AngelMunoz May 30, 2022
cfa578d
update local tools
AngelMunoz May 30, 2022
6835f81
re-order things a bit
AngelMunoz May 30, 2022
30b287e
fix format and simplify CEs
AngelMunoz May 30, 2022
c973f2c
fix sample usage
AngelMunoz May 30, 2022
f218ffe
inline CE, address pr comments
AngelMunoz May 30, 2022
0ff2c7a
fix reactive controller call
AngelMunoz May 30, 2022
a2cecec
update definitions
AngelMunoz May 30, 2022
e999185
update lit version
AngelMunoz May 31, 2022
b4e4872
fix pr comments
AngelMunoz May 31, 2022
f33299f
add full bindings to directives
AngelMunoz May 31, 2022
6c1629c
add experimental module
AngelMunoz May 31, 2022
4d989f0
remove decorator experiment
AngelMunoz Jun 2, 2022
eea6e79
add elmish controller test
AngelMunoz Jun 2, 2022
a09642a
enable controllers + hook.GetController
AngelMunoz Jun 2, 2022
6de1489
Fixing self closing web component tags
leolorenzoluis Jul 7, 2022
2277f3a
Added Lit.mapiUnique
leolorenzoluis Jul 7, 2022
b08eb88
Publish 1.4.2
alfonsogarciacaro Jul 14, 2022
3e29301
Update templates.md
alfonsogarciacaro Jul 25, 2022
52275d9
Merge branch 'master' into experimental-class-bindings
AngelMunoz Jul 30, 2022
e27e10c
Merge branch 'master' into experimental-class-bindings
AngelMunoz Jul 30, 2022
d3da340
Update hook-components.md
JordanMarr Aug 1, 2022
2d99bad
Update hook-components.md
alfonsogarciacaro Aug 22, 2022
4cb291d
Fix #67
alfonsogarciacaro Aug 23, 2022
70937c2
Fix Github actions
alfonsogarciacaro Sep 5, 2022
7c04731
Merge branch 'master' into experimental-class-bindings
AngelMunoz Sep 14, 2022
3cccb17
change the name for consistency
AngelMunoz Sep 14, 2022
9a66338
add class docs
AngelMunoz Sep 14, 2022
c9bd281
add controllers docs
AngelMunoz Sep 14, 2022
0168858
add new pages to the menu
AngelMunoz Sep 14, 2022
e18f8b8
remove the effect controller
AngelMunoz Sep 14, 2022
379fdc1
fix the compilation issue
AngelMunoz Sep 14, 2022
028629d
add doc strings
AngelMunoz Sep 14, 2022
c241b13
add a couple more comments
AngelMunoz Sep 14, 2022
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
10 changes: 8 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
"isRoot": true,
"tools": {
"fable": {
"version": "3.6.1",
"version": "3.7.12",
"commands": [
"fable"
]
},
"femto": {
"version": "0.9.0",
"version": "0.13.0",
"commands": [
"femto"
]
},
"fantomas": {
"version": "5.0.0-alpha-008",
"commands": [
"fantomas"
]
}
}
}
10 changes: 5 additions & 5 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "5.0.300",
"rollForward": "minor"
}
}
"sdk": {
"version": "6.0.200",
"rollForward": "latestMinor"
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"docs:deploy": "npm run docs && gh-pages -d build/docs"
},
"dependencies": {
"lit": "^2.0.0",
"lit": "^2.2.5",
"react": "^17.0.2",
"react-dom": "^17.0.2"
},
Expand All @@ -29,4 +29,4 @@
"vite": "^2.6.14",
"vscode-theme-onelight": "github:akamud/vscode-theme-onelight"
}
}
}
92 changes: 77 additions & 15 deletions sample/App.fs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
module Sample.App

open Browser
open Fable.Core
open Elmish
open Elmish.HMR
open Lit
open Lit.Elmish
open Components

let private hmr = HMR.createToken()
let private hmr = HMR.createToken ()

Clock.register()
Clock.register ()

let init() =
let init () =
{ Value = "World"
ShowClock = true
ShowReact = true }, Cmd.none
ShowReact = true },
Cmd.none

let update msg model =
match msg with
Expand All @@ -23,24 +25,36 @@ let update msg model =
| ToggleReact v -> { model with ShowReact = v }, Cmd.none

let view model dispatch =
html $"""
html
$"""
<div class="vertical-container" style="margin-left: 2rem;">

<button class="button"
style="margin: 1rem 0"
@click={Ev(fun _ -> not model.ShowReact |> ToggleReact |> dispatch)}>
{if model.ShowReact then "Hide" else "Show"} React
{if model.ShowReact then
"Hide"
else
"Show"} React
</button>

{if not model.ShowReact then Lit.nothing
else ReactLitComponent model.ShowClock}
{if not model.ShowReact then
Lit.nothing
else
ReactLitComponent model.ShowClock}

<br />

{elmishNameInput model.Value (ChangeValue >> dispatch)}
{LocalNameInput()}
<hello-there .name={model.Value}></hello-there>
<hello-there-2></hello-there-2>

{ClockDisplay model dispatch}

<user-profile .name={model.Value} age="25"></user-profile>

<element-with-controller><element-with-controller>
</div>
"""

Expand All @@ -49,10 +63,58 @@ let view model dispatch =
// |> Program.run

[<LitElement("sample-app")>]
let App() =
Hook.useHmr(hmr)
let _ = LitElement.init(fun config ->
config.useShadowDom <- false
)
let model, dispatch = Hook.useElmish(init, update)
view model dispatch
let App () =
Hook.useHmr (hmr)
let _ = LitElement.init (fun config -> config.useShadowDom <- false)
let model, dispatch = Hook.useElmish (init, update)
view model dispatch

open Experimental

[<LitElementExperimental(true)>]
let Sample () =
let name = Controllers.GetProperty("name", "Peter")

let state: StateController<int> =
Controllers.GetController<StateController<int>>(10)

let twoArgs = Controllers.GetController<TwoArgsCtrl>(10, 20)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking on something like this to prevent having the LitElement generic initialization, we still do magic, but more javascrip'ty magic (or that's what I want to believe) in the end these are just properties that are in the class that is being used behind this function to use lit's mechanisms without diving too much into directive territory

Side note perhaps Context would be a better name rather than Controllers



let inline updateValues _ =
state.SetState(state.Value + 1)
let (initial, secondial) = twoArgs.Values
twoArgs.updateValues ((initial + 1, secondial + 2))


html $"<p>{state.Value}, {twoArgs.Values}, {name}</p> <button @click={updateValues}>Increment</button>"


registerFuncElement ("hello-there", Sample) {
css $"p {{ color: blue; }}"

property "name" {
use_attribute
use_type PropType.String
}
}
// registerFuncElement ("hello-there-2", Sample2) { css $"p {{ color: blue; }}" }

registerElement<ClassComponents.UserProfile> "user-profile" {
property "name" {
use_attribute
use_type PropType.String
}

property "age" {
use_attribute
use_type PropType.Number
}

css $"p {{ color: red; }}"
}

registerElement<ClassComponents.ElementWithController> "element-with-controller" {
css $"p {{ color: red; }}"
css $"li {{ color: rebeccapurple; }}"
Copy link
Copy Markdown
Member

@alfonsogarciacaro alfonsogarciacaro May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit concerned that the class and the registration (with the props, styles...) are so far away and the compiler cannot tell you when you misspell a prop name. But let's see how it goes (it's true the compiler doesn't check the prop names in the html templates either).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is too far fetched from what current lit users do, but I share this concern arguably I think it would be nice if we stick slightly more close to lit's semantics so people don't have to learn Fable.Lit + Lit

}
76 changes: 76 additions & 0 deletions sample/ClassComponents.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
module ClassComponents

open Lit
open Fable.Core
open Fetch
open Fable.Core.JsInterop

[<ImportMember("./controllers.js")>]
type MouseController(host) =
inherit ReactiveController(host)
member _.x: float = jsNative
member _.y: float = jsNative

type Post =
{| usedId: int
id: int
title: string
body: string |}

type ApiController(host: LitElement) =
inherit ReactiveController(host)

member val Posts: Post array = Array.empty with get, set
member val Page: int = 1 with get, set
member val Limit: int = 10 with get, set

member this.FetchUsers(?page: int, ?limit: int) =
let page = defaultArg page this.Page
let limit = defaultArg limit this.Limit

promise {
let! posts =
promise {
let! result = tryFetch $"https://jsonplaceholder.typicode.com/posts?_page={page}&_limit={limit}" []

match result with
| Ok res ->
let! result = res.json () :?> JS.Promise<Post array>
return result
| Error err -> return Array.empty<Post>
}

this.Posts <- posts
host.requestUpdate ()
}

member this.NextPage() =
this.Page <- this.Page + 1
this.FetchUsers() |> Promise.start

override this.hostConnected() : unit = this.FetchUsers() |> Promise.start

type UserProfile() =
inherit LitElement()

let name = ""
let age = 0

override _.render() = html $"<p>{name} - {age}</p>"

type ElementWithController() =
inherit LitElement()

let mouse = MouseController jsThis
let api = ApiController jsThis

let posts (post: Post) = html $"<li>{post.title}</li>"

override _.render() =
html
$"""
<p>{mouse.x} - {mouse.y}</p>
<p>Page: {api.Page}</p>
<button @click={fun _ -> api.NextPage()}>Next Page</button>
<ul>{Lit.mapUnique (fun (p: Post) -> $"{p.id}") posts api.Posts}</li>
"""
18 changes: 8 additions & 10 deletions sample/Clock.fs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ open Lit.Elmish
module Helpers =
let hmr = HMR.createToken()

type MouseController =
inherit ReactiveController
abstract x: float
abstract y: float

[<Import("MouseController", from="./controllers.js"); Emit("new $0($1)")>]
let createMouseController (host: LitElement): MouseController = jsNative
[<ImportMember("./controllers.js")>]
type MouseController(host) =
inherit ReactiveController(host)
member _.x: float = jsNative
member _.y: float = jsNative

type Time =
| Hour of int
Expand Down Expand Up @@ -123,12 +121,12 @@ let clockHand (color: string) (time: Time) =

let select options selected dispatch =
let option value =
html $"""<option ?selected={value = selected} value={value}>{value}</option>"""
html $"""<option ?selected={value = selected} .value={value}>{value}</option>"""

html $"""
<div class="select mb-2">
<select @change={EvVal dispatch}>
{options |> List.map option}
{Lit.mapUnique id option options}
</select>
</div>
"""
Expand All @@ -137,7 +135,7 @@ let initEl (config: LitConfig<_,_>) =
let split (str: string) =
str.Split(',') |> Array.map (fun x -> x.Trim()) |> Array.toList

config.controllers <- {| mouse = Controller.Of(createMouseController) |}
config.controllers <- {| mouse = Controller.Of(MouseController) |}

config.props <-
{|
Expand Down
4 changes: 3 additions & 1 deletion sample/Sample.fsproj
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="ClassComponents.fs" />
<Compile Include="Clock.fs" />
<Compile Include="Types.fs" />
<Compile Include="Components.fs" />
Expand All @@ -16,6 +17,7 @@
<!-- <PackageReference Include="Fable.Lit.Elmish" Version="1.0.0-rc-001" /> -->
<!-- <PackageReference Include="Fable.Lit.React" Version="1.0.0-rc-001" /> -->
<PackageReference Include="Fable.Elmish.HMR" Version="4.3.1" />
<PackageReference Include="Fable.Fetch" Version="2.4.0" />
<PackageReference Include="Feliz" Version="1.52.0" />
</ItemGroup>
</Project>
Loading