Skip to content

Commit 193bd25

Browse files
feat(spark_css): add typed background-repeat property
1 parent 4fb329b commit 193bd25

2 files changed

Lines changed: 165 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import 'css_value.dart';
2+
3+
/// CSS background-repeat property values.
4+
sealed class CssBackgroundRepeat implements CssValue {
5+
const CssBackgroundRepeat._();
6+
7+
// Keyword values
8+
static const CssBackgroundRepeat repeat = _CssBackgroundRepeatKeyword(
9+
'repeat',
10+
);
11+
static const CssBackgroundRepeat repeatX = _CssBackgroundRepeatKeyword(
12+
'repeat-x',
13+
);
14+
static const CssBackgroundRepeat repeatY = _CssBackgroundRepeatKeyword(
15+
'repeat-y',
16+
);
17+
static const CssBackgroundRepeat space = _CssBackgroundRepeatKeyword('space');
18+
static const CssBackgroundRepeat round = _CssBackgroundRepeatKeyword('round');
19+
static const CssBackgroundRepeat noRepeat = _CssBackgroundRepeatKeyword(
20+
'no-repeat',
21+
);
22+
23+
/// Creates a two-value syntax background-repeat (e.g. `repeat space`).
24+
factory CssBackgroundRepeat.xy(CssBackgroundRepeat x, CssBackgroundRepeat y) =
25+
_CssBackgroundRepeatTwoValue;
26+
27+
/// Creates a comma-separated list of background-repeat values for multiple background images.
28+
factory CssBackgroundRepeat.multiple(List<CssBackgroundRepeat> repeats) =
29+
_CssBackgroundRepeatMultiple;
30+
31+
/// CSS variable reference.
32+
factory CssBackgroundRepeat.variable(String varName) =
33+
_CssBackgroundRepeatVariable;
34+
35+
/// Raw CSS value escape hatch.
36+
factory CssBackgroundRepeat.raw(String value) = _CssBackgroundRepeatRaw;
37+
38+
/// Global keyword (inherit, initial, unset, revert).
39+
factory CssBackgroundRepeat.global(CssGlobal global) =
40+
_CssBackgroundRepeatGlobal;
41+
}
42+
43+
final class _CssBackgroundRepeatKeyword extends CssBackgroundRepeat {
44+
final String keyword;
45+
const _CssBackgroundRepeatKeyword(this.keyword) : super._();
46+
47+
@override
48+
String toCss() => keyword;
49+
}
50+
51+
final class _CssBackgroundRepeatTwoValue extends CssBackgroundRepeat {
52+
final CssBackgroundRepeat x;
53+
final CssBackgroundRepeat y;
54+
const _CssBackgroundRepeatTwoValue(this.x, this.y) : super._();
55+
56+
@override
57+
String toCss() => '${x.toCss()} ${y.toCss()}';
58+
}
59+
60+
final class _CssBackgroundRepeatMultiple extends CssBackgroundRepeat {
61+
final List<CssBackgroundRepeat> repeats;
62+
const _CssBackgroundRepeatMultiple(this.repeats) : super._();
63+
64+
@override
65+
String toCss() => repeats.map((r) => r.toCss()).join(', ');
66+
}
67+
68+
final class _CssBackgroundRepeatVariable extends CssBackgroundRepeat {
69+
final String varName;
70+
const _CssBackgroundRepeatVariable(this.varName) : super._();
71+
72+
@override
73+
String toCss() => 'var(--$varName)';
74+
}
75+
76+
final class _CssBackgroundRepeatRaw extends CssBackgroundRepeat {
77+
final String value;
78+
const _CssBackgroundRepeatRaw(this.value) : super._();
79+
80+
@override
81+
String toCss() => value;
82+
}
83+
84+
final class _CssBackgroundRepeatGlobal extends CssBackgroundRepeat {
85+
final CssGlobal global;
86+
const _CssBackgroundRepeatGlobal(this.global) : super._();
87+
88+
@override
89+
String toCss() => global.toCss();
90+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import 'package:spark_css/src/css_types/css_background_repeat.dart';
2+
import 'package:spark_css/src/css_types/css_value.dart';
3+
import 'package:test/test.dart';
4+
5+
void main() {
6+
group('CssBackgroundRepeat', () {
7+
test('keywords output correct CSS', () {
8+
expect(CssBackgroundRepeat.repeat.toCss(), equals('repeat'));
9+
expect(CssBackgroundRepeat.repeatX.toCss(), equals('repeat-x'));
10+
expect(CssBackgroundRepeat.repeatY.toCss(), equals('repeat-y'));
11+
expect(CssBackgroundRepeat.space.toCss(), equals('space'));
12+
expect(CssBackgroundRepeat.round.toCss(), equals('round'));
13+
expect(CssBackgroundRepeat.noRepeat.toCss(), equals('no-repeat'));
14+
});
15+
16+
test('two-value syntax outputs correct CSS', () {
17+
expect(
18+
CssBackgroundRepeat.xy(
19+
CssBackgroundRepeat.repeat,
20+
CssBackgroundRepeat.space,
21+
).toCss(),
22+
equals('repeat space'),
23+
);
24+
expect(
25+
CssBackgroundRepeat.xy(
26+
CssBackgroundRepeat.noRepeat,
27+
CssBackgroundRepeat.round,
28+
).toCss(),
29+
equals('no-repeat round'),
30+
);
31+
});
32+
33+
test('multiple values output correct CSS', () {
34+
expect(
35+
CssBackgroundRepeat.multiple([
36+
CssBackgroundRepeat.repeatX,
37+
CssBackgroundRepeat.repeatY,
38+
]).toCss(),
39+
equals('repeat-x, repeat-y'),
40+
);
41+
42+
expect(
43+
CssBackgroundRepeat.multiple([
44+
CssBackgroundRepeat.xy(
45+
CssBackgroundRepeat.space,
46+
CssBackgroundRepeat.round,
47+
),
48+
CssBackgroundRepeat.noRepeat,
49+
]).toCss(),
50+
equals('space round, no-repeat'),
51+
);
52+
});
53+
54+
test('variable outputs correct CSS', () {
55+
expect(
56+
CssBackgroundRepeat.variable('bg-repeat').toCss(),
57+
equals('var(--bg-repeat)'),
58+
);
59+
});
60+
61+
test('raw outputs value as-is', () {
62+
expect(
63+
CssBackgroundRepeat.raw('repeat round').toCss(),
64+
equals('repeat round'),
65+
);
66+
});
67+
68+
test('global outputs correct CSS', () {
69+
expect(
70+
CssBackgroundRepeat.global(CssGlobal.inherit).toCss(),
71+
equals('inherit'),
72+
);
73+
});
74+
});
75+
}

0 commit comments

Comments
 (0)