Skip to content

Commit 307d322

Browse files
authored
Add public _ModifierStack type (#998)
* Make BuiltinRegistry and ModifierContainer public * Revert "Make BuiltinRegistry and ModifierContainer public" This reverts commit 4b68667. * Add public `_ModifierStack` type * Convert to ViewModifier
1 parent 962a487 commit 307d322

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// ModifierStack.swift
3+
//
4+
//
5+
// Created by Carson Katri on 6/29/23.
6+
//
7+
8+
import SwiftUI
9+
10+
/// A container that decodes a modifier stack.
11+
///
12+
/// Use this as a modifier:
13+
///
14+
/// ```swift
15+
/// content
16+
/// .modifier(modifierStack)
17+
/// ```
18+
public struct _ModifierStack<R: RootRegistry>: Decodable, ViewModifier {
19+
private let stack: [ModifierContainer<R>]
20+
21+
@ObservedElement private var element
22+
@LiveContext<R> private var context
23+
24+
public init(from decoder: Decoder) throws {
25+
let container = try decoder.singleValueContainer()
26+
self.stack = try container.decode([ModifierContainer<R>].self)
27+
}
28+
29+
public func body(content: Content) -> some View {
30+
content
31+
.applyModifiers(stack[...], element: element, context: context.storage)
32+
}
33+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
defmodule LiveViewNativeSwiftUi.Types.ModifierStack do
2+
use LiveViewNativePlatform.Modifier.Type
3+
def type, do: :map
4+
5+
def cast(%LiveViewNativeSwiftUi.Modifiers{} = value), do: {:ok, Jason.decode!(Jason.encode!(value))}
6+
def cast(_), do: :error
7+
end

0 commit comments

Comments
 (0)