Custom Body Types
Introduction
Use a custom component to render application-specific props, or define a versioned body type when you need to upgrade its stored payload over time.
Using Custom Components
A Component body names a frontend component and passes it props. For example, an order at Scoops Ahoy may show its pickup progress as a stepper. The built-in bodies can display text and fields; a custom component can connect the steps and highlight the current one.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\Component;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedEntity;
class Order extends Model implements Feedable
{
use InteractsWithFeed;
public function toFeed(): FeedEntity
{
return FeedEntity::make()
->label("Order #{$this->id}")
->body(
Component::make()
->name('Orders/Progress')
->props([
'title' => "Order #{$this->id}",
'steps' => ['Placed', 'Confirmed', 'Ready'],
'current' => $this->status_label,
'pickup' => $this->pickup_at->format('g:i A'),
]),
);
}
}<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Body\Component;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedEntity;
class Order extends Model implements Feedable
{
use InteractsWithFeed;
public function toFeed(): FeedEntity
{
return FeedEntity::make(
label: "Order #{$this->id}",
body: Component::make(
name: 'Orders/Progress',
props: [
'title' => "Order #{$this->id}",
'steps' => ['Placed', 'Confirmed', 'Ready'],
'current' => $this->status_label,
'pickup' => $this->pickup_at->format('g:i A'),
],
),
);
}
}Props are stored with the body and reflect their values when the body was built. To display current progress whenever the feed is retrieved, build the body in feedMedia().
Rendering the Component
Your frontend maps each name to a component. In Vue, the component receives the stored props and marks the current step with aria-current:
<script setup>
defineProps(['title', 'steps', 'current', 'pickup'])
</script>
<template>
<section class="order-progress" :aria-label="`${title} pickup progress`">
<strong>{{ title }}</strong>
<p>Pickup at {{ pickup }}</p>
<ol>
<li v-for="step in steps" :key="step"
:aria-current="step === current ? 'step' : undefined">
{{ step }}
</li>
</ol>
</section>
</template>Register the component under the same name used in PHP. The kit's Storyfeed/Body/Component renderer looks the name up and passes the component the body's props:
<script setup lang="ts">
import { provide } from 'vue';
import { FEED_COMPONENTS } from '@/components/storyfeed/keys';
import Progress from '@/components/orders/Progress.vue';
provide(FEED_COMPONENTS, { 'Orders/Progress': Progress });
</script>import { FeedProvider, FeedStream } from '@/components/storyfeed';
import Progress from '@/components/orders/Progress';
<FeedProvider FEED_COMPONENTS={{ 'Orders/Progress': Progress }}>
<FeedStream page={feed} />
</FeedProvider>use Storyfeed\Ui\Support\BodyComponents;
app(BodyComponents::class)->register('Orders/Progress', 'orders.progress');Blade renders <x-orders.progress> and passes it the props. A name with no registered component draws nothing. Style the list as a stepper, with [aria-current="step"] highlighting the current step:
Pickup at 12:15 PM
- Placed
- Confirmed
- Ready
Names are stored unchanged. Like data(), props() merges an array of keys or sets one with ->props('current', 'Ready').
Use Component for props you control. If their structure will change over time, define a body type with its own upgrade() method.
Writing Body Types
Extend Storyfeed\FeedBody and return the body's own fields from body():
<?php
namespace App\Feed;
use Storyfeed\FeedBody;
class Attachment extends FeedBody
{
protected ?int $size = null;
protected ?string $mediaType = null;
protected function __construct(?int $size = null, ?string $mediaType = null)
{
$this->size($size)->mediaType($mediaType);
}
public function size(?int $size): static
{
$this->size = $size;
return $this;
}
public function mediaType(?string $mediaType): static
{
$this->mediaType = $mediaType;
return $this;
}
public static function bodyType(): string
{
return 'Acme/Attachment';
}
protected function body(): array
{
// Values only: no markup, and never another body.
return [
'size' => $this->required($this->size, 'size'),
'mediaType' => $this->mediaType,
];
}
protected static function defaults(): array
{
return ['mediaType' => null];
}
}make() takes the constructor's arguments, so the chain and named arguments build the same body:
use App\Feed\Attachment;
Attachment::make()->size($this->bytes)->mediaType('application/pdf');
Attachment::make(size: $this->bytes, mediaType: 'application/pdf');FeedBody writes the reserved keys, and leaves out any field that still holds its value from defaults(). Its upgrade() method puts those defaults back when the body is read. required() throws when the body is used without a value, naming the method that sets it. Every body also has fallback(), maxHeight(), fullHeight() and withMeta().
A class that cannot extend FeedBody, such as a data object, implements Storyfeed\Contracts\FeedBody and writes toPayload() itself, with the HasPayload trait for toArray().
Type Names
Return a PascalCase body type name from bodyType(), such as Storyfeed/Body/MediaObject or Acme/Attachment. Renderers match it exactly. Stored bodies keep this name even if you move the PHP class.
Reserved Keys
| Key | Constant | Holds |
|---|---|---|
$body | FeedBody::KEY | the body type's name, verbatim |
$v | FeedBody::VERSION | the version that wrote the body |
$fallback | FeedBody::FALLBACK | one line of plain text for a renderer that cannot draw the type |
$meta | FeedBody::META | renderer settings, such as maxHeight |
The $ prefix keeps them apart from your own keys.
Rendering Body Types
Give each kit a renderer for the body type's exact name. The renderer receives the body and the entity that carries it:
@use('App\Feed\Attachment')
@props(['body', 'entity' => null])
@php($body = Attachment::upgrade($body, $body['$v'] ?? 1))
<p>{{ $body['mediaType'] }} · {{ $body['size'] }} bytes</p>import { feedBodies } from '@/components/storyfeed/body';
import Attachment from '@/components/feed/Attachment.vue';
createApp(App).use(feedBodies({ 'Acme/Attachment': Attachment }));import { FeedProvider, FeedStream } from '@/components/storyfeed';
import Attachment from '@/components/feed/Attachment';
<FeedProvider FEED_BODIES={{ 'Acme/Attachment': Attachment }}>
<FeedStream page={feed} />
</FeedProvider>Blade finds the view by the type's name, segment by segment, under resources/views/vendor/storyfeed/components/body. A Vue renderer receives payload, entityLabel, entityUrl, entityLink and entityMedia props; a React renderer receives BodyProps. A renderer registered for one of Storyfeed's own types replaces the kit's. A body type with no renderer draws its $fallback line, or nothing.
Versions and Upgrades
version() starts at 1. When the body's shape changes, raise version() and override upgrade() to convert a payload an older version wrote. Storyfeed stores the body as written, $v included, and returns it unchanged. A Blade view calls upgrade() with the stored $v, as above. A Vue or React renderer applies the same steps in TypeScript.
See Activity Content for built-in bodies and Resolving Bodies When Retrieved for current values.