Giter VIP home page Giter VIP logo

modals's Introduction

Modals

A lightweight alternative to system sheets on iOS.

colors-final.mp4

Installation

You can add Modals to your project using Swift Package Manager.

  • From the File menu, select Add Packages...
  • Enter the repo URL
http://github.com/fieldday-ai/Modals.git

Set Up

To set up Modals in your project, wrap the root view of your project in the ModalStackView. This will allow modals to be overlayed from the root level of the view hierarchy.

@main
struct ModalsExample: App {
    var body: some Scene {
        WindowGroup {
            // Wrap your root view in the ModalStackView
            ModalStackView {
                ContentView()
            }
        }
    }
}

Usage

To register a modal, call the .modal() modifier on any View (just like with system sheets).

struct ContentView: View {
    
    @State var isPresented: Bool = false
    
    var body: some View {
        Button {
            isPresented = true
        } label: {
            Text("Open")
        }
        .modal(isPresented: $isPresented) {
            Text("Modal!")
        }
    }
}

Advanced Usage

Modal Customization

Size

To change the size of the modal, pass in a ModalSize into the .modal() modifier.

.modal(
    ...,
    size: .medium
) { 
    ...
}

There are .small, .medium, and .large default sizes, as well as custom sizes defined by a constant .height or a .fraction of the screen height (inspired by presenationDetents on system sheets).

Corner Radius

To change the corner radius of the modal, use the cornerRadius argument in the .modal() modifier.

.modal(
    ...,
    cornerRadius: 18
) { 
    ... 
}

Options

You can also pass in an array of available customization options to the .modal() modifier.

  • .prefersDragHandle: Replaces the default close button with a center-aligned drag handle.
  • .disableContentDragging: Disables the ability to drag on content to dismiss the modal (sometimes useful when a ScrollView is embedded in the modal).
.modal(
    ...,
    options: [.prefersDragHandle]
) { 
    ... 
}

ModalStack Customization

The ModalStack can be customized by using a variety of supported modifers.

In the below context, "Content" is just whatever view is being overlayed by the modal.

Content Scaling

Content scaling can be disabled by using the .contentScaling() modifier.

ModalStackView {
    ...
}
.contentScaling(false)

Content Saturation

Content saturation can be disabled by using the .contentSaturation() modifier.

ModalStackView {
    ...
}
.contentSaturation(false)

Content Background Color

Content background color can be adjusted using the .contentBackgroundColor() modifier. This is the only way to have a background color ignore safe area completely while content scaling is enabled.

ModalStackView {
    ...
}
.contentBackgroundColor(Color.blue)

Container Background Color

To change the container background color (in the video, the area that is white behind the root content & presented modals), use the .containerBackgroundColor() modifier.

ModalStackView {
    ...
}
.containerBackgroundColor(Color.blue)

modals's People

Contributors

sammcode avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.