项目作者: chackle

项目描述 :
A Swift DSL for writing type-safe CSS
高级语言: Swift
项目地址: git://github.com/chackle/Swish.git
创建时间: 2020-02-11T13:34:13Z
项目社区:https://github.com/chackle/Swish

开源协议:

下载


🎨 Swish



Travis CI



Swift Package Manager

Mac + Linux

Swish is a domain-specific language for writing type-safe CSS in Swift and best used for small, simple websites. Swish shines when combined with other Swift solutions for web development such as Vapor, Plot and Publish.

✍️ Writing CSS in Swift

Swish allows you to write CSS using expressive Swift code with type-safe builders to help reduce mistakes and make code easier to read and understand:

  1. let css = CSS(
  2. .class("home-header",
  3. .position(.relative),
  4. .width(.pct(100)),
  5. .height(.auto),
  6. .padding(.px(32)),
  7. .textAlign(.center)
  8. ),
  9. .element("body",
  10. .background(
  11. color: .hex(0x000000)
  12. )
  13. )
  14. )

The above should look pretty recognizable if you’ve ever used CSS before. Swish property builder functions map to CSS, but they also remove the need to specify all values for some properties. Take padding for example, normally we would have to specify 16px 0px 0px 16px if we wanted to change a top and left value. With Swish you can simply specify .padding(top: .px(16), left: .px(16)) and it’ll fill in the blanks.

Pseudo classes and elements

Swish also supports pseudo-classes and pseudo-elements:

  1. let css = CSS(
  2. .element("li",
  3. .background(color: .aquamarine)
  4. ),
  5. .element("li", pseudoClass: .hover,
  6. .background(color: .forestGreen),
  7. .transition(property: .color, duration: .s(3))
  8. )
  9. )

🤔 Using Unsupported Properties

Swish doesn’t support all CSS properties natively (yet), but if there is a property you wish to use then you can use the .raw builder to inject your own property name and value. Here’s an example of how we would add the border property if it wasn’t supported (it is):

  1. let css = CSS(
  2. .class("container",
  3. .raw(property: "border", value: "2px solid red")
  4. )
  5. )

🔨 TODOs

  • Provide further CSS property coverage
  • Add tests
  • Add internal documentation