Coder Social home page Coder Social logo

kyaw-codes / fitheightsheet Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 45.04 MB

A package that allows you to present and dismiss height auto-resizing SwiftUI sheets.

License: MIT License

Swift 3.65% Makefile 88.51% C++ 2.33% Shell 4.40% Perl 1.11%

fitheightsheet's Introduction

FitHeightSheet

Twitter: @KyawTheMonkey

The fitHeightSheet view modifier allows you to present a sheet that dynamically adjusts its height based on the content it contains. This is particularly useful when the content does not need to scroll and you want a more compact and visually appealing presentation. While the FitHeightSheet package is not intended to replace the native SwiftUI sheet, it complements it by addressing specific use cases where a content-fitted sheet is more appropriate.

Installation

Requires iOS 14+. FitHeightSheet can be installed through the Swift Package Manager. Add the package URL:

https://github.com/kyaw-codes/FitHeightSheet

Tutorial

The FitHeightSheet package is as easy as pie and just as versatile! It works similarly to the native SwiftUI sheet but with some naming tweaks.

1. State-based control

Just like native SwiftUI sheet, you can show/hide fit height sheet via fitHeightSheet(isPresented:content:) modifire.

struct HomeView {
  @State private var showSheet = false
  
  var body: some View {
    Button("Show sheet") {
      showSheet.toggle()
    }
    // Just like sheet(isPresented:content:)
    .fitHeightSheet(isPresented: $showSheet) {
      // Your sheet here...
      Sheet()
    }
  }
}

2. Item-Based Control

Just like native SwiftUI sheet, you can show/hide fit height sheet via fitHeightSheet(item:content:) modifire.

struct HomeView {
  @State private var greetingText: String? = nil
  
  var body: some View {
    Button("Show sheet") {
      greetingText = "Hello ๐Ÿ‘‹"
    }
    // Just like sheet(item:content:)
    .fitHeightSheet(item: $greetingText) {
      // Your sheet here...
      Sheet(greeting: greetingText)
    }
  }
}

3. Customisation options

You can customize backdrop style, presentation and dismissal timing curve, top content inset, and dismiss threshold. If you want to do something once the sheet is dismissed, you can write your logic inside onDismiss clsoure.

Please be advised that onAppear modifire on your custom sheet might give you unexpected results. I would recomend you to use onChange modifire instead.

struct HomeView {
  @State private var showSheet = false
  
  var body: some View {
    Button("Show sheet") {
      showSheet.toggle()
    }
    .fitHeightSheet(isPresented: $showSheet) {
      Sheet()
    }
    .onChange(of: showSheet) { isPresented in
      guard isPresented else { return }
      // Do your sheet's onAppear logic here
    }
  }
}

4. Dismissing a FitHeightSheet

Dismissing a FitHeightSheet is as easy as dismissing a SwiftUI sheet. All you need is fitHeightSheetDismiss environment value and you can call it just like SwiftUI sheet.

struct Sheet {
  @Environment(\.fitHeightSheetDismiss) private var dismiss
  
  var body: some View {
    VStack {
      // Your sheet content here
      Button("Close") {
        dismiss()
      }
    }
  }
}

Key Features

The FitHeightSheet package enhances SwiftUI applications by providing a sheet that fits precisely around its content.

1. Adaptive Height

The sheet adjusts its height to fit the content, ensuring a seamless user experience.

2. State-Based Control

Use a boolean binding to control the presentation and dismissal of the sheet.

3. Item-Based Control

Use an optional item binding to control the presentation and dismissal of the sheet, passing the item to the content builder.

4. Customization

Options for backdrop style, animations, content inset, and dismissal behavior.

5. Dismissal Callback

An optional closure to execute custom logic when the sheet is dismissed.

Usecases

The FitHeightSheet package is ideal for specific scenarios where a sheet that adapts to its content's height is needed.

The FitHeightSheet package is ideal for situations where:

  • You need a sheet that adapts to the height of its content.
  • The content within the sheet does not need to scroll.
  • Showing additional options or details that fit within a specific area.

For more information, please check out the Example directory to discover more additional usecases.

Limitations

While FitHeightSheet is a powerful tool for presenting content-fitted sheets, there are scenarios where the native SwiftUI sheet might be more suitable.

1. Non-Scrolling Content

If the sheet's content exceeds the available height, the sheet will not scroll. In such cases, it's recommended to use the native SwiftUI sheet.

2. Dynamic Height Updates

When dynamically updating the sheet's content height (e.g., expanding/collapsing text), the backdrop opacity will remain fixed instead of adjusting based on the drag offset.

3. Keyboard Support

The package does not currently support sheets with keyboard interactions. If you have an input text field inside your sheet, the backdrop opacity will become fully opaque when the keyboard is displayed.

In above cases, please use the native SwiftUI sheet.

License

MIT License

Copyright (c) 2024 Kyaw Zay Ya Lin Tun

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

fitheightsheet's People

Contributors

kyaw-codes avatar

Stargazers

Ahn Jung Min avatar Kyaw Zay Ya Lin Tun avatar Tun Yar Zar Toe avatar

Watchers

 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.