ComponentSwift

ComponentSwift brings ComponentKit to swift


Keywords
componentkit, ios, listview, react, swift, wrapper
License
MIT
Install
pod try ComponentSwift

Documentation

Platform Swift Build Status

ComponentSwift brings ComponentKit to swift.

ComponentKit is a react-inspired native view framework for iOS developed by Facebook. It intruduces a new abstract layer (component) to automatically handle view reuse, meanwhile provide the ability to build smooth list view easily.

ComponentSwift is an objc wrapper of ComponentKit and refined for swift. ComponentSwift is designed to be a subset of ComponentKit, implementing the most commonly used features.

Example

class CellComponent: CompositeComponent {

    init?(text: String) {
        super.init(
            view:
            ViewConfiguration(
                attributes:
                .backgroundColor(.gray),
                .tapGesture(#selector(didTapSelf))
            ),
            component:
            getAnotherComponent(text) //
    }
// ...

or try real demo with pod try ComponentSwift

Supported Features

Support:

  • Component:
    • almost all build-in components.
    • ability to wrap an an existed CKComponent on your own
  • Datasource:
    • CollectionViewTransactionalDatasource
    • TableViewTransactionalDatasource (unofficial)
    • HostingView
  • Response Chain and Actions
  • Scope and State
  • Animation

Not support:

  • component controller and StatefulViewComponent
  • things about viewContext, mount
  • computeLayoutThatFits: overriding
  • ...

Work with legacy code

Legacy CKComponent classes could continue to work in swift when using ComponentSwift. Classes and methods are provided to wrap your legacy components conveniently. Every attributes in ComponentKit have an equivalent in ComponentSwift.

Subclass CSCompositeComponent and use initWithCKComponent in its implementation. Import ComponentSubclass.h to use this stuff. Reference more from WrapExisted in the demo project.

more document here

TLDR

ComponentKit is implemented and used with objc++, which is pretty cool. It's incompatible with swift, since swift doesn't support objc++. So comes the ComponentSwift.

A objc wrapper is implemented to bridge componentKit to swift. It create an objc version equivalent for every type in componentKit's APIs, objc class for c++ struct/class, hiding the c++ header in the implementation. It doesn't touch deep in componentKit. The runtime code is almost the same to the one using componentKit directly in the simplest situation except for construction of objc layer, so it's safe to use.

It refined the bridged interface for swift, including notating nullability, adding default parameter and swift-only api. C++'s aggregate initialization is very expressive in componentKit. ComponentSwift try hard to simulate the api with default parameters and ExpressibleByXXXLiteral. At least we got code completion here.

Installation

pod "ComponentSwift"

The lib is released with ComponentKit v0.15.1, whose latest version drop support of cocoapods. For version 0.20, go to branch 0.20.

License

ComponentSwift is available under the MIT license.