eril / php-ui
A small PHP library for convenient, framework-independent HTML components.
Requires
- php: >=8.1
Requires (Dev)
- phpunit/phpunit: ^10.5 || ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Small, framework-independent HTML components for PHP. Eril UI has its own restrained visual style and does not depend on Bootstrap or Tailwind.
Requirements
- PHP 8.1 or newer
- Composer
Installation
composer require eril/php-ui
Quick start
Include the styles once in the document <head> and the scripts once before </body>:
<?php use Eril\Ui\Ui; ?> <!doctype html> <html lang="en"> <head> <?= Ui::styles() ?> </head> <body> <?= Ui::alert('Your profile was updated.', 'success', 'Saved', true) ?> <?= Ui::toast( message: 'Your changes have been saved.', title: 'All done', type: 'success', duration: 5000 ) ?> <?= Ui::scripts() ?> </body> </html>
styles() and scripts() return inline tags containing the library assets. Both accept an optional CSP nonce:
<?= Ui::styles($nonce) ?> <!-- page content --> <?= Ui::scripts($nonce) ?>
Include each once per page. The script handles toast stacking, pausing its timer while hovered or focused, dismissing toasts, and dismissing alerts that have a close button.
Components
Alert
Ui::alert( ?string $message, string $type = 'info', ?string $title = null, bool $dismissible = false, array $attributes = [] ): string
Supported types: info, success, warning, error.
<?= Ui::alert( 'Some fields need your attention.', type: 'warning', title: 'Check the form', dismissible: true, attributes: ['id' => 'form-notice'] ) ?>
Toast
Ui::toast( ?string $message, ?string $title = null, string $type = 'info', int $duration = 4000, bool $solid = false, array $attributes = [] ): string;
Supported types: info, success, warning, error. A duration of 0 leaves the toast visible until the user closes it. Negative durations are rejected. If message is null, no markup is returned. solid: true uses the muted type color as the toast background; the same background-fill progress animation remains visible.
<?= Ui::toast( message: 'The appointment was confirmed.', title: 'Confirmed', type: 'success', duration: 5000 ) ?> <?= Ui::toast( message: 'Your profile was updated.', title: 'Changes saved', type: 'info', duration: 5000, solid: true ) ?>
Several toast calls on one page are moved into a shared region and stacked by scripts().
The softly tinted background fill retracts as the timer runs. The timer pauses while a toast is hovered or focused, and users can close it manually.
alert() also returns an empty string when message is null.
Breadcrumbs
With no items, breadcrumbs use the current request URI. Hyphens and underscores become spaces; the final path segment is the current page. labels lets the application supply preferred names for segments.
<?= Ui::breadcrumbs(labels: [ 'appointments' => 'Appointments', 'my-appointments' => 'My appointments', ]) ?>
Manual breadcrumbs use an associative label-to-URL array. Set the current page URL to null:
<?= Ui::breadcrumbs([ 'Home' => '/', 'Appointments' => '/appointments', 'Details' => null, ]) ?>
The optional final uri parameter overrides the current request path in automatic mode:
<?= Ui::breadcrumbs(labels: ['my-appointments' => 'My appointments'], uri: '/patient/my-appointments') ?>
Ui::breadcrumbs( ?array $items = null, array $labels = [], string $separator = '/', array $attributes = [], ?string $uri = null ): string
Menu
Ui::menu( array $items, string $orientation = 'horizontal', array $attributes = [] ): string
Each item has a label, optional url, active, disabled, optional icon, and optional link attributes. icon is a string of CSS classes placed on an <i> element; the library adds its own spacing class and does not bundle an icon font. Orientation is horizontal or vertical.
<?= Ui::menu([ ['label' => 'Home', 'url' => '/', 'active' => true, 'icon' => 'fa-solid fa-house'], ['label' => 'Appointments', 'url' => '/appointments', 'icon' => 'fa-solid fa-calendar'], ['label' => 'Settings', 'disabled' => true, 'icon' => 'fa-solid fa-gear'], ], orientation: 'vertical') ?>
Badge
Ui::badge(string|int $label, string $type = 'default', array $attributes = []): string
Types: default, info, success, warning, error.
<?= Ui::badge('Pending', type: 'warning') ?>
Modal
modal() opens when scripts() initializes. The native dialog backdrop is dimmed and blurred. Action entries can close the modal, call a global JavaScript function, or navigate to a URL:
<?= Ui::modal('Confirm appointment', 'Continue with this appointment?', [ ['label' => 'Cancel'], ['label' => 'Confirm', 'callback' => 'confirmAppointment', 'type' => 'primary'], ['label' => 'View details', 'url' => '/appointments/42'], ]) ?> <script> window.confirmAppointment = function (dialog, button) { // Perform the page-specific action here. }; </script> <?= Ui::scripts() ?>
Callback actions use a global function name, not arbitrary inline JavaScript. Returning false keeps the dialog open. Action types are primary, secondary, and danger.
Ui::modal( string $title, ?string $message = null, array $actions = [], bool $dismissible = true, array $attributes = [] ): string
Card
Use card() for a compact dashboard summary:
<?= Ui::card('Appointments today', 4, 'Two need confirmation') ?>
Ui::card(string $title, string|int|float $value, ?string $description = null, array $attributes = []): string
Accordion
accordion() uses native <details> and <summary> elements, so it needs no JavaScript. Give items the same name to make them behave as a single-open group in supporting browsers.
<?= Ui::accordion([ ['title' => 'How does it work?', 'content' => 'Each item expands natively.', 'open' => true], ['title' => 'Does it need JavaScript?', 'content' => 'No.'], ], name: 'faq') ?>
Ui::accordion(array $items, ?string $name = null, array $attributes = []): string
HTML safety
Text and attribute values are escaped. Attribute names are validated, event-handler attributes such as onclick are omitted, and unsafe URL schemes are not rendered as links. Component content is plain text; the library does not interpret messages or labels as raw HTML.
Development
composer install
composer test
PHPUnit checks component output, escaping, URI-derived and manual breadcrumbs, component options, and asset tags.
To view the components in a browser, install dependencies and start PHP's built-in server from the project directory:
composer install php -S 127.0.0.1:8000
Then open http://127.0.0.1:8000/example.php.
License
MIT. See LICENSE.