Skip to content

Commit

Permalink
Remove Fragment from sketch_lustre
Browse files Browse the repository at this point in the history
  • Loading branch information
ghivert committed Nov 6, 2024
1 parent 17d6841 commit 8cf74e5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 38 deletions.
20 changes: 19 additions & 1 deletion sketch_lustre/src/sketch/internals/ffi.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import gleam/dynamic.{type Dynamic}
import plinth/browser/shadow.{type ShadowRoot}

pub opaque type Mutable(a) {
Mutable(value: a)
}
Expand All @@ -8,11 +11,26 @@ pub fn wrap(value: a) -> Mutable(a) {
}

@external(javascript, "../../sketch_lustre.ffi.mjs", "set")
pub fn set(variable: Mutable(a), value: a) -> Mutable(a) {
pub fn set(_variable: Mutable(a), value: a) -> Mutable(a) {
Mutable(value: value)
}

@external(javascript, "../../sketch_lustre.ffi.mjs", "get")
pub fn get(variable: Mutable(a)) -> a {
variable.value
}

@external(javascript, "../../sketch_lustre.ffi.mjs", "createCssStyleSheet")
pub fn create_document_stylesheet() -> Dynamic {
dynamic.from(0)
}

@external(javascript, "../../sketch_lustre.ffi.mjs", "createCssStyleSheet")
pub fn create_shadow_root_stylesheet(_root: ShadowRoot) -> Dynamic {
dynamic.from(0)
}

@external(javascript, "../../sketch_lustre.ffi.mjs", "setStylesheet")
pub fn set_stylesheet(_content: String, _stylesheet: Dynamic) -> Nil {
Nil
}
20 changes: 5 additions & 15 deletions sketch_lustre/src/sketch/lustre.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@ import sketch.{type Cache}
import sketch/internals/ffi
import sketch/lustre/element

@external(javascript, "../sketch_lustre.ffi.mjs", "createCssStyleSheet")
fn create_document_stylesheet() -> Dynamic

@external(javascript, "../sketch_lustre.ffi.mjs", "createCssStyleSheet")
fn create_shadow_root_stylesheet(root: ShadowRoot) -> Dynamic

@external(javascript, "../sketch_lustre.ffi.mjs", "setStylesheet")
fn set_stylesheet(content: String, stylesheet: Dynamic) -> Nil {
Nil
}

type StyleSheetOption {
Node
Document
Expand Down Expand Up @@ -57,16 +46,17 @@ pub fn compose(
}

@target(erlang)
fn to_stylesheet(options) {
fn to_stylesheet(_options) {
NodeStyleSheet
}

@target(javascript)
fn to_stylesheet(options) {
case options {
Options(Node) -> NodeStyleSheet
Options(Document) -> CssStyleSheet(create_document_stylesheet())
Options(Shadow(root)) -> CssStyleSheet(create_shadow_root_stylesheet(root))
Options(Document) -> CssStyleSheet(ffi.create_document_stylesheet())
Options(Shadow(root)) ->
CssStyleSheet(ffi.create_shadow_root_stylesheet(root))
}
}

Expand All @@ -81,7 +71,7 @@ fn render_stylesheet(content, node, stylesheet) {
}
}
CssStyleSheet(stylesheet) -> {
set_stylesheet(content, stylesheet)
ffi.set_stylesheet(content, stylesheet)
node
}
}
Expand Down
24 changes: 2 additions & 22 deletions sketch_lustre/src/sketch/lustre/element.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import gleam/int
import gleam/list
import gleam/option
import gleam/pair
Expand All @@ -10,7 +9,6 @@ import sketch.{type Cache}
pub opaque type Element(msg) {
Nothing
Text(content: String)
Fragment(key: String, children: List(Element(msg)))
Map(subtree: fn() -> Element(msg))
Element(
key: String,
Expand Down Expand Up @@ -69,7 +67,8 @@ pub fn namespaced_(
}

pub fn fragment(children: List(Element(msg))) {
Fragment("", children)
let attrs = [attribute.style([#("display", "contents")])]
Element("", "", "lustre-fragment", option.None, attrs, children)
}

pub fn keyed(
Expand All @@ -87,21 +86,6 @@ fn do_keyed(element: Element(msg), key: String) {
Nothing -> Nothing
Text(content) -> Text(content)
Map(subtree) -> Map(fn() { do_keyed(subtree(), key) })
Fragment(_, children) ->
children
|> list.index_map(fn(element, idx) {
case element {
Element(el_key, _, _, _, _, _) -> {
let new_key = case el_key {
"" -> key <> "-" <> int.to_string(idx)
_ -> key <> "-" <> el_key
}
do_keyed(element, new_key)
}
_ -> do_keyed(element, key)
}
})
|> Fragment(key, _)
Element(_, namespace, tag, attributes, children, styles) ->
Element(key, namespace, tag, attributes, children, styles)
}
Expand All @@ -112,7 +96,6 @@ pub fn map(element: Element(a), mapper: fn(a) -> b) {
Nothing -> Nothing
Text(content) -> Text(content)
Map(subtree) -> Map(fn() { map(subtree(), mapper) })
Fragment(key, children) -> Fragment(key, list.map(children, map(_, mapper)))
Element(key, namespace, tag, class, attributes, children) -> {
let attributes = list.map(attributes, attribute.map(_, mapper))
let children = list.map(children, map(_, mapper))
Expand All @@ -137,9 +120,6 @@ pub fn unstyled(cache: Cache, element: Element(msg)) {
Nothing -> #(cache, el.none())
Text(content) -> #(cache, el.text(content))
Map(subtree) -> unstyled(cache, subtree())
Fragment(_, children) ->
unstyled_children(cache, children)
|> pair.map_second(fn(node) { el.fragment(node) })
Element(key, namespace, tag, class, attributes, children) -> {
let class = option.map(class, sketch.class_name(_, cache))
let class_name = option.map(class, pair.second)
Expand Down

0 comments on commit 8cf74e5

Please sign in to comment.