Giter Club home page Giter Club logo

swiftui-macros's Introduction

SwiftUI-Macros

A library of useful macros for SwiftUI.

Swift 5.9 or later is required to use this package.

Installation

Swift Package Manager

Add the dependency:

dependencies: [
    .package(url: "https://github.com/Wouter01/SwiftUI-Macros.git", from: "1.0.0")
]

Add the dependency to your target:

.target(
   name: "MyTarget",
   dependencies: [.product(name: "SwiftUIMacros", package: "SwiftUI-Macros")]
)

Environment

To allow easier creation of new environment keys, two macros are available:

EnvironmentValue

Apply this macro to a variable inside an EnvironmentValues extension to add it to the environment. The assigned value is the default value and is required, unless the type is Optional. The type can be inferred by its value, just like in normal Swift code.

import SwiftUIMacros

extension EnvironmentValues {
    @EnvironmentValue
    var alignment: Alignment = .center
}

// Expands to
extension EnvironmentValues {
    var alignment: Alignment {
      get {
        self[EnvironmentKey_alignment.self]
      }
      set {
        self[EnvironmentKey_alignment.self] = newValue
      }
    }
    
    struct EnvironmentKey_alignment: EnvironmentKey {
       static var defaultValue: Alignment = .center
    }
}

EnvironmentStorage

Apply this to an EnvironmentValues extension to add the EnviromentValue macro to each variable inside the extension.

import SwiftUIMacros

@EnvironmentStorage
extension EnvironmentValues {
    var alignment: Alignment = .center
    
    var secondaryFont: Font?
    
    var gridLines = 0
}

// Expands to
extension EnvironmentValues {
    @EnvironmentValue
    var alignment: Alignment = .center
    
    @EnvironmentValue
    var secondaryFont: Font?
    
    @EnvironmentValue
    var gridLines = 0
}

swiftui-macros's People

Contributors

wouter01 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.