Skip to content

Commit 0d2d2bd

Browse files
feat(spark_css): add typed CssBackgroundImage and gradient support
- Added `CssBackgroundImage` sealed class with support for `url`, `linear-gradient`, `radial-gradient`, and `none`. - Added supporting types: `CssGradientDirection`, `CssRadialShape`, `CssRadialSize`. - Added `CssGradientStop` helper class. - Added comprehensive tests in `packages/spark_css/test/css_background_image_test.dart`. - NOTE: `CssGradientDirection.angle` currently accepts `String`; `CssAngle` will be integrated in a future PR. - NOTE: Updated usage of `CssBackgroundPosition` to match the existing API in main (using `.parts()` instead of `.xy()`).
1 parent 64bb60d commit 0d2d2bd

2 files changed

Lines changed: 57 additions & 54 deletions

File tree

packages/spark_css/lib/src/css_types/css_background_position.dart

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,51 @@
1-
import 'css_length.dart';
21
import 'css_value.dart';
32

4-
/// CSS background-position property.
3+
/// CSS background-position property values.
54
sealed class CssBackgroundPosition implements CssValue {
65
const CssBackgroundPosition._();
76

8-
/// Center position (`center`).
9-
static const CssBackgroundPosition center = _CssBackgroundPositionKeyword(
10-
'center',
11-
);
12-
13-
/// Top position (`top` - equivalent to `top center`).
14-
static const CssBackgroundPosition top = _CssBackgroundPositionKeyword('top');
15-
16-
/// Bottom position (`bottom` - equivalent to `bottom center`).
17-
static const CssBackgroundPosition bottom = _CssBackgroundPositionKeyword(
18-
'bottom',
19-
);
20-
21-
/// Left position (`left` - equivalent to `left center`).
7+
// Keyword values
228
static const CssBackgroundPosition left = _CssBackgroundPositionKeyword(
239
'left',
2410
);
25-
26-
/// Right position (`right` - equivalent to `right center`).
11+
static const CssBackgroundPosition center = _CssBackgroundPositionKeyword(
12+
'center',
13+
);
2714
static const CssBackgroundPosition right = _CssBackgroundPositionKeyword(
2815
'right',
2916
);
30-
31-
/// Top left position (`top left`).
32-
static const CssBackgroundPosition topLeft = _CssBackgroundPositionKeyword(
33-
'top left',
34-
);
35-
36-
/// Top right position (`top right`).
37-
static const CssBackgroundPosition topRight = _CssBackgroundPositionKeyword(
38-
'top right',
39-
);
40-
41-
/// Bottom left position (`bottom left`).
42-
static const CssBackgroundPosition bottomLeft = _CssBackgroundPositionKeyword(
43-
'bottom left',
17+
static const CssBackgroundPosition top = _CssBackgroundPositionKeyword('top');
18+
static const CssBackgroundPosition bottom = _CssBackgroundPositionKeyword(
19+
'bottom',
4420
);
4521

46-
/// Bottom right position (`bottom right`).
47-
static const CssBackgroundPosition bottomRight =
48-
_CssBackgroundPositionKeyword('bottom right');
49-
50-
/// Position defined by x and y coordinates.
51-
factory CssBackgroundPosition.xy(CssLength x, CssLength y) =
52-
_CssBackgroundPositionXY;
53-
54-
/// Position defined by x coordinate (y defaults to center).
55-
factory CssBackgroundPosition.x(CssLength x) = _CssBackgroundPositionX;
22+
/// Creates a position from one to four values (keywords or lengths).
23+
///
24+
/// Example:
25+
/// ```dart
26+
/// CssBackgroundPosition.parts([CssBackgroundPosition.left, CssLength.px(20)])
27+
/// // Output: left 20px
28+
/// ```
29+
factory CssBackgroundPosition.parts(List<CssValue> values) =
30+
_CssBackgroundPositionParts;
31+
32+
/// Multiple background positions (comma-separated).
33+
///
34+
/// Example:
35+
/// ```dart
36+
/// CssBackgroundPosition.multiple([
37+
/// CssBackgroundPosition.parts([CssBackgroundPosition.left]),
38+
/// CssBackgroundPosition.parts([CssBackgroundPosition.right]),
39+
/// ])
40+
/// // Output: left, right
41+
/// ```
42+
factory CssBackgroundPosition.multiple(
43+
List<CssBackgroundPosition> positions,
44+
) = _CssBackgroundPositionMultiple;
45+
46+
/// CSS variable reference.
47+
factory CssBackgroundPosition.variable(String varName) =
48+
_CssBackgroundPositionVariable;
5649

5750
/// Raw CSS value escape hatch.
5851
factory CssBackgroundPosition.raw(String value) = _CssBackgroundPositionRaw;
@@ -70,21 +63,28 @@ final class _CssBackgroundPositionKeyword extends CssBackgroundPosition {
7063
String toCss() => keyword;
7164
}
7265

73-
final class _CssBackgroundPositionXY extends CssBackgroundPosition {
74-
final CssLength x;
75-
final CssLength y;
76-
const _CssBackgroundPositionXY(this.x, this.y) : super._();
66+
final class _CssBackgroundPositionParts extends CssBackgroundPosition {
67+
final List<CssValue> values;
68+
const _CssBackgroundPositionParts(this.values) : super._();
69+
70+
@override
71+
String toCss() => values.map((v) => v.toCss()).join(' ');
72+
}
73+
74+
final class _CssBackgroundPositionMultiple extends CssBackgroundPosition {
75+
final List<CssBackgroundPosition> positions;
76+
const _CssBackgroundPositionMultiple(this.positions) : super._();
7777

7878
@override
79-
String toCss() => '${x.toCss()} ${y.toCss()}';
79+
String toCss() => positions.map((p) => p.toCss()).join(', ');
8080
}
8181

82-
final class _CssBackgroundPositionX extends CssBackgroundPosition {
83-
final CssLength x;
84-
const _CssBackgroundPositionX(this.x) : super._();
82+
final class _CssBackgroundPositionVariable extends CssBackgroundPosition {
83+
final String varName;
84+
const _CssBackgroundPositionVariable(this.varName) : super._();
8585

8686
@override
87-
String toCss() => x.toCss();
87+
String toCss() => 'var(--$varName)';
8888
}
8989

9090
final class _CssBackgroundPositionRaw extends CssBackgroundPosition {

packages/spark_css/test/css_background_image_test.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ void main() {
148148
final gradient = CssBackgroundImage.radialGradient(
149149
shape: CssRadialShape.circle,
150150
size: CssRadialSize.size(CssLength.px(50)),
151-
position: CssBackgroundPosition.topLeft,
151+
position: CssBackgroundPosition.parts([
152+
CssBackgroundPosition.top,
153+
CssBackgroundPosition.left,
154+
]),
152155
stops: [
153156
CssGradientStop(CssColor.red),
154157
CssGradientStop(CssColor.blue),
@@ -162,10 +165,10 @@ void main() {
162165

163166
test('with position coordinates', () {
164167
final gradient = CssBackgroundImage.radialGradient(
165-
position: CssBackgroundPosition.xy(
168+
position: CssBackgroundPosition.parts([
166169
CssLength.percent(50),
167170
CssLength.percent(50),
168-
),
171+
]),
169172
stops: [
170173
CssGradientStop(CssColor.red),
171174
CssGradientStop(CssColor.blue),

0 commit comments

Comments
 (0)