Comments (3)
I can confirm that
Lit supports this by default in this sample repl
but can you share a more complete example of what you were trying to do? a simple component definition perhaps
from fable.lit.
Yes here a port of the tuturial of Lit.dev
module Lit.TodoMVC.App
open Lit
open Browser.Types
open Browser
open Fable.Core.JsInterop
type ToDoItem =
{ Text: string
mutable Completed: bool }
let styles =
[ css
$"""
.completed {{
text-decoration-line: line-through;
color: #777;
}}""" ]
[<LitElement("todo-list")>]
let ToDoList () =
// This call is obligatory to initialize the web component
let host, props =
LitElement.init
(fun init ->
init.styles <- styles
init.useShadowDom
init.props <-
{| listItems =
Prop.Of(
[ { Text = "Start Lit tutorial"
Completed = true }
{ Text = "Make to-do list"
Completed = false } ]
)
hideCompleted = Prop.Of(false) |})
let input () : HTMLInputElement =
host?renderRoot?querySelector ("#newitem")
let addToDo (event: Event) =
//let input = event.target
//props.firstname.Value <- input.Value
let input = input ()
props.listItems.Value <-
{ Text = input.Value
Completed = false }
:: props.listItems.Value
input.value <- ""
host.requestUpdate ()
let toggleCompleted (item: ToDoItem) =
item.Completed <- not item.Completed
host.requestUpdate ()
let getItemTemplate item =
html
$"""<li class={if item.Completed then
"completed"
else
""}
@click={fun _ -> toggleCompleted (item)}>{item.Text}</li>"""
let setHideCompleted (e: Event) =
props.hideCompleted.Value <- (e.target :?> HTMLInputElement).``checked``
let items =
if props.hideCompleted.Value then
props.listItems.Value
|> List.filter (fun item -> not item.Completed)
else
props.listItems.Value
let caughtUpMessage =
html
$"""
<p>
You're all caught up!
</p>
"""
let todos =
html
$"""<ul>
{items
|> Lit.mapUnique (fun item -> item.Text) getItemTemplate}
<!-- TODO: Render list items. -->
</ul>
"""
let todosOrMessage =
if items.Length > 0 then
todos
else
caughtUpMessage
html
$"""
<h2>To Do</h2>
{todosOrMessage}
<input id="newitem" aria-label="New item">
<button @click={addToDo}>Add</button>
<br>
<label>
<input type="checkbox"
@change={setHideCompleted}
?checked={props.hideCompleted}>
Hide completed
</label>
"""
<!doctype html>
<html lang="en">
<head>
<title>Lit.TodoMVC</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="fable.ico" />
<!-- Used to hide custom components until they are defined and prevent a flash of unstyled content -->
<style>
*:not(:defined) { display:none }
</style>
<style>
body {
font-family: 'Open Sans', sans-serif;
font-size: 1.5em;
padding-left: 0.5em;
}
</style>
</head>
<body>
<script type="module" src="./build/client/App.js"></script>
<todo-list />
</body>
</html>
from fable.lit.
Thanks for pointing this @evilz! You're definitely right. I was using .el
as a "fake" property to access the HTMLElement because it was not easy to make LitElement inherit from it (right now, HTMLElement is defined as a interface in Fable.Browser.Dom), but renderRoot
is a much better choice because it matches Lit documentation and it also gives you directly the shadowRoot when present. I've changed it 👍
from fable.lit.
Related Issues (20)
- Accessing children in custom element HOT 6
- Problem importing raw css into ShadowDom HOT 6
- Class Based API vnext HOT 1
- What does this mean? HOT 4
- Is there similar pattern in Lit that uses React.useContext (Context propagation)? HOT 1
- Hooks.useTransition is leaving transition-enter classname to its next sibling. HOT 4
- Simple SVG is not rendering properly HOT 3
- Lit.mapiUnique HOT 4
- Self closing tag LitComponents are not closing properly if there's an HTML element after it HOT 2
- LitLabs.motion.animate() throws an error HOT 1
- Q: wrapping requirejs amd module (durandaljs interop) HOT 2
- Incongruence in documentation HOT 1
- Update to .net 6 HOT 5
- Use of Hook.useRef HOT 1
- Upgrading to Fable.Lit 1.4.2 causes issues in Elmish types HOT 1
- Astro integration HOT 2
- Update Fable.React and Feliz Bindings HOT 4
- Hook.useTransition with more events
- Hook.Elmish doesn't rerun when props change HOT 2
- hasChanged and currying problems
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fable.lit.