|
5 | 5 | import 'package:jaspr/dom.dart'; |
6 | 6 | import 'package:jaspr/jaspr.dart'; |
7 | 7 |
|
8 | | -/// The site-wide banner. |
9 | | -class DashBanner extends StatelessComponent { |
10 | | - const DashBanner(this.inlineHtmlContent, {super.key}); |
| 8 | +/// The information to display in the site banner, |
| 9 | +/// as configured in `src/data/banner.yml`. |
| 10 | +@immutable |
| 11 | +final class BannerContent { |
| 12 | + /// The ordered content parts to render in the banner. |
| 13 | + final List<BannerPart> parts; |
| 14 | + |
| 15 | + /// Creates banner content from the specified [parts]. |
| 16 | + const BannerContent({required this.parts}); |
| 17 | + |
| 18 | + /// Creates banner content from the parsed [bannerData]. |
| 19 | + /// |
| 20 | + /// The [bannerData] list is expected to contain |
| 21 | + /// `text`, `link`, or `newLine` entries from `src/data/banner.yml`. |
| 22 | + /// |
| 23 | + /// Throws if any entry has an unsupported structure. |
| 24 | + factory BannerContent.fromList(List<Object?> bannerData) => BannerContent( |
| 25 | + parts: [ |
| 26 | + for (final item in bannerData) |
| 27 | + switch (item) { |
| 28 | + {'text': final String text} => .text(text), |
| 29 | + {'link': final Map<Object?, Object?> link} => .link( |
| 30 | + text: link['text'] as String, |
| 31 | + url: link['url'] as String, |
| 32 | + newTab: link['newTab'] as bool? ?? false, |
| 33 | + ), |
| 34 | + {'newLine': _} => const .newLine(), |
| 35 | + _ => throw FormatException('Invalid banner item: $item'), |
| 36 | + }, |
| 37 | + ], |
| 38 | + ); |
| 39 | +} |
| 40 | + |
| 41 | +/// A single renderable piece of banner content. |
| 42 | +@immutable |
| 43 | +sealed class BannerPart { |
| 44 | + /// Creates a banner content part. |
| 45 | + const BannerPart(); |
| 46 | + |
| 47 | + /// Creates a text part with the specified [text]. |
| 48 | + const factory BannerPart.text(String text) = _BannerText; |
| 49 | + |
| 50 | + /// Creates a link part with the specified [text] and [url]. |
| 51 | + /// |
| 52 | + /// Unless [newTab] is `true`, the link opens in the same tab. |
| 53 | + const factory BannerPart.link({ |
| 54 | + required String text, |
| 55 | + required String url, |
| 56 | + bool newTab, |
| 57 | + }) = _BannerLink; |
| 58 | + |
| 59 | + /// Creates a new line part that renders a line break. |
| 60 | + const factory BannerPart.newLine() = _BannerNewLine; |
| 61 | +} |
| 62 | + |
| 63 | +/// Plain text within a site banner. |
| 64 | +final class _BannerText extends BannerPart { |
| 65 | + /// Creates a text banner part with the specified [text]. |
| 66 | + const _BannerText(this.text); |
| 67 | + |
| 68 | + /// The text to render in the banner. |
| 69 | + final String text; |
| 70 | +} |
11 | 71 |
|
12 | | - /// The raw, inline HTML content to render in the banner. |
| 72 | +/// A link within a site banner. |
| 73 | +final class _BannerLink extends BannerPart { |
| 74 | + /// Creates a link banner part with the specified [text] and [url]. |
13 | 75 | /// |
14 | | - /// This should only be sourced from managed content, |
15 | | - /// such as our checked-in data files. |
16 | | - final String inlineHtmlContent; |
| 76 | + /// Unless [newTab] is `true`, the link opens in the same tab. |
| 77 | + const _BannerLink({ |
| 78 | + required this.text, |
| 79 | + required this.url, |
| 80 | + this.newTab = false, |
| 81 | + }); |
| 82 | + |
| 83 | + /// The link label to render in the banner. |
| 84 | + final String text; |
| 85 | + |
| 86 | + /// The destination URL for this link. |
| 87 | + final String url; |
| 88 | + |
| 89 | + /// Whether this link opens in a new browser tab. |
| 90 | + final bool newTab; |
| 91 | +} |
| 92 | + |
| 93 | +/// A line break within a site banner. |
| 94 | +final class _BannerNewLine extends BannerPart { |
| 95 | + /// Creates a line break banner part. |
| 96 | + const _BannerNewLine(); |
| 97 | +} |
| 98 | + |
| 99 | +/// A site-wide banner rendered from structured content. |
| 100 | +class DashBanner extends StatelessComponent { |
| 101 | + /// Creates a site banner that displays the specified [content]. |
| 102 | + const DashBanner(this.content, {super.key}); |
| 103 | + |
| 104 | + /// The structured content to render in this banner. |
| 105 | + final BannerContent content; |
17 | 106 |
|
18 | 107 | @override |
19 | 108 | Component build(BuildContext context) => div( |
20 | 109 | id: 'site-banner', |
21 | 110 | attributes: {'role': 'alert'}, |
22 | 111 | [ |
23 | 112 | p([ |
24 | | - RawText(inlineHtmlContent), |
| 113 | + for (final part in content.parts) |
| 114 | + switch (part) { |
| 115 | + _BannerText(:final text) => .text(text), |
| 116 | + _BannerLink(:final text, :final url, :final newTab) => a( |
| 117 | + href: url, |
| 118 | + target: newTab ? Target.blank : null, |
| 119 | + attributes: newTab ? const {'rel': 'noopener'} : null, |
| 120 | + [.text(text)], |
| 121 | + ), |
| 122 | + _BannerNewLine() => const br(), |
| 123 | + }, |
25 | 124 | ]), |
26 | 125 | ], |
27 | 126 | ); |
|
0 commit comments