// No bottom sheets (row E of the deck faces, 23 Oct 2026; the owner: sheets are// out of the design). What a sheet carried — a form, a picker, a flow — opens// as a CARD: the sheet's own cream paper cut to a card standing in the middle// of the page it was opened from, the page under it dimmed by `page_dim` and// closed to a tree reader, the X at the card's head (`SheetPaper.close*`),// and a tap on the dim closing it unless the card holds (a recording).//// A card is a LAYER raised as a preference, as a page's own page is// (`PageDestination.swift`): the presenter's environment travels with it, and// it stands over the nearest card host — every page surface is one// (`pageSurface()`), the window is the last (`RootView`), and each card is a// host for the card it opens. A card's content closes it with `closeCard`// (`CardClosing`), which a view that once called `dismiss()` reads first.importSwiftUI/// One card a screen opened, raised to its host.structCardLayer {let id: Stringlet content: AnyView/// The environment of the screen that opened it.let environment: EnvironmentValueslet detents: Set<CottonDetent>/// A tap on the dim leaves it up: only its own controls close it.let holds: Boollet close: @MainActor () -> Void}structCardLayerKey: PreferenceKey {staticvar defaultValue: [CardLayer] { [] }staticfunc reduce(value: inout [CardLayer], nextValue: () -> [CardLayer]) { value.append(contentsOf: nextValue()) }}privatestructCloseCardKey: EnvironmentKey {staticlet defaultValue: (@MainActor () -> Void)? = nil}privatestructCardHoldsKey: EnvironmentKey {staticlet defaultValue = false}extensionEnvironmentValues {/// Closes the card this view stands on; nil off a card.var closeCard: (@MainActor () -> Void)? { get { self[CloseCardKey.self] } set { self[CloseCardKey.self] = newValue } }/// The card this view stands on holds: it has no X, and only its own/// controls close it.var cardHolds: Bool { get { self[CardHoldsKey.self] } set { self[CardHoldsKey.self] = newValue } }}/// How a view that once dismissed its sheet closes: the card it stands on,/// else the presentation or page it is in.enumCardClosing { @MainActor staticfunc close(_ card: (@MainActor () -> Void)?, else dismiss: DismissAction) {iflet card { card() } else { dismiss() } }}extensionView {/// Opens `content` as a card while `isPresented` is true. `onDismiss`/// runs once it is closed, however it closed.func cottonCard<Content: View>( isPresented: Binding<Bool>, detents: Set<CottonDetent> = [.content], holds: Bool = false, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping () -> Content ) -> someView { modifier(CardLayerRaise( id: isPresented.wrappedValue ? "shown" : nil, detents: detents, holds: holds, onDismiss: onDismiss, content: { isPresented.wrappedValue ? AnyView(content()) : nil }, close: { isPresented.wrappedValue = false })) }/// Opens `content` for `item` as a card while it is set.func cottonCard<Item: Identifiable, Content: View>( item: Binding<Item?>, detents: Set<CottonDetent> = [.content], holds: Bool = false, onDismiss: (() -> Void)? = nil, @ViewBuilder content: @escaping (Item) -> Content ) -> someView { modifier(CardLayerRaise( id: item.wrappedValue.map { String(describing: $0.id) }, detents: detents, holds: holds, onDismiss: onDismiss, content: { item.wrappedValue.map { AnyView(content($0)) } }, close: { item.wrappedValue = nil })) }/// This view is a card host: the cards opened inside it stand over it.func cardLayers() -> someView { modifier(CardLayerHost()) }}/// Raises the card while it is open, in the opening screen's environment.privatestructCardLayerRaise: ViewModifier {let id: String?let detents: Set<CottonDetent>let holds: Boollet onDismiss: (() -> Void)?let content: () -> AnyView?let close: @MainActor () -> Void @Environment(\.self) privatevar environment/// Where this screen stands, so two screens' cards never share an id. @State privatevar place = UUID().uuidStringfunc body(content view: Content) -> someView { view .transformPreference(CardLayerKey.self) { cards inguardlet id, let card = content() else { return } cards.append(CardLayer( id: place + id, content: card, environment: environment, detents: detents, holds: holds, close: close)) } .onChange(of: id) { was, now inif was != nil, now == nil { onDismiss?() } } }}/// The host: its page, with the top card opened inside it standing over it.privatestructCardLayerHost: ViewModifier { @Environment(\.theme) privatevar theme/// A card stands over the page: the page under it is not read out. @State privatevar covered = falsefunc body(content: Content) -> someView { content .coveredUnread(covered) .overlayPreferenceValue(CardLayerKey.self) { cards in ZStack {iflet top = cards.last {CardLayerPage(card: top) .id(top.id) .transition(.opacity) } } .animation(.easeOut(duration: fade), value: cards.last?.id) .onChange(of: cards.last?.id, initial: true) { _, top in covered = top != nil } }// The cards stop here: the host around this one never draws them. .transformPreference(CardLayerKey.self) { $0 = [] } }privatevar fade: Double { Double(theme.scale[.fanOpenFadeMs]) / 1000 }}/// One card over its page: the dim, and the card of paper in the middle.privatestructCardLayerPage: View {let card: CardLayervar body: someView {var environment = card.environment environment.closeCard = card.close environment.cardHolds = card.holds// The card is not the page it was opened on: that page's back is// not the card's, and a page the card opens is the card's own. #if os(iOS) environment.tileBack = nil environment.tileHere = nil #endif environment.cottonAtColumnRoot = falselet theme = environment.themereturnGeometryReader { room in ZStack { theme.colors.shadow.color.opacity(Double(theme.scale[.pageDim])) .ignoresSafeArea() .contentShape(Rectangle()) .onTapGesture { if !card.holds { card.close() } } .accessibilityHidden(true)CardLayerPaper(detents: card.detents, room: room.size) { card.content } .cardLayers() } } .environment(\.self, environment) .coversTheRest(true) }}/// The card: the sheet's paper and chrome, as tall as its content asks/// (`.content`), half the room (`.half`) or nearly all of it (`.tall`).privatestructCardLayerPaper<Content: View>: View {let detents: Set<CottonDetent>let room: CGSize @ViewBuilder let content: () -> Content @Environment(\.theme) privatevar theme @State privatevar measured: CGFloat?var body: someView {let most = max(0, room.height - CardLayerKnobs.gutter * 2)let height: CGFloat = switchCardLayerKnobs.height(detents) {case .half: min(most, room.height * CardLayerKnobs.halfShare)case .tall: mostcase .content: min(most, measured ?? CardLayerKnobs.firstHeight) }CottonSheetChrome(fills: !detents.subtracting([.content]).isEmpty, onMeasure: { measured = $0 }, content: content) .frame(width: min(room.width - CardLayerKnobs.gutter * 2, CardLayerKnobs.widest), height: height) .background { PaperSurface(dye: SheetPaper.dye, edge: SheetPaper.edge, seed: SheetPaper.seed) } .clipShape(RoundedRectangle(cornerRadius: SheetsKnobs.paperRadiusMax)) .shadow(color: theme.colors.shadow.color.opacity(CardLayerKnobs.shadowOpacity), radius: CardLayerKnobs.shadowRadius, y: CardLayerKnobs.shadowDrop) .frame(width: room.width, height: room.height) }}/// The sizes a card draws with: Swift's own, beside their reader.enumCardLayerKnobs {/// The page's margin round a card.staticlet gutter: CGFloat = 16staticlet widest: CGFloat = 560staticlet halfShare: CGFloat = 0.55/// A content-sized card's height for the one frame before it is measured.staticlet firstHeight: CGFloat = 240/// The card's head: the X that closes it.staticlet headHeight: CGFloat = 52staticlet shadowOpacity: Double = 0.18staticlet shadowRadius: CGFloat = 14staticlet shadowDrop: CGFloat = 6/// The one height a card takes of those a sheet once offered: the/// smallest it named.staticfunc height(_ detents: Set<CottonDetent>) -> CottonDetent { detents.contains(.half) ? .half : detents.contains(.tall) ? .tall : .content }}