Search by

preflow / block-library

smyr

Preflow block library — ready-made blocks a site switches on and restyles: columns (a column layout from a site-defined list), hero, text, image, gallery (with an optional lightbox), quote, divider, call to action, cards, video (a privacy player), table, FAQ

Package info

github.com/getpreflow/block-library

pkg:composer/preflow/block-library

Statistics

Installs: 26

Dependents: 2

Suggesters: 0

Stars: 0

Open Issues: 0

v0.98.0 2026-11-29 21:08 UTC

This package is auto-updated.

Last update: 2026-12-01 01:19:54 UTC


README

Ready-made blocks that a site switches on and restyles: columns, and the starter blocks hero, text, image, gallery, quote, divider, call_to_action, cards, video, table and faq (with card and faq_item, which live inside cards and faq).

The problem: every site writes the same blocks again

Almost every site needs an opening section, a block of formatted text, an image, a few images in a grid, a quote, a box that asks the visitor to do something, a row of cards, a video, a table, questions with their answers and a line between two sections.

Without a library each site writes them again: its own fields, its own markup, its own CSS, its own mistakes. One forgets the image's alt text, one loads YouTube before anyone clicks play, one nests a link inside a link. A site moved to another design has to learn them all anew, and an editor who works on two sites finds a "Hero" with other fields on each.

The idea: blocks a site switches on and restyles

The library brings these blocks ready to use: their fields, their labels in English, German and French, a Twig and a Blade template, and plain CSS. A site decides four things about them:

Question Where it lives Example
Which blocks does the site get? config/blocks.php packages 'preflow/block-library' => true, or ['except' => ['gallery']]
Which blocks does a field offer? the field's allowed (a model file, a #[BlockField]) "allowed": ["hero", "text", "cards"]
What do they look like? CSS custom properties, on :root or one section --pf-block-accent: #b4462b;
What if a block should be different? a site block with the same key app/Components/Blocks/Hero/Hero.php with key: 'hero'

What you don't write: the fields, the templates for two engines, the CSS, the lightbox's script, the privacy player, the checks of a save, the editor's forms. Everything is core: a site without Folio renders the same blocks, and a REST write is checked by the same rules as Folio's form.

Install

composer require preflow/block-library

Then list the provider in config/providers.php (with or without Folio):

return [
    // …
    \Preflow\BlockLibrary\BlockLibraryServiceProvider::class,
];

Installing the package registers only the columns block (its composer.json lists it under extra.preflow.blocks). The provider registers the column layouts the block takes its layouts from (the area layout set columns) and the texts in English, German and French. A site that installs the package but does not list the provider gets this error when the block registry is built:

Block "columns": field "columns": No package registers the area layout set "columns". The package
that brings the block registers it in its service provider (config/providers.php).

Switching the starter blocks on

Installing the package switches nothing on except columns (as since 0.92). The other blocks are listed under extra.preflow.blocks-on-request in the package's composer.json, and the site asks for them by naming the package in config/blocks.php:

// config/blocks.php
return [
    'packages' => [
        'preflow/block-library' => true,                          // every block of the package
        // 'preflow/block-library' => ['except' => ['gallery']], // all but these keys
    ],
];

except takes block keys, and may name columns too, to switch it off. A key the package has no block for is refused when the registry is built, with the keys it does have:

config/blocks.php packages "preflow/block-library" except names "galery"; the package's blocks are:
hero, text, image, gallery, quote, divider, call_to_action, cards, card, video, table, faq, faq_item, columns.

Switched on, the blocks use the template functions of preflow/fields (links, the video player, tables) and preflow/media (images). A site with Folio has both. A site without Folio lists their providers:

// config/providers.php
return [
    \Preflow\Blocks\BlocksServiceProvider::class,
    \Preflow\Fields\FieldsServiceProvider::class,
    \Preflow\Media\MediaServiceProvider::class,
    \Preflow\BlockLibrary\BlockLibraryServiceProvider::class,
];

Without one of them the boot stops and says what to do:

The blocks of preflow/block-library are switched on (config/blocks.php packages) and need the
template functions of preflow/media; add \Preflow\Media\MediaServiceProvider::class to
config/providers.php (a site with Folio has them already), or remove "preflow/block-library" from
config/blocks.php packages to switch them off.

Then offer them where editors add blocks: a blocks field without allowed offers every block (except card and faq_item, below); one with allowed offers the keys it lists.

The blocks

Every block follows the same rules, so a site can count on them:

  • One root class, pf-<key> with - for _ (pf-call-to-action), parts pf-<key>__part, variants pf-<key>--variant. No style attributes, no !important.
  • Headings only where the editor gave a title. A block's title is an <h2>: the page's own template owns the <h1>. A block prints no landmark (<section>, <nav>).
  • Images keep their alt text from the media library (media_image()).
  • Nothing half-made. A block whose required value is missing (a hero without a title, a gallery whose images are all gone) renders nothing, and an optional part that is empty (a caption, a second button) is left out, never printed empty. Since 0.96 a required text that is only blank (spaces, line breaks, no-break or zero-width characters) counts as missing: the text, a question or answer, a quote, a hero's or call to action's title, a caption and a card's title are all judged this way.
  • No link without a name or an address. A button is printed only when link_href() gives an address and link_label() a text in the page's language, so a javascript: link or a link to an unpublished page never renders.
  • Words come from the lang files. A separator, a button text for screen readers, the lightbox's labels: block-library.<key>.…, in the page's language.

In the tables, translatable means one value per language, as every translatable field.

hero: a section's opening

A title, a short text, a button, and an image beside them on a wide block, above them on a narrow one. Group Content.

field type notes
title string, required, translatable an <h2>; without it the block renders nothing
text text, translatable paragraphs at blank lines
image asset (an image) beside the text from --pf-hero-breakpoint up, above it below; never a background image
button link shown only with an address and a label
property default what it does
--pf-hero-breakpoint 40rem below this width of the block the image goes above the text
--pf-hero-gap 2rem the space between the text and the image
--pf-hero-title-size 2.5em the title's font size
--pf-hero-button-color Canvas the button's text (the page's background colour)

The text comes first in the markup, so it is read first; the CSS puts the image above it when the block stacks. The breakpoint is the block's own width, not the window's: no media query.

text: formatted text

The editor's rich text: paragraphs, headings, lists and links. Group Content.

field type notes
text rich text, required, translatable cleaned on every save and again on render, as all rich text
property default what it does
--pf-text-measure none the longest line; 65ch reads well

It renders <div class="pf-text"> with the cleaned HTML and leaves the typography to the site.

image: one image with its caption

Group Media.

field type notes
image asset (an image), required its alt text comes from the media library
caption string, translatable a <figcaption>; none when empty
width select: content (as wide as the text, the default), wide, full a class, pf-image--wide
property default what it does
--pf-image-content 40rem the width of an image "as wide as the text"
--pf-image-wide 64rem the width of a "wide" image

full takes the whole width of the block. An image the library no longer has renders nothing.

gallery: several images in a grid

Group Media.

field type notes
images assets (several images), required in the editor's order; images the library no longer has are left out
columns select: 2, 3 (the default), 4 at most that many; fewer when the block is too narrow
lightbox yes or no (default no) a click opens the image in a dialog on the page; see below
property default what it does
--pf-gallery-gap 0.75rem the space between thumbnails
--pf-gallery-min 8rem the narrowest a thumbnail gets before a column goes
--pf-gallery-ratio 1 the thumbnails' shape (4 / 3, or auto for the image's own)
--pf-gallery-backdrop rgb(0 0 0 / 0.85) behind the lightbox's image
--pf-gallery-control rgb(255 255 255 / 0.9) the lightbox's buttons
--pf-gallery-control-bg rgb(0 0 0 / 0.5) behind the lightbox's buttons

Each thumbnail is a link to its image's file (media_url()), so the large image works without any script. A thumbnail whose image has no alt text in the page's language is named by its place ("Image 2 of 5", block-library.gallery.open), so no link is without a name.

quote: a quotation

Group Content.

field type notes
quote text, required, translatable paragraphs at blank lines, inside <blockquote>
author string who said it
source string, translatable where: a book, a talk, a website; a <cite>
property default what it does
--pf-quote-bar 0.25rem the bar's width at the quote's start (0 for none)
--pf-quote-indent 1.25rem the space between the bar and the text
--pf-quote-size 1.25em the quote's font size
--pf-quote-style italic the quote's font style

It renders <figure class="pf-quote"><blockquote>…</blockquote><figcaption>, with no figcaption when author and source are both empty. The dash before the author is decoration, not read aloud.

divider: a line or some space

Group Layout.

field type notes
kind select: line (the default), space a line is an <hr>; a space an empty <div aria-hidden="true">
size select: s, m (the default), l a line's space above and below; a space's height
property default what it does
--pf-divider-s 1rem the small size
--pf-divider-m 2rem the medium size
--pf-divider-l 4rem the large size
--pf-divider-thickness 1px the line's thickness

An unknown stored value reads as the default, so nothing an import writes reaches a class name.

call_to_action: a box that asks for one thing

A title, a short text and one or two buttons. Group Content.

field type notes
title string, required, translatable an <h2>
text text, translatable paragraphs at blank lines
button link, required without an address and a label the block renders nothing
has_second yes or no switches the second button on
second_button link shown in the form only while has_second is on (show_when); beside the first, less prominent
property default what it does
--pf-call-to-action-padding 2rem the space inside the box
--pf-call-to-action-title-size 1.75em the title's font size
--pf-call-to-action-button-color Canvas the first button's text

Switching has_second off keeps the second button's stored value (a field its show_when hides keeps its value on every write, the API's too); switching it on again shows it.

cards and card: cards in a grid

cards holds a list of card blocks in two, three or four columns. Group Content. card is offered only inside cards (it is nested; below).

cards field type notes
title string, translatable an <h2>; the cards' titles are then <h3>, else <h2>
cards blocks: only card, at least one
columns select: 2, 3 (the default), 4 at most that many; fewer when the block is too narrow
card field type notes
title string, translatable empty: the linked record's title (see "A card that links to a record")
text text, translatable paragraphs at blank lines
image asset (an image) shown above the title; after it in the markup, so the title is read first
link link the whole card is clickable; the card's title names the link
property default what it does
--pf-cards-gap 1.5rem the space between cards
--pf-cards-min 14rem the narrowest a card gets before a column goes
--pf-cards-padding 1rem the space inside a card
--pf-cards-ratio 16 / 9 the images' shape (or auto for the image's own)

A card has one link: an <a> around its title, stretched over the card by CSS. The image is never a second link, and no link sits inside another. A card left without a title is not shown, and neither is a cards block with no card to show. Since 0.96 every checked write (Folio's form, publishing, an import, a seed, a REST write) refuses a card with neither a title nor a link to a page: "A card needs a title, or a link to a page whose title it shows." A draft is checked when it is published.

video: a video that loads only when played

Group Media.

field type notes
video embed (YouTube, Vimeo), required a value that is no video, or does not match its address, renders nothing
caption string, translatable a <figcaption>; it also names the player (else "Video")
property default what it does
--pf-video-width 40rem the player's width (none for the whole width)
--pf-video-background color-mix(in srgb, currentColor 8%, transparent) the play area before the video loads

It renders preflow/fields' privacy player (embed_player()): a play button, and nothing loads from the video's host before a click. The player brings its own small script from preflow/fields; the library adds none.

table: a table of text

Group Media.

field type notes
table table, required the header row names the columns (<th scope="col">); an empty table renders nothing
caption string, translatable above the table: what it shows
property default what it does
--pf-table-padding 0.5em 0.75em the space inside a cell
--pf-table-head transparent the header row's background
--pf-table-head-rule 2px the line under the header row
--pf-table-stripe transparent every second row's background

It renders table_html() inside <figure class="pf-table">; a table wider than the block scrolls sideways inside it, not the page.

faq and faq_item: questions and answers

faq holds a list of faq_item blocks. Group Content. faq_item is offered only inside faq.

faq field type notes
title string, translatable an <h2>
items blocks: only faq_item, at least one
faq_item field type notes
question string, required, translatable the <summary>
answer rich text, required, translatable cleaned like every rich text
property default what it does
--pf-faq-padding 0.75rem 1rem the space inside a question and an answer
--pf-faq-spacing 0.5rem the space between two questions
--pf-faq-weight 600 the question's font weight

Each question is a <details> with its <summary>: it opens and closes without a script and works with the keyboard. An item without a question or an answer is left out.

Blocks that live inside another: card and faq_item

A card on its own at the top of a page means nothing. So card and faq_item are declared #[Block(..., nested: true)] (preflow/blocks): a blocks field offers them only when its allowed lists them, as cards and faq do. They are data only: their parent's template prints them. A save that puts one anywhere else is refused, through Folio's form and the API alike:

sections.<uid>._block: "Card" can only be placed inside a block that offers it.

Restyling every block at once

Each block reads the same shared custom properties, each with its default, so one rule changes them all, on :root or on one section:

:root { --pf-block-gap: 2rem; --pf-block-accent: #b4462b; }
property default what it does
--pf-block-gap 1.5rem the space around a block
--pf-block-radius 0.5rem the corners of boxes and images
--pf-block-accent currentColor a quote's bar, links, the hero's and the call to action's buttons
--pf-block-muted color-mix(in srgb, currentColor 60%, transparent) captions, a quote's author
--pf-block-surface transparent the background of boxes: a call to action, a card, a question
--pf-block-border color-mix(in srgb, currentColor 20%, transparent) lines: a divider, a table's rules, a question's outline

A site with design tokens maps them once, and the blocks follow its dark mode too:

:root {
    --pf-block-accent: var(--accent);
    --pf-block-border: var(--line);
    --pf-block-surface: var(--card);
}

A block's own properties (the tables above) are named after it and listed at the top of its CSS file (src/Quote/Quote.css). Set them on the block's class (.pf-quote { --pf-quote-bar: 0; }) or on any parent. Each block adds its CSS once per page, into the page's style element with its CSP nonce, outside the component's CSS scope, so a site's rules on .pf-… apply as written.

The gallery's lightbox

With lightbox switched on, a click on a thumbnail opens the image in a <dialog>: previous and next buttons and the arrow keys move through the gallery's images, each move is announced ("Image 2 of 5"), Esc, the close button or a click beside the image close it, and the focus goes back to the thumbnail of the image shown last. A click with a modifier key (to open the file in a new tab) is left to the browser.

src/Gallery/Gallery.js is the library's only script. It is added once per page, inline with the page's CSP nonce, and only when a gallery on the page has the lightbox on and images to show. Its words for screen readers come from the lang files (block-library.gallery.lightbox.*, on the gallery as data-pf-gallery-* attributes), so the script holds no words. Without the script, or in a browser without <dialog>, each thumbnail is still the link to its image's file.

The dialog loads a large variant, not the file: the widest WebP variant the media package makes of the image (ImageTag::sizes()), which the link carries as data-pf-gallery-large (since 0.96). An image without variants opens its file, as the link does.

A card that links to a record

A card whose own title is empty and whose link points to a record shows the record's title, as the page reads the record: the published title on the site, the working copy's in a preview.

The cards block does not read the record itself (the data manager always reads working copies, which would leak a draft's title onto the site). It asks the bound Preflow\Fields\RecordAddresses, when that object also implements Preflow\Fields\RecordTitles:

interface RecordTitles
{
    public function titleOf(string $type, string $id, string $locale): ?string;
}

Folio's does. A site without Folio implements it on its own RecordAddresses (the fields README). The title is used only while the record has an address on the page: none when it is not in the page's language or its type has no pages. A card that ends up without a title is not shown. So the site shows no card for an unpublished or deleted record, no link without a name and no record card that links nowhere. Without RecordTitles, a record card needs a title of its own.

The card's rule (Preflow\BlockLibrary\Card\TitleOrPage, on its title) makes the same test as the page: a card without a title of its own is saved only when its record has an address and a title (in its working copy, see below). Without RecordTitles the rule cannot know the title, so any card that links to a record is saved (and shows only with a title of its own).

The rule checks the default language of the type's locales only, as required does for a translatable field: a card whose title is written only in another language is refused, and the record must have its address and title in the default language.

Every write judges the linked record by one fixed read, its working copy, whatever the request reads (since 0.96). Folio's form, its publish action, REST, RecordInput in a script, a seed and an import give the same verdict for the same value. The rule asks the bound RecordAddresses for its asWritten() view when it also implements Preflow\Fields\RecordsAsWritten; Folio's does, through publishing's LiveContent::findWorkingCopy(). So a card that links to a draft is saved, and the site leaves the card out until the record is published. A site without Folio implements RecordsAsWritten on its own RecordAddresses for the same verdict; without it the rule asks the RecordAddresses as it is.

Replacing a block

A site that wants other markup or other fields for one block writes its own block with the same key. A site block (in app/Components, app/Blocks or config classes) replaces a package block with that key; nothing else changes. BlockRegistry::replaced() names each replacement, so a replacement is never silent:

$container->get(\Preflow\Blocks\BlockRegistry::class)->replaced();
// ['hero' => ['site' => App\Components\Blocks\Hero\Hero::class, 'package' => Preflow\BlockLibrary\Hero\Hero::class]]

To start from the library's block, copy its folder (src/Hero/) into app/Components/Blocks/, change the namespace, and edit the template. The class's comment lists what its template gets; the template calls only template functions every site with the library has (link_href, link_attrs, link_label, embed_player, table_html, media_image, media_url, t and blocks), so the copy works as it is. Keep the field names when records already store the block: stored values are matched by name.

To drop a block instead, list its key in except.

Columns: one section, many column layouts

A page section is sometimes one column, sometimes two with a wide left side, sometimes three cards in a row. Without a columns block, a site either writes a block per variant (two_columns, three_columns, wide_left), each with its own template and CSS, or it gives editors raw HTML. Both break when an editor changes their mind: the content has to be moved by hand into another block, and something gets lost on the way.

The columns block holds a column layout and the blocks of each column. The site lists the layouts an editor can pick from; a default set ships. One template renders every layout. When an editor switches to fewer columns, the blocks of the columns that are gone are kept and shown in a "Not shown in this layout" panel. They are never rendered and never dropped.

Question Where it lives Example
Which layouts can an editor pick? config/blocks.php column_layouts full, 1-1, 2-1, 1-2, 1-1-1, 1-1-1-1
How wide is each column? the layout's widths (shares of the row, 1–12) [2, 1]: two thirds and one third
Which blocks may go into a column? config/blocks.php column_blocks every block except columns (the default)
How is it stored? the block's columns field (an areas field) {"layout": "2-1", "areas": {"c1": [...], "c2": [...]}}
How does it look? Columns.css and two CSS custom properties --pf-columns-gap, --pf-columns-breakpoint

Column layouts

Without any config the editor picks from the default set:

key label widths
full Full width [1]
1-1 1/2 + 1/2 [1, 1]
2-1 2/3 + 1/3 [2, 1]
1-2 1/3 + 2/3 [1, 2]
1-1-1 1/3 × 3 [1, 1, 1]
1-1-1-1 1/4 × 4 [1, 1, 1, 1]

A site's own list replaces the default set. To add to the defaults, spread them:

// config/blocks.php
use Preflow\BlockLibrary\ColumnLayouts;

return [
    'column_layouts' => [
        ...ColumnLayouts::defaults(),
        ['key' => '3-1', 'label' => '3/4 + 1/4', 'widths' => [3, 1]],
    ],
];

Each entry has a key (stored with the block, so keep it once editors use it; lower-case letters, digits, - and _), a label (a text, or a translation key such as block-library.layouts.full) and widths (1 to 6 columns, each a whole number from 1 to 12). The columns are named c1, c2, … in that order, and the editor labels them "Column 1", "Column 2", … in its language (the key block-library.columns.column with the column's number).

A mistake is refused when the block registry is built (when preflow optimize builds the boot manifest, or on the first request without one), naming the entry, for example:

Block "columns": field "columns": config/blocks.php column_layouts[2] ("1-1-1-1-1-1-1"): 7 column
widths; a layout has 1 to 6 columns, one width each, such as [2, 1].

A layout that is removed

Removing a layout from the list is safe. A block still stored with that key renders with the list's first layout that has as many columns, else full, else the list's first layout. Columns the new layout lacks are kept and shown in the editor's "Not shown in this layout" panel; preflow db:check lists the records.

Which blocks a column takes

'column_blocks' => ['except' => ['columns']],        // the default: every block but columns itself
'column_blocks' => ['allowed' => ['text', 'image']], // only these
'column_blocks' => ['except' => []],                 // columns inside columns too (within max_depth)

Use one of allowed and except, not both.

Rendering

The block renders through its own Twig or Blade template, whichever engine the site uses:

<div class="pf-columns pf-columns--2-1">
  <div class="pf-columns__cell pf-columns__cell--w2">…the blocks of c1…</div>
  <div class="pf-columns__cell pf-columns__cell--w1">…the blocks of c2…</div>
</div>

The blocks inside a column render like any other blocks: in the page's language, with rich text cleaned, and with the preview marks in Folio's live preview.

You don't write a template per layout, and you don't add the CSS yourself: the block adds Columns.css to the page's style element once (with the page's CSP nonce, no style attributes).

Restyling

The columns stack below a breakpoint without a media query: the breakpoint is the block's own width, so it works the same in a narrow sidebar as in a full-width section. Two custom properties change the look, on .pf-columns or any parent:

.pf-columns { --pf-columns-gap: 2rem; --pf-columns-breakpoint: 48rem; }
property default what it does
--pf-columns-gap 1.5rem space between columns, and between stacked cells
--pf-columns-breakpoint 40rem below this block width every column takes a row of its own

pf-columns--{key} lets a site style one layout differently, for example .pf-columns--1-1-1.

Without Folio

Everything above is core: the switch in config/blocks.php, the blocks and their templates, the layouts, the stored values, the rules a save is checked against (required fields, which blocks a column or a list takes, nested blocks) and the CSS. Folio only draws the editor: the block picker, each block's form, the layout picker, one block list per column and the "Not shown in this layout" panel. A REST or import write of a page with library blocks is checked by the same rules as Folio's form, and ContentDescription describes every library block's fields like a site's own.

One sentence to remember: a site lists the package in config/blocks.php to get ready-made, accessible blocks, restyles them with --pf-block-* custom properties, and replaces any one of them by writing a block with the same key.