Building Your Own Components
Introduction
Build your own Blade components to render activity headlines, bodies, groups, and pagination. For ready-made components, use Storyfeed UI.
Building Your Own Components
These components carry no styling. They are your application's own anonymous components in resources/views/components/feed, so Blade names them <x-feed>, <x-feed.item>, and so on, separately from Storyfeed UI's <x-storyfeed::feed>.
The Feed Components
Create these anonymous Blade components in resources/views/components/feed:
| Component | File | Renders |
|---|---|---|
<x-feed> | feed.blade.php | the feed and pagination link |
<x-feed.item> | item.blade.php | an activity or group |
<x-feed.activity> | activity.blade.php | an activity row |
<x-feed.group> | group.blade.php | a group row and its members |
<x-feed.glyph> | glyph.blade.php | the icon |
<x-feed.time> | time.blade.php | the publication time |
<x-feed.body> | body.blade.php, body/key-value.blade.php, … | a body using its type |
Blade renders feed/feed.blade.php as <x-feed> because the file name matches its directory. See Laravel's anonymous index components.
Accessing Feed Items
Iterating over a feed page returns each item as a Storyfeed\Support\FeedItem. Use its methods to access the payload:
@foreach ($page as $item)
{{ $item->headline() }}
{{ $item->actor()?->label() }}
{{ $item->publishedAt()?->diffForHumans() }}
@endforeachEcho $item->headline() to render the headline with linked entity labels. Items also support array access, such as $item['verb'], and toArray() returns the item's payload array. See FeedItem API for all methods.
Parts of a Row
| Part of a Row | Methods | Payload Fields |
|---|---|---|
| icon | glyph(), intent(), actor() | glyph, glyph_intent, actor |
| headline | headline() | headline_template or headline, the role keys |
| time | publishedAt() | published_at |
| media | object()->media() | object.media |
| body | object()->bodies() | an entity's body list |
| group images | actors(), distinct('actors') | a group's sample, distinct |
| group members | children(), count() | children, count |
Omit elements whose corresponding fields are empty.
Rendering Activities
Headlines
To render a headline in Blade, echo the value returned by the headline method:
{{ $activity->headline() }}The headline replaces role tokens in headline_template with entity labels. Entities with a link render as links with its attributes, such as target. If the item contains a completed headline, that text is displayed. Other text is escaped.
The headline is a sentence with links in it, so never wrap it in another link. See Links in a Headline.
Use the toString method to return plain text for a page title or notification:
<title>{{ $activity->headline()->toString() }}</title>Entity Links
Each role method returns a Storyfeed\Support\Entity, or null for an empty role. Echo the entity to display its label, linked when it has a URL:
{{ $activity->object() }}Use the label, url, and type methods to access individual values.
To customize entity markup, pass a closure to the toHtml method. It receives each Entity and returns HTML. Escape values included in that HTML. An empty role, such as an anonymous actor, reaches the closure too, as an entity whose isAbsent() is true and that reads as Someone:
@use('Storyfeed\Support\Entity')
{!! $activity->headline()->toHtml(
fn (Entity $entity) => '<strong>'.$entity->toHtml().'</strong>'
) !!}Timestamps
The publishedAt method returns published_at as a CarbonImmutable instance, or null for a dateless entry:
@props(['at'])
@if ($at)
<time datetime="{{ $at->toAtomString() }}" {{ $attributes }}>
{{ $at->diffForHumans() }}
</time>
@endifIcons and Intents
The glyph method returns the registered icon identifier, such as shopping-bag. Provide a view for each icon and a fallback for unknown values:
@props(['glyph', 'intent' => null])
<span {{ $attributes->merge(['data-intent' => $intent]) }}>
@includeFirst(["icons.{$glyph}", 'icons.activity'])
</span>The intent method returns the application-defined value used to style the icon:
successpendingDefine intent values with the verb's intent method. Storyfeed provides no default values or colours and does not validate these strings. Map values such as success, pending, and danger to your CSS, for example with a [data-intent="success"] selector.
If no intent is defined, the method returns null and the component omits data-intent. Undefined colours leave the icon's default styling unchanged.
Activity Rows
Combine the icon, headline, and timestamp in the activity component:
@props(['activity'])
<article {{ $attributes->class('flex items-center gap-3') }}>
<x-feed.glyph
:glyph="$activity->glyph()"
:intent="$activity->intent()"
/>
<div class="flex-1">{{ $activity->headline() }}</div>
<x-feed.time :at="$activity->publishedAt()" />
</article>Rendering Groups
Group Rows
Use the isGroup method to identify a group. The children method returns its members as feed items for the activity component to render. See Aggregation for grouping rules and headline tokens.
@props(['group'])
<article {{ $attributes }}>
<x-feed.glyph
:glyph="$group->glyph()"
:intent="$group->intent()"
/>
<div>{{ $group->headline() }}</div>
<x-feed.time :at="$group->publishedAt()" />
<details @if ($group->headline()->isFallback()) open @endif>
<summary>{{ $group->count() }} activities</summary>
@foreach ($group->children() as $child)
<x-feed.activity :activity="$child" />
@endforeach
</details>
</article>The count method returns the total member count. If children contains fewer members, childrenTruncated returns true.
Plural Roles
Plural tokens such as :actors display the sampled entities and a count of the remainder: "Ana, Ben, Cy and 2 more". A singular token such as :actor displays one entity when all members share it, or the list otherwise. The :count token displays the group's activity count.
To render the sample as images, get the role's entities and total count:
@foreach ($group->actors() as $actor)
<img
src="{{ $actor->media()?->get('icon.src') }}"
alt="{{ $actor->label() }}"
>
@endforeach
@if ($group->distinct('actors') > $group->actors()->count())
+{{ $group->distinct('actors') - $group->actors()->count() }}
@endifGroups Without Headlines
If neither a group headline nor the single-activity headline applies, both payload headline fields are null. The PHP reader's $group->headline() returns a Headline value: isFallback() is true, and toString() returns the translated count, such as “3 activities”. An explicitly returned empty string does not use this null-field fallback.
To style the fallback headline separately, add a class to it:
<div @class(['muted' => $group->headline()->isFallback()])>
{{ $group->headline() }}
</div>See Fallback Headlines.
Rendering Content
The data method returns values stored with the activity. Choose which values to display.
Bodies
The bodies method returns an entity's structured content. Render the object's bodies in the activity row:
@foreach ($activity->object()?->bodies() ?? [] as $body)
<x-feed.body :body="$body" />
@endforeachEach body's $body field identifies its type, such as Storyfeed/Body/KeyValue. The component maps each $body identifier to a class and a view, upgrades older versions, and skips types it does not know:
@props(['body'])
@php
use Storyfeed\Body\Excerpt;
use Storyfeed\Body\KeyValue;
$renderers = [
KeyValue::bodyType() => [KeyValue::class, 'feed.body.key-value'],
Excerpt::bodyType() => [Excerpt::class, 'feed.body.excerpt'],
];
$renderer = $renderers[$body['$body'] ?? ''] ?? null;
$version = $body['$v'] ?? 1;
$component = null;
if ($renderer !== null && is_int($version) && $version >= 1) {
[$class, $view] = $renderer;
if ($version <= $class::version()) {
$body = $class::upgrade($body, $version);
$component = $view;
}
}
@endphp
@if ($component !== null)
<x-dynamic-component :component="$component" :body="$body" />
@endifAdd a component for each body type you render. This KeyValue component displays the placeholder when a value is null, or an empty string when both are null:
@props(['body'])
<dl {{ $attributes }}>
@foreach ($body['items'] as $item)
<dt>{{ $item['key'] }}</dt>
<dd>{{ $item['value'] ?? $item['placeholder'] ?? '' }}</dd>
@endforeach
</dl>- Pickup
- 12:10 pm
- Items
- 1
- Reference
- 1035
- Table
- not seated
An Excerpt body holds quoted words. This component shows the attribution only when from has a value:
@props(['body'])
<figure {{ $attributes }}>
<blockquote>
{{ $body['text'] }}@if ($body['truncated'] ?? false)…@endif
</blockquote>
@if ($body['from'] ?? null)
<figcaption>{{ $body['from'] }}</figcaption>
@endif
</figure>Planck’s constant is 6.62607004.
See Activity Content for body types and fields, or Custom Body Types to define your own.
Handling Missing Values
An entity's label and link may be null. An anonymous activity has no recorded actor and uses a placeholder in its headline:
| Condition | Display | Check |
|---|---|---|
| an anonymous actor | Someone | actor() is null; in a toHtml closure, isAbsent() |
| a missing label | Someone for the actor, Something for another role | isDegraded() |
| a deleted model | a removed order, a former customer, based on its former type | isTombstone(), formerType() |
| a group without a headline | 5 activities | headline()->isFallback() |
An entity without a URL is displayed as plain text. For anonymous activities, you may use a headline without an actor token.
Placeholders use Storyfeed's translation strings for the current locale. Publish the language file to customize them:
php artisan vendor:publish --tag=storyfeed-translationsThe command publishes lang/vendor/storyfeed/en/feed.php.
Assembling the Feed
The item component selects the component matching the item's kind:
@props(['item'])
@if ($item->isActivity())
<x-feed.activity :activity="$item" />
@elseif ($item->isGroup())
<x-feed.group :group="$item" />
@endifThe feed component renders each item, followed by the pagination link:
@props(['page'])
<div role="feed" {{ $attributes }}>
@foreach ($page as $item)
<x-feed.item :item="$item" />
@endforeach
</div>
{{ $page->links() }}Attributes such as <x-feed :page="$page" class="…" /> are applied to the feed's root element.
The links method renders Laravel's simple pagination view. You may customize it through Laravel's pagination views. Feeds support forward pagination only; the previous-page link is disabled and no links appear on the last page.
Displaying the Feed
Pass a paginator to the view. The cursorPaginate method retrieves the current request's cursor for subsequent pages:
use Illuminate\Support\Facades\Route;
use Storyfeed\Facades\Storyfeed;
Route::get('/', function () {
return view('feed', [
'page' => Storyfeed::feed()->cursorPaginate(15)->withQueryString(),
]);
});Render the feed with the x-feed component:
<x-feed :page="$page" />For Vue components and Inertia usage, see the Vue kit.