|
| 1 | +// |
| 2 | +// TPopUpModifier.swift |
| 3 | +// DesignSystem |
| 4 | +// |
| 5 | +// Created by 박민서 on 1/16/25. |
| 6 | +// Copyright © 2025 yapp25thTeamTnT. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import SwiftUI |
| 10 | + |
| 11 | +/// 팝업 컨테이너 (공통 레이아웃) |
| 12 | +public struct TPopUpModifier<InnerContent: View>: ViewModifier { |
| 13 | + |
| 14 | + /// 팝업 내부 콘텐츠의 기본 패딩 |
| 15 | + private let defaultInnerPadding: CGFloat = 20 |
| 16 | + /// 팝업 배경의 기본 불투명도 |
| 17 | + /// 0.0 = 완전 투명, 1.0 = 완전 불투명 |
| 18 | + private let defaultBackgroundOpacity: Double = 0.8 |
| 19 | + /// 팝업의 기본 배경 색상 |
| 20 | + private let defaultPopUpBackgroundColor: Color = .white |
| 21 | + /// 팝업 모서리의 기본 곡률 (Corner Radius) |
| 22 | + private let defaultCornerRadius: CGFloat = 16 |
| 23 | + /// 팝업 그림자의 기본 반경 |
| 24 | + private let defaultShadowRadius: CGFloat = 10 |
| 25 | + /// 팝업 콘텐츠의 기본 크기 |
| 26 | + private let defaultContentSize: CGSize = .init(width: 297, height: 175) |
| 27 | + |
| 28 | + /// 팝업에 표시될 내부 콘텐츠 클로저 |
| 29 | + private let innerContent: () -> InnerContent |
| 30 | + /// 팝업 표시 여부 |
| 31 | + @Binding private var isPresented: Bool |
| 32 | + |
| 33 | + /// TPopupModifier 초기화 메서드 |
| 34 | + /// - Parameters: |
| 35 | + /// - isPresented: 팝업 표시 여부를 제어하는 Binding |
| 36 | + /// - newContent: 팝업에 표시될 내부 콘텐츠 클로저 |
| 37 | + public init( |
| 38 | + isPresented: Binding<Bool>, |
| 39 | + newContent: @escaping () -> InnerContent |
| 40 | + ) { |
| 41 | + self._isPresented = isPresented |
| 42 | + self.innerContent = newContent |
| 43 | + } |
| 44 | + |
| 45 | + public func body(content: Content) -> some View { |
| 46 | + ZStack { |
| 47 | + // 기존 뷰 |
| 48 | + content |
| 49 | + .zIndex(0) |
| 50 | + |
| 51 | + if isPresented { |
| 52 | + // 반투명 배경 |
| 53 | + Color.black.opacity(defaultBackgroundOpacity) |
| 54 | + .ignoresSafeArea() |
| 55 | + .zIndex(1) |
| 56 | + .onTapGesture { |
| 57 | + isPresented = false |
| 58 | + } |
| 59 | + |
| 60 | + // 팝업 뷰 |
| 61 | + self.innerContent() |
| 62 | + .frame(minWidth: defaultContentSize.width, minHeight: defaultContentSize.height) |
| 63 | + .padding(defaultInnerPadding) |
| 64 | + .background(defaultPopUpBackgroundColor) |
| 65 | + .cornerRadius(defaultCornerRadius) |
| 66 | + .shadow(radius: defaultShadowRadius) |
| 67 | + .padding() |
| 68 | + .zIndex(2) |
| 69 | + } |
| 70 | + } |
| 71 | + .animation(.easeInOut, value: isPresented) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +public extension View { |
| 76 | + /// 팝업 표시를 위한 View Modifier |
| 77 | + /// - Parameters: |
| 78 | + /// - isPresented: 팝업 표시 여부를 제어하는 Binding |
| 79 | + /// - content: 팝업 내부에 표시할 콘텐츠 클로저 |
| 80 | + /// - Returns: 팝업이 추가된 View |
| 81 | + func tPopUp<Content: View>( |
| 82 | + isPresented: Binding<Bool>, |
| 83 | + @ViewBuilder content: @escaping () -> Content |
| 84 | + ) -> some View { |
| 85 | + self.modifier(TPopUpModifier<Content>(isPresented: isPresented, newContent: content)) |
| 86 | + } |
| 87 | + |
| 88 | + /// `TPopUp.Alert` 팝업 전용 View Modifier |
| 89 | + /// - Parameters: |
| 90 | + /// - isPresented: 팝업 표시 여부를 제어하는 Binding |
| 91 | + /// - content: 팝업 알림 내용을 구성하는 클로저 |
| 92 | + /// - Returns: 팝업이 추가된 View |
| 93 | + func tPopUp(isPresented: Binding<Bool>, content: @escaping () -> TPopUpAlertView) -> some View { |
| 94 | + self.modifier(TPopUpModifier(isPresented: isPresented, newContent: content)) |
| 95 | + } |
| 96 | +} |
0 commit comments