Skip to content

Commit

Permalink
Returned modal handling back from components
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk committed May 2, 2024
1 parent d0fc4d5 commit ae7bb2b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor
composer.lock
composer.lock
version.json
16 changes: 16 additions & 0 deletions src/Interfaces/Modal.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

/**
* @copyright Martin Procházka (c) 2023
* @license MIT License
*/

namespace JuniWalk\Utils\Interfaces;

use Nette\ComponentModel\IComponent;

interface Modal extends IComponent
{
public function setModalOpen(bool $open): void;
public function renderModal(bool $keyboard = false, bool|string $backdrop = 'static'): void;
}
42 changes: 42 additions & 0 deletions src/Traits/Modals.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);

/**
* @copyright Martin Procházka (c) 2023
* @license MIT License
*/

namespace JuniWalk\Components\Traits;

use JuniWalk\Utils\Interfaces\EventHandler;
use JuniWalk\Utils\Interfaces\Modal;
use Nette\InvalidArgumentException;

trait Modals
{
/**
* @throws InvalidArgumentException
*/
public function openModal(Modal|string $modal, array $params = []): void
{
if (is_string($modal) && !str_starts_with($modal, '#')) {
$modal = $this->getComponent($modal, true);
}

if ($modal instanceof Modal) {
if ($modal instanceof EventHandler && $modal->isWatched('render')) {
$modal->when('render', fn($m, $t) => $t->setParameters($params));
$params = [];
}

$modal->setModalOpen(true);
$modal = '#'.$modal->getName();
}

$template = $this->getTemplate();
$template->add('openModal', $modal);
$template->setParameters($params);

$this->redrawControl('modals');
$this->redirect('this');
}
}

0 comments on commit ae7bb2b

Please sign in to comment.