Content cards
Customizing the feed
Change the default feed's colors, fonts and spacing, filter and sort cards, replace the view for a card type and handle taps yourself.
Every part of the default feed can be changed without giving up its automatic logging: the theme, which cards appear and in what order, how each card type looks, and what a tap does.
Before you start
- The default feed is on screen.
1. Apply a theme
A HeraldTheme holds light and dark color sets, fonts, a corner radius and a base spacing unit. Pass the same theme to the in-app message presenter so cards and messages match.
import SwiftUI
import HeraldUI
let lumenTheme: HeraldTheme = {
var light = HeraldColors.light
light.accent = Color(red: 0.07, green: 0.55, blue: 0.42)
var dark = HeraldColors.dark
dark.accent = Color(red: 0.36, green: 0.84, blue: 0.67)
return HeraldTheme(
lightColors: light,
darkColors: dark,
typography: HeraldTypography(title: .custom("Avenir Next", size: 17, relativeTo: .headline)),
cornerRadius: 16
)
}()
struct InboxView: View {
var body: some View {
ContentCardFeed().heraldTheme(lumenTheme)
}
}import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.herald.ui.ContentCardFeed
import com.herald.ui.HeraldColors
import com.herald.ui.HeraldTheme
import com.herald.ui.LocalHeraldTheme
val LumenTheme = HeraldTheme(
lightColors = HeraldColors.Light.copy(accent = Color(0xFF128C6B)),
darkColors = HeraldColors.Dark.copy(accent = Color(0xFF5CD6AB)),
cornerRadius = 16.dp,
)
@Composable
fun InboxScreen() {
CompositionLocalProvider(LocalHeraldTheme provides LumenTheme) {
ContentCardFeed()
}
}What each color does in the feed:
| Color | Used for |
|---|---|
background | Behind the feed |
surface | Card background |
onSurface | Card title |
onSurfaceVariant | Card body and the empty-state text |
accent | Call-to-action text |
outline | Card border and image placeholder |
onAccent and scrim are used by in-app messages. Fonts: title, body and button (the call to action). spacing is the card padding and the gap around cards. darkMode set to true or false forces dark or light colors; nil / null follows the system.
Keep text scalable when you change fonts. On iOS, build custom fonts with Font.custom(_:size:relativeTo:) so Dynamic Type still applies. On Android, give sizes in sp.
2. Filter and sort
filter keeps the cards it returns true for. order replaces the default pinned-first, newest-first sort.
// Only workout tips, oldest first.
ContentCardFeed(
filter: { $0.categories.contains("tips") },
order: { $0.createdAt < $1.createdAt }
)// Only workout tips, oldest first.
ContentCardFeed(
filter = { "tips" in it.categories },
order = compareBy { it.createdAt },
)The default sort is available as ContentCardFeedDefaults.order (iOS) and ContentCardFeedDefaults.Order (Android) if you want to filter but keep it.
3. Handle taps yourself
By default a tap opens the card's url. Pass onCardClick to route it yourself. The SDK logs the click before your handler runs.
ContentCardFeed(onCardClick: { card in
router.open(card)
})ContentCardFeed(onCardClick = { card -> navController.navigate("card/${card.id}") })4. Replace the view for a card type
Pass cardContent to draw cards yourself. Call onClick when your view is tapped, so the click is logged and handled. Fall back to the default view for the types you do not replace. The feed still measures impressions and handles swipe to dismiss around your view.
ContentCardFeed { card, onClick in
if card.type == .classic {
LumenTipCard(card: card, onClick: onClick)
} else {
ContentCardView(card: card, onClick: onClick)
}
}
struct LumenTipCard: View {
let card: ContentCard
let onClick: () -> Void
var body: some View {
Button(action: onClick) {
Label(card.title ?? "", systemImage: "figure.run")
.frame(maxWidth: .infinity, minHeight: 44, alignment: .leading)
}
}
}ContentCardFeed(
cardContent = { card, onClick ->
if (card.type == ContentCardType.CLASSIC) {
LumenTipCard(card, onClick)
} else {
ContentCardFeedDefaults.Card(card, onClick)
}
},
)
@Composable
fun LumenTipCard(card: ContentCard, onClick: () -> Unit) {
TextButton(onClick = onClick, modifier = Modifier.fillMaxWidth().heightIn(min = 48.dp)) {
Text(card.title.orEmpty())
}
}The default per-type views are public, so you can reuse them inside your own layout: ClassicContentCardView, CaptionedImageContentCardView and ImageOnlyContentCardView on iOS, ClassicContentCard, CaptionedImageContentCard and ImageOnlyContentCard on Android.
5. Change the empty state and labels
ContentCardFeed(emptyMessage: "You're all caught up")ContentCardFeed(emptyContent = { Text("You're all caught up") })- iOS looks up
emptyMessage, the Dismiss action label and the Close button label in your app's strings files, so add those keys to translate them. - Android reads the
herald_feed_empty,herald_dismissandherald_closestring resources. Override them in your app'sres/values*/strings.xml. - On Android,
contentPaddingsets the padding around the list, andmodifierapplies to the list itself.
Verify it works
Switch the device between light and dark mode and raise the system text size: the feed should follow both. Tap a card with your onCardClick handler and confirm it runs once.
Troubleshooting
- The theme has no effect (iOS).
.heraldTheme(_:)must be applied to the feed or one of its ancestors, not to a sibling view. - The theme has no effect (Android).
ContentCardFeedmust be inside theCompositionLocalProviderthat providesLocalHeraldTheme. - Custom cards are never marked as viewed. Impressions are measured on the view you return, so it needs a real size. A zero-height view never counts as half visible.
- A custom card's tap is not logged. Call the
onClickyou were given, not your own navigation code directly.
Next
- Building a custom UI: drop the feed entirely.
- Theming and dark mode: apply the same theme to in-app messages.