Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit 291abcf

Browse files
committed
Update
- Improved Styles StaticFactory for integration generation - Improved dimensions API with static functions
1 parent 7d9406e commit 291abcf

13 files changed

+203
-39
lines changed

README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ Dimensions are present but not integrated in styles factory, so u can render the
6464
Style.margin(10.in.pt.render())
6565
// the same as
6666
Style.margin(Dimension.Length(value: 10, unit: .pt).render())
67+
// or
68+
Style.margin(Dimension.Length.pt(10)).render())
69+
// Need for `render()` call will be removed in beta releases (It's currently alpha)
6770
```
6871

6972
Also you can convert some dimensions or replace units
@@ -88,11 +91,11 @@ Corneres & Edges are supported too
8891

8992
### Colors
9093

91-
See [GenericColor](https://github.com/MakeupStudio/GenericColor) and [Palette](https://github.com/MakeupStudio/Palette) for more
94+
See [GenericColor](https://github.com/MakeupStudio/GenericColor) and [Palette](https://github.com/MakeupStudio/Palette) for more.
9295

9396
### Fonts
9497

95-
I'm gonna provide a static factory for google fonts later.
98+
I'm gonna provide a static factory for google fonts later, and Font type behaviour may change.
9699

97100
### Installation
98101

@@ -107,6 +110,44 @@ I'm gonna provide a static factory for google fonts later.
107110

108111
You can use `generateIntegration` methods from CSSKit itself by passing needed arguments or use **[this](https://makeupstudio.herokuapp.com/CSSKit)** tool for autogenerating integration code.
109112

113+
So it will generate all the needed fields for your container type, for example for HTML type it will look like
114+
115+
```swift
116+
// MARK: - Implementation
117+
118+
extension HTML {
119+
120+
internal func appendingStyle(_ style: Style) -> Self {
121+
<#Implementation#>
122+
}
123+
124+
}
125+
126+
// MARK: - Generated
127+
128+
extension HTML {
129+
130+
public func alignContent(_ value: String) -> Self {
131+
appendingStyle(.alignContent(value))
132+
}
133+
134+
public func alignItems(_ value: String) -> Self {
135+
appendingStyle(.alignItems(value))
136+
}
137+
138+
// ...
139+
140+
}
141+
```
142+
143+
And you'll be able to use chaining like this
144+
145+
```swift
146+
someHTMLInstance
147+
.display(.flex)
148+
.transform(.rotate(x: .deg(30)))
149+
```
150+
110151
---
111152

112153
_Hopefully I'll add more typo-free static factories & extend fonts support a bit next week_

Sources/CSSKit/Measurements/Dimensions/Dimension+Angle.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ extension Dimension {
1515
self.value = value
1616
self.unit = unit
1717
}
18+
19+
/// Represents an angle in a number of turns.
20+
///
21+
/// One full circle is 1turn. Examples: 0turn, 0.25turn, 1.2turn.
22+
static func turn(_ value: Double) -> Self { value.in.turn }
1823

1924
public struct Unit: CSSUnit, ExpressibleByStringLiteral {
2025
public var rawValue: String
@@ -50,3 +55,22 @@ extension Dimension {
5055
}
5156

5257
}
58+
59+
extension Dimension.Angle {
60+
61+
/// Represents an angle in degrees.
62+
///
63+
/// One full circle is 360deg. Examples: 0deg, 90deg, 14.23deg.
64+
static func deg(_ value: Double) -> Self { .init(value, .deg) }
65+
66+
/// Represents an angle in gradians.
67+
///
68+
/// One full circle is 400grad. Examples: 0grad, 100grad, 38.8grad.
69+
static func grad(_ value: Double) -> Self { .init(value, .grad) }
70+
71+
/// Represents an angle in radians.
72+
///
73+
/// One full circle is 2π radians which approximates to 6.2832rad. 1rad is 180/π degrees. Examples: 0rad, 1.0708rad, 6.2832rad.
74+
static func rad(_ value: Double) -> Self { .init(value, .rad) }
75+
76+
}

Sources/CSSKit/Measurements/Dimensions/Dimension+Frequency.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ extension Dimension {
4949
}
5050

5151
}
52+
53+
extension Dimension.Frequency {
54+
55+
/// Represents a frequency in hertz.
56+
public static func hz(_ value: Double) -> Self { .init(value, .hz) }
57+
58+
/// Represents a frequency in kilohertz.
59+
public static func kHz(_ value: Double) -> Self { .init(value, .kHz) }
60+
61+
}

Sources/CSSKit/Measurements/Dimensions/Dimension+Length.swift

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@
88

99
extension Dimension {
1010

11-
public struct Length: CSSDimension {
11+
public struct Length: CSSDimension, ExpressibleByIntegerLiteral, ExpressibleByFloatLiteral {
1212
public var value: Double
1313
public var unit: Unit
14+
15+
public init(floatLiteral value: Double) {
16+
self.init(value, .void)
17+
}
18+
19+
public init(integerLiteral value: Double) {
20+
self.init(Double(value), .void)
21+
}
22+
1423
public init(value: Double, unit: Unit) {
1524
self.value = value
1625
self.unit = unit
@@ -90,3 +99,67 @@ extension Dimension {
9099
}
91100

92101
}
102+
103+
extension Dimension.Length {
104+
105+
/// Centimeters
106+
public static func cm(_ value: Double) -> Self { .init(value, .cm) }
107+
108+
/// Millimeters
109+
public static func mm(_ value: Double) -> Self { .init(value, .mm) }
110+
111+
/// inches (1inch = 96px = 2.54cm)
112+
public static func inch(_ value: Double) -> Self { .init(value, .inch) }
113+
114+
/// pixels (1px = 1/96th of 1in)
115+
public static func px(_ value: Double) -> Self { .init(value, .px) }
116+
117+
/// points (1pt = 1/72 of 1in)
118+
public static func pt(_ value: Double) -> Self { value.in.pt }
119+
120+
/// picas (1pc = 12 pt)
121+
public static func pc(_ value: Double) -> Self { .init(value, .pc) }
122+
123+
/// Relative to the font-size of the element (2em means 2 times the size of the current font)
124+
///
125+
/// Nice for scalable layouts
126+
public static func em(_ value: Double) -> Self { .init(value, .em) }
127+
128+
/// Relative to the x-height of the current font (rarely used)
129+
public static func ex(_ value: Double) -> Self { .init(value, .ex) }
130+
131+
/// Relative to width of the "0" (zero)
132+
public static func ch(_ value: Double) -> Self { .init(value, .ch) }
133+
134+
/// Relative to font-size of the root element
135+
///
136+
/// Nice for scalable layouts
137+
public static func rem(_ value: Double) -> Self { .init(value, .rem) }
138+
139+
/// Relative to 1% of the width of the viewport
140+
///
141+
/// Viewport = the browser window size. If the viewport is 50cm wide, 1vw = 0.5cm.
142+
public static func vw(_ value: Double) -> Self { .init(value, .vw) }
143+
144+
/// Relative to 1% of the height of the viewport
145+
///
146+
/// Viewport = the browser window size. If the viewport is 50cm wide, 1vw = 0.5cm.
147+
public static func vh(_ value: Double) -> Self { .init(value, .vh) }
148+
149+
/// Relative to 1% of viewport's smaller dimension
150+
///
151+
/// Viewport = the browser window size. If the viewport is 50cm wide, 1vw = 0.5cm.
152+
public static func vmin(_ value: Double) -> Self { .init(value, .vmin) }
153+
154+
/// Relative to 1% of viewport's larger dimension
155+
///
156+
/// Viewport = the browser window size. If the viewport is 50cm wide, 1vw = 0.5cm.
157+
public static func vmax(_ value: Double) -> Self { .init(value, .vmax) }
158+
159+
/// Relative to the parent element
160+
public static func percent(_ value: Double) -> Self { .init(value, .percent) }
161+
162+
/// Unspecified
163+
public static func void(_ value: Double) -> Self { .init(value, .void) }
164+
165+
}

Sources/CSSKit/Measurements/Dimensions/Dimension+Time.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ extension Dimension {
4949
}
5050

5151
}
52+
53+
extension Dimension.Time {
54+
55+
/// Represents a time in seconds.
56+
public static func s(_ value: Double) -> Self { .init(value, .s) }
57+
58+
/// Represents a time in milliseconds.
59+
public static func ms(_ value: Double) -> Self { .init(value, .ms) }
60+
61+
}

Sources/CSSKit/Styles/Enums/Alignment/Style+AlignContent.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import Foundation
1010

1111
extension Style {
1212

13-
public static func alignContent(_ value: AlignContent) -> Self {
14-
alignContent(value.rawValue)
15-
}
16-
1713
/// The CSS align-content property sets the distribution of space between
1814
/// and around content items along a flexbox's cross-axis or a grid's block axis.
1915
public struct AlignContent: ExpressibleByStringLiteral, RawRepresentable {

Sources/CSSKit/Styles/Enums/Alignment/Style+AlignItems.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@
88

99
extension Style {
1010

11-
/// The CSS align-items property sets the align-self value on all direct children as a group.
12-
/// In Flexbox, it controls the alignment of items on the Cross Axis.
13-
/// In Grid Layout, it controls the alignment of items on the Block Axis within their grid area.
14-
public static func alignItems(_ value: AlignItems) -> Self {
15-
alignSelf(value.rawValue)
16-
}
17-
1811
public struct AlignItems: ExpressibleByStringLiteral, RawRepresentable {
1912
public var rawValue: String
2013

Sources/CSSKit/Styles/Enums/Alignment/Style+AlignSelf.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@
88

99
extension Style {
1010

11-
/// The align-self CSS property overrides a grid or flex item's align-items value.
12-
/// In Grid, it aligns the item inside the grid area.
13-
/// In Flexbox, it aligns the item on the cross axis.
14-
///
15-
/// Requires `.display` to be `.flex`
16-
public static func alignSelf(_ value: AlignSelf) -> Self {
17-
alignSelf(value.rawValue)
18-
}
19-
2011
public struct AlignSelf: ExpressibleByStringLiteral, RawRepresentable {
2112
public var rawValue: String
2213

Sources/CSSKit/Styles/Enums/Alignment/Style+TextAlignment.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
extension Style {
1010

11-
public static func textAlign(_ value: TextAlignment) -> Self {
12-
textAlign(value.rawValue)
13-
}
14-
1511
public struct TextAlignment: ExpressibleByStringLiteral, RawRepresentable {
1612
public var rawValue: String
1713

Sources/CSSKit/Styles/Enums/Style+Display.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@
88

99
extension Style {
1010

11-
public static func display(_ value: Display) -> Self {
12-
display(value.rawValue)
13-
}
14-
1511
/// The display CSS property sets whether an element is treated as a block or inline element
1612
/// and the layout used for its children, such as flow layout, grid or flex.
1713
///

0 commit comments

Comments
 (0)