drago-ex / component
Collection of Bootstrap components and widgets for Nette Framework.
Requires
- php: >=8.3 <9
- latte/latte: ^3.1
- nette/application: ^3.2
Requires (Dev)
- nette/tester: ^2.5
- phpstan/phpstan-nette: ^2.0
- tracy/tracy: ^2.10
Suggests
- drago-ex/translator: Configures Latte translation support for bundled templates using translation macros.
Provides
None
Conflicts
None
Replaces
None
README
Bootstrap modal and offcanvas opened by Nette signals over Naja, and Latte widgets for dropdowns and tabs, in the look of compostrap.
Requirements
- PHP >= 8.3, < 9
- Nette Framework
- Composer
- Bootstrap
- compostrap (the styles of the modal and the tabs)
- Bootstrap Icons (the icon of a confirmation)
- Naja
- Node.js
Installation
composer require drago-ex/component
Project Tools copies the Latte widgets @modal, @offcanvas, @tabs,
@dropdown and @dismiss-button into app/Presentation/Accessory/Widget, where you adapt them.
Modal and offcanvas
In the Control component, use the Drago\Component\Component trait.
Passing variables to the template:
$template->offcanvasId = $this->getUniqueIdComponent(self::Offcanvas); $template->modalId = $this->getUniqueIdComponent(self::Modal);
You can then implement the Drago\Component\ModalHandle and Drago\Component\OffcanvasHandle interfaces.
The optional argument of modalComponent() and offCanvasComponent() is the name of a snippet to redraw;
here it is the modal and offcanvas snippets from the templates below:
#[Requires(ajax: true)] public function handleOpenModal(): void { $this->modalComponent('modal'); } #[Requires(ajax: true)] public function handleOpenOffcanvas(): void { $this->offCanvasComponent('offcanvas'); }
Attributes on interface methods are not inherited, so repeat #[Requires(ajax: true)] in your implementation.
closeComponent() closes the open modal or offcanvas (e.g. after a saved form). You can also write your own signal handler and redraw the related snippets manually.
#[Requires(ajax: true)] public function handleOpenModalWindow(): void { $this->modalComponent(); $this->redrawControl('...'); }
Use the widgets in Latte by the path to the copied file. If you need to redraw multiple blocks, add additional snippets inside the embedded template.
<a n:href="openOffcanvas!" class="ajax" data-naja-history="off">Open Offcanvas</a> {embed 'path/to/@offcanvas.latte', id: $offcanvasId} {block title}Title{/block} {block body} {snippet offcanvas} ... {/snippet} {/block} {/embed} <a n:href="openModal!" class="ajax" data-naja-history="off">Open Modal</a> {embed 'path/to/@modal.latte', id: $modalId} {import 'path/to/@dismiss-button.latte'} {block title}Title{/block} {block body} {snippet modal} ... {/snippet} {/block} {block footer} {include dismiss-button, type: modal} {/block} {/embed}
The blocks of the page see the variables of the page. Neither closes by a click outside; the modal closes by Esc.
Modal
id (required), theme (danger, warning, success, info, …) draws a colored line at its top, confirm: true
makes it a narrow centered confirmation with a large icon in the color of the theme (icon replaces the icon). The
body and the footer are left out when they are empty.
{embed 'path/to/@modal.latte', id: $modalId, theme: danger, confirm: true} {block title}Delete the article?{/block} ... {/embed}
Offcanvas
id (required), size (width of the panel, 61rem by default, e.g. 100vw for the whole screen). The block
header stands between the title and the close button, e.g. for tabs of the panel.
Dismiss button
A button that closes a modal, offcanvas, alert or toast: type (required), label (Close by default, e.g.
Cancel in a confirmation), class (btn btn-secondary). Import it inside the {embed} that uses it.
{include dismiss-button, type: modal, label: 'Cancel'}
Dropdown
{embed 'path/to/@dropdown.latte', name: 'Actions', icon: 'bi bi-gear', align: end} {block menu} {include item, name: 'Edit', link: 'edit'} {include divider} {include item, name: 'Delete', link: 'delete!'} {/block} {/embed}
name (required, translated), icon (e.g. bi bi-person of Bootstrap Icons), align of the menu (end,
lg-start, …, appended to dropdown-menu-). Helper blocks of the menu: item (name translated, link) and
divider; anything else goes into the block menu as an <li>.
Tabs
The keys of tabs are the ids of the tabs and the names of the blocks with their panes:
{embed 'path/to/@tabs.latte', id: product, tabs: [form: 'Product', photos: 'Photos']} {block form}…{/block} {block photos}…{/block} {/embed}
id (required, prefix of the ids of the tabs and panes), tabs ([key => label], the labels are translated),
active (key of the active tab, the first one by default). The tabs are a segmented button group of compostrap.
Assets
Add the Composer package as a local npm dependency in package.json (Drago Project installs it by itself), so the bundler knows drago-component:
{
"type": "module",
"dependencies": {
"drago-component": "file:vendor/drago-ex/component"
}
}
Then run npm install.
Naja extensions
import { ModalHandler, OffcanvasHandler, DropdownHandler } from 'drago-component'; naja.registerExtension(new ModalHandler()); naja.registerExtension(new OffcanvasHandler()); naja.registerExtension(new DropdownHandler());
ModalHandler and OffcanvasHandler open the element whose id comes in the payload modal or offcanvas of the
Component trait and close them on close. DropdownHandler toggles the dropdowns by its own listener, also in
snippets, and closes the other open ones: when the bundle has Bootstrap twice (e.g. bootstrap and
bootstrap/js/dist/dropdown from another package), each copy has its own data API and one click would open the menu
and close it at once.
Styles
The look of the modal (the colored line, the confirmation) comes from the module dialogs of compostrap, the tabs
from its theme (Drago Project already imports both in assets/core/base.scss):
@import 'compostrap/src/scss/theme'; @import 'compostrap/src/scss/dialogs';