Skip to content

Commit e304362

Browse files
committed
Fix Text view not responding to DOM changes
1 parent fc1f36c commit e304362

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

Sources/LiveViewNative/BuiltinRegistry.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct BuiltinRegistry {
1919
case "securefield":
2020
SecureField<R>(element: element, context: context)
2121
case "text":
22-
Text(element: element, context: context)
22+
Text(context: context)
2323
case "hstack":
2424
HStack<R>(element: element, context: context)
2525
case "vstack":

Sources/LiveViewNative/Views/Text Input and Output/Text.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,24 @@ fileprivate let dateFormatter: DateFormatter = {
2222
}()
2323

2424
struct Text<R: CustomRegistry>: View {
25-
let element: ElementNode
2625
let context: LiveContext<R>
2726

28-
init(element: ElementNode, context: LiveContext<R>) {
29-
self.element = element
27+
// The view that's in the SwiftUI view tree needs to observe an element to respond to DOM changes,
28+
// but we also need to construct a Text view with a specific element to handle nested <text>s.
29+
// The `element` property returns the effective one, avoiding accessing the @ObservedElement when not
30+
// installed in a view.
31+
@ObservedElement private var observedElement: ElementNode
32+
private var overrideElement: ElementNode?
33+
private var element: ElementNode {
34+
overrideElement ?? observedElement
35+
}
36+
37+
init(context: LiveContext<R>) {
38+
self.context = context
39+
}
40+
41+
private init(overrideElement: ElementNode, context: LiveContext<R>) {
42+
self.overrideElement = overrideElement
3043
self.context = context
3144
}
3245

@@ -119,7 +132,7 @@ struct Text<R: CustomRegistry>: View {
119132
if let element = next.asElement() {
120133
switch element.tag {
121134
case "text":
122-
prev = prev + Self(element: element, context: context).body
135+
prev = prev + Self(overrideElement: element, context: context).body
123136
case "lvn-link":
124137
prev = prev + SwiftUI.Text(
125138
.init("[\(element.innerText())](\(element.attributeValue(for: "destination")!))")

0 commit comments

Comments
 (0)