PHPackages                             ucscode/php-bs-modal - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ucscode/php-bs-modal

ActiveLibrary

ucscode/php-bs-modal
====================

A bootstrap modal box managed and rendered in php

1.3.2(11mo ago)064↓100%MITPHPPHP &gt;=8.2

Since Dec 22Pushed 11mo ago1 watchersCompare

[ Source](https://github.com/ucscode/php-bootstrap-modal)[ Packagist](https://packagist.org/packages/ucscode/php-bs-modal)[ RSS](/packages/ucscode-php-bs-modal/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (2)Versions (13)Used By (0)

Bootstrap Modal PHP Component
=============================

[](#bootstrap-modal-php-component)

A lightweight PHP library to generate dynamic Bootstrap modals effortlessly. Customize titles, content, buttons, and attributes programmatically, making it ideal for web applications with reusable and dynamic UI components.

[![Screenshot](./example/screenshot.png)](./example/screenshot.png)

Features
--------

[](#features)

- Dynamically create Bootstrap modals.
- Customizable titles, body content, and footer buttons.
- Fully compatible with Bootstrap 5.
- Lightweight and easy to integrate.

Requirements
------------

[](#requirements)

- PHP 8.2 or higher.
- Bootstrap 5 CSS and JavaScript files loaded in your project.

Examples
--------

[](#examples)

To view a live sample, run a temporary PHP Server

```
php -S localhost:8000

```

Then open on your browser: `localhost:8000/example`.

Installation
------------

[](#installation)

Install via Composer:

```
composer require ucscode/php-bs-modal
```

Usage
-----

[](#usage)

Here is an example of how to use the library:

### Basic Example

[](#basic-example)

```
require 'vendor/autoload.php';

use Ucscode\HtmlComponent\BsModal\BsModal;
use Ucscode\HtmlComponent\BsModal\BsModalButton;

$modal = new BsModal([
    'title' => 'Welcome Dev',
    'message' => 'This is a bootstrap message',
]);
```

After that, all you need to do is render it.

```
echo $modal->render();
```

> You can view `example/php-bs-modal.php` for more coding samples

### BsModal Methods

[](#bsmodal-methods)

   Method Description     `getBuilder` Get the modal builder instance   `getModalId` Returns the id attribute value of the modal   `getElement` Return the main modal [element](https://github.com/ucscode/uss-element)   `setTitle` Define the header title for the modal   `setCloseButton` Whether to show or hide the close button at the modal top right   `setSize` Define the size of the modal options. Possible values are `sm`, `lg`, `xl`, `fullscreen`   `setCloseOnEscape` Prevent or allow the modal close when `esc` key is pressed   `setBackdropStatic` Prevent or allow the modal to close when the modal background is clicked   `setScrollable` Whether to allow the modal generate a scroll bar when the text is too large   `setVerticalCenter` Align the modal to the center of the page   `addButton` Insert custom button to the footer of the modal   `createTriggerButton` Generate a new button that will be used to trigger (open) the modal box   `setShow` Display the modal automatically when the page is loaded   `setMessage` Define the message that will be displayed in the modal box   `addEventListener` Add Javascript event listener to the modal element. See modal events [Here...](https://getbootstrap.com/docs/5.3/components/modal/#events)  > NOTE: All the setter method can be called by passing an array to the `BsModal` instance, using the method name (without the **set** prefix) as the *key*

More Examples
-------------

[](#more-examples)

```
$modal = new BsModal([
    'message' => 'This is an example of a modal',
    'closeButton' => false,
    'backdropStatic' => true,
    'closeOnEscape' => false,
    'verticalCenter' => true,
    'size' => 'sm',
    'buttons' => [
      new BsModalButton('Cancel'),
      new BsModalButton('Continue'),
    ]
])
```

You can dynamically update the modal properties

```
$modal->setTitle('Dynamic Modal')
```

```
$modal->setMessage('This is dynamically generated content.');
```

Adding or removing buttons
--------------------------

[](#adding-or-removing-buttons)

By default:

- If `okButton` (bool) is not defined,
- and If no custom button was added during the `new BsModal` instantiation,

An `Ok` button will be automatically added to the modal.

You can remove or add more custom buttons after initialization

### Working with buttons

[](#working-with-buttons)

```
$button = new BsModalButton();
```

Configure a button with label and attributes

```
$configuredButton = new BsModalButton('Save Changes', [
    'class' => 'btn btn-success',
    'data-bs-toggle' => 'modal',
]);
```

Adding buttons to the modal

```
$modal
    ->addButton($button)
    ->addButton($configuredButton)
    ->removeButton(0) // remove button at index 0
    ->removeButton($button) // remove the button instance
;
```

### Rendering the modal

[](#rendering-the-modal)

You can get the HTML string of the modal by calling the `render()` method

> Optional: You can pass a positive integer argument to return an indented HTML

```
echo $modal->render(1);
```

Since the modal (and buttons) implements `Stringable`, you can render them directly

```
echo $modal;
```

Accessing the elements
----------------------

[](#accessing-the-elements)

The PHP Bootstrap Modal library is powered by [UssElement](https://github.com/ucscode/uss-element) which provides you with many benefits of manipulating the modal.
To access many section of the modal element, you can get the `BsModalBuilder` instance

```
$builder = $modal->getBuilder();
```

To access the modal element itself, you can get the `container` element from the builder

```
$modal->getBuilder()->getContainerElement();
```

`BsModal::getElement()` method also provides an easy access to the modal container

```
$modal->getElement();
```

Modifying element attributes
----------------------------

[](#modifying-element-attributes)

Since PHP bootstrap modal is composed of [UssElement](https://github.com/ucscode/uss-element)s, you can easily query the elements and make significant updates.

```
$modal->getElement()->setAttribute('data-custom', 'example-value');
```

You can query DOM sections such as header, body, or footer

```
$modal->getElement()
    ->querySelector('.modal-header')
        ->getClassList()
            ->add('custom-header-class')
;
```

```
$modal->getElement()
    ->querySelector('.modal-body')
        ->setAttribute('id', 'custom-body-id')
;
```

Or as spoken earlier, you can access sections from the builder directly

```
$modal->getBuilder()
    ->getFooterElement()
        ->setVisible(false)
;
```

### 3. Rendering the Modal

[](#3-rendering-the-modal)

The library generates valid HTML for a Bootstrap modal. You can insert the output into your HTML structure:

```

```

Output Example
==============

[](#output-example)

```

  Open

        Modal title

        ...

        Close
        Save changes

```

### Auto Display

[](#auto-display)

By default, the bootstrap modal will be hidden unless triggered by a button.
To automatically display the modal on page load, use the `show` option

```
$modal = new BsModal([
  'message' => 'This will display on page load',
  'show' => true,
])
```

After that, render it on the page using the `$modal->render()` method

### Trigger Events

[](#trigger-events)

To trigger events on the modal, you can pass options prefixed with `event:`

```
$modal = new BsModal([
  'message' => "This will trigger multiple events",
  'event:show.bs.modal' => 'showFunc',
  'event:hidden.bs.modal' => 'hiddenFunc',
]);
```

The `showFunc` and `hiddenFunc` are functions that should have been defined in the javascript environment.
The process generates javascript event listener syntax similar to the ones below

```
element.addEventListener('show.bs.modal', showFunc);
element.addEventListener('hidden.bs.modal', hiddenFunc);
```

If the event values are not valid function names, the event will not be added

License
-------

[](#license)

This library is open-source software licensed under the [MIT license](LICENSE).

Contributing
------------

[](#contributing)

Contributions are welcome! Please fork the repository and submit a pull request with your changes.

Support
-------

[](#support)

For any questions or issues, please open an issue on the [GitHub repository](https://github.com/your-vendor-name/bootstrap-modal-generator).

###  Health Score

34

—

LowBetter than 79% of packages

Maintenance50

Moderate activity, may be stable

Popularity10

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~10 days

Recently: every ~16 days

Total

11

Last Release

356d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/65673b1b31e87471999a7614d107e7e061a38bf72191d149c66c1b943124e09c?d=identicon)[ucscode](/maintainers/ucscode)

---

Top Contributors

[![ucscode](https://avatars.githubusercontent.com/u/34024404?v=4)](https://github.com/ucscode "ucscode (29 commits)")[![codelsie](https://avatars.githubusercontent.com/u/150831864?v=4)](https://github.com/codelsie "codelsie (3 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ucscode-php-bs-modal/health.svg)

```
[![Health](https://phpackages.com/badges/ucscode-php-bs-modal/health.svg)](https://phpackages.com/packages/ucscode-php-bs-modal)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
