werewolf-ui 0.1.0-beta.2 → 0.1.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -5
- package/data/manifest.json +3779 -484
- package/data/native/android/WerewolfAlert.kt +1 -1
- package/data/native/android/WerewolfAlertDialog.kt +156 -0
- package/data/native/android/WerewolfAvatar.kt +393 -0
- package/data/native/android/WerewolfBackdropBlur.kt +89 -0
- package/data/native/android/WerewolfBadge.kt +1 -1
- package/data/native/android/WerewolfBottomNav.kt +333 -0
- package/data/native/android/WerewolfButton.kt +1 -1
- package/data/native/android/WerewolfButtonGroup.kt +54 -0
- package/data/native/android/WerewolfCard.kt +1 -1
- package/data/native/android/WerewolfCheckbox.kt +387 -0
- package/data/native/android/WerewolfCounter.kt +640 -0
- package/data/native/android/WerewolfDialog.kt +131 -0
- package/data/native/android/WerewolfEmptyState.kt +1 -1
- package/data/native/android/WerewolfField.kt +66 -0
- package/data/native/android/WerewolfIcons.kt +39 -1
- package/data/native/android/WerewolfInput.kt +1 -1
- package/data/native/android/WerewolfKbd.kt +1 -1
- package/data/native/android/WerewolfLabel.kt +2 -2
- package/data/native/android/WerewolfLayers.kt +385 -0
- package/data/native/android/WerewolfLink.kt +163 -0
- package/data/native/android/WerewolfList.kt +504 -0
- package/data/native/android/WerewolfListRuntime.kt +384 -0
- package/data/native/android/WerewolfMetadata.kt +180 -0
- package/data/native/android/WerewolfMeter.kt +189 -0
- package/data/native/android/WerewolfModifiers.kt +58 -4
- package/data/native/android/WerewolfProgress.kt +281 -0
- package/data/native/android/WerewolfRadioGroup.kt +382 -0
- package/data/native/android/WerewolfSelect.kt +330 -0
- package/data/native/android/WerewolfSeparator.kt +1 -1
- package/data/native/android/WerewolfSheet.kt +204 -0
- package/data/native/android/WerewolfSkeleton.kt +1 -1
- package/data/native/android/WerewolfSpinner.kt +1 -1
- package/data/native/android/WerewolfSwitch.kt +9 -57
- package/data/native/android/WerewolfTabs.kt +591 -0
- package/data/native/android/WerewolfTextarea.kt +329 -0
- package/data/native/android/WerewolfTheme.kt +3 -2
- package/data/native/android/WerewolfToast.kt +226 -0
- package/data/native/android/WerewolfToaster.kt +282 -0
- package/data/native/android/WerewolfToggle.kt +395 -0
- package/data/native/android/WerewolfToggleGroup.kt +427 -0
- package/data/native/android/WerewolfToken.kt +1 -1
- package/data/native/ios/WerewolfAlert.swift +1 -1
- package/data/native/ios/WerewolfAlertDialog.swift +185 -0
- package/data/native/ios/WerewolfAvatar.swift +552 -0
- package/data/native/ios/WerewolfBadge.swift +1 -1
- package/data/native/ios/WerewolfBottomNav.swift +422 -0
- package/data/native/ios/WerewolfButton.swift +1 -1
- package/data/native/ios/WerewolfButtonGroup.swift +62 -0
- package/data/native/ios/WerewolfCard.swift +1 -1
- package/data/native/ios/WerewolfCheckbox.swift +621 -0
- package/data/native/ios/WerewolfCounter.swift +768 -0
- package/data/native/ios/WerewolfDialog.swift +170 -0
- package/data/native/ios/WerewolfEmptyState.swift +1 -1
- package/data/native/ios/WerewolfField.swift +103 -0
- package/data/native/ios/WerewolfIcons.swift +13 -1
- package/data/native/ios/WerewolfInput.swift +1 -1
- package/data/native/ios/WerewolfKbd.swift +1 -1
- package/data/native/ios/WerewolfLabel.swift +2 -2
- package/data/native/ios/WerewolfLayers.swift +376 -0
- package/data/native/ios/WerewolfLink.swift +188 -0
- package/data/native/ios/WerewolfList.swift +603 -0
- package/data/native/ios/WerewolfListRuntime.swift +414 -0
- package/data/native/ios/WerewolfMetadata.swift +205 -0
- package/data/native/ios/WerewolfMeter.swift +186 -0
- package/data/native/ios/WerewolfModifiers.swift +106 -1
- package/data/native/ios/WerewolfProgress.swift +338 -0
- package/data/native/ios/WerewolfRadioGroup.swift +483 -0
- package/data/native/ios/WerewolfSelect.swift +418 -0
- package/data/native/ios/WerewolfSeparator.swift +1 -1
- package/data/native/ios/WerewolfSheet.swift +208 -0
- package/data/native/ios/WerewolfSkeleton.swift +1 -1
- package/data/native/ios/WerewolfSpinner.swift +1 -1
- package/data/native/ios/WerewolfSwitch.swift +18 -77
- package/data/native/ios/WerewolfTabs.swift +756 -0
- package/data/native/ios/WerewolfTextarea.swift +403 -0
- package/data/native/ios/WerewolfTheme.swift +1 -1
- package/data/native/ios/WerewolfToast.swift +278 -0
- package/data/native/ios/WerewolfToaster.swift +269 -0
- package/data/native/ios/WerewolfToggle.swift +647 -0
- package/data/native/ios/WerewolfToggleGroup.swift +511 -0
- package/data/native/ios/WerewolfToken.swift +1 -1
- package/dist/cli.mjs +2212 -352
- package/dist/core.mjs +2121 -308
- package/dist/lint.mjs +142 -18
- package/dist/project.mjs +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// Werewolf native runtime. Hand-written, independent of the seed.
|
|
2
|
+
import SwiftUI
|
|
3
|
+
import UIKit
|
|
4
|
+
|
|
5
|
+
public enum WerewolfToastKind {
|
|
6
|
+
case neutral, success, danger, warning, info
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
public struct WerewolfToastItem: Identifiable {
|
|
10
|
+
public let id: String
|
|
11
|
+
public var title: String
|
|
12
|
+
public var description: String?
|
|
13
|
+
public var variant: WerewolfToastKind
|
|
14
|
+
public var actionLabel: String?
|
|
15
|
+
public var action: (() -> Void)?
|
|
16
|
+
public var dismissible: Bool
|
|
17
|
+
public var duration: TimeInterval
|
|
18
|
+
var closing = false
|
|
19
|
+
|
|
20
|
+
public init(
|
|
21
|
+
id: String,
|
|
22
|
+
title: String,
|
|
23
|
+
description: String? = nil,
|
|
24
|
+
variant: WerewolfToastKind = .neutral,
|
|
25
|
+
actionLabel: String? = nil,
|
|
26
|
+
action: (() -> Void)? = nil,
|
|
27
|
+
dismissible: Bool = true,
|
|
28
|
+
duration: TimeInterval
|
|
29
|
+
) {
|
|
30
|
+
self.id = id
|
|
31
|
+
self.title = title
|
|
32
|
+
self.description = description
|
|
33
|
+
self.variant = variant
|
|
34
|
+
self.actionLabel = actionLabel
|
|
35
|
+
self.action = action
|
|
36
|
+
self.dismissible = dismissible
|
|
37
|
+
self.duration = duration
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public final class WerewolfToaster: ObservableObject {
|
|
42
|
+
public static let stackScale: CGFloat = 0.05
|
|
43
|
+
static let actionDurationFactor: Double = 2
|
|
44
|
+
|
|
45
|
+
@Published public private(set) var items: [WerewolfToastItem] = []
|
|
46
|
+
public var maxVisible: Int
|
|
47
|
+
public let defaultDuration: TimeInterval
|
|
48
|
+
// Set to draw each toast with the generated WerewolfToast surface instead of the built-in card.
|
|
49
|
+
public var surface: ((WerewolfToastItem, @escaping () -> Void) -> AnyView)?
|
|
50
|
+
var exitDelay: TimeInterval = 0
|
|
51
|
+
|
|
52
|
+
private var waiting: [WerewolfToastItem] = []
|
|
53
|
+
private var timers: [String: DispatchWorkItem] = [:]
|
|
54
|
+
private var remaining: [String: TimeInterval] = [:]
|
|
55
|
+
private var startedAt: [String: Date] = [:]
|
|
56
|
+
private var paused = false
|
|
57
|
+
|
|
58
|
+
public init(maxVisible: Int = 3, defaultDuration: TimeInterval = 4) {
|
|
59
|
+
self.maxVisible = maxVisible
|
|
60
|
+
self.defaultDuration = defaultDuration
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
@discardableResult
|
|
64
|
+
public func show(
|
|
65
|
+
id: String = UUID().uuidString,
|
|
66
|
+
title: String,
|
|
67
|
+
description: String? = nil,
|
|
68
|
+
variant: WerewolfToastKind = .neutral,
|
|
69
|
+
actionLabel: String? = nil,
|
|
70
|
+
action: (() -> Void)? = nil,
|
|
71
|
+
dismissible: Bool = true,
|
|
72
|
+
duration: TimeInterval? = nil
|
|
73
|
+
) -> String {
|
|
74
|
+
let base = duration ?? (actionLabel == nil ? defaultDuration : defaultDuration * WerewolfToaster.actionDurationFactor)
|
|
75
|
+
let item = WerewolfToastItem(id: id, title: title, description: description, variant: variant, actionLabel: actionLabel, action: action, dismissible: dismissible, duration: base)
|
|
76
|
+
if let index = items.firstIndex(where: { $0.id == id }) {
|
|
77
|
+
items[index] = item
|
|
78
|
+
schedule(item)
|
|
79
|
+
} else if let index = waiting.firstIndex(where: { $0.id == id }) {
|
|
80
|
+
waiting[index] = item
|
|
81
|
+
} else {
|
|
82
|
+
waiting.append(item)
|
|
83
|
+
fill()
|
|
84
|
+
}
|
|
85
|
+
announce(item)
|
|
86
|
+
return id
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public func dismiss(_ id: String) {
|
|
90
|
+
waiting.removeAll { $0.id == id }
|
|
91
|
+
cancel(id)
|
|
92
|
+
guard let index = items.firstIndex(where: { $0.id == id }) else { return }
|
|
93
|
+
items[index].closing = true
|
|
94
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + exitDelay) { [weak self] in
|
|
95
|
+
self?.items.removeAll { $0.id == id }
|
|
96
|
+
self?.fill()
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
func pause() {
|
|
101
|
+
paused = true
|
|
102
|
+
for (id, began) in startedAt {
|
|
103
|
+
remaining[id] = max(0, (remaining[id] ?? 0) - Date().timeIntervalSince(began))
|
|
104
|
+
timers[id]?.cancel()
|
|
105
|
+
}
|
|
106
|
+
timers.removeAll()
|
|
107
|
+
startedAt.removeAll()
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
func resume() {
|
|
111
|
+
paused = false
|
|
112
|
+
for item in items where !item.closing && item.duration > 0 {
|
|
113
|
+
start(item.id, after: remaining[item.id] ?? item.duration)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
private func fill() {
|
|
118
|
+
while items.filter({ !$0.closing }).count < maxVisible, !waiting.isEmpty {
|
|
119
|
+
let item = waiting.removeFirst()
|
|
120
|
+
items.append(item)
|
|
121
|
+
schedule(item)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
private func schedule(_ item: WerewolfToastItem) {
|
|
126
|
+
cancel(item.id)
|
|
127
|
+
remaining[item.id] = item.duration
|
|
128
|
+
guard item.duration > 0, !paused else { return }
|
|
129
|
+
start(item.id, after: item.duration)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private func start(_ id: String, after seconds: TimeInterval) {
|
|
133
|
+
let work = DispatchWorkItem { [weak self] in self?.dismiss(id) }
|
|
134
|
+
timers[id] = work
|
|
135
|
+
startedAt[id] = Date()
|
|
136
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + seconds, execute: work)
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private func cancel(_ id: String) {
|
|
140
|
+
timers[id]?.cancel()
|
|
141
|
+
timers[id] = nil
|
|
142
|
+
startedAt[id] = nil
|
|
143
|
+
remaining[id] = nil
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
private func announce(_ item: WerewolfToastItem) {
|
|
147
|
+
let text = [item.title, item.description].compactMap { $0 }.joined(separator: ". ")
|
|
148
|
+
UIAccessibility.post(notification: .announcement, argument: text)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private struct WerewolfToasterKey: EnvironmentKey {
|
|
153
|
+
static let defaultValue = WerewolfToaster()
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
extension EnvironmentValues {
|
|
157
|
+
public var werewolfToaster: WerewolfToaster {
|
|
158
|
+
get { self[WerewolfToasterKey.self] }
|
|
159
|
+
set { self[WerewolfToasterKey.self] = newValue }
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
struct WerewolfToastStack: View {
|
|
164
|
+
@Environment(\.werewolfTheme) private var theme
|
|
165
|
+
@Environment(\.scenePhase) private var scenePhase
|
|
166
|
+
private let motion = WerewolfMotion()
|
|
167
|
+
@ObservedObject var toaster: WerewolfToaster
|
|
168
|
+
let bottomInset: CGFloat
|
|
169
|
+
@State private var touching = false
|
|
170
|
+
|
|
171
|
+
var body: some View {
|
|
172
|
+
let count = toaster.items.count
|
|
173
|
+
ZStack(alignment: .bottom) {
|
|
174
|
+
ForEach(Array(toaster.items.enumerated()), id: \.element.id) { index, item in
|
|
175
|
+
WerewolfToastCard(item: item, surface: toaster.surface, depth: count - 1 - index, touching: $touching) { toaster.dismiss(item.id) }
|
|
176
|
+
.zIndex(Double(index))
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .bottom)
|
|
180
|
+
.padding(.bottom, theme.spacingLg + bottomInset)
|
|
181
|
+
.animation(motion.animation(theme.easeStandard, duration: theme.durationMedium), value: bottomInset)
|
|
182
|
+
.onAppear { toaster.exitDelay = WerewolfLayerMotion.exitDuration(.toast, theme: theme) }
|
|
183
|
+
.onChange(of: scenePhase) { phase in
|
|
184
|
+
if phase == .active && !touching { toaster.resume() } else { toaster.pause() }
|
|
185
|
+
}
|
|
186
|
+
.onChange(of: touching) { isTouching in
|
|
187
|
+
if isTouching { toaster.pause() } else if scenePhase == .active { toaster.resume() }
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
private struct WerewolfToastCard: View {
|
|
193
|
+
@Environment(\.werewolfTheme) private var theme
|
|
194
|
+
@Environment(\.werewolfIcons) private var icons
|
|
195
|
+
@State private var appeared = false
|
|
196
|
+
@State private var drag: CGFloat = 0
|
|
197
|
+
let item: WerewolfToastItem
|
|
198
|
+
let surface: ((WerewolfToastItem, @escaping () -> Void) -> AnyView)?
|
|
199
|
+
let depth: Int
|
|
200
|
+
@Binding var touching: Bool
|
|
201
|
+
let dismiss: () -> Void
|
|
202
|
+
|
|
203
|
+
private var shown: Bool { appeared && !item.closing }
|
|
204
|
+
|
|
205
|
+
@ViewBuilder private var card: some View {
|
|
206
|
+
if let surface {
|
|
207
|
+
surface(item, dismiss)
|
|
208
|
+
} else {
|
|
209
|
+
builtIn
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
private var builtIn: some View {
|
|
214
|
+
HStack(alignment: .top, spacing: theme.spacingSm) {
|
|
215
|
+
VStack(alignment: .leading, spacing: theme.spacingXs) {
|
|
216
|
+
Text(item.title)
|
|
217
|
+
.werewolfFont(.sans, size: theme.fontSizeSm, weight: .medium)
|
|
218
|
+
.foregroundStyle(theme.colorTextPrimary)
|
|
219
|
+
if let description = item.description {
|
|
220
|
+
Text(description)
|
|
221
|
+
.werewolfFont(.sans, size: theme.fontSizeSm, weight: .regular)
|
|
222
|
+
.foregroundStyle(theme.colorTextSecondary)
|
|
223
|
+
}
|
|
224
|
+
if let label = item.actionLabel, let action = item.action {
|
|
225
|
+
Button(label) {
|
|
226
|
+
action()
|
|
227
|
+
dismiss()
|
|
228
|
+
}
|
|
229
|
+
.werewolfFont(.sans, size: theme.fontSizeSm, weight: .medium)
|
|
230
|
+
.foregroundStyle(theme.colorTextPrimary)
|
|
231
|
+
.werewolfHitArea()
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
Spacer(minLength: 0)
|
|
235
|
+
if item.dismissible {
|
|
236
|
+
Button(action: dismiss) { icons.x.foregroundStyle(theme.colorIconSecondary) }
|
|
237
|
+
.accessibilityLabel("Dismiss")
|
|
238
|
+
.werewolfHitArea()
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
.padding(theme.spacingLg)
|
|
242
|
+
.frame(maxWidth: .infinity, alignment: .leading)
|
|
243
|
+
.background(RoundedRectangle(cornerRadius: theme.radiusContainer).fill(theme.colorBackgroundRaised))
|
|
244
|
+
.overlay(RoundedRectangle(cornerRadius: theme.radiusContainer).strokeBorder(theme.colorBorder, lineWidth: theme.borderWidth))
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
var body: some View {
|
|
248
|
+
card
|
|
249
|
+
.frame(maxWidth: theme.sizePanelMd)
|
|
250
|
+
.padding(.horizontal, theme.spacingLg)
|
|
251
|
+
.scaleEffect(1 - CGFloat(depth) * WerewolfToaster.stackScale)
|
|
252
|
+
.offset(y: drag - CGFloat(depth) * theme.spacingSm)
|
|
253
|
+
.modifier(WerewolfLayerAppearance(surface: .toast, shown: shown))
|
|
254
|
+
.gesture(
|
|
255
|
+
DragGesture()
|
|
256
|
+
.onChanged { value in
|
|
257
|
+
touching = true
|
|
258
|
+
drag = max(0, value.translation.height)
|
|
259
|
+
}
|
|
260
|
+
.onEnded { value in
|
|
261
|
+
touching = false
|
|
262
|
+
if value.translation.height > theme.sizeTargetMin { dismiss() } else { drag = 0 }
|
|
263
|
+
}
|
|
264
|
+
)
|
|
265
|
+
.accessibilityElement(children: .combine)
|
|
266
|
+
.accessibilityAction(.escape) { dismiss() }
|
|
267
|
+
.onAppear { appeared = true }
|
|
268
|
+
}
|
|
269
|
+
}
|