Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion site/lib/src/components/pages/widget_catalog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class WidgetCatalogCard extends StatelessComponent {
return a(href: widget.link, classes: 'card outlined-card', [
_buildCardImageHolder(),
div(classes: 'card-header', [
span(classes: 'card-title', [.text(widget.name)]),
span(classes: 'card-title', _splitCamelCase(widget.name)),
]),
div(classes: 'card-content', [
p([
Expand Down Expand Up @@ -184,3 +184,50 @@ class WidgetCatalogCard extends StatelessComponent {
);
}
}

class WidgetCardGrid extends StatelessComponent {
const WidgetCardGrid({
required this.widgets,
required this.isMaterialCatalog,
this.subcategory,
});

final List<WidgetCatalogWidget> widgets;
final bool isMaterialCatalog;
final WidgetCatalogSubcategory? subcategory;

@override
Component build(BuildContext _) => div(
classes: [
'card-grid',
if (isMaterialCatalog) 'material-cards',
].toClasses,
[
for (final widget in widgets)
WidgetCatalogCard(
widget: widget,
isMaterialCatalog: isMaterialCatalog,
subcategory: subcategory,
),
],
);
}

final RegExp _camelCaseSplitRegex = RegExp(
r'(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])',
);

/// Splits an UpperCamelCase [name] into its constituent parts,
/// interleaving `<wbr>` elements to allow the browser to
/// break long widget names across lines.
List<Component> _splitCamelCase(String name) {
final parts = name.split(_camelCaseSplitRegex);
if (parts.length <= 1) return [.text(name)];
return [
.text(parts.first),
for (var partIndex = 1; partIndex < parts.length; partIndex += 1) ...[
const wbr(),
.text(parts[partIndex]),
],
];
Comment on lines +224 to +232
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This implementation can be simplified by using the intersperse method from the collection package, which is already imported in this file. This will make the code more concise and declarative.

Suggested change
final parts = name.split(_camelCaseSplitRegex);
if (parts.length <= 1) return [.text(name)];
return [
.text(parts.first),
for (var partIndex = 1; partIndex < parts.length; partIndex += 1) ...[
const wbr(),
.text(parts[partIndex]),
],
];
final parts = name.split(_camelCaseSplitRegex);
return parts.map((p) => .text(p)).intersperse(const wbr()).toList();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two issues with this:

  1. The dot shorthand syntax doesn't work properly in the map call since there's no inferred Component context type there.
  2. There's no in intersperse method in package:collection or the standard library. Add separated, separatedList and separate to iterables and lists. dart-lang/core#919 adds separated and separatedList but those haven't been released yet.

}
29 changes: 4 additions & 25 deletions site/lib/src/pages/widget_catalog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ List<MemoryPage> get widgetCatalogPages {

// Only show main category widgets for non-material catalogs.
if (!isMaterialCatalog && widgetsInCategory.isNotEmpty)
_buildCardGrid(
widgetsInCategory,
WidgetCardGrid(
widgets: widgetsInCategory,
isMaterialCatalog: isMaterialCatalog,
),

Expand Down Expand Up @@ -166,31 +166,10 @@ List<Component> _buildSubcategorySection(

return [
h2(id: slugify(subName), [.text(subName)]),
_buildCardGrid(
widgets,
WidgetCardGrid(
widgets: widgets,
isMaterialCatalog: isMaterialCatalog,
subcategory: subcategory,
),
];
}

Component _buildCardGrid(
List<WidgetCatalogWidget> widgets, {
required bool isMaterialCatalog,
WidgetCatalogSubcategory? subcategory,
}) {
return div(
classes: [
'card-grid',
if (isMaterialCatalog) 'material-cards',
].toClasses,
[
for (final widget in widgets)
WidgetCatalogCard(
widget: widget,
isMaterialCatalog: isMaterialCatalog,
subcategory: subcategory,
),
],
);
}
Loading