|
| 1 | +import 'css_value.dart'; |
| 2 | +import 'css_color.dart'; |
| 3 | +import 'css_background_image.dart'; |
| 4 | +import 'css_background_position.dart'; |
| 5 | +import 'css_background_size.dart'; |
| 6 | +import 'css_background_repeat.dart'; |
| 7 | +import 'css_background_attachment.dart'; |
| 8 | +import 'css_background_origin.dart'; |
| 9 | +import 'css_background_clip.dart'; |
| 10 | + |
| 11 | +/// CSS `background` shorthand property values. |
| 12 | +sealed class CssBackground implements CssValue { |
| 13 | + const CssBackground._(); |
| 14 | + |
| 15 | + /// The `none` keyword. |
| 16 | + static const CssBackground none = _CssBackgroundKeyword('none'); |
| 17 | + |
| 18 | + /// Simple color background. |
| 19 | + factory CssBackground.color(CssColor color) = _CssBackgroundColor; |
| 20 | + |
| 21 | + /// Full background shorthand with optional components. |
| 22 | + /// |
| 23 | + /// Assembles the shorthand per CSS spec: |
| 24 | + /// `<bg-image> <position> / <size> <repeat> <attachment> <origin> <clip> <color>` |
| 25 | + /// |
| 26 | + /// The `position / size` slash syntax is used when both are provided. |
| 27 | + factory CssBackground.shorthand({ |
| 28 | + CssBackgroundImage? image, |
| 29 | + CssBackgroundPosition? position, |
| 30 | + CssBackgroundSize? size, |
| 31 | + CssBackgroundRepeat? repeat, |
| 32 | + CssBackgroundAttachment? attachment, |
| 33 | + CssBackgroundOrigin? origin, |
| 34 | + CssBackgroundClip? clip, |
| 35 | + CssColor? color, |
| 36 | + }) = _CssBackgroundShorthand; |
| 37 | + |
| 38 | + /// Multiple background layers (comma-separated). |
| 39 | + factory CssBackground.layers(List<CssBackground> layers) = |
| 40 | + _CssBackgroundLayers; |
| 41 | + |
| 42 | + /// CSS variable reference. |
| 43 | + factory CssBackground.variable(String varName) = _CssBackgroundVariable; |
| 44 | + |
| 45 | + /// Raw CSS value escape hatch. |
| 46 | + factory CssBackground.raw(String value) = _CssBackgroundRaw; |
| 47 | + |
| 48 | + /// Global keyword (inherit, initial, unset, revert). |
| 49 | + factory CssBackground.global(CssGlobal global) = _CssBackgroundGlobal; |
| 50 | +} |
| 51 | + |
| 52 | +final class _CssBackgroundKeyword extends CssBackground { |
| 53 | + final String keyword; |
| 54 | + const _CssBackgroundKeyword(this.keyword) : super._(); |
| 55 | + |
| 56 | + @override |
| 57 | + String toCss() => keyword; |
| 58 | +} |
| 59 | + |
| 60 | +final class _CssBackgroundColor extends CssBackground { |
| 61 | + final CssColor color; |
| 62 | + const _CssBackgroundColor(this.color) : super._(); |
| 63 | + |
| 64 | + @override |
| 65 | + String toCss() => color.toCss(); |
| 66 | +} |
| 67 | + |
| 68 | +final class _CssBackgroundShorthand extends CssBackground { |
| 69 | + final CssBackgroundImage? image; |
| 70 | + final CssBackgroundPosition? position; |
| 71 | + final CssBackgroundSize? size; |
| 72 | + final CssBackgroundRepeat? repeat; |
| 73 | + final CssBackgroundAttachment? attachment; |
| 74 | + final CssBackgroundOrigin? origin; |
| 75 | + final CssBackgroundClip? clip; |
| 76 | + final CssColor? color; |
| 77 | + |
| 78 | + const _CssBackgroundShorthand({ |
| 79 | + this.image, |
| 80 | + this.position, |
| 81 | + this.size, |
| 82 | + this.repeat, |
| 83 | + this.attachment, |
| 84 | + this.origin, |
| 85 | + this.clip, |
| 86 | + this.color, |
| 87 | + }) : super._(); |
| 88 | + |
| 89 | + @override |
| 90 | + String toCss() { |
| 91 | + final parts = <String>[]; |
| 92 | + |
| 93 | + if (image != null) parts.add(image!.toCss()); |
| 94 | + |
| 95 | + if (position != null && size != null) { |
| 96 | + parts.add('${position!.toCss()} / ${size!.toCss()}'); |
| 97 | + } else if (position != null) { |
| 98 | + parts.add(position!.toCss()); |
| 99 | + } else if (size != null) { |
| 100 | + parts.add(size!.toCss()); |
| 101 | + } |
| 102 | + |
| 103 | + if (repeat != null) parts.add(repeat!.toCss()); |
| 104 | + if (attachment != null) parts.add(attachment!.toCss()); |
| 105 | + if (origin != null) parts.add(origin!.toCss()); |
| 106 | + if (clip != null) parts.add(clip!.toCss()); |
| 107 | + if (color != null) parts.add(color!.toCss()); |
| 108 | + |
| 109 | + return parts.join(' '); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +final class _CssBackgroundLayers extends CssBackground { |
| 114 | + final List<CssBackground> layers; |
| 115 | + const _CssBackgroundLayers(this.layers) : super._(); |
| 116 | + |
| 117 | + @override |
| 118 | + String toCss() => layers.map((l) => l.toCss()).join(', '); |
| 119 | +} |
| 120 | + |
| 121 | +final class _CssBackgroundVariable extends CssBackground { |
| 122 | + final String varName; |
| 123 | + const _CssBackgroundVariable(this.varName) : super._(); |
| 124 | + |
| 125 | + @override |
| 126 | + String toCss() => 'var(--$varName)'; |
| 127 | +} |
| 128 | + |
| 129 | +final class _CssBackgroundRaw extends CssBackground { |
| 130 | + final String value; |
| 131 | + const _CssBackgroundRaw(this.value) : super._(); |
| 132 | + |
| 133 | + @override |
| 134 | + String toCss() => value; |
| 135 | +} |
| 136 | + |
| 137 | +final class _CssBackgroundGlobal extends CssBackground { |
| 138 | + final CssGlobal global; |
| 139 | + const _CssBackgroundGlobal(this.global) : super._(); |
| 140 | + |
| 141 | + @override |
| 142 | + String toCss() => global.toCss(); |
| 143 | +} |
0 commit comments