Skip to content

Bring ComponentKit to Swift, A React-inspired view framework for iOS

License

Notifications You must be signed in to change notification settings

leavez/ComponentSwift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 providing the ability to build smooth list views 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 a real demo with pod try ComponentSwift

Supported Features

Supported:

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

Not supported:

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

Working 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 attribute in ComponentKit has 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 that's the reason for ComponentSwift.

A objc wrapper is implemented to bridge componentKit to swift. It creates 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 tries hard to simulate the api with default parameters and ExpressibleByXXXLiteral. At least we got code completion here.

Installation

pod "ComponentSwift"

Swift version:

  • swift 4.2: v0.5
  • swift 4.1: v0.4.1
  • swift 3: v0.3

License

ComponentSwift is available under the MIT license.